From 587f1d63da7264c115eb436703b46cb9af30e443 Mon Sep 17 00:00:00 2001 From: Nassim Date: Sun, 31 Dec 2023 21:07:16 -0500 Subject: [PATCH] reorg --- maps/mapUtil.js => mapUtil.js | 4 +- scenes/world.js | 2 +- utils/Camera.js | 17 -------- utils/DebugMode.js | 25 ------------ utils/collisionLogic.js | 35 ---------------- utils/input.js | 15 ------- utils/spritesheetUtils.js | 40 ------------------ utils/ui.js | 77 ----------------------------------- 8 files changed, 3 insertions(+), 212 deletions(-) rename maps/mapUtil.js => mapUtil.js (94%) delete mode 100644 utils/Camera.js delete mode 100644 utils/DebugMode.js delete mode 100644 utils/collisionLogic.js delete mode 100644 utils/input.js delete mode 100644 utils/spritesheetUtils.js delete mode 100644 utils/ui.js diff --git a/maps/mapUtil.js b/mapUtil.js similarity index 94% rename from maps/mapUtil.js rename to mapUtil.js index 870f336..d79be03 100644 --- a/maps/mapUtil.js +++ b/mapUtil.js @@ -1,5 +1,5 @@ -import { makeCollidable } from "../entities/collidable.js"; -import { drawTile, getFramesPos } from "../utils.js"; +import { makeCollidable } from "./entities/collidable.js"; +import { drawTile, getFramesPos } from "./utils.js"; export function makeTiledMap(p, x, y) { return { diff --git a/scenes/world.js b/scenes/world.js index 700cb20..7eae79a 100644 --- a/scenes/world.js +++ b/scenes/world.js @@ -1,6 +1,6 @@ import { makeNPC } from "../entities/npc.js"; import { makePlayer } from "../entities/player.js"; -import { makeTiledMap } from "../maps/mapUtil.js"; +import { makeTiledMap } from "../mapUtil.js"; import { makeCamera, makeDialogBox } from "../utils.js"; export function makeWorld(p, setScene) { diff --git a/utils/Camera.js b/utils/Camera.js deleted file mode 100644 index 9c03ba6..0000000 --- a/utils/Camera.js +++ /dev/null @@ -1,17 +0,0 @@ -export function makeCamera(p, x, y) { - return { - p, - x, - y, - prevX: x, - prevY: y, - attachTo(entity) { - this.entity = entity; - }, - - update() { - this.x = -this.entity.x + this.p.width / 2; - this.y = -this.entity.y + this.p.height / 2; - }, - }; -} diff --git a/utils/DebugMode.js b/utils/DebugMode.js deleted file mode 100644 index 4290b92..0000000 --- a/utils/DebugMode.js +++ /dev/null @@ -1,25 +0,0 @@ -function makeDebugMode() { - return { - enabled: false, - drawFpsCounter(p) { - if (!this.enabled) return; - p.push(); - p.fill("yellow"); - p.textSize(24); - p.text(Math.trunc(p.frameRate()), 10, 20); - p.pop(); - }, - - toggle() { - this.enabled = !this.enabled; - }, - - drawHitbox(p, hitbox) { - if (!this.enabled) return; - p.fill(255, 0, 0, 63); - p.rect(hitbox.screenX, hitbox.screenY, hitbox.width, hitbox.height); - }, - }; -} - -export const debugMode = makeDebugMode(); diff --git a/utils/collisionLogic.js b/utils/collisionLogic.js deleted file mode 100644 index 8cce655..0000000 --- a/utils/collisionLogic.js +++ /dev/null @@ -1,35 +0,0 @@ -export function checkCollision(objA, objB) { - return !( - objA.x + objA.width < objB.x || - objA.x > objB.x + objB.width || - objA.y + objA.height < objB.y || - objA.y > objB.y + objB.height - ); -} - -export function preventOverlap(objA, objB) { - const overlapX = - Math.min(objA.x + objA.width, objB.x + objB.width) - - Math.max(objA.x, objB.x); - const overlapY = - Math.min(objA.y + objA.height, objB.y + objB.height) - - Math.max(objA.y, objB.y); - - if (overlapX < overlapY) { - if (objA.x < objB.x) { - // right - objB.x = objA.x + objA.width; - } else { - // left - objB.x = objA.x - objA.width; - } - } else { - if (objA.y < objB.y) { - // bottom - objB.y = objA.y + objA.height; - } else { - // top - objB.y = objA.y - objB.height; - } - } -} diff --git a/utils/input.js b/utils/input.js deleted file mode 100644 index df88726..0000000 --- a/utils/input.js +++ /dev/null @@ -1,15 +0,0 @@ -export function isMaxOneKeyDown(p) { - let isOnlyOneKeyDown = false; - for (const key of [p.RIGHT_ARROW, p.LEFT_ARROW, p.UP_ARROW, p.DOWN_ARROW]) { - if (!isOnlyOneKeyDown && p.keyIsDown(key)) { - isOnlyOneKeyDown = true; - continue; - } - - if (isOnlyOneKeyDown && p.keyIsDown(key)) { - return false; - } - } - - return true; -} diff --git a/utils/spritesheetUtils.js b/utils/spritesheetUtils.js deleted file mode 100644 index 0555adb..0000000 --- a/utils/spritesheetUtils.js +++ /dev/null @@ -1,40 +0,0 @@ -export function getFramesPos(nbCols, nbRows, tileWidth, tileHeight) { - const framesPos = []; - let currentTileX = 0; - let currentTileY = 0; - for (let i = 0; i < nbRows; i++) { - for (let j = 0; j < nbCols; j++) { - framesPos.push({ x: currentTileX, y: currentTileY }); - currentTileX += tileWidth; - } - currentTileX = 0; - currentTileY += tileHeight; - } - - return framesPos; -} - -export function drawTile( - p, - src, - destinationX, - destinationY, - srcX, - srcY, - tileWidth, - tileHeight -) { - p.image( - src, - destinationX, - destinationY, - tileWidth, - tileHeight, - srcX, - srcY, - // need to offset this by one because - // the first pixel is accounted for by the origin - tileWidth - 1, - tileHeight - 1 - ); -} diff --git a/utils/ui.js b/utils/ui.js deleted file mode 100644 index 0be7b87..0000000 --- a/utils/ui.js +++ /dev/null @@ -1,77 +0,0 @@ -import { characterInterface } from "../entities/entity.js"; - -export function makeDialogBox(p, x, y) { - return { - p, - x, - y, - spriteRef: null, - currentTime: 0, - previousTime: 0, - lineChars: null, - line: "", - isVisible: false, - onCompleteCallback: null, - isComplete: false, - load() { - this.spriteRef = characterInterface.loadAssets( - this.p, - "./assets/overlay_message.png" - ); - }, - - setVisibility(isVisible) { - this.isVisible = isVisible; - }, - - displayTextImmediately(content) { - this.line = content; - this.isComplete = true; - }, - - displayText(content, onComplete) { - this.lineChars = content.split(""); - this.isComplete = false; - if (onComplete) { - this.onCompleteCallback = onComplete; - return; - } - - this.onCompleteCallback = null; - }, - - clearText() { - this.line = ""; - this.lineChars = []; - }, - - update() { - if (!this.isVisible) return; - this.currentTime += this.p.deltaTime; - const durationPerFrame = 1000 / 60; - if (this.currentTime >= durationPerFrame) { - this.currentTime -= durationPerFrame; - - const nextChar = this.lineChars.shift(); - - if (this.isComplete) return; - - if (!nextChar && !this.isComplete) { - this.isComplete = true; - if (this.onCompleteCallback) this.onCompleteCallback(); - return; - } - - this.line += nextChar; - } - }, - draw() { - if (!this.isVisible) return; - this.p.noSmooth(); - this.p.image(this.spriteRef, this.x, this.y); - this.p.fill("black"); - this.p.textSize(24); - this.p.text(this.line, this.x + 30, this.y + 42); - }, - }; -}