-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunkGenerator.js
242 lines (217 loc) · 8.63 KB
/
chunkGenerator.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
var chunkGenerator = function(){
this.type = null;
this.lastChunk = null;
};
chunkGenerator.prototype.generateChunk = function(lastPoint, r, g, b, dist, speed, gravity) {
// TODO - algorithm to make this based on difficulty
var difficulty = 1;
// leave vStart as a power of 2 to save computation
var vStart = Math.pow(speed, 2) + Math.pow(25, 2), // 25 is jumpV
theta = Math.asin(25/speed);
var maxJumpRange = vStart * 2 * Math.sin(theta) * Math.cos(theta) / gravity;
//console.log(maxJumpRange);
var RNG = Math.random();
if (RNG < 0.20 && this.lastChunk != "mountain") return this.generateMountain(lastPoint, gravity/speed);
else if (RNG < 0.4 && this.lastChunk != "spikes" && dist > /*50*/ 5) return this.generateSpikes(lastPoint, maxJumpRange);
else if (RNG < 0.60 && this.lastChunk != "cliff") return this.generateCliff(lastPoint);
else if (RNG < 0.80 && this.lastChunk != "u") return this.generateU(lastPoint, maxJumpRange, gravity/speed, (dist > 20));
else return this.generateStraight(lastPoint);
};
chunkGenerator.prototype.generateFork = function(startPoint) {
//console.log("Generating Fork");
var result = new GroundPoly(false, startPoint, null);
result.upper.push({'x': startPoint.x,
'y': startPoint.y - 400,
'damage': false });
result.upper.push({'x': startPoint.x + 1000,
'y': startPoint.y - 700,
'damage': false });
result.upper.push({'x': startPoint.x + 2000,
'y': startPoint.y - 700,
'damage': false });
result.upper.push({'x': startPoint.x + 3000,
'y': startPoint.y - 1000,
'damage': false });
result.upper.push({'x': startPoint.x + 4000,
'y': startPoint.y - 1400,
'damage': false });
result.lower.push({'x': startPoint.x,
'y': startPoint.y - 360,
'damage': false });
result.lower.push({'x': startPoint.x + 1000,
'y': startPoint.y - 360,
'damage': false });
result.lower.push({'x': startPoint.x + 2000,
'y': startPoint.y - 400,
'damage': false });
result.lower.push({'x': startPoint.x + 4000,
'y': startPoint.y - 1200,
'damage': false });
result.lastUpper = result.upper[result.upper.length -1];
result.lastLower = result.lower[result.lower.length -1];
return result;
};
//make mountain from last point
chunkGenerator.prototype.generateMountain = function(startPoint, maxSlope){
//console.log("Generating Mountain");
var result = {};
result.upper = [];
result.lower =[];
var peak = [startPoint.x + Math.random()*1000 +400, startPoint.y - Math.random()*400 - 100];
var endX = peak[0] + Math.random()* 1000 + 400;
var endY = peak[1] + Math.random()*(endX - peak[0])*maxSlope;
result.upper.push({'x': peak[0],
'y': peak[1],
'damage': false });
result.upper.push({'x': endX,
'y': endY,
'damage': false});
result.lower.push({'x': endX,
'y': Math.max(2*getHeight(), endY + 100),
'damage': false});
this.lastChunk = "mountain";
return result;
};
//make spikes
chunkGenerator.prototype.generateSpikes = function(startPoint, maxJumpRange){
var result = {};
//console.log("Generating Spikes");
result.upper = [];
result.lower = [];
result.upper.push(startPoint);
result.platform = null;
var numSpikes = Math.round(Math.random() * ((maxJumpRange / 15)*2)) + 20;
//console.log("Max jump range: " + maxJumpRange + ", spike distance generated: " + numSpikes*15);
if (numSpikes > maxJumpRange/15) {
// generate a platform
//console.log("Platform");
var startPoint = {'x': Math.random() * maxJumpRange - 25 + startPoint.x,
'y': startPoint.y - 200,
'damage': false};
result.platform = [];
result.platform.push(new GroundPoly(true, startPoint, 300));
}
for (var i = 0; i < numSpikes; i++) {
var spike = this.generateSpike(result.upper[result.upper.length-1]);
for (var j=0; j < spike.length; j++) {
result.upper.push(spike[j]);
}
}
result.upper.splice(0,1);
result.lower.push({'x': result.upper[result.upper.length -1].x,
'y': Math.max(2*getHeight(), startPoint.y + 100),
'damage': false});
this.lastChunk="spikes";
return result;
};
chunkGenerator.prototype.generateSpike = function(startPoint) {
var points =[];
points.push({'x': startPoint.x + 5,
'y': startPoint.y - 10,
'damage': true });
points.push({'x': startPoint.x + 10,
'y': startPoint.y,
'damage': true });
points.push({'x': startPoint.x + 15,
'y': startPoint.y,
'damage': true });
return points;
}
chunkGenerator.prototype.generateCliff = function(startPoint){
//console.log("Generating Cliff");
var result = {};
result.upper = [];
result.lower = [];
var bottom = startPoint.y + Math.random()*300 + 100;
var endX = startPoint.x + Math.random(200) + 50;
result.upper.push({'x': startPoint.x,
'y': bottom,
'damage': false});
result.upper.push({'x': endX,
'y': bottom,
'damage': false});
result.lower.push({'x': endX,
'y': Math.max(2*getHeight(), bottom + 200),
'damage': false});
this.lastChunk="cliff";
return result;
};
chunkGenerator.prototype.generateStraight = function(startPoint){
//console.log("Generating Straight");
var result = {};
result.upper = [];
result.lower = [];
result.platform = null;
var endX = startPoint.x + Math.random()*1000 + 500;
result.upper.push({'x': endX,
'y': startPoint.y,
'damage': false});
result.lower.push({'x': endX,
'y': Math.max(2*getHeight(), startPoint.y + 100),
'damage': false});
this.lastChunk="straight";
var RNG = Math.random();
if (RNG < 0.2) {
var startPoint = {'x': Math.random()*1000 + startPoint.x,
'y': startPoint.y - 200,
'damage': false};
this.platform = [];
this.platform.push(result.platform = new GroundPoly(true, startPoint, 300));
if (RNG < 0.1) {
startPoint = {'x': startPoint.x + 500,
'y': startPoint.y - 400,
'damage': false};
this.platform.push(result.platform = new GroundPoly(true, startPoint, 300));
}
}
return result;
};
chunkGenerator.prototype.generateU = function(startPoint, maxJumpRange, maxSlope, spikes) {
//console.log("Generating U");
var result = {};
result.upper = [];
result.lower = [];
result.platform = null;
/*
var uSpikes = false;
var platformSpikes = false;
if (spikes) {
var RNG = Math.random();
if (RNG < 0.4) uSpikes = true;
else if(RNG < 0.8) platformSpikes = true;
else {platformSpikes=true; uSpikes = true;}
}*/
result.upper.push({'x': startPoint.x + 100,
'y': startPoint.y,
'damage': false});
result.upper.push({'x': startPoint.x + 400,
'y': startPoint.y + 250,
'damage': false});
if (spikes) {
var numSpikes = 600 / 15;
for (var i=0; i < numSpikes; i++) {
var spike = this.generateSpike({'x': startPoint.x + 400 + 15*i,
'y': startPoint.y + 250,
'damage': true});
for (var j=0; j < spike.length; j++) {
result.upper.push(spike[j]);
}
}
}
result.upper.push({'x': startPoint.x + 1000,
'y': startPoint.y + 250,
'damage': false});
result.upper.push({'x': startPoint.x + 1300,
'y': startPoint.y,
'damage': false});
result.lower.push({'x': startPoint.x + 1300,
'y': Math.max(2*getHeight(), startPoint.y + 260),
'damage': false});
var platformStart = {'x': startPoint.x + 500,
'y': startPoint.y -50,
'damage': false}
result.platform = [];
result.platform.push(new GroundPoly(true, platformStart, 300));
this.lastChunk="u";
return result;
};