-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayer.pde
84 lines (72 loc) · 1.93 KB
/
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
// Copyright (C) 2018 Dylan Neve <[email protected]>
class Player {
// Player float variables.
float x = displayWidth/2;
float y = displayHeight*0.9;
float leftEdge;
float rightEdge;
float topEdge;
float bottomEdge;
float playerLength = displayWidth*0.1;
float move = displayWidth*0.015;
// Player int variables.
int lives = 10;
// Player boolean variables.
boolean moveLeft = false;
boolean moveRight = false;
// Call all sub-functions.
public void call() {
show();
move();
edgeDetect();
dead();
restrict();
outputMovementToHandler();
mouseReleased();
}
// Show player on screen.
private void show() {
imageMode(CENTER);
image(ship, x, y, playerLength, playerLength);
}
// Takes raw movement input and toggles movement booleans to true if moving.
private void move() {
if (mousePressed == true) {
if (mouseX > displayWidth/2 && mouseY > displayHeight/2 && moveLeft == false) {
moveRight = true;
}
if (mouseX < displayWidth/2 && mouseY > displayHeight/2 && moveRight == false) {
moveLeft = true;
}
}
}
// Calls out of bounds handler.
private void restrict() {
// Sent to handler.
h.playerOutOfBoundsEvent();
}
// If player is dead end the game.
private void dead() {
if (lives <= -1) {
// Send to handler.
h.playerDeadEvent();
}
}
// Create values for the edges of the player.
private void edgeDetect() {
leftEdge = x - playerLength/2;
rightEdge = x + playerLength/2;
topEdge = y - playerLength/2;
bottomEdge = y + playerLength/2;
}
// Takes movement booleans and outputs them to movement handler.
private void outputMovementToHandler() {
// Send to handler.
h.playerMoveEvent(moveLeft, moveRight);
}
// If the mouse (touchscreen) is released reset movement booleans.
private void mouseReleased() {
moveLeft = false;
moveRight = false;
}
}