-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVehicle.pde
More file actions
98 lines (75 loc) · 2.38 KB
/
Vehicle.pde
File metadata and controls
98 lines (75 loc) · 2.38 KB
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
import java.util.function.Consumer;
class Vehicle extends SceneElement {
// ----- Fields
private final String[] spritePaths = new String[] {
"ambulance.png", "audi.png", "black-viper.png",
"car.png", "mini-truck.png", "mini-van.png",
"police.png", "taxi.png", "truck.png"
};
private final Consumer<Vehicle> onDestroyed;
protected float speed;
private Vec2Int direction;
private PImage sprite;
private boolean enabled;
private boolean destroyed;
// ----- Constructors
public Vehicle(Consumer<Vehicle> onDestroyed) {
this.onDestroyed = onDestroyed;
}
// ----- Methods
public void initAndEnable(Vec2Int movementDir, float minSpeed, float maxSpeed) {
direction = movementDir;
rotation = atan2(direction.y, direction.x) + radians(90);
speed = random(minSpeed, maxSpeed);
scale = 1f;
String randomSpritePath = spritePaths[(int)random(spritePaths.length)];
sprite = loadImage(randomSpritePath);
destroyed = false;
enabled = true;
}
public void disable() {
enabled = false;
}
@Override
public void draw() {
if (!enabled) return;
scale(.75f);
imageMode(CENTER);
image(sprite, 0, 0);
position = new Vec2Int(position.x + direction.x * speed, position.y + direction.y * speed);
}
public void destroy(boolean playAnimation) {
destroyed = true;
speed = 0f;
if (playAnimation) {
Timeline.createParallel()
.push(
TweenEngineHelpers.tween(this, SceneElementTweenAccessor.SCALE_ID, .5f)
.target(0f)
.ease(TweenEquations.easeInOutElastic)
.setCallback((type, _source) -> {
if (type == TweenCallback.COMPLETE)
onDestroyed.accept(this);
})
)
.start(tweenManager);
} else {
onDestroyed.accept(this);
}
}
@Override
public RectCollider getCollider() {
if (destroyed) return null; //<>//
int w = round(sprite.width * scale / 2), h = round(sprite.height * scale / 4);
float rotation = this.rotation - radians(90);
float a = abs(cos(rotation)), b = abs(sin(rotation));
Vec2Int size = new Vec2Int(
round(w * a + h * b),
round(w * b + h * a)
);
return new RectCollider(
new Vec2Int(width / 2 + position.x, height / 2 + position.y),
new Vec2Int(size.x, size.y)
);
}
}