-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapBase.js
More file actions
61 lines (59 loc) · 1.85 KB
/
mapBase.js
File metadata and controls
61 lines (59 loc) · 1.85 KB
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
/**
* Created by qxl on 2017/3/17.
*/
var MapBase = function(opts,data){
this.data = [];
this.tileWidth = null;
this.tileHeight = null;
this.wrapper = null;
this.tw = 64;
this.th = 32;
this.mapWidth = 320;
this.mapHeight = 160;
this.init(opts,data);
};
MapBase.prototype = {
convertData:function(data){
var result = [];
for(var i=0;i<data.length;i++){
var x = i%this.tileWidth;
var y = Math.floor(i/this.tileHeight);
if(x==0){
result.push([]);
}
result[y].push(data[i]);
}
return result;
},
init:function(opts,data){
opts = opts ||{};
this.tileWidth = opts.tileWidth;
this.tileHeight = opts.tileHeight;
this.wrapper = opts.wrapper || document.body;
this.wrapper.style.width = this.tileWidth * this.tw+'px';
this.wrapper.style.height = this.tileWidth * this.tw*0.5+'px';
this.data = this.convertData(data);
this.draw(this.data);
},
draw:function(data){
var str = "";
var origin = [this.mapWidth*0.5+0.5*this.tw,0];
for(var y=0;y<data.length;y++){
for(var x=0;x<data[y].length;x++){
var classNames = data[y][x]?'tile disable':'tile';
var top = x*this.th*0.5+y*this.th*0.5;
var left = (x*0.5-0.5)*this.tw-0.5*(y+1)*this.tw+origin[0];
str += '<div style="left:'+left+'px;top:'+top+'px;" class="'+classNames+'" data-Type ="'+data[y][x]+'">('+x+','+y+')</div>';
}
}
this.wrapper.innerHTML = str;
},
clearPath:function(){
},
drawPath:function(path){
for(var i in path){
var num = path[i].x+path[i].y*this.tileWidth;
this.wrapper.children[num].classList.add('red');
}
}
};