-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.js
More file actions
107 lines (91 loc) · 2.71 KB
/
player.js
File metadata and controls
107 lines (91 loc) · 2.71 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
// player.js (inalterado - mas incluindo para referência)
class Player {
constructor(id) {
this.id = id;
this.name = "";
this.rotX = 0;
this.rotY = 0;
this.posX = 0.0;
this.posY = 0.0;
this.posZ = 0.0;
this.animationIndex = 0;
// 2 slots fixos para IDs de armas
this.slots = [null, null];
this.currentSlotIndex = 0;
this.maxHealth = 100;
this.health = this.maxHealth;
this.isAlive = true;
}
toJSON() {
return {
id: this.id,
name: this.name,
rotX: this.rotX,
rotY: this.rotY,
posX: this.posX,
posY: this.posY,
posZ: this.posZ,
animationIndex: this.animationIndex,
slots: this.slots,
currentSlotIndex: this.currentSlotIndex,
health: this.health,
maxHealth: this.maxHealth,
isAlive: this.isAlive
};
}
fromJSON(json) {
if (json.name !== undefined) this.name = json.name;
if (json.rotX !== undefined) this.rotX = json.rotX;
if (json.rotY !== undefined) this.rotY = json.rotY;
if (json.posX !== undefined) this.posX = json.posX;
if (json.posY !== undefined) this.posY = json.posY;
if (json.posZ !== undefined) this.posZ = json.posZ;
if (json.animationIndex !== undefined) this.animationIndex = json.animationIndex;
if (json.currentSlotIndex !== undefined) this.currentSlotIndex = json.currentSlotIndex;
return this;
}
// Pega uma arma e coloca em slot vazio
pick(weaponId) {
for (let i = 0; i < this.slots.length; i++) {
if (this.slots[i] === null) {
this.slots[i] = weaponId;
return i;
}
}
return -1;
}
// Larga a arma do slot atual
drop() {
const weaponId = this.slots[this.currentSlotIndex];
if (weaponId === null) return null;
this.slots[this.currentSlotIndex] = null;
return weaponId;
}
hit(amount) {
if (!this.isAlive) return false;
this.health -= amount;
if (this.health <= 0) {
this.die();
return true;
}
return false;
}
heal(amount) {
if (!this.isAlive) return false;
this.health = Math.min(this.maxHealth, this.health + amount);
return true;
}
die() {
this.isAlive = false;
this.health = 0;
}
respawn(x, y, z) {
this.isAlive = true;
this.health = this.maxHealth;
this.posX = x;
this.posY = y;
this.posZ = z;
return true;
}
}
module.exports = Player;