-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathhero.js
More file actions
133 lines (121 loc) · 3.49 KB
/
Copy pathhero.js
File metadata and controls
133 lines (121 loc) · 3.49 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
// We probably want to move "map" and moving function to another place.
// These functions is only used in 2d version.
class Hero {
constructor(map, x, y, character, name) {
this.map = map;
if (Number.isNaN(x)) {
x = 0;
}
if (Number.isNaN(y)) {
y = 0;
}
this.target_x = this.x = x;
this.target_y = this.y = y;
this.audioLevel = 0;
this.width = map.tsize;
this.height = map.tsize;
this.row = 0;
this.col = 0;
this.character = character;
this.name = name;
this.messages = [];
this.loadImage();
}
// Pixels per second.
// This is a little stupid...,
// But static member variable is not standardized yet.
get SPEED() {
return 256; // Pixels per second
}
loadImage() {
if (!this.character) return;
const char = this.character;
const img = Loader.getImage('hero:' + char);
if (null !== img) {
this.image = img;
} else {
Loader.loadImage(`hero:${char}`, `sprite/${char}.png`).then(
() => {
this.image = Loader.getImage('hero:' + char);
},
);
}
}
getMessages(me, now) {
return this.messages;
}
changeCharacter(character) {
this.character = character;
this.loadImage();
}
move(delta, dirX, dirY) {
// move hero
this.x += dirX * this.SPEED * delta;
this.y += dirY * this.SPEED * delta;
if (dirX || dirY) {
this.col += this.SPEED * delta;
}
// check if we walked into a non-walkable tile
this._collide(dirX, dirY);
// clamp values
const maxX = this.map.cols * this.map.tsize;
const maxY = this.map.rows * this.map.tsize;
this.x = Math.max(0, Math.min(this.x, maxX));
this.y = Math.max(0, Math.min(this.y, maxY));
}
otherMove(delta) {
let deltaX = 0;
let deltaY = 0;
let row = 0;
if (this.target_x != this.x) {
const dirX = (this.target_x > this.x) ? 1 : -1;
row = (this.target_x > this.x) ? 2 : 1;
deltaX = Math.min(
this.SPEED * delta, Math.abs(this.target_x - this.x)) * dirX;
} else if (this.target_y != this.y) {
const dirY = (this.target_y > this.y) ? 1 : -1;
deltaY = Math.min(
this.SPEED * delta, Math.abs(this.target_y - this.y)) * dirY;
row = (this.target_y > this.y) ? 0 : 3;
}
// move hero
this.x += deltaX;
this.y += deltaY;
if (deltaX || deltaY) {
this.row = row;
this.col += Math.max(deltaX, deltaY);
}
}
_collide(dirX, dirY) {
let row;
let col;
// -1 in right and bottom is because image ranges from 0..63
// and not up to 64
const left = this.x - this.width / 2;
const right = this.x + this.width / 2 - 1;
const top = this.y - this.height / 2;
const bottom = this.y + this.height / 2 - 1;
// check for collisions on sprite sides
const collision =
this.map.isSolidTileAtXY(left, top) ||
this.map.isSolidTileAtXY(right, top) ||
this.map.isSolidTileAtXY(right, bottom) ||
this.map.isSolidTileAtXY(left, bottom);
if (!collision) {
return;
}
if (dirY > 0) {
row = this.map.getRow(bottom);
this.y = -this.height / 2 + this.map.getY(row);
} else if (dirY < 0) {
row = this.map.getRow(top);
this.y = this.height / 2 + this.map.getY(row + 1);
} else if (dirX > 0) {
col = this.map.getCol(right);
this.x = -this.width / 2 + this.map.getX(col);
} else if (dirX < 0) {
col = this.map.getCol(left);
this.x = this.width / 2 + this.map.getX(col + 1);
}
}
}