forked from MalphasWats/glixl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glixl.Sprite.class.js
112 lines (89 loc) · 3.4 KB
/
glixl.Sprite.class.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
var glixl = (function(glixl)
{
glixl.Sprite = function Sprite(parameters)
{
this.x = parameters.x || 0;
this.y = parameters.y || 0;
this.z = parameters.z || 0;
this.width = parameters.width || 16;
this.height = parameters.height || 16;
this.speed = parameters.speed || 64;//32;
this.direction = parameters.direction || 0;
this.angle = parameters.angle || 0;
this.scale = parameters.scale || 1;
this.frame = parameters.frame || 0;
this.animations = { idle: { frames: [this.frame], frame_duration: 1000, frame_index: 0} };
this.current_animation = this.animations['idle'];
this.animation_timer = (new Date()).getTime();
this.destination = false;
this.path = [];
this.move_timer = (new Date()).getTime();
this.move_remainder = {x: 0, y: 0};
}
glixl.Sprite.prototype.update = function()
{
var now = (new Date()).getTime();
var delta = now - this.animation_timer;
if(delta > this.current_animation.frame_duration)
{
this.animation_timer = now;
this.current_animation.frame_index += 1;
if (this.current_animation.frame_index >= this.current_animation.frames.length)
this.current_animation.frame_index = 0;
this.frame = this.current_animation.frames[this.current_animation.frame_index];
}
if (this.destination)
{
delta = now - this.move_timer;
var step = this.speed / 1000 * delta + this.move_remainder.x;
if (step >= 1.0)
{
var fl_step = Math.floor(step);
this.move_remainder.x = step - fl_step;
var dx = this.x - this.destination.x;
if (Math.abs(dx) < step)
fl_step = 1
if (dx != 0)
{
this.x -= Math.floor(fl_step / 1 * (dx / Math.abs(dx)));
this.move_timer = now;
}
}
step = this.speed / 1000 * delta + this.move_remainder.y
if (step >= 1.0)
{
var fl_step = Math.floor(step);
this.move_remainder.y = step - fl_step;
var dy = this.y - this.destination.y;
if (Math.abs(dy) < step)
fl_step = 1
if (dy != 0)
{
this.y -= Math.floor(fl_step / 1 * (dy / Math.abs(dy)));
this.move_timer = now;
}
}
// TODO: This messes up on the bottom row :(
if (this.x == this.destination.x && this.y == this.destination.y)
{
if (this.path.length > 0)
this.destination = this.path.shift();
else
this.destination = false;
}
}
}
glixl.Sprite.prototype.add_animation = function(name, frames, frame_duration)
{
this.animations[name] = { frames: frames, frame_duration: frame_duration, frame_index:0 };
}
glixl.Sprite.prototype.set_animation = function(name)
{
this.current_animation = this.animations[name];
this.frame = this.current_animation.frames[this.current_animation.frame_index];
}
glixl.Sprite.prototype.use = function()
{
}
return glixl;
})(glixl || {});