forked from oberhamsi/prettytiles-gamejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
210 lines (175 loc) · 6.36 KB
/
main.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
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
/**
* binarymillenium
* November-December 2010
* GNU GPL v3
*
* 'w','a','s','d' to move.
*
* Art from http://opengameart.org/content/isometric-64x64-outside-tileset
* Yar
* CC-BY 3.0
*
* Changed: <[email protected]>, port to GameJs, Feb 2010
*
*/
var gamejs = require('gamejs');
var perlin = require('./perlin');
var idata = require('./imagedata');
// setup noise fn
var n = new perlin.ClassicalNoise();
var noise = function() {
var r = 0.5 + n.noise.apply(n, arguments);
//if (r < 0 || r > 1) console.log(r);
return r;
}
var width = 940;
var height = 560;
function main() {
var display = gamejs.display.setMode([width, height]);
idata.init();
var ew_mv_size = 32;
var ns_mv_size = 16;
var maxmv = 32;
function handleEvent(event) {
if (event.type === gamejs.event.KEY_UP) {
switch(event.key) {
case gamejs.event.K_a:
ew_mv_size -= 32;
break;
case gamejs.event.K_d:
ew_mv_size += 32;
break;
case gamejs.event.K_w:
ns_mv_size += 16;
break;
case gamejs.event.K_s:
ns_mv_size -= 16;
break;
}
if (ew_mv_size > maxmv) ew_mv_size = maxmv;
if (ew_mv_size < -maxmv) ew_mv_size = -maxmv;
}
return;
};
var t = 0.0;
var xoff = 0;
var yoff = 0;
var dec_ind_max = 0;
var dec_ind_min = 100000;
var MAX_HEIGHT=8;
function getElevation(x_noise, y_noise) {
var frac = 10.0;
var elevation = parseInt(MAX_HEIGHT*noise(x_noise/frac+2000, y_noise/frac,t), 10);
elevation -=4;
if (elevation <0) elevation = 0;
return elevation;
};
function draw() {
display.clear();
//t += 0.00001;
xoff += ew_mv_size;
yoff -= ns_mv_size;
var x_part = xoff % 32;
var x_rnd = xoff/32;
var y_part = yoff % 16;
var y_rnd = yoff/16;
//println(x_part);
for (var i = 12; i>= -12/*-2; i < width/32+1*/; i--) {
// diagonal down to right
for (var j = -12; j <=12; j++) {
var x = j*32 - x_part;
var y = -i*32 + y_part;
var x_rot = x - y/2 + width/2 + i*16 -32;
var y_rot = x/2 + y + height/2 + i*16 -16*3;
var x_noise = (i + x_rnd);
var y_noise = (j + y_rnd);
var offset = 0;
//////////////////////////////////////
// get a random flat tile for base
var frac = 9.0;
// random(tiles.length);
var tile_ind = offset +
parseInt(((idata.tiles.length-offset) *
3.0*noise( x_noise/frac, y_noise/frac,t))%(idata.tiles.length-offset), 10);
if (tile_ind < 0) tile_ind = 0;
if (idata.tiles[tile_ind] != null) {
display.blit(idata.tiles[tile_ind], [x_rot, y_rot]);
}
if (true) {
///////////////////////////////////////
// raise the elevation
var elevation = getElevation(x_noise,y_noise);
var elev_factor = 32;
// draw elev tiles upwards
for (var k = 0; k < elevation; k++) {
var nval = 3.0*noise( x_noise/frac,y_noise/frac, k/frac + t);
var ind = parseInt((idata.elev.length * nval) % idata.elev.length);
if (idata.elev[ind] != null) {
display.blit(idata.elev[ind], [x_rot, y_rot - k*elev_factor]);
}
}
var is_slope = false;
/// put a slope if neighboring tiles are elevated differently
if (getElevation(x_noise+1,y_noise) > elevation) {
display.blit(idata.slope[1], [x_rot, y_rot - elevation*elev_factor]);
is_slope = true;
} else if (getElevation(x_noise, y_noise-1) > elevation) {
display.blit(idata.slope[2], [x_rot, y_rot - elevation*elev_factor]);
is_slope = true;
} else if (getElevation(x_noise-1,y_noise) > elevation) {
display.blit(idata.slope[6], [x_rot, y_rot - elevation*elev_factor]);
is_slope = true;
} else if (getElevation(x_noise, y_noise+1) > elevation) {
display.blit(idata.slope[5], [x_rot, y_rot - elevation*elev_factor]);
is_slope = true;
} else if (getElevation(x_noise+1,y_noise-1) > elevation) {
/// directly above
display.blit(idata.slope[0], [x_rot, y_rot - elevation*elev_factor]);
is_slope = true;
} else if (getElevation(x_noise+1,y_noise+1) > elevation) {
///
display.blit(idata.slope[1], [x_rot, y_rot - elevation*elev_factor]);
is_slope = true;
} else if (getElevation(x_noise-1,y_noise-1) > elevation) {
///
display.blit(idata.slope[4], [x_rot, y_rot - elevation*elev_factor]);
is_slope = true;
} else
//////////////////////////
/// now put a tree on it
if ((!is_slope) && (noise( 500 + x_noise/frac, y_noise/frac,t) > 0.55)) {
frac = 11.0;
var nval = 3.0*noise( 100 + x_noise/frac,y_noise/frac,t);
var ind = 1+ parseInt(((idata.dec.length-1) * nval) % (idata.dec.length-1));
if (idata.dec[ind] != null) {
display.blit(idata.dec[ind],
[x_rot,
y_rot - (elevation-2)*elev_factor - idata.dec[ind].rect.height
]
);
}
if (ind > dec_ind_max){
dec_ind_max = ind;
// console.log(idata.dec.length + " max " + ind + " " + nval);
}
if (ind < dec_ind_min){
dec_ind_min = ind;
// console.log(idata.dec.length + " min " + ind + " " + nval);
}
}
} // extra terrain
} // j loop
} // i loop
}
function tick(msDuration) {
gamejs.event.get().forEach(handleEvent);
draw();
};
gamejs.time.fpsCallback(tick, this, 7);
};
// preload & start
gamejs.preload(idata.tile_names);
gamejs.preload(idata.dec_names);
gamejs.preload(idata.elev_names);
gamejs.preload(idata.slope_names);
gamejs.ready(main);