-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOvni.pde
More file actions
152 lines (134 loc) · 4.11 KB
/
Copy pathOvni.pde
File metadata and controls
152 lines (134 loc) · 4.11 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
enum OvniType {
NORMAL,
ELITE
};
class Ovni extends Entity {
private OvniType type;
private int drawScale;
private int radius;
private int nextMovementUpdate;
private int nextShootUpdate;
private int horDirection;
private float velocity;
private Shooter shooter;
private boolean isDestroyed;
Ovni(OvniType _type, PVector location) {
super(location);
type = _type;
drawScale = OvniType.NORMAL== type ? 12 : 6;
velocity = OvniType.NORMAL == type ? 2 : 2.8;
radius = drawScale * 2;
horDirection = random(1) < 0.5 ? -1 : 1;
setLocation(new PVector(horDirection < 0 ? width + radius : 0, random(height)));
applyForce((new PVector(horDirection, 0)).normalize().mult(velocity));
nextMovementUpdate = millis() + 2 * 1000;
nextShootUpdate = calculeNextShootUpdate();
shooter = new Shooter(ShooterOriginType.ENEMY);
isDestroyed = false;
playAudio();
}
public void update() {
updateLogic();
updateRender();
shooter.update();
}
public int getRadius() {
return radius;
}
public void setDestroyedByShip() {
setDestroyed();
if (type == OvniType.NORMAL) {
explosionManager.createNormalOvniExplosion(super.getLocation());
hud.addPoints(200);
} else if (type == OvniType.ELITE) {
explosionManager.createEliteOvniExplosion(super.getLocation());
hud.addPoints(1000);
}
}
public void setDestroyed() {
isDestroyed = true;
stopAudio();
}
public boolean isDestroyed() {
return isDestroyed;
}
private int calculeNextShootUpdate() {
if (OvniType.NORMAL == type) {
return millis() + 1000;
} else {
return millis() + 500;
}
}
private void updateLogic() {
if (millis() > nextMovementUpdate) {
super.stopMovement();
applyForce((new PVector(horDirection, random(-1, 1)).normalize().mult(velocity)));
nextMovementUpdate = millis() + 1000 * 2;
}
if (millis() > nextShootUpdate) {
shoot();
nextShootUpdate = calculeNextShootUpdate();
}
super.updateMovement();
updateEdges();
}
private void shoot() {
if (OvniType.NORMAL == type) {
float angle = random(TWO_PI);
PVector direction = (new PVector(cos(angle), sin(angle))).normalize();
shooter.shoot(direction, getLocation());
} else if (OvniType.ELITE == type) {
PVector direction = PVector.sub(ship.location, getLocation()).normalize();
shooter.shoot(direction, getLocation());
}
}
private void playAudio() {
if (type == OvniType.NORMAL) {
soundManager.beginPlayingNormalOvni();
} else if (type == OvniType.ELITE) {
soundManager.beginPlayingEliteOvni();
}
}
private void stopAudio() {
if (type == OvniType.NORMAL) {
soundManager.endPlayingNormalOvni();
} else if (type == OvniType.ELITE) {
soundManager.endPlayingEliteOvni();
}
}
private void updateEdges() {
if (getLocation().x > width) {
setLocation(new PVector(0, getLocation().y));
} else if (getLocation().x < 0) {
setLocation(new PVector(width, getLocation().y));
}
if (getLocation().y > height) {
setLocation(new PVector(getLocation().x, 0));
} else if (getLocation().y < 0) {
setLocation(new PVector(getLocation().x, height));
}
}
private void updateRender() {
noFill();
stroke(255);
//ellipse(getLocation().x, getLocation().y, getRadius(), getRadius());
pushMatrix();
translate(getLocation().x, getLocation().y);
beginShape();
vertex(-2 * drawScale, 0);
vertex(-1 * drawScale, -1 * drawScale);
vertex(-1 * drawScale, -2 * drawScale);
vertex(1 * drawScale, -2 * drawScale);
vertex(1 * drawScale, -1 * drawScale);
vertex(1 * drawScale, -1 * drawScale);
vertex(2 * drawScale, 0);
vertex(1 * drawScale, 1 * drawScale);
vertex(0, 1 * drawScale);
vertex(-1 * drawScale, 1 * drawScale);
endShape(CLOSE);
line (-2 * drawScale, 0, 2 * drawScale, 0);
line (-1 * drawScale, -1 * drawScale, 1 * drawScale, -1 * drawScale);
// arc(0, -drawScale, drawScale*2, drawScale, -drawScale, PI);
popMatrix();
}
}