From 25adfc618e77db9a5ee3b98ce0dab0be832efed0 Mon Sep 17 00:00:00 2001 From: niliara-edu Date: Tue, 24 Dec 2024 17:58:20 +0100 Subject: hello kitty base done --- src/engine/clock.js | 2 -- src/engine/collision.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/engine/keyboard.js | 3 +++ src/engine/radio.js | 14 ++++++++++++++ src/engine/screen.js | 15 +++++++++++++-- src/engine/vector.js | 3 --- 6 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 src/engine/collision.js create mode 100644 src/engine/radio.js (limited to 'src/engine') 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() { -- cgit v1.2.3