Coverage Summary for Class: Obstacle (com.mygdx.game.Entitys)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| Obstacle | 100% (1/1) | 87.5% (7/8) | 87.5% (21/24) |
1 package com.mygdx.game.Entitys; 2 3 import com.mygdx.game.Components.ObstacleControl; 4 import com.mygdx.game.Components.Renderable; 5 import com.mygdx.game.Components.RigidBody; 6 import com.mygdx.game.Components.Transform; 7 import com.mygdx.game.Managers.RenderLayer; 8 import com.mygdx.game.Managers.ResourceManager; 9 import com.mygdx.game.Physics.CollisionCallBack; 10 import com.mygdx.game.Physics.CollisionInfo; 11 import com.mygdx.game.Physics.PhysicsBodyType; 12 13 14 /** 15 * // Added for Assessment 2 Requirements // 16 * A class for obstacles in the game world, both trigger or contact. 17 */ 18 public class Obstacle extends Entity implements CollisionCallBack { 19 20 private boolean doKill; 21 private boolean colliding; 22 private CollisionInfo info; 23 24 /** 25 * Generate an obstacle which only triggers a hit on initial collision and does not break. 26 * @param texName The texture to show for the obstacle. 27 * @param trigger True if the obstacle is trigger, otherwise is contact. 28 * @param damage The damage that the obstacle does per 'hit' 29 */ 30 public Obstacle(String texName, boolean trigger, float damage) { 31 this(texName, trigger, damage, -1f, -1); 32 } 33 34 /** 35 * Generate an obstacle. 36 * @param texName The texture to show for the obstacle. 37 * @param trigger True if the obstacle is trigger, otherwise is contact. 38 * @param damage The damage that the obstacle does per 'hit' 39 * @param hitRate The rate at which 'hits' occur while colliding 40 * @param hitLimit The number of 'hits' required to break the obstacle 41 */ 42 public Obstacle(String texName, boolean trigger, float damage, float hitRate, int hitLimit) { 43 super(4); 44 45 Transform t = new Transform(); 46 Renderable r = new Renderable(ResourceManager.getId("obstacles.txt"), texName, RenderLayer.Transparent); 47 RigidBody rb = new RigidBody(PhysicsBodyType.Kinematic, r, t, trigger); 48 rb.setCallback(this); 49 ObstacleControl o = new ObstacleControl(damage, hitRate, hitLimit); 50 51 addComponents(t, r, rb, o); 52 53 doKill = false; 54 colliding = false; 55 } 56 57 /** 58 * Sets the obstacle to be removed on next update 59 */ 60 public void kill() { 61 doKill = true; 62 } 63 64 /** 65 * Runs once per frame 66 */ 67 @Override 68 public void update() { 69 super.update(); 70 71 if (doKill) { 72 getComponent(Transform.class).setPosition(1000, -1000); 73 doKill = false; 74 } 75 if (colliding) getComponent(ObstacleControl.class).TryHit(info, false); 76 } 77 78 /** 79 * Takes the collision info and verifies the results of the collision 80 * @param info the info linked to the collision 81 */ 82 @Override 83 public void BeginContact(CollisionInfo info) { 84 colliding = true; 85 this.info = info; 86 getComponent(ObstacleControl.class).TryHit(info, true); 87 } 88 89 /** 90 * Sets the obstacle's status as not colliding 91 */ 92 @Override 93 public void EndContact(CollisionInfo info) { 94 colliding = false; 95 } 96 97 /** 98 * Takes the collision info and verifies the results of the collision 99 * @param info the info linked to the collision 100 */ 101 @Override 102 public void EnterTrigger(CollisionInfo info) { 103 colliding = true; 104 this.info = info; 105 getComponent(ObstacleControl.class).TryHit(info, true); 106 } 107 108 /** 109 * Sets the obstacle's status as not colliding 110 */ 111 @Override 112 public void ExitTrigger(CollisionInfo info) { 113 colliding = false; 114 } 115 116 // public Obstacle(String texName, boolean trigger, float damage, int hitLimit) { 117 // this(texName, trigger, damage, -1f, hitLimit); 118 // } 119 120 // public Obstacle(String texName, boolean trigger, float damage, float hitRate) { 121 // this(texName, trigger, damage, hitRate, -1); 122 // } 123 }