-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
244 lines (229 loc) · 10.3 KB
/
controller.js
File metadata and controls
244 lines (229 loc) · 10.3 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Player1Controls objects
let upPlayer1Button;
let downPlayer1Button;
let leftPlayer1Button;
let rightPlayer1Button;
let attackPlayer1Button;
let switchPlayer1Button;
// Player2Controls objects
let upPlayer2Button;
let downPlayer2Button;
let leftPlayer2Button;
let rightPlayer2Button;
let attackPlayer2Button;
let switchPlayer2Button;
// Music Player1Controls
let musicMuteButton;
let musicPlus1Button;
let musicPlus2Button;
let musicMinus1Button;
let musicMinus2Button;
let controller = {
prepController: function () {
cursors = game.input.keyboard.createCursorKeys();
game.input.mouse.capture = true;
},
// Set player 1 controls
player1Controls: function() {
// Keyboard controls
upPlayer1Button = game.input.keyboard.addKey(Phaser.Keyboard.W);
downPlayer1Button = game.input.keyboard.addKey(Phaser.Keyboard.S);
leftPlayer1Button = game.input.keyboard.addKey(Phaser.Keyboard.A);
rightPlayer1Button = game.input.keyboard.addKey(Phaser.Keyboard.D);
attackPlayer1Button = game.input.keyboard.addKey(Phaser.Keyboard.F);
switchPlayer1Button = game.input.keyboard.addKey(Phaser.Keyboard.E);
resetGameButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
// Xbox controller controls
},
// Set player 2 controls
player2Controls: function() {
// Keyboard controls
upPlayer2Button = game.input.keyboard.addKey(Phaser.Keyboard.UP);
downPlayer2Button = game.input.keyboard.addKey(Phaser.Keyboard.DOWN);
leftPlayer2Button = game.input.keyboard.addKey(Phaser.Keyboard.LEFT);
rightPlayer2Button = game.input.keyboard.addKey(Phaser.Keyboard.RIGHT);
attackPlayer2Button = game.input.keyboard.addKey(Phaser.Keyboard.L);
// Xbox controller controls
},
musicControls: function() {
// Keyboard controls
musicMuteButton = game.input.keyboard.addKey(Phaser.Keyboard.P);
musicPlus1Button = game.input.keyboard.addKey(Phaser.Keyboard.EQUALS);
musicPlus2Button = game.input.keyboard.addKey(Phaser.Keyboard.PLUS);
musicMinus1Button = game.input.keyboard.addKey(Phaser.Keyboard.MINUS);
musicMinus2Button = game.input.keyboard.addKey(Phaser.Keyboard.UNDERSCORE);
// Add callbacks to control the music volume
musicMuteButton.onDown.add(audio.muteMusicVolume, this);
musicPlus1Button.onDown.add(audio.increaseMusicVolume, this);
musicPlus2Button.onDown.add(audio.increaseMusicVolume, this);
musicMinus1Button.onDown.add(audio.decreaseMusicVolume, this);
musicMinus2Button.onDown.add(audio.decreaseMusicVolume, this);
},
controlAllPlayers: function (player1Group, player2Group) {
// Control movement and animations for player 1
if (player1Group.length > 0) {
player1Group.forEach(function(player) {controller.controlPlayer(player, player1Group);}, this);
}
// Control movement and animations for player 2
if (player2Group.length > 0) {
player2Group.forEach(function(player) {controller.controlPlayer(player, player2Group);}, this);
}
},
// When both players attack at the same time
clashPlayers: function (hitbox1, hitbox2) {
// These are not really player1 and player2, could be swapped?
let p1Group = hitbox1.player.group;
let p2Group = hitbox2.player.group;
if (p1Group === player1Group) {
p1group = player1Group;
} else {
p1group = player2Group;
}
if (p2Group === player1Group) {
p2group = player1Group;
} else {
p2group = player2Group;
}
//console.log('clashing?');
//hitbox1.destroy();
//hitbox2.destroy();
if (p1Group.length > 0) {
//console.log('wut?');
p1Group.forEach(function(p) {
if (!p) return;
p.isClashing = true;
p.body.velocity.y = -10;
//p.body.bounce.y = 0.5
if (p.playerDirection == 'left') { // if facing left then move player to the right +num
p.body.velocity.x = 150;
} else if (p.playerDirection == 'right') { // if facing right then move player to the left -num
p.body.velocity.x = -150;
}
}, this);
}
if (p2Group.length > 0) {
p2Group.forEach(function(p) {
if (!p) return;
p.isClashing = true;
p.body.velocity.y = -10;
//p.body.bounce.y = 0.5;
if (p.playerDirection == 'left') { // if facing left then move player to the right +num
p.body.velocity.x = 150;
} else if (p.playerDirection == 'right') { // if facing right then move player to the left -num
p.body.velocity.x = -150;
}
}, this);
}
},
controlPlayer: function(player, group) {
let upButton;
let downButton;
let leftButton;
let rightButton;
let attackButton;
// Function accept the character (knight, mech, etc) and "player1" or "player2"
if (group === player1Group) {
upButton = upPlayer1Button;
downButton = downPlayer1Button;
leftButton = leftPlayer1Button;
rightButton = rightPlayer1Button;
attackButton = attackPlayer1Button;
} else if (group == player2Group) {
upButton = upPlayer2Button;
downButton = downPlayer2Button;
leftButton = leftPlayer2Button;
rightButton = rightPlayer2Button;
attackButton = attackPlayer2Button;
}
// PLatform collisions are based on player only
if (!downButton.isDown) {
let platformCollision = game.physics.arcade.collide(player, platformGroup);
}
// Reset the players velocity (movement)
if(!player.isClashing) {
player.body.velocity.x = 0;
}
if (attackButton.isDown || player.isAttacking) {
// Attacking
if (!player.isAttacking) {
player.isAttacking = true;
player.attackTimer2 = game.time.create(true);
player.attackTimer2.loop(300, characters.attackPlayer, this, player);
player.attackTimer2.start();
//attackPlayer(player);
}
if (player.isClashing && player.playerDirection == 'left') {
player.animations.play(player.playerStage + '_fall_left');
} else if (player.isClashing && player.playerDirection == 'right') {
player.animations.play(player.playerStage + '_fall_right');
} else if (player.playerDirection == 'left') {
player.animations.play(player.playerStage + '_attack_left');
} else if (player.playerDirection == 'right') {
player.animations.play(player.playerStage + '_attack_right');
}
} else if (leftButton.isDown) {
// Move to the left
player.body.velocity.x = -player.playerMoveSpeed;
player.playerDirection = 'left';
if (player.body.velocity.y < player.playerJumpSensitivity || !(player.body.touching.down || player.body.blocked.down)) {
if (player.body.velocity.y < player.playerJumpSensitivity || player.isJumping) {
player.animations.play(player.playerStage + '_jump_left');
} else {
player.animations.play(player.playerStage + '_fall_left');
}
} else {
//audio.playSFXRun();
audio.audioRunCheck(player);
player.animations.play(player.playerStage + '_walk_left');
}
} else if (rightButton.isDown) {
// Move to the right
player.body.velocity.x = player.playerMoveSpeed;
player.playerDirection = 'right';
if (player.body.velocity.y < player.playerJumpSensitivity || !(player.body.touching.down || player.body.blocked.down)) {
if (player.body.velocity.y < player.playerJumpSensitivity || player.isJumping) {
player.animations.play(player.playerStage + '_jump_right');
} else {
player.animations.play(player.playerStage + '_fall_right');
}
} else {
//udio.playSFXRun();
audio.audioRunCheck(player);
player.animations.play(player.playerStage + '_walk_right');
}
} else {
// Standing still idle
if (player.playerDirection == 'left') {
if (player.body.velocity.y < player.playerJumpSensitivity) {
player.animations.play(player.playerStage + '_jump_left');
} else if (!(player.body.touching.down || player.body.blocked.down)) {
player.animations.play(player.playerStage + '_fall_left');
} else {
//audio.pauseSFXRun();
audio.audioRunPauseCheck(player);
player.animations.play(player.playerStage + '_idle_left');
}
} else if (player.playerDirection == 'right') {
if (player.body.velocity.y < player.playerJumpSensitivity) {
player.animations.play(player.playerStage + '_jump_right');
} else if (!(player.body.touching.down || player.body.blocked.down)) {
player.animations.play(player.playerStage + '_fall_right');
} else {
//audio.pauseSFXRun();
audio.audioRunPauseCheck(player);
player.animations.play(player.playerStage + '_idle_right');
}
}
}
// Allow the player to jump if they are touching the ground + dynamic height
if (!upButton.isDown && player.playerJumping && player.body.velocity.y < 0) {
player.body.velocity.y = player.body.velocity.y * 0.5;
player.playerJumping = false;
} else if (upButton.isDown && (player.body.touching.down && !player.body.touching.up && player.isGrounded )) {
//audio.playSFXLand();
player.body.velocity.y = player.playerJumpSpeed;
player.playerJumping = true;
player.isGrounded = false;
}
}
};