From 85add3ddb420411dd46d0100151d349c4bc813c4 Mon Sep 17 00:00:00 2001 From: Nassim Date: Thu, 4 Jan 2024 16:07:42 -0500 Subject: [PATCH] major code cleanup --- entities/character.js | 14 ++++++++++---- entities/dialogBox.js | 1 - entities/npc.js | 6 +++++- entities/player.js | 24 ++++++++++++++---------- main.js | 1 + scenes/menu.js | 1 - scenes/world.js | 6 ++---- 7 files changed, 32 insertions(+), 21 deletions(-) diff --git a/entities/character.js b/entities/character.js index 1f89396..2af37fb 100644 --- a/entities/character.js +++ b/entities/character.js @@ -13,10 +13,16 @@ export function makeCharacter(p) { height: 32, setAnim(name) { - this.currentAnim = name; - this.currentFrame = 0; - this.animationTimer = 0; - this.previousTime = 0; + if (this.currentAnim !== name) { + this.currentAnim = name; + this.currentFrame = 0; + this.animationTimer = 0; + this.previousTime = 0; + } + }, + + setDirection(direction) { + if (this.direction !== direction) this.direction = direction; }, setAnimFrame(animData) { diff --git a/entities/dialogBox.js b/entities/dialogBox.js index 0c56b53..b3799df 100644 --- a/entities/dialogBox.js +++ b/entities/dialogBox.js @@ -61,7 +61,6 @@ export function makeDialogBox(p, x, y) { }, draw() { if (!this.isVisible) return; - p.noSmooth(); p.image(this.spriteRef, this.x, this.y); p.fill("black"); p.textSize(24); diff --git a/entities/npc.js b/entities/npc.js index 6c5b9ab..4dd0db0 100644 --- a/entities/npc.js +++ b/entities/npc.js @@ -28,6 +28,11 @@ export function makeNPC(p, x, y) { }; }, + setup() { + this.prepareAnims(); + this.setAnim("idle-down"); + }, + update() { this.previousTime = this.animationTimer; this.animationTimer += p.deltaTime; @@ -39,7 +44,6 @@ export function makeNPC(p, x, y) { this.screenX = this.x + camera.x; this.screenY = this.y + camera.y; - p.noSmooth(); debugMode.drawHitbox(p, this); drawTile( p, diff --git a/entities/player.js b/entities/player.js index 12053fb..be6d7c9 100644 --- a/entities/player.js +++ b/entities/player.js @@ -5,7 +5,6 @@ import { drawTile, getFramesPos, isMaxOneKeyDown } from "../utils.js"; export function makePlayer(p, x, y) { return { ...makeCharacter(p), - direction: "down", speed: 200, x, y, @@ -35,30 +34,36 @@ export function makePlayer(p, x, y) { if (!isMaxOneKeyDown(p) || this.freeze) return; if (p.keyIsDown(p.RIGHT_ARROW)) { - if (this.direction !== "right") this.direction = "right"; - if (this.currentAnim !== "run-side") this.setAnim("run-side"); + this.setDirection("right"); + this.setAnim("run-side"); this.x += moveBy; } if (p.keyIsDown(p.LEFT_ARROW)) { - if (this.direction !== "left") this.direction = "left"; - if (this.currentAnim !== "run-side") this.setAnim("run-side"); + this.setDirection("left"); + this.setAnim("run-side"); this.x -= moveBy; } if (p.keyIsDown(p.UP_ARROW)) { - if (this.direction !== "up") this.direction = "up"; - if (this.currentAnim !== "run-up") this.setAnim("run-up"); + this.setDirection("up"); + this.setAnim("run-up"); this.y -= moveBy; } if (p.keyIsDown(p.DOWN_ARROW)) { - if (this.direction !== "down") this.direction = "down"; - if (this.currentAnim !== "run-down") this.setAnim("run-down"); + this.setDirection("down"); + this.setAnim("run-down"); this.y += moveBy; } }, + setup() { + this.prepareAnims(); + this.direction = "down"; + this.setAnim("idle-down"); + }, + update() { this.previousTime = this.animationTimer; this.animationTimer += p.deltaTime; @@ -79,7 +84,6 @@ export function makePlayer(p, x, y) { p.scale(-1, 1); p.translate(-2 * this.screenX - this.tileWidth, 0); } - p.noSmooth(); debugMode.drawHitbox(p, this); drawTile( p, diff --git a/main.js b/main.js index 2404243..99cede0 100644 --- a/main.js +++ b/main.js @@ -31,6 +31,7 @@ new p5((p) => { canvasEl.canvas.style = ""; p.textFont(font); + p.noSmooth(); // for pixels to not become blurry world.setup(); battle.setup(); diff --git a/scenes/menu.js b/scenes/menu.js index 4b60061..99248af 100644 --- a/scenes/menu.js +++ b/scenes/menu.js @@ -21,7 +21,6 @@ export function makeMenu(p) { }, draw() { p.clear(); - p.noSmooth(); p.image(this.startScreenImgRef, 0, 0); p.tint(255, this.alpha); p.image(this.startTextImgRef, 0, 320); diff --git a/scenes/world.js b/scenes/world.js index 4b5ba42..c4b4f55 100644 --- a/scenes/world.js +++ b/scenes/world.js @@ -37,12 +37,10 @@ export function makeWorld(p, setScene) { default: } } - this.player.prepareAnims(); - this.player.setAnim("idle-down"); + this.player.setup(); this.camera.attachTo(this.player); - this.npc.prepareAnims(); - this.npc.setAnim("idle-down"); + this.npc.setup(); }, update() {