-
Notifications
You must be signed in to change notification settings - Fork 11
/
sprite.js
188 lines (181 loc) · 3.09 KB
/
sprite.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*\
* sprite
*
* sprite-animator for LF2
\*/
define(['F.core/sprite','F.core/animator'], function (Fsprite, Fanimator)
{
/*\
* sprite
[ class ]
- bmp (object) data structure as defined in data files
- parent (DOM node) where to append the new sprite
\*/
function sprite (bmp, parent)
{
/*\
* sprite.num_of_images
[ property ]
\*/
var num_of_images = this.num_of_images = bmp.file.length;
/*\
* sprite.w
[ property ]
* width
\*/
/*\
* sprite.h
[ property ]
* height
\*/
var w = this.w = bmp.file[0].w+1;
var h = this.h = bmp.file[0].h+1;
/*\
* sprite.ani
[ property ]
- Fanimator (object)
\*/
var ani = this.ani = [];
/*\
* sprite.dir
[ property ]
* `'left'` or `'right'`
\*/
this.dir = 'right';
/*\
* sprite.cur_img
[ property ]
* current image index
\*/
this.cur_img = 0;
var sp_con=
{
canvas: parent,
wh: {x:w,y:h},
img:{}
}
/*\
* sprite.sp
[ property ]
- Fsprite (object)
\*/
var sp = this.sp = new Fsprite(sp_con);
for( var i=0; i<bmp.file.length; i++)
{
var imgpath='';
for( var j in bmp.file[i])
{
if( typeof bmp.file[i][j] === 'string' &&
j.indexOf('file')===0 )
imgpath = bmp.file[i][j];
}
if( imgpath==='')
console.log( 'cannot find img path in data:\n'+JSON.stringify(bmp.file[i]) );
sp.add_img( imgpath, i);
var ani_con=
{
x:0, y:0, //top left margin of the frames
w:bmp.file[i].w+1, h:bmp.file[i].h+1, //width, height of a frame
gx:bmp.file[i].row, gy:bmp.file[i].col,//define a gx*gy grid of frames
tar:sp, //target sprite
borderleft: 0,
bordertop: 0,
borderright: 1,
borderbottom: 1
};
ani.length++;
ani[i] = new Fanimator(ani_con);
}
}
/*\
* sprite.destroy
[ method ]
* clear memory so that itself and the DOM nodes can be garbage collected
\*/
sprite.prototype.destroy = function()
{
this.sp.remove();
this.sp=null;
this.ani.length=0;
}
/*\
* sprite.show_pic
[ method ]
- I (number) picture index to show
\*/
sprite.prototype.show_pic = function(I)
{
var slot=0;
for( var k=0; k<this.ani.length; k++)
{
var i = I - this.ani[k].config.gx * this.ani[k].config.gy;
if( i >= 0)
{
I = i;
slot++;
}
else
break;
}
if( slot >= this.ani.length)
{
slot = this.ani.length-1;
I=999;
}
this.cur_img = slot;
this.sp.switch_img(this.cur_img);
this.ani[this.cur_img].set_frame(I);
this.w = this.ani[this.cur_img].config.w;
this.h = this.ani[this.cur_img].config.h;
}
/*\
* sprite.switch_lr
[ method ]
* switch sprite direction
- dir (string) `'left'` or `'right'`
\*/
sprite.prototype.switch_lr = function(dir) //switch to `dir`
{
if( dir!==this.dir)
{
this.dir=dir;
this.sp.mirror(dir==='left');
}
}
/*\
* sprite.set_xy
[ method ]
- x (number)
- y (number)
\*/
sprite.prototype.set_x_y = function(x,y)
{
this.sp.set_x_y(x,y);
}
/*\
* sprite.set_z
[ method ]
- Z (number)
\*/
sprite.prototype.set_z = function(Z)
{
this.sp.set_z(Z);
}
/*\
* sprite.show
[ method ]
\*/
sprite.prototype.show = function()
{
this.sp.show();
}
/*\
* sprite.hide
[ method ]
\*/
sprite.prototype.hide = function()
{
this.sp.hide();
}
return sprite;
});