-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fight.js
225 lines (202 loc) · 8.7 KB
/
Fight.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
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
var FightStatus = {
MainMenu: {numTitles: 2},
Actions: { numTitles: 4 },
InFightAtk: { numTitles: 1 },
InFightDef: { numTitles: 1 },
OnWin: { numTitles: 1 },
OnLoose: { numTitles: 1 },
OnReturn: { numTitles: 1 }
};
function Fight(stockemon, enemy, world, enemyTile) {
this.stockemon = stockemon;
this.enemy = enemy;
this.world = world;
this.enemyTile = enemyTile;
this.selected = 1;
this.countdownKeys = 0;
this.text = "Oh, ein" + (enemy.gender == "f"? "e " : " ") + enemy.name + "!\nZeit für eine Tracht Prügel!";
this.fightStatus = FightStatus.MainMenu;
this.numTitles = 0;
this.justSwitchedMenu = false;
this.OnFight = function() {
this.fightStatus = FightStatus.Actions;
this.selected = 3;
}
this.OnFlee = function() {
//TODO: try to run *muhaha*
this.text = "Du kannst nicht fliehen.\nDu bist ein " + this.stockemon.name+"!";
}
this.Draw = function (canvas) {
canvas.fillStyle = "#ff9001";
// Draw the enemies status
var partHP = this.enemy.hp_current / this.enemy.hp_max;
var bar = {};
bar.x = 50 + 500;
bar.y = 175;
bar.w = 500 * partHP;
bar.h = 30;
DrawScaledPos(canvas, globalImageHandler.GetImage("UIbar"), bar);
var ui = {};
ui.x = 0;
ui.y = 100;
ui.w = 1100;
ui.h = 200;
DrawScaledPos(canvas, globalImageHandler.GetImage("UIleft"), ui);
DrawScaledText(canvas, "" + this.enemy.hp_current + "/" + this.enemy.hp_max, 1050, 205, 15, "right");
DrawScaledText(canvas, this.enemy.name, 550, 110, 40, "left");
DrawScaledText(canvas, "Lv: " + this.enemy.lvl, 1050, 150, 20, "right");
var pic = {};
pic.x = 1400;
pic.y = 100;
pic.w = 400;
pic.h = 400;
if(this.enemy.hp_current > 0)
DrawScaledPos(canvas, globalImageHandler.GetImage(this.enemy.name), pic);
// Draw the own stock's status
partHP = this.stockemon.hp_current / this.stockemon.hp_max;
var ep = this.stockemon.epTillLvlUp;
bar.x = 1350;
bar.y = 675;
bar.w = 500 * partHP;
bar.h = 30;
if(this.fightStatus != FightStatus.OnLoose)
DrawScaledPos(canvas, globalImageHandler.GetImage("UIbar"), bar);
var ui = {};
ui.x = 1300;
ui.y = 600;
ui.w = 600;
ui.h = 200;
DrawScaledPos(canvas, globalImageHandler.GetImage("UIright"), ui);
DrawScaledText(canvas, "" + this.stockemon.hp_current + "/" + this.stockemon.hp_max, 1850, 705, 15, "right");
DrawScaledText(canvas, this.stockemon.name, 1350, 610, 40, "left");
DrawScaledText(canvas, "Lv: " + this.stockemon.lvl, 1850, 650, 20, "right");
DrawScaledText(canvas, "" + ep + " EP zum nächsten Level", 1840, 760, 20, "right");
pic.x = 600;
pic.y = 400;
pic.w = 600;
pic.h = 600;
if (this.stockemon.hp_current > 0)
DrawScaledPos(canvas, globalImageHandler.GetImage(this.stockemon.name), pic);
// Draw left menu
var titles = [];
this.numTitles = this.fightStatus.numTitles;
switch (this.fightStatus) {
case FightStatus.Actions:
for (var i = 0; i < 4; ++i)
titles[i] = this.stockemon.actions[i].name;
break;
case FightStatus.MainMenu:
titles[0] = "Fliehen";
titles[1] = "Kämpfen";
break;
default:
titles[0] = "Weiter";
break;
};
for (var i = 0; i < this.numTitles; ++i) {
var pos = new Object();
pos.x = 0;
pos.y = 1000 - ((i + 2) * 200);
pos.w = 500;
pos.h = 200;
var img = this.selected == i ? globalImageHandler.GetImage("UIon") : globalImageHandler.GetImage("UIoff");
if (this.selected == i) canvas.fillStyle = "#555555";
DrawScaledPos(canvas, img, pos);
DrawScaledText(canvas, titles[i], 50, pos.y + 50, 70, "left");
canvas.fillStyle = "#ff9001";
}
// Draw text box
ui.x = 0;
ui.y = 800;
ui.w = 1200;
ui.h = 200;
DrawScaledPos(canvas, globalImageHandler.GetImage("UItext"), ui);
DrawScaledText(canvas, this.text, 30, 830, 50, "left");
canvas.fillStyle = "#ffffff";
}
this.Update = function (keys, timeSinceLastUpdate) {
if (this.justSwitchedMenu && !keys[13] && !keys[32])
this.justSwitchedMenu = false;
if (this.countdownKeys > 0) {
this.countdownKeys -= timeSinceLastUpdate;
return this;
}
if ((keys[13] || keys[32]) && !this.justSwitchedMenu) {
this.justSwitchedMenu = true;
var article;
switch (this.enemy.gender) {
case "f": article = "Die "; break;
case "m": article = "Der "; break;
case "n": article = "Das "; break;
}
switch (this.fightStatus) {
case FightStatus.MainMenu:
if (this.selected == 0) this.OnFlee();
if (this.selected == 1) this.OnFight();
break;
case FightStatus.Actions:
var dmg = this.stockemon.attackEnemy(this.stockemon.actions[this.selected], this.enemy);
//var poison = this.enemy.onTick();
this.text = "Du setzt " + this.stockemon.actions[this.selected].name + " ein.\nDer Gegner verliert " + dmg + " Lebenspunkte.";
this.fightStatus = FightStatus.InFightAtk;
if (this.enemy.hp_current == 0)
this.fightStatus = FightStatus.OnWin;
this.selected = 0;
break;
case FightStatus.InFightDef:
this.fightStatus = FightStatus.MainMenu;
this.selected = 1;
this.text = "";
break;
case FightStatus.InFightAtk:
var enemyAttack = this.enemy.actions[Math.floor(Math.random() * 4)]
var dmg = this.enemy.attackEnemy(enemyAttack, this.stockemon);
//var poison = this.stockemon.onTick();
this.text = article + this.enemy.name + " setzt " + enemyAttack.name + " ein.\nDu verlierst " + dmg + " Lebenspunkte.";
this.fightStatus = FightStatus.InFightDef;
if (this.stockemon.hp_current == 0)
this.fightStatus = FightStatus.OnLoose;
break;
case FightStatus.OnWin:
this.text = article + this.enemy.name + " ist zerbrochen. \nDu bekommst " + this.enemy.epOnDeath + " EP.";
this.fightStatus = FightStatus.OnReturn;
this.stockemon.getEP(this.enemy.epOnDeath);
if (this.enemyTile) {
for (var i = 0; i < this.enemyTile.weapons.length - 1; ++i) {
this.enemyTile.weapons[i] = this.enemyTile.weapons[i + 1];
}
this.enemyTile.weapons.length--;
if (this.enemyTile.weapons.length == 0) {
if (this.enemyTile.type.value == 16)
return new Win();
this.enemyTile.type = TileType.MUD;
}
}
break;
case FightStatus.OnLoose:
this.text = "Du wurdest besiegt!\n" + article + this.enemy.name + " zieht lachend davon.";
this.world.playerX = 25;
this.world.playerY = 295;
this.stockemon.epTillLvlUp *= 10;
this.stockemon.heal();
this.fightStatus = FightStatus.OnReturn;
break;
case FightStatus.OnReturn:
for (var key in keys) {
keys[key] = false;
}
return this.world;
};
}
if (keys[37] || keys["A".charCodeAt(0)] || keys[38] || keys["W".charCodeAt(0)]) {
this.selected = (this.selected + 1) % this.numTitles;
this.countdownKeys = 0.1;
} else
if (keys[39] || keys["D".charCodeAt(0)] || keys[40] || keys["S".charCodeAt(0)]) {
this.selected = (this.selected - 1 + this.numTitles) % this.numTitles;
this.countdownKeys = 0.1;
} else
return this;
return this;
}
}