-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.js
More file actions
162 lines (139 loc) · 3.69 KB
/
debug.js
File metadata and controls
162 lines (139 loc) · 3.69 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
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
'use strict';
class Space {
constructor(value) {
this.value = value;
}
isEmpty() { return this.value == ' '; }
isStairs() { return this.value == '>'; }
isEnemy() { return this.value == 'E'; }
isCaptive() { return this.value == 'C'; }
isWall() { return this.value == 'W'; }
isTicking() { return this.value == 'T'; }
}
class Enemy {
constructor(type, position) {
this.type = type;
this.hp = 24;
this.position = position;
this.willAttack = false;
}
sense(warrior) {
var diff = {
x: warrior.position.x - this.position.x,
y: warrior.position.y - this.position.y
}
this.willAttack = (diff.x*diff.x + diff.y*diff.y <= 1);
}
takeTurn(warrior) {
if (!this.willAttack)
return;
var diff = {
x: warrior.position.x - this.position.x,
y: warrior.position.y - this.position.y
}
if (diff.x*diff.x + diff.y*diff.y <= 1) {
warrior._health -= 3;
}
}
}
class Warrior {
constructor() {
this._health = 20;
this.map = {
width: 8,
height: 3,
tiles: Array.from('WWWWWWWW' +
'W@ CSS>W' +
'WWWWWWWW')
}
var position = { x: 1, y: 1 };
for (let y = 0; y < this.map.height; y++) {
for (let x = 0; x < this.map.width; x++) {
let tileIndex = y * this.map.width + x;
let cellType = this.map.tiles[tileIndex];
let tile = {
type: cellType
};
if (cellType == '@') {
tile.type = ' ';
position = { x: x, y: y };
} else if (cellType == 'S' || cellType == 'a') {
tile.type = 'E';
tile.enemy = new Enemy(cellType, { x: x, y: y });
enemies.push(tile.enemy);
}
this.map.tiles[tileIndex] = tile;
}
}
this.facing = 'east';
this.position = position;
}
_getCell(x, y) {
if ((x < 0 || x >= this.map.width) ||
(y < 0 || y >= this.map.height)) {
return 'W';
}
return this.map.tiles[x + y * this.map.width];
}
_setCell(x, y, value) {
this.map.tiles[x + y * this.map.width] = value;
}
walk(direction) {
var position = stepInDir(this.facing, direction, this.position);
if (this._getCell(position.x, position.y).type == ' ') {
this.position = position;
}
}
attack(direction) {
var position = stepInDir(this.facing, direction, this.position);
var cell = this._getCell(position.x, position.y);
var enemy = cell.enemy;
if (enemy != null && (enemy.hp -= 5) <= 0) {
this._setCell(position.x, position.y, {
type: ' '
});
}
}
rescue(direction) {
var position = stepInDir(this.facing, direction, this.position);
if (this._getCell(position.x, position.y).type == 'C') {
this._setCell(position.x, position.y, {
type: ' '
});
}
}
pivot(direction) {
this.facing = dirToCard(this.facing, direction);
}
feel(direction) {
var position = stepInDir(this.facing, direction, this.position);
return new Space(this._getCell(position.x, position.y).type);
}
look(direction) {
var spaces = [];
var position = this.position;
for (let i = 0; i < 3; i++) {
position = stepInDir(this.facing, direction, position);
spaces.push(new Space(this._getCell(position.x, position.y).type));
}
return spaces;
}
health() {
return this._health;
}
rest() {
this._health += 2;
}
}
var enemies = [];
var player = new Player();
var warrior = new Warrior();
for (let i = 0; i < 100; i++) {
for (let i = 0; i < enemies.length; i++) {
enemies[i].sense(warrior);
}
player.playTurn(warrior);
for (let i = 0; i < enemies.length; i++) {
enemies[i].takeTurn(warrior);
}
}