-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.pde
108 lines (97 loc) · 2.82 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
//Represents the player avatar and has functions for controloing its movements with the mouse keys, updating its movements, and being drawn to the screen.
class Player implements ICharacter{
int sizeValue; //For ease of size adjustment and for use in collision detection.
PShape avatar;
//movement
PVector position;
PVector orientation;
PVector velocity;
PVector acceleration;
//Basic constuctor
Player(){
position = new PVector(width/2,height/2);
orientation = new PVector(1,0);
orientation.limit(1);
velocity = new PVector(0,0);
velocity.limit(3);
acceleration = new PVector(0,0);
sizeValue = 5; //For ease of adjustment
avatar = createShape();
avatar.beginShape();
avatar.noFill();
avatar.vertex(-1*sizeValue,0);
avatar.vertex(-2*sizeValue,1.5*sizeValue);
avatar.vertex(2*sizeValue,0);
avatar.vertex(-2*sizeValue,-1.5*sizeValue);
avatar.vertex(-1*sizeValue,0);
avatar.endShape();
avatar.setStroke(255);
}
//Updates the player's location and oreintation.
void update(){
//acceleration, velocity, and position
boolean noForce = true;
if(keyPressed){
PVector direction = orientation.copy();
if(keyCode == LEFT){
acceleration = direction.rotate(-1*HALF_PI);
noForce = false;
}else if(keyCode == RIGHT){
acceleration = direction.rotate(HALF_PI);
noForce = false;
}else if(keyCode == UP){
acceleration = direction;
noForce = false;
}else if(keyCode == DOWN){
acceleration = direction.rotate(PI);
noForce = false;
}
}
if(noForce){
acceleration = PVector.mult(velocity,-1);
acceleration.normalize();
}
if(power == Powerup.SPEEDY){
acceleration.div(8);
velocity.add(acceleration);
velocity.limit(3.5);
}else{
acceleration.div(15);
velocity.add(acceleration);
velocity.limit(3);
}
position.add(velocity);
if(position.x < 0){
position.x = width;
} else if(position.x > width){
position.x = 0;
}
if(position.y < 0){
position.y = height;
} else if(position.y > height){
position.y = 0;
}
//orientation
PVector mVector = new PVector(mouseX,mouseY);
orientation = PVector.sub(mVector,position);
orientation.normalize();
}
//Draws the player on the screen.
void draw(){
pushMatrix();
translate(position.x,position.y);
rotate(orientation.heading());
color outline = color(255,255-(immunityTimer*2),255-(immunityTimer*2));
avatar.setStroke(outline);
shape(avatar);
popMatrix();
}
//returns the players's position
PVector position(){
return position;
}
//returns the the distence between the ships center and its tail or tip.
int radius(){
return sizeValue;
}
}