-
Notifications
You must be signed in to change notification settings - Fork 7
/
TileSet.js
executable file
·109 lines (105 loc) · 2.55 KB
/
TileSet.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
define(['./jo',
'./Class',
'./Tile',
'./Animation'],
function(jo, Class, Tile, Animation){
/**
* @class takes care of Tiles, they can be single frames or looping animations
*/
jo.TileSet = jo.Object.extend(
/**
* @lends jo.TileSet.prototype
*/
{
tiles: [],
/**
* @constructs
* @param tiles an array of tile definitions can be indexes or or animation arrays
* @param width
* @param height
* @param sprite
* @see jo.Animation
*/
init: function(opt){
this._super(opt);
this.width = opt.width;
this.height = opt.height;
this.sprite = opt.sprite;
this.tiles = this.readTiles(opt.tiles);
this.solid=[];
},
/**
* reads the tiles array
* @param tiles
* @returns transformed tiles array
*/
readTiles: function(tiles){
if(typeof tiles === 'undefined'){
tiles = [];
}
if(typeof tiles.length !== 'undefined'){
for(var i=0; i< tiles.length; i++){
//A Tile can be an Animation
if(typeof tiles[i] === 'object'){
tiles[i] = new Animation(tiles[i], this.width, this.height, this.sprite);
}else{//or just a single Frame.
tiles[i] = new Animation([{i: tiles[i], t: 200}], this.width, this.height, this.sprite);
}
}
}
return tiles;
},
readTile: function(tile){
//A Tile can be an Animation
if(typeof tile === 'object'){
tile = new Animation(tile, this.width, this.height, this.sprite);
}else{//or just a single Frame.
tile = new Animation([{i: tile, t: 200}], this.width, this.height, this.sprite);
}
},
push: function(tile){
this.tiles.push(this.readTile(tile));
},
/**
* advances the tile animations
* @param ticks
*/
update: function(ticks){
for(var i=0; i< this.tiles.length; i++){
this.tiles[i].update(ticks);
}
},
/**
* draws a tile specified by options.tile
* @param options
* @param position
* @param surface
* @see jo.Sprite.draw jo.Animation.draw
*/
draw: function(options, position, surface){
if(typeof options.tile !== 'number'){
options.tile = 0;
}
this.tiles[options.tile].draw(options, position, surface);
},
getCss: function(t){
var fr,
self=this,
css = function(t){
fr= self.tiles[t].getDrawFrame(0);
return 'background: url('+self.sprite.src+') -'+fr.x+'px -'+fr.y+'px;'
+'width: '+fr.width+'px ; height: '+fr.height+'px;';
};
if(t){
return css(t);
}else{
var ar=[];
for(var i in this.tiles){
ar.push(css(i));
}
return ar;
}
}
});
return jo.TileSet;
});