-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTilemap.js
executable file
·155 lines (143 loc) · 3.79 KB
/
Tilemap.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
define([ './jo', './Grid', './Point', './Tile', './TileSet', './Camera' ],
function(jo, Grid, Point, Tile, TileSet, Camera) {
/**
* @class holds Level data
* @augments jo.Grid
*/
jo.TileMap = Grid.extend(
/**
* @lends jo.Tilemap
*/
{
joObject: 'TileMap',
/**
* @constructs
* @param options
* width
* height
* tileSet
* data
*/
init : function(opt) {
this._super(opt);
this.name = opt.name;
this.tileSet = opt.tileSet;
this.clearTo({index: -1});
if(typeof opt.data === 'string'){
opt.data = JSON.parse(opt.data);
}
if(opt.data && opt.data.length === this.data.length){
this.data = opt.data;
jo.log('successful data acquisition');
}
},
/**
* update animations
* @param ticks
*/
update : function(ticks) {
this._super(ticks);
this.tileSet.update(ticks);
},
/**
* draws the level, will not crop tiles that go over the border of options.frame
* @param options
* @param position
* @param surface
* @see jo.Sprite.draw
*/
draw : function(options, position, surface) {
var tw = this.tileSet.width,
th = this.tileSet.height;
var frame = {x: 0, y: 0, width: surface.width, height: surface.height};
if(options && typeof options.frame !== 'undefined'){
frame = options.frame;
}else if(options && options.cam){
frame = {x: options.cam.x, y: options.cam.y, width: surface.width, height: surface.height};
}
var con = this.convertFrame(frame);
for(var i = con.y; i < con.y + con.height; i++) {
for(var j = con.x; j < con.x + con.width; j++) {
var index = this.get(j, i).index;
var pos = new Point(j * tw, i * th);
pos = pos.add(position);
var p = pos;
if(options && options.cam){
p = options.cam.toScreen(pos);
}
if(index >= 0){
this.tileSet.draw({tile: index}, p, surface);
}
if(options.grid){
surface.rect({fill:0, stroke: '#ccc'}, p, tw,th);
}
}
}
this._super();
},
/**
* return an object containing tile information
* @param x {Number}
* @param y {Number}
* @returns {pos: {jo.Point},
* width: {Number},
* height: {Number},
* index: {Number},
* anim: {jo.Animation}}
*/
getTile : function(x, y) {
var tw = this.tileSet.width,
th = this.tileSet.height;
return {
pos : new Point(x * tw, y * th),
width : tw,
height : th,
index : this.get(x, y).index,
anim : this.tileSet.tiles[this.get(x, y).index]
};
},
getFrame: function(){
return {x: 0, y: 0, width: this.width*this.tileSet.width, height: this.height*this.tileSet.height};
},
getIntersection: function(frame){
var inter = [];
var con = this.convertFrame(frame);
for ( var i = con.y; i < con.y + con.height; i++) {
for ( var j = con.x; j < con.x + con.width; j++) {
inter.push(this.getTile(j, i));
}
}
return inter;
},
convertFrame : function(frame){
var con = new Point(0,0);
var tw = this.tileSet.width,
th = this.tileSet.height;
var tileOff = new Point(frame.x / tw, frame.y / th);
con.x = Math.floor(Math.max(0, tileOff.x));
con.y = Math.floor(Math.max(0, tileOff.y));
con.width = Math.min(Math.ceil(frame.width / tw + 1), this.width - con.x),
con.height = Math.min(Math.ceil(frame.height / th + 1), this.height - con.y);
return con;
},
forFrame: function(frame, fn){
var con = this.convertFrame(frame);
for ( var i = con.x; i < con.x + con.width; i++) {
for ( var j = con.y; j < con.y + con.height; j++) {
fn(i, j, this.getTile(i,j));
}
}
},
find: function(index){
for(var i=0; i< this.width; i++){
for(var j =0; j< this.height; j++){
if(this.get(i,j).index==index){
return this.getTile(i,j);
}
}
}
return null;
}
});
return jo.TileMap;
});