-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel.js
More file actions
212 lines (189 loc) · 6.12 KB
/
level.js
File metadata and controls
212 lines (189 loc) · 6.12 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
function Level(width,height){
this.width = width;
this.height = height;
this.ticks = 0;
this.tiles = [];
this.data = [];
this.entities = [];
this.seed = -1;
}
function drawLine(level,x0, x1, y0, y1,z,tile){
var deltax = x1 - x0;
var deltay = y1 - y0;
var error = 0;
try{
var deltaerr = Math.abs(deltay / deltax);
}catch(err){
var deltaerr = 0;
}
var y = y0;
for(var x=x0;x<x1;x++){
level.tiles[z][x+(y*level.width)] = tile.id;
error = error + deltaerr;
while(error >= 0.5){
level.tiles[z][x+(y*level.width)] = tile.id;
var t = (y1 - y0);
if(t>0){
y++;
}else{
y--;
}
var dif = Math.random();
if(dif>0.5){
if(dif>0.6){
x++;
}else if(dif>0.7){
x--;
}else if(dif>0.8){
y--;
}else if(dif>0.9){
y++;
}
}
error = error - 1.0;
}
}
}
Level.prototype.generateLevel = function(seed){
console.log("Generate Level");
noise.seed(seed);
this.seed = seed;
Math.seedrandom(""+seed);
this.tiles[0] = Array.apply(null, new Array(this.width*this.height)).map(Number.prototype.valueOf,4);
var veins = [];
while(veins.length<400)
veins.push([Math.floor(Math.random()*this.width),Math.floor(Math.random()*this.height)]);
for(var j=0;j<veins.length;j++){
var i = veins[j];
var end = [Math.floor((Math.random()*i[0])+30)-15,Math.floor((Math.random()*i[1])+30)-15];
drawLine(this,i[0],end[0],i[1],end[1],0,tree);//change to ore
}
for (var x = 0; x < this.width; x++) {
for (var y = 0; y < this.height; y++) {
var n = noise.perlin2(x/16, y/16);
if(n>0)
this.tiles[0][x+(y*this.width)] = water.id;//change to stone floor
}
}
this.tiles[1] = Array.apply(null, new Array(this.width*this.height)).map(Number.prototype.valueOf,5);
this.tiles[2] = Array.apply(null, new Array(this.width*this.height)).map(Number.prototype.valueOf,2);
for (var x = 0; x < this.width; x++) {
for (var y = 0; y < this.height; y++) {
var n = noise.perlin2(x/25, y/25);
if(n>0.033){
this.tiles[2][x+(y*this.width)] = grass.id;
}else if(n>0){
this.tiles[2][x+(y*this.width)] = sand.id;
}else{
this.tiles[2][x+(y*this.width)] = water.id;
}
}
}
for (var x = 0; x < this.width; x++) {
for (var y = 0; y < this.height; y++) {
if(this.getTile(x,y,2)!=grass)continue;
var n = noise.perlin2(x/8, y/8);
if(n>0.3)this.tiles[2][x+(y*this.width)] = tree.id;
}
}
this.data[0] = Array.apply(null, new Array(this.width*this.height)).map(Number.prototype.valueOf,0);
this.data[1] = Array.apply(null, new Array(this.width*this.height)).map(Number.prototype.valueOf,0);
this.data[2] = Array.apply(null, new Array(this.width*this.height)).map(Number.prototype.valueOf,0);
for (var x = 0; x < this.width; x++) {
for (var y = 0; y < this.height; y++) {
j = Math.floor( Math.random() * ( 0 + 3 ) );
if(Math.floor(Math.random() * 10)==1&&this.getTile(x,y,2).hasChildren())this.tiles[2][x+(y*this.width)] = grass.getChildren()[j].id
//console.log('Testing console');
}
}
if(isMultiplayer&&isHost)sendSeed(seed);
};
Level.prototype.tick = function(){
this.ticks++;
for (var i = 0; i < this.entities.length; i++) {
if(this.entities[i].z==player.z)this.entities[i].tick();
}
for (var i = 0; i < tile.length; i++) {
tile[i].tick();
}
};
Level.prototype.render = function(xoff,yoff){
var xmin,xmax,ymin,ymax;
if(xoff<1){
xmin=0;
xmax = 32;
}else if(xoff>3071){
xmin = this.width-32;
xmax = this.width;
}else{
xmin = (player.x>>5)-16 < 0 ? 0 : (player.x>>5)-16;
xmax = (player.x>>5)+17 > this.width ? this.width : (player.x>>5)+17;
}
if(yoff<1){
ymin=0;
ymax = 18;
}else if(yoff>3519){
ymin = this.height-18;
ymax = this.height;
}else{
ymin = (player.y>>5)-9 < 0 ? 0 : (player.y>>5)-9;
ymax = (player.y>>5)+10 > this.height ? this.height : (player.y>>5)+10;
}
for (var x = xmin; x < xmax; x++) {
for (var y = ymin; y < ymax; y++) {
var z = player.z;
while(!this.getTile(x,y,z).id)z--;
// var isVisable = false;
// for(var i=0;i<9;i++){
// if(i==4)continue;
// var dx = (i%3)-1;
// var dy = Math.floor(i/3)-1;
// if(!this.getTile(x+dx,y+dy,z).isSolid){
// isVisable = true;
// continue;
// }
// }
// if(!isVisable)continue;
if(z==player.z){
this.getTile(x,y,z).render((x<<5)-xoff,(y<<5)-yoff,this.getData(x,y,z));
}else{
this.getTile(x,y,z).render((x<<5)-xoff,(y<<5)-yoff-16,this.getData(x,y,z));
this.getTile(x,y+1,z).render((x<<5)-xoff,(y+1<<5)-yoff-16,this.getData(x,y,z));
// for (var i = 0; i < this.entities.length; i++) {
// if(this.entities[i].z==z)this.entities[i].render(xoff,yoff+16,this.getData(x,y,z));
// }
this.getTile(x,y-1,player.z).render((x<<5)-xoff,(y-1<<5)-yoff,this.getData(x,y,z));
}
for (var i = 0; i < this.entities.length; i++) {
var e = this.entities[i];
if(e.z==player.z)e.render(xoff,yoff);
}
}
}
};
Level.prototype.setTile = function(x,y,z,tile){
if(x < 0 || y < 0 || x >= this.width || y >= this.height)return;
this.tiles[z][x+(y*this.width)] = tile.id;
this.data[z][x+(y*this.width)] = 0;
if(isMultiplayer)sendTile(x,y,z,tile.id);
};
Level.prototype.incrementData = function(x,y,z,d){
if(x < 0 || y < 0 || x >= this.width || y >= this.height)return;
this.data[z][x+(y*this.width)]+= d;
};
Level.prototype.getTile = function(x,y,z){
if(x < 0 || y < 0 || x >= this.width || y >= this.height || z<0)return VOID;
return tile[this.tiles[z][x+(y*this.width)]];
};
Level.prototype.getData = function(x,y,z){
if(x < 0 || y < 0 || x >= this.width || y >= this.height || z<0)return -1;
return this.data[z][x+(y*this.width)];
};
Level.prototype.canPassTile = function(tile,x,y,entity){
if(tile.isSolid)return false;
for (var e = 0; e < this.entities.length; e++) {
var i = this.entities[i];
if(i.x>>5 == x && i.y>>5 ==y && i.blocksPath)return false;
}
return true;
}