-
Notifications
You must be signed in to change notification settings - Fork 0
/
battle.js
142 lines (111 loc) · 3.31 KB
/
battle.js
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
class Spell {
constructor(name,desc,func,damage) {
this.name = name;
this.desc = desc;
this.damage = damage;
this.cast = func;
}
}
class Character {
constructor(name,fileName,proficiency,goodGuy,spells,damage,speed,maxHP) {
this.name = name;
this.fileName = fileName;
this.proficiency = proficiency;
this.goodGuy = goodGuy;
this.spells = spells;
this.damage = damage;
this.speed = speed;
this.maxHP = maxHP;
this.hp = maxHP;
this.buffs = [];
}
};
class GoodGuy extends Character {
constructor(name,fileName,proficiency,spells,speed,maxHP) {
super(name,fileName,proficiency,true,spells,1,speed,maxHP);
this.activeWeapon = '';
}
};
class BadGuy extends Character {
constructor(name,fileName,proficiency,spells,damage,speed,maxHP) {
super(name,fileName,proficiency,false,spells,damage,speed);
}
};
var battle = {
active:false,
characters:{
'mark': new GoodGuy('Mark','mark','melee',[],6,80),
'owen': new GoodGuy('Owen','owen','',[
new Spell('Computer Virus','Infect all enemies with the "Computer Virus" debuff, decreasing attack power for a few turns',(enemy,damage)=>{
activeCharacters.forEach((individual)=>{
if (!individual.goodGuy) {
individual.buffs.push('computerVirus');
};
});
},0),
new Spell('Debug','Cure a friend of most ailments. Effective 75% of the time',()=>{},0)
],8,60),
'ciaAgent': new BadGuy('CIA Agent','ciaAgent','melee',[],8,4,100)
}
};
battle.characters.mark.activeWeapon = 0;
class Battle {
constructor(memberList,location) {
this.memberList = memberList;
this.location = location;
}
updateCurrentMember() {
this.currentMember = this.memberList[this.currentMemberIndex];
}
takeTurn(action) {
this.updateCurrentMember();
switch (action.type) {
case 'useItem':
let item = items[inventory[action.itemId]];
console.log(item);
if (item.hpRestore || item.hpRestore == 0) { // Eat food
// Increase target's HP
this.currentMember.hp += item.hpRestore;
if (this.currentMember.hp > this.currentMember.maxHP) {
this.currentMember.hp = this.currentMember.maxHP;
};
// Remove the food
inventory.splice(action.itemId,1);
} else { // Can't use item
};
break;
};
this.currentMemberIndex = (this.currentMemberIndex + 1) % this.memberList.length;
}
initiate() {
battle.active = true;
this.currentMemberIndex = 0;
// Order by speed
this.memberList.sort((a,b)=>{return b.speed - a.speed});
}
render() {
// Draw background
ctx.drawImage(graphics.images['battle/backgrounds/' + this.location + '.png'],0,0);
// Get good guys
let goodGuys = [];
this.memberList.forEach((member)=>{
if (member.goodGuy) {
goodGuys.push(member);
};
});
// Get bad guys
let badGuys = [];
this.memberList.forEach((member)=>{
if (!member.goodGuy) {
badGuys.push(member);
};
});
// Now draw each
goodGuys.forEach((member,index)=>{
ctx.drawImage(graphics.images['battle/' + member.fileName + '/idle.png'],30,c.height / 4 + (index * 80),48*2,48*2);
});
badGuys.forEach((member,index)=>{
ctx.drawImage(graphics.images['battle/' + member.fileName + '/idle.png'],c.width - 60 - 48,c.height / 4 + (index * 80),48*2,48*2);
});
}
};