-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathChunk.js
172 lines (159 loc) · 5.54 KB
/
Chunk.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
import {SpaceMap} from '../shared/SpaceMap'
import Utils from '../shared/Utils'
import World from '../shared/World'
var TREE_ALPHA = 1;
function Chunk(data, tilesetData, scene){
this.tilesetData = tilesetData;
this.scene = scene;
this.id = data.id;
this.x = parseInt(data.x);
this.y = parseInt(data.y);
this.defaultTile = data.default;
this.layers = data.layers;
this.decor = data.decor;
this.ground = new SpaceMap();
this.ground.fromList(this.layers[0],true); // true = compact list
this.wood = data.wood;
this.tiles = [];
this.tilesMap = new SpaceMap();
this.images = [];
this.displayed = false;
this.leavesPos = [[0,-1],[0,0],[0,1],[1,1],[1,0],[2,0],[2,1],[2,-1]];
this.draw();
}
Chunk.prototype.draw = function(){
this.wood.forEach(function(w){
this.addResource(this.x+w[0],this.y+w[1]);
},this);
// Ground
for(var x_ = 0; x_ < World.chunkWidth; x_++){
for(var y_ = 0; y_ < World.chunkHeight; y_++) {
var tx = this.x + x_;
var ty = this.y + y_;
if(this.hasWater(tx,ty)) continue;
if(this.defaultTile == 'grass') {
var gs = this.tilesetData.config.grassSize;
var t = (tx % gs) + (ty % gs) * gs;
this.drawTile(tx,ty,this.tilesetData.config.grassPrefix+'_'+t);
}
}
}
// Layers
this.layers.forEach(function(layer){
layer.forEach(function(data){
var tile = data[2];
if(tile === undefined) return;
var x = this.x + parseInt(data[0]);
var y = this.y + parseInt(data[1]);
var name = this.tilesetData.shorthands[tile];
if(!(tile in this.tilesetData.shorthands)) return;
this.drawTile(x, y, name);
},this);
},this);
if(this.tiles.length > 700) console.warn(this.tiles.length); // TODO: remove eventually
// Decor
this.decor.forEach(function (data) {
var x = this.x + parseInt(data[0]);
var y = this.y + parseInt(data[1]);
this.addImage(x, y, data[2]);
if(data[2][0] == 't') this.addOverlay(x,y);
}, this);
this.displayed = true;
};
Chunk.prototype.has = function(x,y,v){
var cx = x - this.x;
var cy = y - this.y;
return (this.ground.get(cx,cy) == v);
};
Chunk.prototype.hasWater = function(x,y){
return this.has(x,y,'w');
};
Chunk.prototype.drawTile = function(x,y,tile){
/*if(BLIT){ // TODO: remove?
Editor.ground.create(x * World.tileWidth, y * World.tileHeight, tile);
return;
}*/
var sprite = this.scene.add.image(x*World.tileWidth,y*World.tileHeight,'tileset',tile);
sprite.setDisplayOrigin(0,0);
sprite.tileID = tile;
this.tiles.push(sprite);
if(this.getAtlasData(tile,'collides',true)) this.addCollision(x,y);
this.postDrawTile(x,y,tile,sprite);
};
Chunk.prototype.getAtlasData = function(image,data,longname){
if(longname){
return this.tilesetData.atlas[image][data];
}else {
if (!(image in this.tilesetData.shorthands)){
console.warn('Unknown shorthand',image);
return false;
}
return this.tilesetData.atlas[this.tilesetData.shorthands[image]][data];
}
};
Chunk.prototype.drawImage = function(x,y,image,depth,crop){
var offset = this.getAtlasData(image,'offset');
if(offset){
x += offset.x;
y += offset.y;
}
var img = this.scene.add.image(x*World.tileWidth,y*World.tileHeight,'tileset',this.tilesetData.shorthands[image]);
if(crop) img.setCrop(crop);
var depthOffset = this.getAtlasData(image,'depthOffset') || 0;
depth = depth || y;
img.setDepth(depth+depthOffset);
var anchor = this.getAtlasData(image,'anchor');
img.setOrigin(anchor.x,anchor.y);
this.images.push(img);
this.postDrawImage(x,y,image,img);
return img;
};
Chunk.prototype.addImage = function(x,y,image){
var isTree = (image[0] == 't');
if(isTree){
var frame = this.getAtlasData(image,'frame');
var ycutoff = frame.h*0.4;
this.drawImage(x,y,image, y, new Phaser.Geom.Rectangle(0,0,frame.w,ycutoff)).setAlpha(TREE_ALPHA);
this.drawImage(x,y,image, y+1, new Phaser.Geom.Rectangle(0,ycutoff,frame.w,frame.h-ycutoff)).setAlpha(TREE_ALPHA);
}else{
this.drawImage(x,y,image);
}
// Manage collisions
var collisions = this.getAtlasData(image,'collisions');
if(collisions) {
collisions.forEach(function(coll){
this.addCollision(x+coll[0],y+coll[1]);
},this);
}
// Draw dead leaves on the ground
if(isTree && Utils.randomInt(1,10) > 6){ // TODO: conf
var nbleaves = 5; //TODO: conf
Utils.shuffle(this.leavesPos);
for(var j = 0; j < nbleaves; j++) {
var c = this.leavesPos[j];
var type = Utils.randomInt(1,3);
var lx = x+c[0];
var ly = y+c[1];
this.drawImage(lx,ly,'l'+type);
// if(this.hasWater(lx,ly)) console.warn('on water');
}
}
// Add ivy
if(isTree && (image[1] == 1 || image[1] == 2) && Utils.randomInt(1,10) > 6){ // TODO: conf
this.drawImage(x+1,y-1,'i'+Utils.randomInt(1,2),y+1);
}
};
Chunk.prototype.erase = function(){
for(var x = this.x; x < this.x + World.chunkWidth; x++){
for(var y = this.y; y < this.y + World.chunkHeight; y++){
this.removeCollision(x,y);
}
}
this.tiles.forEach(function(tile){
tile.destroy();
});
this.images.forEach(function(image){
image.destroy();
});
};
export default Chunk