-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.pde
140 lines (119 loc) · 2.96 KB
/
Controller.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
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
class Controller {
Entity entity;
public Controller(Entity entity) {
this.entity = entity;
}
public void update(float delta) {
}
}
class CircleController extends Controller {
PVector center = new PVector(40, 40);
float radius = 20;
float angularVelocity = 3;
float theta = 0;
public CircleController(Entity entity) {
super(entity);
}
public void update(float delta) {
theta += angularVelocity * delta;
entity.position.x = center.x + radius * cos(theta);
entity.position.y = center.y + radius * sin(theta);
entity.weapon.emit(entity, delta);
}
}
class DownController extends Controller {
float speed = 30;
public DownController(Entity entity){
super(entity);
}
public void update(float delta) {
entity.position.y += speed * delta;
entity.weapon.emit(entity, delta);
}
}
class StrafeController extends Controller {
float theta = 0;
float angularVelocity = 5;
float radius = 30;
float xCenter;
StrafeController(Entity entity){
super(entity);
xCenter = entity.position.x;
}
public void update(float delta){
theta += angularVelocity * delta;
entity.position.x = xCenter + radius * cos(theta);
}
}
class KeyboardController extends Controller implements KeyListener {
boolean directions[] = new boolean[4];//L,U,R,D
boolean fire = false;
float speed = 100;
public KeyboardController(Entity entity) {
super(entity);
keyListeners.add(this);
}
public void update(float delta) {
if (entity.weapon != null) {
if (fire)
entity.weapon.emit(entity, delta);
else
entity.weapon.roll(delta);
}
}
public void keyPressed(int keyCode) {
switch (keyCode) {
case 37: /* left */
if (!directions[0]) {
directions[0] = true;
entity.velocity.x += -speed;
}
break;
case 38: /* up */
if (!directions[1]) {
directions[1] = true;
entity.velocity.y += -speed;
}
break;
case 39: /* right */
if (!directions[2]) {
directions[2] = true;
entity.velocity.x += speed;
}
break;
case 40: /* down */
if (!directions[3]) {
directions[3] = true;
entity.velocity.y += speed;
}
break;
}
if (key == ' ') {
fire = true;
entity.weapon.emit(entity, 0);
}
}
public void keyReleased(int keyCode) {
switch (keyCode) {
case 37: /* left */
directions[0] = false;
entity.velocity.x -= -speed;
break;
case 38: /* up */
directions[1] = false;
entity.velocity.y -= -speed;
break;
case 39: /* right */
directions[2] = false;
entity.velocity.x -= speed;
break;
case 40: /* down */
directions[3] = false;
entity.velocity.y -= speed;
break;
}
if (key == ' ') {
fire = false;
}
}
}