Coverage Summary for Class: ObstacleControl (com.mygdx.game.Components)
Class |
Class, %
|
Method, %
|
Line, %
|
ObstacleControl |
100%
(1/1)
|
100%
(2/2)
|
92.9%
(13/14)
|
1 package com.mygdx.game.Components;
2
3 import com.badlogic.gdx.utils.TimeUtils;
4 import com.mygdx.game.Entitys.Obstacle;
5 import com.mygdx.game.Entitys.Player;
6 import com.mygdx.game.Physics.CollisionInfo;
7
8 /**
9 * // Added for Assessment 2 Requirements //
10 * Makes an Entity act as an Obstacle to the Player, causing damage upon collision in a variety of differing ways.
11 */
12 public class ObstacleControl extends Component {
13 private final float hitDamage;
14 private final float hitRate;
15 private final int hitLimit;
16
17 private long lastHit;
18 private int hitCount;
19
20 /**
21 * Generate an obstacle controller component.
22 * @param damage The damage that the obstacle does per 'hit'
23 * @param hitRate The rate at which 'hits' occur while colliding
24 * @param hitLimit The number of 'hits' required to break the obstacle
25 */
26 public ObstacleControl(float damage, float hitRate, int hitLimit) {
27 this.hitDamage = damage;
28 this.hitRate = hitRate * 1000f;
29 this.hitLimit = hitLimit;
30
31 lastHit = 0;
32 hitCount = 0;
33 }
34
35 /**
36 * Attempts to perform a collision action
37 * @param collision the collision info to be attempted
38 * @param enter boolean that determines whether the collision is allowed
39 */
40 public void TryHit(CollisionInfo collision, boolean enter) {
41 boolean doHit = (enter || (TimeUtils.timeSinceMillis(lastHit) >= hitRate && hitRate > 0));
42 if (collision.a instanceof Player && doHit) {
43 Player player = (Player) collision.a;
44 player.getComponent(Pirate.class).takeDamage(hitDamage);
45 lastHit = TimeUtils.millis();
46 hitCount++;
47 if (hitCount >= hitLimit && hitLimit > 0) {
48 ((Obstacle) parent).kill();
49 }
50 }
51 }
52 }