Home | History | Annotate | Download | only in replicaisland
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.replica.replicaisland;
     18 
     19 /**
     20  * A game component that implements gravity.  Adding this component to a game object will cause
     21  * it to be pulled down towards the ground.
     22  */
     23 public class GravityComponent extends GameComponent {
     24     private Vector2 mGravity;
     25     private Vector2 mScaledGravity;
     26     private static final Vector2 sDefaultGravity = new Vector2(0.0f, -400.0f);
     27 
     28     public GravityComponent() {
     29         super();
     30         mGravity = new Vector2(sDefaultGravity);
     31         mScaledGravity = new Vector2();
     32         setPhase(ComponentPhases.PHYSICS.ordinal());
     33     }
     34 
     35     @Override
     36     public void reset() {
     37         mGravity.set(sDefaultGravity);
     38     }
     39 
     40     @Override
     41     public void update(float timeDelta, BaseObject parent) {
     42     	mScaledGravity.set(mGravity);
     43         mScaledGravity.multiply(timeDelta);
     44         ((GameObject) parent).getVelocity().add(mScaledGravity);
     45     }
     46 
     47     public Vector2 getGravity() {
     48         return mGravity;
     49     }
     50 
     51     public void setGravityMultiplier(float multiplier) {
     52         mGravity.set(sDefaultGravity);
     53         mGravity.multiply(multiplier);
     54     }
     55 }
     56