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 /** 21 * A game component that implements velocity-based movement. 22 */ 23 public class MovementComponent extends GameComponent { 24 // If multiple game components were ever running in different threads, this would need 25 // to be non-static. 26 private static Interpolator sInterpolator = new Interpolator(); 27 28 public MovementComponent() { 29 super(); 30 setPhase(ComponentPhases.MOVEMENT.ordinal()); 31 } 32 33 @Override 34 public void reset() { 35 36 } 37 38 @Override 39 public void update(float timeDelta, BaseObject parent) { 40 GameObject object = (GameObject) parent; 41 42 sInterpolator.set(object.getVelocity().x, object.getTargetVelocity().x, 43 object.getAcceleration().x); 44 float offsetX = sInterpolator.interpolate(timeDelta); 45 float newX = object.getPosition().x + offsetX; 46 float newVelocityX = sInterpolator.getCurrent(); 47 48 sInterpolator.set(object.getVelocity().y, object.getTargetVelocity().y, 49 object.getAcceleration().y); 50 float offsetY = sInterpolator.interpolate(timeDelta); 51 float newY = object.getPosition().y + offsetY; 52 float newVelocityY = sInterpolator.getCurrent(); 53 54 if (object.positionLocked == false) { 55 object.getPosition().set(newX, newY); 56 } 57 58 object.getVelocity().set(newVelocityX, newVelocityY); 59 } 60 61 } 62