-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.js
More file actions
203 lines (170 loc) · 7.16 KB
/
player.js
File metadata and controls
203 lines (170 loc) · 7.16 KB
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
import {} from './main.js';
import {testcollide} from './testcollide.js';
export{playermove}
var stopjump = false
function playermove(timerfall, jump, walljumptimer, nbjump, walljump, player, platforme, myGameArea) { // Player movements
var testbas = "";
for (var i = 0; i < platforme.length; i++) { //
var testbas = testcollide(player, platforme[i])
if (testbas == "bas") {
break
} else {
testbas = ""
}
}
//-------------------------- Manages jumps/double jump --------------------------------------------------
if (myGameArea.keys && myGameArea.keys[32]) {
if (testbas == "bas"){
if (nbjump == 0 && stopjump == false && jump == true){ //&& (timerfall > 100 || timerfall == 0) ) { // First jump
timerfall++;
player.accumulationY = -150; // Adds a Y force allowing the jump
var audio = new Audio('\\sprite\\Audio\\jump2.wav'); // Jump audio
audio.play();
nbjump++ // Jump amount goes up
timerfall = 1;
jump = false
walljumptimer = 0
stopjump = true
}
} else {
if ((nbjump == 0 && (timerfall > 12 ) ) || (nbjump == 1 && jump == true && walljump == false ) ) { // Second jump
var audio = new Audio('sprite\\Audio\\jump2.wav');
audio.play(); // Jump audio
player.accumulationY = -160; // Adds a Y force allowing the jump
timerfall = 1;
jump = false;
nbjump++; // Jump amount goes up
if (nbjump == 1){
nbjump++;
}
walljumptimer = 0
}
}
} else {
stopjump = false
jump = true;
}
// ------------------------------------------- Manage wall jumps ------------------------------------------------
for (var i = 0; i < platforme.length; i++) { //
var collidepos = testcollide(player, platforme[i])
if (collidepos != null) {
if (platforme[i].walljump == true) {
if (player.x + player.width - 3 < platforme[i].x ) {
walljump = true
if (myGameArea.keys && myGameArea.keys[32] && jump == true) {
var audio = new Audio('sprite\\Audio\\DoubleJump.wav'); // Jump audio
audio.play();
player.accumulationY = -110; // Adds a Y force allowing the jump
player.speedX = -20; // Adds a X force allowing the jump
timerfall = 1;
walljump = false
jump = false
walljumptimer = 1 // Cooldown timer for X movement after a wall jump
}
} else if (player.x + 3 > platforme[i].x + platforme[i].width) {
walljump = true
if (myGameArea.keys && myGameArea.keys[32] && jump == true) {
var audio = new Audio('sprite\\Audio\\Jump.wav'); // Jump audio
audio.play();
player.accumulationY = -110; // Adds a Y force allowing the jump
player.speedX = 20; // Adds a X force allowing the jump
timerfall = 1;
walljump = false
jump = false
walljumptimer = 1 // Cooldown timer for X movement after a wall jump
}
} else {
walljump = false
player.speedX = 0
}
}
}
}
var test = false;
for (var i = 0; i < platforme.length; i++) { //
var collidepos = testcollide(player, platforme[i])
if (collidepos != null) {
test = true;
break
}
}
if (test == false){
walljump = false;
}
// Increase timers
if (walljumptimer > 0) {
walljumptimer++;
}
if (timerfall > 0) {
timerfall++
}
player.speedY = player.accumulationY // Increase Y speed depending on the forces applied by the jumps
var v0 = -player.speedY //+ player.speedX;
player.speedY = 6 * timerfall / 16.7 + v0 / 16.5 * Math.sin(180) // Calculate the Y speed depending on hourly equations
if (myGameArea.keys && myGameArea.keys[81]) { // If the player presses Q he moves to the left
if (player.accumulationX > 0) {
player.accumulationX = 0
}
player.accumulationX -= 0.5;// Increase the speed towards the left
} else if (myGameArea.keys && myGameArea.keys[68]) { // If the player presses D he moves to the right
if (player.accumulationX < 0) {
player.accumulationX = 0
}
player.accumulationX += 0.5; // Increase the speed towards the right
} else {
player.accumulationX = 0
if (walljumptimer == 0) {
player.speedX = 0 //---------------------- Remove to get kinetics in jumps -----------------------------------------------------------
}
}
if ((myGameArea.keys && myGameArea.keys[81]) && (myGameArea.keys && myGameArea.keys[68])) {
}
if (walljumptimer == 0 || walljumptimer > 15) {
player.speedX += player.accumulationX
} else {
player.accumulationX = 0
}
if (player.speedX > 5) {
player.speedX = 5
}
if (player.speedX < -5) {
player.speedX = -5
}
//---------------------------------------------------------- Manage the sprites depending on the player's actions---------------------------------------------------------
if (player.speedX > 0) {
player.image = "PlayerRunningRight"
} else if (player.speedX < 0) {
player.image = "PlayerRunningLeft"
} else {
player.image = "PlayerStandingFace"
}
if (timerfall > 1) {
if (player.speedX > 0) {
player.image = "PlayerJumpRight"
} else if (player.speedX < 0) {
player.image = "PlayerJumpLeft"
}
}
for (var i = 0; i < platforme.length; i++) { // Allows to prevent the player from glitching into walls
var collidepos = testcollide(player, platforme[i]) // Tests the collision between players and obstacles
if (collidepos != null) {
console.log(player.x, player.width)
console.log(platforme[i].x,platforme[i].width)
if (collidepos == "gauche") {
// player.x = platforme[i].x - player.width;
if (player.speedX > 0) {
player.speedX = 0
// player.accumulationX = 0
}
}
if (collidepos == "droite") {
// player.x = platforme[i].x + platforme[i].width;
if (player.speedX < 0) {
player.speedX = 0
// player.accumulationX=0
}
}
}
}
return [timerfall, jump, walljumptimer, nbjump, walljump, player, platforme]
}