From 4d3aba7167fbfd5e002714e3c4a577cc17f18af0 Mon Sep 17 00:00:00 2001
From: Mark Powers <markppowers0@gmail.com>
Date: Thu, 14 May 2020 10:51:54 -0500
Subject: Refactor updates, fix gameover, add level changing

---
 src/paperflight/static/draw.js   |  8 ++++++--
 src/paperflight/static/levels.js | 12 +++++++-----
 src/paperflight/static/update.js | 11 ++++++-----
 src/paperflight/static/util.js   |  9 +++++++++
 4 files changed, 28 insertions(+), 12 deletions(-)

(limited to 'src/paperflight/static')

diff --git a/src/paperflight/static/draw.js b/src/paperflight/static/draw.js
index 4646581..5dd919f 100644
--- a/src/paperflight/static/draw.js
+++ b/src/paperflight/static/draw.js
@@ -9,13 +9,17 @@ function draw() {
         item.draw()
     })
     drawExit(currLevel.exit)
-
     drawPlane()
 
     ctx.fillStyle = "#222"
     ctx.font = "20px Courier"
     ctx.fillText(`Level ${currLevelIndex} (${collected}/${currLevel.coinsNeeded}) - ${currLevel.title} - Time ${Math.round(t / fps / 60)}:${Math.round(t / fps) % 60}`, 20, 20)
-
+    
+    if(gameIsOver){
+        ctx.font = "20px Courier"
+        ctx.fillStyle = "black"
+        ctx.fillText("Game Over!", 200, 200);
+    }
 }
 function drawPlane() {
     ctx.fillStyle = "#fff"
diff --git a/src/paperflight/static/levels.js b/src/paperflight/static/levels.js
index 7555afb..8ba4a0d 100644
--- a/src/paperflight/static/levels.js
+++ b/src/paperflight/static/levels.js
@@ -6,7 +6,8 @@ var levels = {
         coinsNeeded: 0,
         exit: {
             x: 40,
-            y: 460
+            y: 460,
+            radius: 20
         },
         title: "Switch madness",
         items: [
@@ -20,19 +21,20 @@ var levels = {
         coinsNeeded: 5,
         exit: {
             x: 40,
-            y: 460
+            y: 460,
+            radius: 20
         },
         title: "Downwards Dash",
         items: [
-            constructVent(420, 580, 60, 300),
+            constructVent(420, 580, 60, 600),
             constructCoin(40, 80),
             constructVent(120, 100, 280, 100),
             constructBlock(580, 200, 10, 250),
             constructCoin(700, 240),
             constructCoin(700, 320),
             constructCoin(700, 400),
-            constructRamp(70, 410, 200, 10, 2/5),
-            constructRamp(70, 470, 200, 10, 2/5),
+            constructRamp(70, 400, 200, 10, 2/5),
+            constructRamp(70, 480, 200, 10, 2/5),
             constructCoin(170, 405),
             constructBlock(0, 0, 800, 40)
         ]
diff --git a/src/paperflight/static/update.js b/src/paperflight/static/update.js
index 2a2a949..0b44089 100644
--- a/src/paperflight/static/update.js
+++ b/src/paperflight/static/update.js
@@ -8,14 +8,17 @@ function update() {
         plane.x += plane.vx * plane.dir
         plane.y += plane.vy
     }
-    if (plane.x < 0 || plane.x > width - plane.width || plane.y < 0 || plane.y + plane.height > height) {
+    if (!isPlaneInBox({ x: plane.width, y: plane.height, width: width - (2*plane.width), height: height - (2*plane.height) })) {
         gameOver()
     }
     currLevel.items.forEach(item => {
         item.update()
     })
     if (atExit()) {
-        console.log("exit!")
+        setLevel(currLevelIndex + 1)
+    }
+    if(gameIsOver){
+        window.clearInterval(gameInterval)
     }
 }
 function updateVent() {
@@ -43,9 +46,7 @@ function updateRamp() {
     }
 }
 function updateCoin() {
-    if (!this.collected
-        && (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)) {
+    if (!this.collected && isPlaneInCircle(this)) {
         this.collected = true
         collected++
     }
diff --git a/src/paperflight/static/util.js b/src/paperflight/static/util.js
index f818809..3f14182 100644
--- a/src/paperflight/static/util.js
+++ b/src/paperflight/static/util.js
@@ -13,4 +13,13 @@ function isPlaneInBox(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 isPointInCircle(x, y, circle) {
+    return Math.sqrt(Math.pow(x - circle.x, 2) + Math.pow(y - circle.y, 2)) < circle.radius
+}
+function isPlaneInCircle(circle) {
+    return isPointInCircle(plane.x, plane.y, circle)
+        || isPointInCircle(plane.x + plane.width, plane.y, circle)
+        || isPointInCircle(plane.x + plane.width, plane.y + plane.height, circle)
+        || isPointInCircle(plane.x, plane.y + plane.height, circle)
 }
\ No newline at end of file
-- 
cgit v1.2.3