-
Notifications
You must be signed in to change notification settings - Fork 0
/
C_Player.pde
254 lines (217 loc) · 6.14 KB
/
C_Player.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
class Player extends MasterEntity {
final float JUMP_VEL = 5;
final float MAX_SPEED = 4;
final float ACC = 3;
final float INITIAL_SPEED = 0.05;
Camera cam;
color col;
HUD hud;
int id;
//Stats
float health = 100;
int deaths = 0;
Gun gun;
Player(float x1, float y1, float z1, float x2, float y2, float z2) { //Size and position params
super(x1, y1, z1, x2, y2, z2);
vel = new Vector(0, 0, 0); //Init player specific Vectors
dir = new Vector(0, 90);
pos.y -= 50;
cam = new Camera(dir, pos); //Create camera and HUD
hud = new HUD("hud");
gun = new Gun(pos.x, pos.y, pos.z);
}
void move() { //Adds movement acceleration when the player presses a key
if (MOONJUMP) {
if (keys.getKey("up")) {
ground = false;
vel.y = -JUMP_VEL;
}
if (keys.getKey("down")) {
vel.y = JUMP_VEL;
}
}
if (keys.getKey("jump") && ground == true) {
vel.y -= JUMP_VEL;
ground = false;
}
int moveDir = getMoveDirect(); //Gets the direction to move the player
if (moveDir != -23) { //-23 means no keys are being pressed
float move = cam.center.headingH()+moveDir;
move = (360+move)%360;
if (getXZ(vel).mag() <= 0) {
Vector t = new Vector(move).setMag(INITIAL_SPEED);
vel.add(t.x, 0, t.y);
}
Vector moveAccel = new Vector(move).setMag(
map(getXZ(vel).mag(), 0, MAX_SPEED, 0.03, ACC));
vel.add(moveAccel.x, 0, moveAccel.y);
Vector velHor = new Vector(vel.x, vel.z);
velHor.limit(MAX_SPEED);
vel.x = velHor.x;
vel.z = velHor.y;
}
ground = false;
cam.moveTo(pos.copy());
gun.updatePos(pos);
}
boolean isMoving() { //If any key is pressed, used for decceleration
boolean w = keys.getKey("forward");
boolean s = keys.getKey("backward");
boolean a = keys.getKey("left");
boolean d = keys.getKey("right");
return (w^s)|(a^d);
}
int getMoveDirect() { //Calculates the direction to move in, -23 means don't move
boolean w = keys.getKey("forward");
boolean s = keys.getKey("backward");
boolean a = keys.getKey("left");
boolean d = keys.getKey("right");
int x, z;
if (w^s) {
if (w) {
x = 0;
} else {
x = 180;
}
} else {
x = -23;
}
if (a^d) {
if (a) {
z = 270;
} else {
z = 90;
}
} else {
z = -23;
}
if (x==-23&&z==-23) {
return -23;
}
if (z == -23) {
return x;
} else if (x==-23) {
return z;
} else {
return int(lerp(x, w&a?180-z:z, 0.5));
}
}
void collideWith(int x, MasterObject mo) { //Actions to perform if the player collides with an object
switch(x) {
case TOUCH_BOTTOM:
if (vel.y <= 0) {
pos.y = mo.getBottom()+size.y/2;
vel.y = 0;
}
break;
case TOUCH_TOP:
if (vel.y >= 0) {
pos.y = mo.getTop()-size.y/2;
ground = true;
vel.y = 0;
}
break;
case TOUCH_BACK:
pos.z = mo.getBack()+size.z/2;
break;
case TOUCH_FRONT:
pos.z = mo.getFront()-size.z/2;
break;
case TOUCH_RIGHT:
pos.x = mo.getRight()+size.x/2;
break;
case TOUCH_LEFT:
pos.x = mo.getLeft()-size.x/2;
break;
case TOUCH_NOT:
default:
break;
}
}
void physics() { //The physics method called by the physics thread.
if (!ground) { //If in the sky
vel.add(GRAVITY);
vel.y = constrain(vel.y, -MAX_INT, PLAYER_VELOCITY_TERMINAL); //Stop the player accelerating constantly
}
if (!isMoving()) { //If not moving, deccelerate
Vector vh = getXZ(vel).setMag(ACC*2);
vh.mult(map(sqrt(getXZ(vel).mag()), 0, sq(MAX_SPEED), 0, 2));
vel.sub(vh.x, 0, vh.y);
}
pos.add(vel); //Add velocity to position
// OOB
if (abs(player.pos.x) > 2048) player.pos.x = constrain(player.pos.x, -2048, 2048);
if (abs(player.pos.y) > 1000) player.pos.y = constrain(player.pos.y, -1000, 1000);
if (abs(player.pos.z) > 2048) player.pos.z = constrain(player.pos.z, -2048, 2048);
gun.run();
collide();
}
void mouseCheck() {
PointerInfo a = MouseInfo.getPointerInfo();
mouse = a.getLocation();
if (!(abs(pmouse.x-mouseX) == 0 && abs(pmouse.y-mouseY) == 0)) {
screen.x = mouse.x-mouseX;
screen.y = mouse.y-mouseY;
if (!keys.getKey("mouseLock")) { //If mouseLock is on
dir.x += (mouse.x-(screen.x+width/2))*MOUSE_SENSITIVITY; //Makes the camera rotate
dir.y += (mouse.y-(screen.y+height/2))*MOUSE_SENSITIVITY;//*10/7;
dir.y = constrain(dir.y, -89, 89);
robot.mouseMove(screen.x+width/2, screen.y+height/2);
}
}
pmouse.x = mouse.x;
pmouse.y = mouse.x;
mouseX = screen.x+width/2; //Should prevent any glitches as mouseX is updated by processing at the beginning of every frame
mouseY = screen.y+height/2;
cam.changeDir(dir.x, dir.y); //changes the camera direction
}
void run() { //Called by main thread evey frame
if (health <= 0) {
deaths++;
health = 100;
pos.set(0, -300, 0);
}
hud.updateItem("fps", nfc(frameRate, 2)); //Updates HUD elements
hud.updateItem("health-bar", health/100);
}
void buffer() {
buffer.startObject(0, id);
buffer.addVal(pos);
buffer.addVal(cam.center);
buffer.addVal(gun.shoot);
gun.shoot = false;
buffer.endObject();
}
void collide() {
try {
for (Integer k : players.keySet()) {
if (players.get(k).bullet) {
hit(players.get(k).pos.copy(), players.get(k).center.copy().normalise());
}
}
}
catch(Exception e) {
e.printStackTrace();
}
}
boolean hit(Vector bpos, Vector dir) {
for (int i = 1; i < 4096; i++) {
for (int j = 0; j < map.geometry.size(); j++) {
if (map.geometry.get(j).collide(bpos)) {
return true;
}
}
if (this.collide(bpos)) {
health -= 5;
return true;
}
bpos.add(dir);
}
return false;
}
void display() { //Draws HUD and camera
hud.display(); //MUST be before camera update
cam.display(size);
gun.display();
}
}