-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.pde
138 lines (120 loc) · 3.55 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
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
//A player in a game.
class Player implements Collider{
final PVector origin; //Where we started
PVector position; //Where we are
PVector velocity; //How fast we're going
float accel = 1.0; //The rate of acceleration
float frict = 0.9; //The rate of normal deceleration if no input is applied.
PlayerController controller; //Who is controlling this player?
color col; //Colour to show
final float size = 20; //How big the player is.
//Holding the enemey player helps the ai player controller to have information.
Player enemy;
LightTrail trail; //The trail that will follow the player to block the opponent.
boolean alive = true; //Checks if the player is alive.
//Constructors
public Player(PVector start, PlayerController action, color team){
origin = start.copy(); //The origin is where we start
position = start.copy(); //We also start there
velocity = new PVector(0, 0); //Start at 0 velocity.
controller = action; //The player controller will tell this player where to go.
col = team;
trail = new LightTrail(col, position); //Init the light trail
//System.out.println("Starting at " + origin.x + ", " + origin.y);
}
//Updates logic
public void update(){
if(alive){ //If we are alive
int input = controller.getDirection(this); //Ask the controller which way to go
if(input == 0){ //Then act based on it.
velocity.x += accel; //Right
velocity.y = 0;
}else if(input == 1){
velocity.x -= accel; //Left
velocity.y = 0;
}else if(input == 2){
velocity.y += accel; //Down
velocity.x = 0;
}else if(input == 3){
velocity.y -= accel; //Up
velocity.x = 0;
}
//Then move the player.
position.add(velocity);
velocity.mult(frict);
//Then run the trail
trail.update(position, input);
}
}
public void show(){ //Draws to screen
noStroke(); //Might enable for visability.
fill(col);
if(alive){ //Only render if we are alive
rect(position.x-size/2, position.y-size/2, size, size);
}
trail.showAll(); //But always show the trail.
}
//Used to check if the opponent has hit this player
public boolean impactPlayer(Collider check){
if(collides(check, this)){
return true;
}else{
return false;
}
}
//Used to check if the opponent has hit this trail
public boolean impactTrail(Collider check){
if(trail.overlap(check)){
return true;
}else{
return false;
}
}
//Used to check if we have hit our own trail.
public boolean impactOwnTrail(){
if(trail.collideOwn(this)){
return true;
}else{
return false;
}
}
public int getTurnsMade(){
return trail.trail.size();
}
//Kills the player and sets it up for respawn.
void die(){
alive = false;
position = origin.copy();
}
//Implements collision logic.
@Override
public float getX(){
return position.x-size/2;
}
@Override
public float getY(){
return position.y-size/2;
}
@Override
public float getX2(){
return position.x + size/2;
}
@Override
public float getY2(){
return position.y + size/2;
}
//Supposed to calculate how far the player has gone but doesn't work.
public float getDistance(){
float x = fAbs(position.x-origin.x);
float y = fAbs(position.y-origin.y);
double hsq = Math.pow(x, 2) + Math.pow(y, 2);
return (float) Math.sqrt(hsq);
}
public float fAbs(float fin){
if(fin < 0){
return -fin;
}else{
return fin;
}
}
}