summaryrefslogtreecommitdiff
path: root/src/engine
diff options
context:
space:
mode:
authorniliara-edu <nil.jimeno@estudiant.fjaverianas.com>2024-12-24 17:58:20 +0100
committerniliara-edu <nil.jimeno@estudiant.fjaverianas.com>2024-12-24 17:58:20 +0100
commit25adfc618e77db9a5ee3b98ce0dab0be832efed0 (patch)
tree36ca56758ddd4674ab5a43df4b76bbfcc5a47af1 /src/engine
parenta840990bdcabf45fb0d377478ba0ab27222434ae (diff)
hello kitty base done
Diffstat (limited to 'src/engine')
-rw-r--r--src/engine/clock.js2
-rw-r--r--src/engine/collision.js45
-rw-r--r--src/engine/keyboard.js3
-rw-r--r--src/engine/radio.js14
-rw-r--r--src/engine/screen.js15
-rw-r--r--src/engine/vector.js3
6 files changed, 75 insertions, 7 deletions
diff --git a/src/engine/clock.js b/src/engine/clock.js
index e817863..d1e0898 100644
--- a/src/engine/clock.js
+++ b/src/engine/clock.js
@@ -1,6 +1,4 @@
import { Engine } from "../engine.js"
-
-export { Engine } from "../engine.js"
const fps = 60
/* cap the game's fps */
diff --git a/src/engine/collision.js b/src/engine/collision.js
new file mode 100644
index 0000000..78e9a1a
--- /dev/null
+++ b/src/engine/collision.js
@@ -0,0 +1,45 @@
+import { boshy, enemy } from "../main.js"
+import { Vector } from "./vector.js"
+
+export class Collision {
+ collidingWithBoshy(position, hitbox = position) {
+ if (boshy == undefined) return false
+ return this.areColliding({
+ position_a: position,
+ position_b: boshy.position,
+ hitbox_a: hitbox,
+ hitbox_b: boshy.hitbox,
+ })
+ }
+
+ collidingWithEnemy(position, hitbox = position) {
+ if (enemy == undefined) return false
+ return this.areColliding({
+ position_a: position,
+ position_b: enemy.position,
+ hitbox_a: hitbox,
+ hitbox_b: enemy.hitbox,
+ })
+ }
+
+ areColliding({
+ position_a,
+ position_b,
+ hitbox_a= position_a,
+ hitbox_b= position_b,
+ }) {
+ let hitbox_value = new Vector(
+ (Math.abs(hitbox_a.x) + Math.abs(hitbox_b.x)) / 2,
+ (Math.abs(hitbox_a.y) + Math.abs(hitbox_b.y)) / 2,
+ )
+ let position_difference = new Vector(
+ Math.abs( Math.abs(position_a.x) - Math.abs(position_b.x)),
+ Math.abs( Math.abs(position_a.y) - Math.abs(position_b.y)),
+ )
+
+ return (
+ position_difference.x <= hitbox_value.x &&
+ position_difference.y <= hitbox_value.y
+ )
+ }
+}
diff --git a/src/engine/keyboard.js b/src/engine/keyboard.js
index d634519..3a9c39e 100644
--- a/src/engine/keyboard.js
+++ b/src/engine/keyboard.js
@@ -1,3 +1,5 @@
+import { SCENE, SCENES } from "../scenes.js"
+
export function setUpKeyboard() {
document.addEventListener("keyup", k => trigger(k))
document.addEventListener("keydown", k => trigger(k))
@@ -19,5 +21,6 @@ function trigger(event) {
case "ArrowUp": keys.up = on; break;
case "ArrowDown": keys.down = on; break;
case "z": case "Z": keys.shoot = on; break;
+ case "r": case "R": if (on) SCENE.load(SCENES.PREQUEL); break;
}
}
diff --git a/src/engine/radio.js b/src/engine/radio.js
new file mode 100644
index 0000000..d39524a
--- /dev/null
+++ b/src/engine/radio.js
@@ -0,0 +1,14 @@
+export class Radio {
+ bgm
+ playMusic(file) {
+ if (this.bgm != undefined) this.bgm.pause()
+ this.bgm = new Audio(file)
+ this.bgm.play()
+ this.bgm.loop = true
+ }
+
+ playSound(file) {
+ let audio = new Audio(file)
+ audio.play()
+ }
+}
diff --git a/src/engine/screen.js b/src/engine/screen.js
index 2d32b62..173e285 100644
--- a/src/engine/screen.js
+++ b/src/engine/screen.js
@@ -20,12 +20,14 @@ export class Screen {
this.div = document.createElement("div")
this.div.id = "screen"
this.div.style.backgroundColor = "black"
+ this.div.style.zIndex = -11
/* create background image */
this.background = document.createElement("img")
this.background.className = "background"
this.background.src = `${BACKGROUND.DEFAULT}`
this.background.style.visibility = "hidden"
+ this.background.style.zIndex = -10
this.div.appendChild(this.background)
/* create relative div to append children */
@@ -77,11 +79,20 @@ export class Screen {
draw(span, position, size, /* rotation = 0 */) {
span.style.width = `${size.x * this.scale}px`
span.style.height = `${size.y * this.scale}px`
- span.style.top = `${(position.y - size.x / 2) * this.scale}px`
- span.style.left = `${(position.x - size.y / 2) * this.scale}px`
+ span.style.top = `${(position.y - size.y / 2) * this.scale}px`
+ span.style.left = `${(position.x - size.x / 2) * this.scale}px`
}
setBackground(img) { this.background.src = img }
showBackground() { this.background.visibility = "visible" }
hideBackground() { this.background.visibility = "hidden" }
+
+ isOffLimits(position, size) {
+ return (
+ position.x + size.x < 0 ||
+ position.y + size.y < 0 ||
+ position.x - size.x > this.viewport.x ||
+ position.y - size.y > this.viewport.y
+ )
+ }
}
diff --git a/src/engine/vector.js b/src/engine/vector.js
index 452e47b..f596c51 100644
--- a/src/engine/vector.js
+++ b/src/engine/vector.js
@@ -7,7 +7,6 @@ export class Vector {
add(newVector) {
this.x += newVector.x
this.y += newVector.y
- return this
}
normalize() {
@@ -19,13 +18,11 @@ export class Vector {
this.x = this.x / Math.abs(this.x) * c
this.y = this.y / Math.abs(this.x) * c
}
- return this
}
multiply(multiplier) {
this.x *= multiplier
this.y *= multiplier
- return this
}
clone() {