Skip to content

Commit 6431e5a

Browse files
committed
In progress changes for attacking and enemy spawning
1 parent 4e9e897 commit 6431e5a

File tree

4 files changed

+89
-16
lines changed

4 files changed

+89
-16
lines changed

client/components/cursors.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ export default class Cursors {
33
this.channel = channel
44
this.cursors = scene.input.keyboard.createCursorKeys()
55

6+
this.keys = scene.input.keyboard.addKeys({
7+
w: Phaser.Input.Keyboard.KeyCodes.W,
8+
a: Phaser.Input.Keyboard.KeyCodes.A,
9+
s: Phaser.Input.Keyboard.KeyCodes.S,
10+
d: Phaser.Input.Keyboard.KeyCodes.D
11+
});
12+
613
scene.events.on('update', this.update, this)
714
}
815

@@ -14,18 +21,18 @@ export default class Cursors {
1421
down: false,
1522
none: true
1623
}
17-
if (this.cursors.left.isDown) {
24+
if (this.cursors.left.isDown || this.keys.a.isDown) {
1825
move.left = true
1926
move.none = false
20-
} else if (this.cursors.right.isDown) {
27+
} else if (this.cursors.right.isDown || this.keys.d.isDown) {
2128
move.right = true
2229
move.none = false
2330
}
2431

25-
if (this.cursors.up.isDown) {
32+
if (this.cursors.up.isDown || this.keys.w.isDown) {
2633
move.up = true
2734
move.none = false
28-
} else if (this.cursors.down.isDown) {
35+
} else if (this.cursors.down.isDown || this.keys.s.isDown) {
2936
move.down = true
3037
move.none = false
3138
}

client/scenes/gameScene.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,45 @@ export default class GameScene extends Scene {
3232
new Cursors(this, this.channel)
3333
new Controls(this, this.channel)
3434

35+
// Disable the browser's context menu for right-click
36+
this.input.mouse.disableContextMenu();
37+
38+
// Listen for pointerdown events
39+
this.input.on('pointerdown', function (pointer) {
40+
let attack = {
41+
sword: false,
42+
fireball: false,
43+
none: true
44+
}
45+
if (pointer.leftButtonDown()) {
46+
attack.sword = true
47+
attack.none = false
48+
} else if (pointer.rightButtonDown()) {
49+
attack.fireball = true
50+
attack.none = false
51+
}
52+
if (attack.sword || attack.fireball || attack.none !== this.prevNoAttack) {
53+
let total = 0
54+
if (attack.sword) total += 1
55+
if (attack.fireball) total += 2
56+
let str36 = total.toString(36)
57+
this.channel.emit('playerAttack', str36)
58+
}
59+
}, this); // 'this' ensures the callback context is the scene
60+
3561
FullscreenButton(this)
3662

37-
let addDummyDude = this.add
38-
.text(this.cameras.main.width / 2, this.cameras.main.height / 2 - 100, 'CLICK ME', { fontSize: 48 })
39-
.setOrigin(0.5)
40-
addDummyDude.setInteractive().on('pointerdown', () => {
41-
this.channel.emit('addDummy')
42-
})
63+
// let addDummyDude = this.add
64+
// .text(this.cameras.main.width / 2, this.cameras.main.height / 2 - 100, 'CLICK ME', { fontSize: 48 })
65+
// .setOrigin(0.5)
66+
// addDummyDude.setInteractive().on('pointerdown', () => {
67+
// this.channel.emit('addDummy')
68+
// })
4369

4470
let healthText = this.add.text(0,0, "HP: ", { fontSize: 48 })
4571
let scoreText = this.add.text(0,48, "Score: ", { fontSize: 48 })
46-
let directionText = this.add.text(0,96, "Direction: ", { fontSize: 48 })
72+
let orbText = this.add.text(0,96, "Orbs: ", { fontSize: 48 })
73+
let directionText = this.add.text(0,144, "Direction: ", { fontSize: 48 })
4774

4875
const parseUpdates = updates => {
4976
if (typeof updates === undefined || updates === '') return []
@@ -56,15 +83,16 @@ export default class GameScene extends Scene {
5683
let u2 = []
5784

5885
u.forEach((el, i) => {
59-
if (i % 7 === 0) {
86+
if (i % 8 === 0) {
6087
u2.push({
6188
playerId: u[i + 0],
6289
x: parseInt(u[i + 1], 36),
6390
y: parseInt(u[i + 2], 36),
6491
dead: parseInt(u[i + 3]) === 1 ? true : false,
6592
health: parseInt(u[i + 4], 36),
6693
score: parseInt(u[i + 5], 36),
67-
direction: parseInt(u[i + 6], 36)
94+
direction: parseInt(u[i + 6], 36),
95+
orbs: parseInt(u[i + 7], 36)
6896
})
6997
}
7098
})
@@ -74,7 +102,7 @@ export default class GameScene extends Scene {
74102
const updatesHandler = updates => {
75103
updates.forEach(gameObject => {
76104
//console.log(gameObject)
77-
const { playerId, x, y, dead, health, score, direction } = gameObject
105+
const { playerId, x, y, dead, health, score, direction, orbs } = gameObject
78106
const alpha = dead ? 0 : 1
79107

80108
if (Object.keys(this.objects).includes(playerId)) {
@@ -87,6 +115,7 @@ export default class GameScene extends Scene {
87115
healthText.setText("HP: " + health)
88116
scoreText.setText("Score: " + score)
89117
directionText.setText("Direction: " + direction)
118+
orbText.setText("Orbs: " + orbs)
90119
}
91120
} else {
92121
// if the gameObject does NOT exist,

server/game/components/player.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export class Player extends Phaser.Physics.Arcade.Sprite {
2-
constructor(scene, playerId, x = 200, y = 200, dummy = false, health = 100, score = 0, direction = 4) {
2+
constructor(scene, playerId, x = 200, y = 200, dummy = false, health = 100, score = 0, direction = 4, orbs = 0) {
33
super(scene, x, y, '')
44
scene.add.existing(this)
55
scene.physics.add.existing(this)
@@ -14,10 +14,12 @@ export class Player extends Phaser.Physics.Arcade.Sprite {
1414

1515
this.playerId = playerId
1616
this.move = {}
17+
this.attack = {}
1718

1819
this.health = health
1920
this.score = score
2021
this.direction = direction
22+
this.orbs = orbs
2123

2224
this.setDummy(dummy)
2325

@@ -69,15 +71,19 @@ export class Player extends Phaser.Physics.Arcade.Sprite {
6971

7072
switch(int){
7173
case 1:
74+
// left
7275
this.direction = 1
7376
break;
7477
case 2:
78+
// right
7579
this.direction = 2
7680
break;
7781
case 4:
82+
// up
7883
this.direction = 4
7984
break;
8085
case 8:
86+
// down
8187
this.direction = 8
8288
break;
8389
}
@@ -87,6 +93,18 @@ export class Player extends Phaser.Physics.Arcade.Sprite {
8793
this.move = move
8894
}
8995

96+
setAttack(data) {
97+
let int = parseInt(data, 36)
98+
99+
let attack = {
100+
sword: int === 1,
101+
fireball: int === 2,
102+
none: int === 4
103+
}
104+
105+
this.attack = attack
106+
}
107+
90108
update() {
91109
if (this.move.left) this.setVelocityX(-160)
92110
else if (this.move.right) this.setVelocityX(160)
@@ -95,6 +113,16 @@ export class Player extends Phaser.Physics.Arcade.Sprite {
95113
if (this.move.up) this.setVelocityY(-160)
96114
else if (this.move.down) this.setVelocityY(160)
97115
else this.setVelocityY(0)
116+
117+
if (this.attack.sword) {
118+
//this.setVelocityX(160)
119+
}
120+
else if (this.attack.fireball) {
121+
//this.setVelocityX(-160)
122+
}
123+
else {
124+
//this.setVelocityX(0)
125+
}
98126
}
99127

100128
postUpdate() {

server/game/gameScene.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class GameScene extends Scene {
2424
}
2525

2626
prepareToSync(player) {
27-
return `${player.playerId},${Math.round(player.x).toString(36)},${Math.round(player.y).toString(36)},${player.dead === true ? 1 : 0},${Math.round(player.health).toString(36)},${Math.round(player.score).toString(36)},${Math.round(player.direction).toString(36)},`
27+
return `${player.playerId},${Math.round(player.x).toString(36)},${Math.round(player.y).toString(36)},${player.dead === true ? 1 : 0},${Math.round(player.health).toString(36)},${Math.round(player.score).toString(36)},${Math.round(player.direction).toString(36)},${Math.round(player.orbs).toString(36)},`
2828
}
2929

3030
getState() {
@@ -37,6 +37,7 @@ export class GameScene extends Scene {
3737

3838
create() {
3939
this.playersGroup = this.add.group()
40+
this.enemiesGroup = this.add.group()
4041

4142
const addDummy = () => {
4243
let x = Phaser.Math.RND.integerInRange(50, 800)
@@ -78,6 +79,14 @@ export class GameScene extends Scene {
7879
})
7980
})
8081

82+
channel.on('playerAttack', data => {
83+
this.playersGroup.children.iterate(player => {
84+
if (player.playerId === channel.playerId) {
85+
player.setAttack(data)
86+
}
87+
})
88+
})
89+
8190
channel.on('addPlayer', data => {
8291
let dead = this.playersGroup.getFirstDead()
8392
if (dead) {

0 commit comments

Comments
 (0)