Skip to content

Commit

Permalink
major code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
JSLegendDev committed Jan 4, 2024
1 parent 3e5a4bc commit 85add3d
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 21 deletions.
14 changes: 10 additions & 4 deletions entities/character.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion entities/dialogBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion entities/npc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
24 changes: 14 additions & 10 deletions entities/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion scenes/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions scenes/world.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 85add3d

Please sign in to comment.