-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItem.java
111 lines (93 loc) · 2.82 KB
/
Item.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import java.util.Random;
/**
* Base class for static entities such as pickups or projectiles
*/
public class Item extends Entity {
protected boolean BOUNCEY;
protected final float DAMP = 0.7f;
protected final float FRICTION = 0.95f;
protected boolean EXPIRE = false;
public boolean remove = false;
public Item(float x, float y, float width, float height) {
super(x, y, width, height);
}
// on collide with map tile block
public void onCollide(Block b, boolean onX, boolean onY) {
if (onX) {
if (velocity.x > 0) {
position.x = b.getX() - getWidth() - 1;
} else {
position.x = b.getRight() + 1;
}
if (BOUNCEY && velocity.x != 0) {
velocity.x *= -DAMP;
} else {
velocity.x = 0;
}
}
if (onY) {
if (velocity.y < 0) {
position.y = b.getTop();
} else {
position.y = b.getY() - getHeight() - 1;
}
if (BOUNCEY && Math.abs(velocity.y) > 50) {
velocity.y *= -DAMP;
} else {
velocity.y = 0;
grounded = true;
}
}
}
public Item setBouncey(boolean bouncey) {
this.BOUNCEY = bouncey;
return this;
}
public Item setExpire(boolean expire) {
this.EXPIRE = expire;
return this;
}
/**
* Give this item a random velocity based on it's speed stat
*/
public Item randomVelocity() {
Random r = new Random();
velocity.x = (r.nextFloat() * speed * 2) - speed; // left or right
velocity.y = r.nextFloat() * speed; // only up
return this;
}
@Override
public void act(float delta) {
if (EXPIRE) {
if (velocity.x == 0 && grounded) {
markForRemoval();
}
}
super.act(delta);
velocity.x *= FRICTION;
if (Math.abs(velocity.x * delta) < 0.1) {
velocity.x = 0;
}
if (EXPIRE && velocity.x == 0 && grounded) {
markForRemoval();
}
}
/**
* Mark this item for removal in the next game step
*/
protected void markForRemoval() {
this.remove = true;
}
public void drawDebug(ShapeRenderer debug) {
debug.begin(ShapeRenderer.ShapeType.FilledCircle);
debug.setColor(debugColor);
debug.filledCircle(getCenterX(), getCenterY(), getWidth() / 2);
debug.end();
debug.begin(ShapeRenderer.ShapeType.Circle);
debug.setColor(Color.BLACK);
debug.circle(getCenterX(), getCenterY(), getWidth() / 2);
debug.end();
}
}