-
Notifications
You must be signed in to change notification settings - Fork 0
/
projeto.js
144 lines (125 loc) · 2.9 KB
/
projeto.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
var field, player;
class Field {
constructor(cols, rows, containerId) {
this.cols = cols;
this.rows = rows;
this.container = document.querySelector(containerId);
this.createField();
}
createField() {
var field = [];
for (var i = 0; i < this.rows; i++) {
field[i] = [];
for (var j = 0; j < this.cols; j++) {
field[i].push(this.createRock());
}
}
this.field = field;
this.drawField();
}
createRock() {
return Math.trunc(Math.random() * 5) === 1 ? '@' : '';
}
drawField() {
var template = '';
for (var i = 0; i < this.rows; i++) {
template += '<tr>'
for (var j = 0; j < this.cols; j++) {
template += `<td>${this.field[i][j]}</td>`;
}
template += '</tr>';
}
this.container.innerHTML = template;
}
}
class Character {
constructor(field, x, y, face) {
this.face = face;
this.x = x;
this.y = y;
this.table = field;
if (!this.setPosition(this.x, this.y)) {
throw Error();
}
}
up() {
if (this.y > 0) {
this.setPosition(this.x, this.y - 1);
}
}
down() {
if (this.y + 1 < this.table.rows) {
this.setPosition(this.x, this.y + 1);
}
}
left() {
if (this.x > 0) {
this.setPosition(this.x - 1, this.y);
}
}
right() {
if (this.x + 1 < this.table.cols) {
this.setPosition(this.x + 1, this.y);
}
}
setPosition(x, y) {
if (this.table.field[y][x] === '') {
this.table.field[this.y][this.x] = '';
this.x = x;
this.y = y;
this.table.field[this.y][this.x] = this.face;
this.table.drawField();
return true;
}
return false;
}
}
class Player extends Character {
constructor(field) {
super(field, 0, 0, 'o_o');
}
}
class Npc extends Character {
constructor(field) {
var x = Math.trunc(Math.random() * field.cols),
y = Math.trunc(Math.random() * field.rows);
super(field, x, y, '-_-');
setInterval(this.walk.bind(this), 500);
}
walk() {
var direction = Math.trunc(Math.random() * 4) + 1;
switch (direction) {
case 1: this.up(); break;
case 2: this.down(); break;
case 3: this.right(); break;
case 4: this.left(); break;
}
}
}
function startField() {
var cols = document.querySelector('#cols').value || 3,
rows = document.querySelector('#rows').value || 3
document.querySelector('button').disabled = true;
field = new Field(cols, rows, '#myTable');
try {
player = new Player(field);
npc = new Npc(field);
} catch (e) {
console.log('starting field again');
startField();
}
}
window.addEventListener('keyup', function (event) {
if (player) {
const A = 65,
S = 83,
D = 68,
W = 87;
switch (event.keyCode) {
case A: player.left(); break;
case S: player.down(); break;
case D: player.right(); break;
case W: player.up(); break;
}
}
})