-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActor.js
71 lines (61 loc) · 1.89 KB
/
Actor.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
// wrapper for "class" Actor
import {WebSystemObject} from './WebSystemObject';
export class Actor extends WebSystemObject {
constructor(x, y) {
super();
// (x, y) = center of object
// ATTENTION:
// it represents the player position on the world(room), not the canvas position
this.x = x;
this.y = y;
// move speed in pixels per second
this.speed = 200;
// render properties
this.width = 50;
this.height = 50;
}
update(step, worldWidth, worldHeight) {
// parameter step is the time between frames ( in seconds )
// check controls and move the player accordingly
if (Game.controls.left) {
this.x -= this.speed * step;
}
if (Game.controls.up) {
this.y -= this.speed * step;
}
if (Game.controls.right) {
this.x += this.speed * step;
}
if (Game.controls.down) {
this.y += this.speed * step;
}
// don't let player leaves the world's boundary
if (this.x - this.width / 2 < 0) {
this.x = this.width / 2;
}
if (this.y - this.height / 2 < 0) {
this.y = this.height / 2;
}
if (this.x + this.width / 2 > worldWidth) {
this.x = worldWidth - this.width / 2;
}
if (this.y + this.height / 2 > worldHeight) {
this.y = worldHeight - this.height / 2;
}
}
draw(context, xView, yView) {
// draw a simple rectangle shape as our player model
context.save();
context.fillStyle = 'black';
// before draw we need to convert player world's position to canvas position
context.fillRect((this.x - this.width / 2) - xView, (this.y - this.height / 2) - yView, this.width, this.height);
context.restore();
}
};
// Existen varios tipos de actores (implementar en z-order).
// 1._ Personajes.
// 2._ Decorados fijos.
// 3._ Decorados en movimiento (sprites).
// 4._ Cámaras.
// 5._ Sonidos (multicanal, real audio y MIDI).
// 6._ Interactividades.