Skip to content

Commit 131b32d

Browse files
committed
health and score working
1 parent c6d96ae commit 131b32d

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

client/scenes/gameScene.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ export default class GameScene extends Scene {
4141
this.channel.emit('addDummy')
4242
})
4343

44+
let healthText = this.add.text(0,0, "HP: ", { fontSize: 48 })
45+
let scoreText = this.add.text(0,48, "Score: ", { fontSize: 48 })
46+
4447
const parseUpdates = updates => {
4548
if (typeof updates === undefined || updates === '') return []
4649

@@ -51,12 +54,14 @@ export default class GameScene extends Scene {
5154
let u2 = []
5255

5356
u.forEach((el, i) => {
54-
if (i % 4 === 0) {
57+
if (i % 6 === 0) {
5558
u2.push({
5659
playerId: u[i + 0],
5760
x: parseInt(u[i + 1], 36),
5861
y: parseInt(u[i + 2], 36),
59-
dead: parseInt(u[i + 3]) === 1 ? true : false
62+
dead: parseInt(u[i + 3]) === 1 ? true : false,
63+
health: parseInt(u[i + 4], 36),
64+
score: parseInt(u[i + 5], 36)
6065
})
6166
}
6267
})
@@ -65,7 +70,7 @@ export default class GameScene extends Scene {
6570

6671
const updatesHandler = updates => {
6772
updates.forEach(gameObject => {
68-
const { playerId, x, y, dead } = gameObject
73+
const { playerId, x, y, dead, health, score } = gameObject
6974
const alpha = dead ? 0 : 1
7075

7176
if (Object.keys(this.objects).includes(playerId)) {
@@ -74,6 +79,10 @@ export default class GameScene extends Scene {
7479
let sprite = this.objects[playerId].sprite
7580
sprite.setAlpha(alpha)
7681
sprite.setPosition(x, y)
82+
if (this.playerId == playerId){
83+
healthText.setText("HP: " + health)
84+
scoreText.setText("Score: " + score)
85+
}
7786
} else {
7887
// if the gameObject does NOT exist,
7988
// create a new gameObject

server/game/components/player.js

Lines changed: 4 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) {
2+
constructor(scene, playerId, x = 200, y = 200, dummy = false, health = 100, score = 0) {
33
super(scene, x, y, '')
44
scene.add.existing(this)
55
scene.physics.add.existing(this)
@@ -15,6 +15,9 @@ export class Player extends Phaser.Physics.Arcade.Sprite {
1515
this.playerId = playerId
1616
this.move = {}
1717

18+
this.health = health
19+
this.score = score
20+
1821
this.setDummy(dummy)
1922

2023
this.body.setSize(32, 48)

server/game/gameScene.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ 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)},${
28-
player.dead === true ? 1 : 0
29-
},`
27+
return `${player.playerId},${Math.round(player.x).toString(36)},${Math.round(player.y).toString(36)},
28+
${player.dead === true ? 1 : 0},${Math.round(player.health).toString(36)},${Math.round(player.score).toString(36)},`
3029
}
3130

3231
getState() {

0 commit comments

Comments
 (0)