Skip to content

Commit

Permalink
Adding some difficulty measures to the chunk Generator.. "u" chunks n…
Browse files Browse the repository at this point in the history
…ow have spikes after a certain distance.

Added some prettiness for colour pickups
  • Loading branch information
brdunfield committed Jan 26, 2014
1 parent fcd83bb commit caf97e3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
49 changes: 40 additions & 9 deletions chunkGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var chunkGenerator = function(){

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(30, 2), // 20 is jumpV
Expand All @@ -16,7 +16,7 @@ chunkGenerator.prototype.generateChunk = function(lastPoint, r, g, b, dist, spee
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);
else if (RNG < 0.80 && this.lastChunk != "u") return this.generateU(lastPoint, maxJumpRange, gravity/speed, (dist > 20));
else return this.generateStraight(lastPoint);
};

Expand Down Expand Up @@ -98,9 +98,10 @@ chunkGenerator.prototype.generateSpikes = function(startPoint, maxJumpRange){
// generate a platform
console.log("Platform");
var startPoint = {'x': Math.random() * maxJumpRange - 25 + startPoint.x,
'y': startPoint.y - 150,
'y': startPoint.y - 200,
'damage': false};
result.platform = new GroundPoly(true, startPoint, 300);
result.platform = [];
result.platform.push(new GroundPoly(true, startPoint, 300));
}

for (var i = 0; i < numSpikes; i++) {
Expand All @@ -110,7 +111,7 @@ chunkGenerator.prototype.generateSpikes = function(startPoint, maxJumpRange){
}
}
result.upper.splice(0,1);
result.lower.push({'x': result.upper[result.upper.length -1],
result.lower.push({'x': result.upper[result.upper.length -1].x,
'y': 2*getHeight(),
'damage': false});
this.lastChunk="spikes";
Expand Down Expand Up @@ -170,27 +171,56 @@ chunkGenerator.prototype.generateStraight = function(startPoint){
var RNG = Math.random();
if (RNG < 0.2) {
var startPoint = {'x': Math.random()*1000 + startPoint.x,
'y': startPoint.y - 150,
'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 = result.platform = new GroundPoly(true, startPoint, 300);
this.platform.push(result.platform = new GroundPoly(true, startPoint, 300));
}
}

return result;
};

chunkGenerator.prototype.generateU = function(startPoint, maxJumpRange, maxSlope) {
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});
Expand All @@ -205,7 +235,8 @@ chunkGenerator.prototype.generateU = function(startPoint, maxJumpRange, maxSlope
var platformStart = {'x': startPoint.x + 500,
'y': startPoint.y -50,
'damage': false}
result.platform = new GroundPoly(true, platformStart, 300);
result.platform = [];
result.platform.push(new GroundPoly(true, platformStart, 300));
this.lastChunk="u";
return result;
};
7 changes: 6 additions & 1 deletion colourPickup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ var colourPickup = function(type, x, y) {
};

colourPickup.prototype.render = function(context) {
var gradient = context.createRadialGradient(this.x, this.y, this.radius/2, this.x, this.y, this.radius);
gradient.addColorStop(0, this.type);
gradient.addColorStop(0.7, 'rgba(255,255,255,0.8)');
gradient.addColorStop(1, 'rgba(255,255,255,0');

context.beginPath();
context.arc(this.x, this.y, this.radius, 0, Math.PI*2, false);
context.closePath();
context.fillStyle = this.type;
context.fillStyle = gradient;
context.fill();
};
17 changes: 12 additions & 5 deletions engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,13 @@ Engine.prototype.init = function()
// Handlers //
// Jump handler
window.addEventListener('keydown', function(e) {
if (e.keyCode == 32 && self.g != 0) {
if (e.keyCode == 32 /*&& self.g != 0*/) {
if (!self.char.falling){
if(self.colourDecay[1] == 2) self.colourDecay[1] += 10;
//if(self.colourDecay[1] == 2) self.colourDecay[1] += 10;
self.char.jump();
} else if (self.char.falling && self.g > 25) {
self.char.jump();
self.g -= 25;
}
}
});
Expand Down Expand Up @@ -406,13 +409,16 @@ Engine.prototype.updateWorld = function() {
{
var newPoly = gp.extend(this.cG, this.distance, this.speed, this.gravity);
if (newPoly) {
this.groundPolys.push(newPoly);
for (var i=0; i < newPoly.length; i++)
this.groundPolys.push(newPoly[i]);
}
}

//remove old
if (gp.u.length == 1) {
delete gp;
delete this.groundPolys.splice(i, 1);
i--;
continue;
} else {
if( gp.u[1].x < 0 )
gp.u.splice(0, 1);
Expand Down Expand Up @@ -537,7 +543,8 @@ Engine.prototype.getGroundIntersect = function(x)
{
var newPoly = this.groundPolys[0].extend(this.cG, this.distance, this.speed, this.gravity);
if (newPoly) {
this.groundPolys.push(newPoly);
for (var i=0; i < newPoly.length; i++)
this.groundPolys.push(newPoly[i]);
}
}

Expand Down

0 comments on commit caf97e3

Please sign in to comment.