Coverage Summary for Class: PowerUpPickup (com.mygdx.game.Entitys)
Class |
Class, %
|
Method, %
|
Line, %
|
PowerUpPickup |
100%
(1/1)
|
55.6%
(5/9)
|
63.3%
(19/30)
|
1 package com.mygdx.game.Entitys;
2
3 import com.badlogic.gdx.math.Vector2;
4 import com.badlogic.gdx.utils.TimeUtils;
5 import com.mygdx.game.Components.PowerUpAssigned;
6 import com.mygdx.game.Components.Renderable;
7 import com.mygdx.game.Components.RigidBody;
8 import com.mygdx.game.Components.Transform;
9 import com.mygdx.game.Managers.RenderLayer;
10 import com.mygdx.game.Managers.ResourceManager;
11 import com.mygdx.game.Physics.CollisionCallBack;
12 import com.mygdx.game.Physics.CollisionInfo;
13 import com.mygdx.game.Physics.PhysicsBodyType;
14 import com.mygdx.game.PowerUps.PowerUp;
15
16 /**
17 * Added for Assessment 2
18 * Simple entity shown on locate quests origin
19 */
20 public class PowerUpPickup extends Entity implements CollisionCallBack {
21
22 private final PowerUp powerUp;
23 private boolean hideToggle;
24 private boolean showToggle;
25 private final Vector2 position;
26 private final long cooldown;
27 private long lastHit;
28
29 /**
30 * Generate an obstacle.
31 * @param powerUp The Power Up being picked up
32 * @param texName The texture to show for the obstacle.
33 * @param pos The position of the power up
34 * @param cooldown The cooldown before the power up can be used again
35 */
36 public PowerUpPickup(PowerUp powerUp, String texName, Vector2 pos, int cooldown) {
37 super(3);
38
39 Transform t = new Transform();
40 Renderable r = new Renderable(ResourceManager.getId("powerups.txt"), texName, RenderLayer.Transparent);
41 RigidBody rb = new RigidBody(PhysicsBodyType.Kinematic, r, t, true);
42 rb.setCallback(this);
43
44 addComponents(t, r, rb);
45
46 this.powerUp = powerUp;
47
48 hideToggle = false;
49 showToggle = false;
50
51 position = pos;
52 setPosition(pos);
53
54 this.cooldown = 1000L * cooldown;
55 lastHit = 0;
56 }
57
58 /**
59 * @param pos The position of the power up to be set
60 */
61 private void setPosition(Vector2 pos) {
62 getComponent(RigidBody.class).setPosition(pos);
63 }
64
65 /**
66 * Reveals the power-up if its cooldown has expired
67 */
68 private void tryShow() {
69 if (TimeUtils.timeSinceMillis(lastHit) > cooldown) {
70 setPosition(position);
71 showToggle = false;
72 }
73 }
74
75 /**
76 * Hides the power-up
77 */
78 private void tryHide() {
79 setPosition(new Vector2(1000, -1000));
80 hideToggle = false;
81 showToggle = true;
82 lastHit = TimeUtils.millis();
83 }
84
85 /**
86 * Called once per frame
87 */
88 @Override
89 public void update() {
90 super.update();
91 if (hideToggle) tryHide();
92 else if (showToggle) tryShow();
93 }
94
95 /**
96 * applies the attached power up to the colliding entity then sets the power up to be hidden next update
97 * @param info the collision info used in the conjunction
98 */
99 @Override
100 public void EnterTrigger(CollisionInfo info) {
101 PowerUpAssigned playerPow = info.a.getComponent(PowerUpAssigned.class);
102 playerPow.AssignPowerUp(powerUp);
103 hideToggle = true;
104 }
105
106 /**
107 * `unused`
108 */
109 @Override
110 public void BeginContact(CollisionInfo info) {
111 }
112
113 /**
114 * `unused`
115 */
116 @Override
117 public void EndContact(CollisionInfo info) {
118 }
119
120 /**
121 * `unused`
122 */
123 @Override
124 public void ExitTrigger(CollisionInfo info) {
125 }
126 }