aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/paperflight/index.html50
-rw-r--r--src/paperflight/static/draw.js8
-rw-r--r--src/paperflight/static/levels.js12
-rw-r--r--src/paperflight/static/update.js11
-rw-r--r--src/paperflight/static/util.js9
5 files changed, 60 insertions, 30 deletions
diff --git a/src/paperflight/index.html b/src/paperflight/index.html
index ef26a03..a620a1c 100644
--- a/src/paperflight/index.html
+++ b/src/paperflight/index.html
@@ -24,7 +24,9 @@
<script>
var username = undefined
- var score, plane, gameInterval, currLevelIndex, currLevel, t, collected, startLevel, switchState
+ var score, plane, gameInterval, currLevelIndex,
+ currLevel, t, collected, startLevel, switchState,
+ gameIsOver
var DEFAULT_VX = 3
var DEFAULT_VY = 0.5
@@ -48,11 +50,12 @@
if (gameInterval) {
clearInterval(gameInterval)
}
+ gameIsOver = false
score = 0
t = 0
plane = {
x: 30,
- y: height / 4,
+ y: 100,
vx: DEFAULT_VX,
vy: DEFAULT_VY,
width: 30,
@@ -66,28 +69,39 @@
}, 1000 / fps)
}
function setLevel(to) {
+ plane.x = 30
+ plane.y = 100
+ plane.dir= 1
currLevelIndex = to
- currLevel = levels[currLevelIndex]
- currLevel.items = currLevel.items.slice()
- currLevel.items.forEach(item => {
- if("collected" in item){
- item.collected = false
+ if (to in levels) {
+ currLevel = levels[currLevelIndex]
+ currLevel.items = currLevel.items.slice()
+ currLevel.items.forEach(item => {
+ if ("collected" in item) {
+ item.collected = false
+ }
+ })
+ collected = 0
+ switchState = 0
+ startLevel = false
+ } else {
+ currLevel = {
+ title: "YOU WIN!",
+ coinsNeeded: 0,
+ exit: {
+ x: -100,
+ y: -100,
+ radius: 0
+ },
+ items: []
}
- })
- collected = 0
- switchState = 0
- startLevel = false
+ }
}
function atExit() {
- return (Math.sqrt(Math.pow(plane.x - currLevel.exit.x, 2) + Math.pow(plane.y - currLevel.exit.y, 2)) < 20
- || Math.sqrt(Math.pow(plane.x + plane.width - currLevel.exit.x, 2) + Math.pow(plane.y - currLevel.exit.y, 2)) < 20)
- && collected >= currLevel.coinsNeeded
+ return isPlaneInCircle(currLevel.exit) && collected >= currLevel.coinsNeeded
}
function gameOver() {
- ctx.font = "20px Courier"
- ctx.fillStyle = "black"
- ctx.fillText("Game Over!", 200, 200);
- window.clearInterval(gameInterval)
+ gameIsOver = true
}
function keyDown(e) {
startLevel = true
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