-
Notifications
You must be signed in to change notification settings - Fork 0
/
classPlayer.js
108 lines (69 loc) · 1.61 KB
/
classPlayer.js
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
class Player extends GameObject
{
constructor(x, y, width, height){
super(x, y, width, height)
this.sprite = new Image;
this.flipSprite = new Image;
this.speedY = 0;
this.gravityScale = 0.25 * (Global.blockSize/32);
this.jumpForce = 9.4 * (Global.blockSize/32);
this.maxSpeed = 9.5 * (Global.blockSize/32);
this.grounded = false;
}
start(){
this.sprite.src = 'imgs/player/player.png';
this.flipSprite.src = 'imgs/player/flippedPlayer.png';
}
update(){
this.gravityControl();
this.speedControl();
this.draw();
}
die(){
Global.gameIsLost = true;
Global.gameIsPaused = true;
pauseLoop();
}
draw(){
if(this.gravityScale > 0)
{
Global.ctx.drawImage(this.sprite,this.x,this.y,this.width,this.height);
}
else
{
Global.ctx.drawImage(this.flipSprite,this.x,this.y,this.width,this.height);
}
}
gravityControl(){
if(Math.abs(this.speedY) >= this.maxSpeed)
this.speedY = this.maxSpeed * Math.sign(this.gravityScale);
else
this.speedY += this.gravityScale * Global.deltaTime/10;
}
reverseGravity(){
this.gravityScale *= -1;
this.speedY *= -1;
}
jump()
{
if(this.grounded)
{
if(this.gravityScale > 0)
player.speedY = -player.jumpForce;
else
player.speedY = player.jumpForce;
}
}
setGravity(way)
{
//check if gravity can be altered
if(way == "up" && this.gravityScale > 0)
{
this.reverseGravity();
}
else if (way == "down" && this.gravityScale < 0)
{
this.reverseGravity();
}
}
}