-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tank.pde
98 lines (79 loc) · 2.46 KB
/
Tank.pde
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
class Tank extends PhysicsEntity {
color colorTint;
float rotation;
boolean flipped;
PVector aimDirection;
PImage imageBody;
PImage imageGun;
float imageSize;
Tank(PVector origin, color colorTint) {
super(origin, new PVector(), 16.0, 10.0, 0.15, 0.0);
this.zIndex = 1000;
this.solid = true;
this.colorTint = colorTint;
this.rotation = 0.0;
this.flipped = false;
this.aimDirection = new PVector(2, 0);
this.imageBody = loadImage("textures/tank.png");
this.imageGun = loadImage("textures/tank-gun.png");
this.imageSize = 48;
}
void accelerate(float amount) {
this.velocity.add(PVector.fromAngle(this.rotation).mult(amount));
}
void updateAim() {
this.aimDirection = camera.getMousePos().sub(this.origin);
}
void shootGrenade() {
PVector projectileOrigin = this.origin.copy().add(this.aimDirection.copy().setMag(12));
PVector projectileVelocity = this.aimDirection.copy().mult(0.02).limit(6.0).add(this.velocity);
new Grenade(projectileOrigin, projectileVelocity, this.colorTint).spawn();
this.velocity.add(projectileVelocity.copy().mult(-0.25));
SOUND_GRENADE_SHOOT.play();
}
void shootRocket() {
PVector projectileOrigin = this.origin.copy().add(this.aimDirection.copy().setMag(12));
new Rocket(projectileOrigin, this.aimDirection.copy().setMag(6.0), color(#ffab26)).spawn();
SOUND_ROCKET_SHOOT.play();
}
void OnTick() {
this.updateAim();
float accelScale = 0.0;
if (inputs.getIsActive("moveleft")) accelScale -= 0.05;
if (inputs.getIsActive("moveright")) accelScale += 0.05;
if (!this.onGround) accelScale = 0.0;
if (accelScale != 0) {
this.accelerate(accelScale);
this.flipped = accelScale > 0.0;
this.roll = 0.96;
} else {
this.roll = lerp(0.0, 0.96, constrain(this.velocity.mag() - 0.1, 0.0, 1.0));
}
super.OnTick();
}
void OnCollision(Collision collision) {
if (this.onGround)
this.rotation = collision.normal.heading() + HALF_PI;
}
void OnMousePressed() {
if (mouseButton == LEFT) {
this.shootRocket();
} else if (mouseButton == RIGHT) {
this.shootGrenade();
}
}
void OnDraw() {
tint(this.colorTint);
imageMode(CENTER);
translate(this.origin.x, this.origin.y);
pushMatrix();
rotate(this.rotation);
if (this.flipped)
scale(-1.0, 1.0);
image(this.imageBody, 0, 0, this.imageSize, this.imageSize);
popMatrix();
translate(0, this.imageSize / -16);
rotate(this.aimDirection.heading() + PI);
image(this.imageGun, 0, 0, this.imageSize, this.imageSize / 8);
}
}