-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemy.js
64 lines (60 loc) · 2.29 KB
/
enemy.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
var gameover=false;
function createEnemy(lv) {
var enemy = new Object()
console.log(lv)
enemy.LV = Math.floor(Math.random() * 20 ) + lv;
enemy.HP = 50 + 5 * enemy.LV;
enemy.atk = 2 + 2 * Math.floor(enemy.LV / 5);
enemy.def = Math.floor(enemy.LV / 8);
enemy.speeed = 10 + Math.floor(enemy.LV / 10);
enemy.getDamage = (atk) => {
enemy.HP -= Math.max(1, atk - Math.floor(enemy.def));
var enemyS = document.getElementById("enemyStatment");
enemyS.type = "text";
enemyS.innerHTML = "Enemy:<br>LV:" + (enemy.LV).toString()
enemyS.innerHTML = enemyS.innerHTML + "<br>HP:" + enemy.HP.toString()
enemyS.innerHTML = enemyS.innerHTML + "<br>ATK:" + enemy.atk.toString()
enemyS.innerHTML = enemyS.innerHTML + "<br>DEF:" + enemy.def.toString()
enemyS.innerHTML = enemyS.innerHTML + "<br>SPD:" + enemy.speeed.toString()
if (enemy.HP <= 0) {
getXP(enemy.LV * 10);
return 1;
}
return 0;
};
return enemy;
}
function enemy_attack() {
var textBox = document.getElementById("textbox");
textBox.type = "text";
nowplace = { x: Math.floor(player.location.x), y: Math.floor(player.location.y) };
const { x, y } = nowplace;
var index = 0;
for (var i = 0; i < map[player.nowRoom].mobLocation.length; ++i) {
if (x == map[player.nowRoom].mobLocation[i].x && y == map[player.nowRoom].mobLocation[i].y) {
index = i;
break;
}
}
// 敵人攻擊玩家
if (getDamage(map[player.nowRoom].mobLocation[index].data.atk)) {
// 玩家被擊敗
textBox.innerHTML = "You were defeated by the enemy!";
player.onBattle = false;
} else {
textBox.innerHTML += "<br>Enemy attacks! You receive " + Math.max(1, map[player.nowRoom].mobLocation[index].data.atk - Math.floor(player.def)) + " damage.";
}
// 檢查玩家是否倖存
if (player.HP <= 0) {
if(player.live>=1){
player.live-=1
player.HP=player.MAXHP
textBox.innerHTML += "<br>You died! You have " + player.live + " lives left.";
}
else{
textBox.innerHTML += " Game Over!";
// 可以增加重置遊戲或其他遊戲結束邏輯
gameover=true
}
}
}