-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation.js
111 lines (96 loc) · 2.47 KB
/
Animation.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
function Animation_Manager()
{
var animation = {};
var currentAnimation = "idle";
this.setAnimation = function(newAnimation)
{
if(currentAnimation != newAnimation)
{
currentAnimation = newAnimation;
animation[currentAnimation].resetFull();
}
}
this.updateAnimations = function()
{
$.each(animation, function(key, value) {
this.update();
});
}
this.addAnimation = function(NAME, TEX_NAME, START_TILE, END_TILE, TIME_PER_TILE, TILE_SIZE, SHEET_SIZE, REPEAT)
{
if( !animation.hasOwnProperty(NAME) ) {
system.log("Adding Animation: " + NAME);
animation[NAME] = new Animation(NAME, TEX_NAME, START_TILE, END_TILE, TIME_PER_TILE, TILE_SIZE, SHEET_SIZE, REPEAT);
} else {
system.log("Tried to add multiple key in Animation: " + NAME);
}
}
this.addPreMadeAnimation = function(NAME, newAnimation)
{
if( !animation.hasOwnProperty(NAME) ) {
system.log("Adding Animation: " + NAME);
animation[NAME] = newAnimation;
} else {
system.log("Tried to add multiple key in Animation: " + NAME);
}
}
this.currentTexture = function()
{
return animation[currentAnimation].texture;
}
this.top = function()
{
return animation[currentAnimation].topOffset();
}
this.left = function()
{
return animation[currentAnimation].leftOffset();
}
this.tileSize = function()
{
return animation[currentAnimation].size;
}
}
function Animation(NAME, TEX_NAME, START_TILE, END_TILE, TIME_PER_TILE, TILE_SIZE, SHEET_SIZE, REPEAT)
{
system.log("Constructing Animation " + NAME + "...");
var self = this;
self.name = NAME;
self.texture = TEX_NAME;
self.start = START_TILE;
self.end = END_TILE;
self.tpt = TIME_PER_TILE;
self.size = TILE_SIZE;
self.sheetSize = SHEET_SIZE;
self.repeat = REPEAT;
self.tile = self.start;
self.inc = 0;
var columns = (self.sheetSize / self.size);
this.resetAnimationAndCheckRepeat = function()
{
self.inc = 0;
if(self.repeat){return self.start;} else {return self.end;}
}
this.resetFull = function()
{
self.inc = 0;
self.tile = self.start;
}
this.topOffset = function()
{
return Math.floor(self.tile / columns) * self.size;
}
this.leftOffset = function()
{
return ((self.tile / columns) - Math.floor(self.tile / columns)) * self.sheetSize;
}
this.changeTile = function()
{
self.tile = (self.tile == self.end) ? self.resetAnimationAndCheckRepeat() : self.tile + 1;
return 0;
}
this.update = function()
{
self.inc = (self.inc >= self.tpt) ? self.changeTile() : self.inc + (clock.delta * 1000);
}
}