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 /** A light-weight physics implementation for use with non-complex characters (enemies, etc). */ 20 public class SimplePhysicsComponent extends GameComponent { 21 private static final float DEFAULT_BOUNCINESS = 0.1f; 22 private float mBounciness; 23 SimplePhysicsComponent()24 public SimplePhysicsComponent() { 25 super(); 26 setPhase(GameComponent.ComponentPhases.POST_PHYSICS.ordinal()); 27 reset(); 28 } 29 30 @Override reset()31 public void reset() { 32 mBounciness = DEFAULT_BOUNCINESS; 33 } 34 setBounciness(float bounciness)35 public void setBounciness(float bounciness) { 36 mBounciness = bounciness; 37 } 38 39 @Override update(float timeDelta, BaseObject parent)40 public void update(float timeDelta, BaseObject parent) { 41 GameObject parentObject = (GameObject) parent; 42 43 final Vector2 impulse = parentObject.getImpulse(); 44 float velocityX = parentObject.getVelocity().x + impulse.x; 45 float velocityY = parentObject.getVelocity().y + impulse.y; 46 47 if ((parentObject.touchingCeiling() && velocityY > 0.0f) 48 || (parentObject.touchingGround() && velocityY < 0.0f)) { 49 velocityY = -velocityY * mBounciness; 50 51 if (Utils.close(velocityY, 0.0f)) { 52 velocityY = 0.0f; 53 } 54 } 55 56 if ((parentObject.touchingRightWall() && velocityX > 0.0f) 57 || (parentObject.touchingLeftWall() && velocityX < 0.0f)){ 58 velocityX = -velocityX * mBounciness; 59 60 if (Utils.close(velocityX, 0.0f)) { 61 velocityX = 0.0f; 62 } 63 } 64 65 parentObject.getVelocity().set(velocityX, velocityY); 66 impulse.zero(); 67 } 68 } 69