From caf97e33d7823bff6e805c4a5659bfa6c0af0fef Mon Sep 17 00:00:00 2001 From: Bethany Date: Sun, 26 Jan 2014 10:51:45 -0500 Subject: [PATCH] Adding some difficulty measures to the chunk Generator.. "u" chunks now have spikes after a certain distance. Added some prettiness for colour pickups --- chunkGenerator.js | 49 ++++++++++++++++++++++++++++++++++++++--------- colourPickup.js | 7 ++++++- engine.js | 17 +++++++++++----- 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/chunkGenerator.js b/chunkGenerator.js index 5209f1e..2845dcf 100644 --- a/chunkGenerator.js +++ b/chunkGenerator.js @@ -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 @@ -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); }; @@ -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++) { @@ -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"; @@ -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}); @@ -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; }; \ No newline at end of file diff --git a/colourPickup.js b/colourPickup.js index bc1a12e..3464e0c 100644 --- a/colourPickup.js +++ b/colourPickup.js @@ -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(); }; \ No newline at end of file diff --git a/engine.js b/engine.js index efd84ba..7c33b0b 100644 --- a/engine.js +++ b/engine.js @@ -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; } } }); @@ -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); @@ -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]); } }