aboutsummaryrefslogtreecommitdiff
path: root/src/paperflight/static/update.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/paperflight/static/update.js')
-rw-r--r--src/paperflight/static/update.js48
1 files changed, 35 insertions, 13 deletions
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