From e0552a705e3ddfd79a684ee7a67ca02f57fbb2bc Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Wed, 13 May 2020 22:23:28 -0500 Subject: Complete switches --- src/paperflight/index.html | 2 +- src/paperflight/static/draw.js | 8 +++++-- src/paperflight/static/levels.js | 45 +++++++++++++++++++++++++++++++++++++ src/paperflight/static/update.js | 48 +++++++++++++++++++++++++++++----------- 4 files changed, 87 insertions(+), 16 deletions(-) diff --git a/src/paperflight/index.html b/src/paperflight/index.html index 9432244..c22f322 100644 --- a/src/paperflight/index.html +++ b/src/paperflight/index.html @@ -48,7 +48,6 @@ } score = 0 t = 0 - switchState = 0 plane = { x: 30, y: height / 4, @@ -69,6 +68,7 @@ currLevel = levels[currLevelIndex] currLevel.items = currLevel.items.slice() collected = 0 + switchState = 0 startLevel = false } function atExit() { diff --git a/src/paperflight/static/draw.js b/src/paperflight/static/draw.js index de64e27..13ecce5 100644 --- a/src/paperflight/static/draw.js +++ b/src/paperflight/static/draw.js @@ -2,11 +2,12 @@ function draw() { ctx.fillStyle = "#4444ff" ctx.fillRect(0, 0, width, height) - drawPlane() currLevel.items.forEach(item => { item.draw() }) drawExit(currLevel.exit) + + drawPlane() ctx.fillStyle = "#222" ctx.font = "20px Courier" @@ -85,8 +86,11 @@ function drawSwitchRect(){ ctx.setLineDash([5, 5]); ctx.beginPath() ctx.moveTo(this.x, this.y) + ctx.lineTo(this.x + this.width, this.y) ctx.lineTo(this.x + this.width, this.y + this.height) - ctx.lineTo(this.x + this.width, this.y + this.height) + ctx.lineTo(this.x, this.y + this.height) ctx.lineTo(this.x, this.y) + ctx.stroke() + ctx.setLineDash([]); } } \ No newline at end of file diff --git a/src/paperflight/static/levels.js b/src/paperflight/static/levels.js index a7116b4..aba9712 100644 --- a/src/paperflight/static/levels.js +++ b/src/paperflight/static/levels.js @@ -1,5 +1,50 @@ var levels = { 1: { + coinsNeeded: 0, + exit: { + x: 40, + y: 460 + }, + title: "Switch madness", + items: [{ + draw: drawSwitch, + update: updateSwitch, + x: 120, + y: 400, + width: 60, + height: 60, + stateCount: 3 + }, + { + draw: drawSwitchRect, + update: updateSwitchRect, + x: 300, + y: 400, + width: 60, + height: 60, + state: 0 + }, + { + draw: drawSwitchRect, + update: updateSwitchRect, + x: 400, + y: 400, + width: 60, + height: 60, + state: 1 + }, + { + draw: drawSwitchRect, + update: updateSwitchRect, + x: 500, + y: 400, + width: 60, + height: 60, + state: 2 + }, + ] + }, + 2: { coinsNeeded: 5, exit: { x: 40, diff --git a/src/paperflight/static/update.js b/src/paperflight/static/update.js index 226d015..ac5c284 100644 --- a/src/paperflight/static/update.js +++ b/src/paperflight/static/update.js @@ -1,7 +1,20 @@ +function isPointInRamp(x, y, ramp) { + var yDelta = (x - ramp.x) * ramp.slope + return ramp.x < x && x < ramp.x + ramp.width && ramp.y < y + yDelta && y + yDelta < ramp.y + ramp.height +} +function isPointInBox(x, y, box) { + return box.x <= x && x <= box.x + box.width && box.y <= y && y <= box.y + box.height +} +function isPlaneInBox(box) { + return isPointInBox(plane.x, plane.y, box) + || isPointInBox(plane.x + plane.width, plane.y, box) + || isPointInBox(plane.x + plane.width, plane.y + plane.height, box) + || isPointInBox(plane.x, plane.y + plane.height, box) +} function update() { t++ score++ - if(startLevel){ + if (startLevel) { plane.x += plane.vx * plane.dir plane.y += plane.vy } @@ -11,27 +24,21 @@ function update() { currLevel.items.forEach(item => { item.update() }) - currLevel.items = currLevel.items.filter(item => !item.collected ) + currLevel.items = currLevel.items.filter(item => !item.collected) if (atExit()) { console.log("exit!") } } function updateVent() { - if (plane.x >= this.x - plane.width && plane.x <= this.x + this.width && this.y - this.height < plane.y && plane.y < this.y) { - // console.log(plane.y, this.y, this.y-this.height) + if (isPlaneInBox(this)) { plane.y -= 2 } } function updateBlock() { - if (plane.x >= this.x - plane.width && plane.x <= this.x + this.width - && plane.y >= this.y - plane.height && plane.y <= this.y + this.height) { + if (isPlaneInBox(this)) { gameOver() } } -function isPointInRamp(x, y, ramp) { - var yDelta = (x - ramp.x) * ramp.slope - return ramp.x < x && x < ramp.x + ramp.width && ramp.y < y + yDelta && y + yDelta < ramp.y + ramp.height -} function updateRamp() { if (isPointInRamp(plane.x, plane.y, this) || isPointInRamp(plane.x + plane.width, plane.y, this) || @@ -40,10 +47,25 @@ function updateRamp() { gameOver() } } -function updateCoin(){ - if( Math.sqrt(Math.pow(plane.x - this.x, 2) + Math.pow(plane.y - this.y, 2)) < this.radius - || Math.sqrt(Math.pow(plane.x + plane.width - this.x, 2) + Math.pow(plane.y - this.y, 2)) < this.radius){ +function updateCoin() { + if (Math.sqrt(Math.pow(plane.x - this.x, 2) + Math.pow(plane.y - this.y, 2)) < this.radius + || Math.sqrt(Math.pow(plane.x + plane.width - this.x, 2) + Math.pow(plane.y - this.y, 2)) < this.radius) { this.collected = true collected++ } } +function updateSwitch() { + if(isPlaneInBox(this)){ + if(!this.inBox){ + this.inBox = true + switchState = (switchState + 1) % this.stateCount + } + } else { + this.inBox = false + } +} +function updateSwitchRect() { + if(isPlaneInBox(this) && switchState == this.state){ + gameOver() + } +} \ No newline at end of file -- cgit v1.2.3