From c8e8d7eb7056c61a3cc029f32995c2e1fdb0ce9e Mon Sep 17 00:00:00 2001 From: HyperOnion <> Date: Sat, 24 Jun 2023 09:52:53 +0200 Subject: [PATCH] begin refactoring movement logic out of player code into entity code. --- js/entity.mjs | 46 +++++++++++++++++++++++++++++++++++++++++++++- js/player.mjs | 42 +++++++----------------------------------- js/world.mjs | 4 ++++ 3 files changed, 56 insertions(+), 36 deletions(-) diff --git a/js/entity.mjs b/js/entity.mjs index 8660b94..857caf8 100644 --- a/js/entity.mjs +++ b/js/entity.mjs @@ -1,12 +1,14 @@ import * as world from "./world.mjs"; +const BOX_SIZE = world.BOX_SIZE; const entities = []; -export function create (x, y, color = "#fff") { +export function create (box, x = 0, y = 0, color = "#fff") { const entity = { id: entities.length, + box, x, y, color, @@ -25,3 +27,45 @@ export function for_each (callback) { callback(entity, entity.id); }); } + +function clamp_pos (entity) { + if (entity.x < 0) entity.x = 0; + if (entity.x >= BOX_SIZE) entity.x = BOX_SIZE - 1; + if (entity.y < 0) entity.y = 0; + if (entity.y >= BOX_SIZE) entity.y = BOX_SIZE - 1; + return entity; +} + +export function set_pos (entity, x, y) { + entity.x = x; + entity.y = y; + + /* + if (out_of_bounds(entity)) { + const could_exit = world.exit_box(); + if (could_exit) { + set_player_pos(0, 0); + } else { + set_player_pos(previous_x, previous_y); + } + return; + } + + if (world.get_tile(x, y)?.type === "box") { + world.enter_box(world.get_tile(x, y).box); + set_player_pos(world.CENTER, world.CENTER); + return; + } + */ + clamp_pos(entity); +} + +export function move (entity, d_x, d_y) { + set_pos(entity, entity.x + d_x, entity.y + d_y); +} + +function out_of_bounds (entity) { + const { x, y } = entity; + return x < 0 || x >= BOX_SIZE || + y < 0 || y >= BOX_SIZE +} diff --git a/js/player.mjs b/js/player.mjs index cedd474..cab9b5d 100644 --- a/js/player.mjs +++ b/js/player.mjs @@ -4,53 +4,25 @@ import * as entity from "./entity.mjs"; -const player = entity.create(2, 1, "#0f0"); -world.set_tile(player.x, player.y, { type: "player" }); +const player = entity.create(world.get_root(), 2, 1, "#0f0"); -function out_of_bounds (x, y) { - return x < 0 || x >= world.BOX_SIZE || - y < 0 || y >= world.BOX_SIZE -} -export function set_player_pos (x, y) { - const previous_x = player.x; - const previous_y = player.y; - if (out_of_bounds(x, y)) { - const could_exit = world.exit_box(); - if (could_exit) { - set_player_pos(0, 0); - } else { - set_player_pos(previous_x, previous_y); - } - return; - } - - if (world.get_tile(x, y)?.type === "box") { - world.enter_box(world.get_tile(x, y).box); - set_player_pos(world.CENTER, world.CENTER); - return; - } - - player.x = x; - player.y = y; -} - -export function move_player (d_x, d_y) { - set_player_pos(player.x + d_x, player.y + d_y); +export function move (d_x, d_y) { + entity.move(player, d_x, d_y); } on_press("ArrowLeft", _ => { - move_player(-1, 0); + move(-1, 0); }); on_press("ArrowRight", _ => { - move_player(1, 0); + move(1, 0); }); on_press("ArrowUp", _ => { - move_player(0, -1); + move(0, -1); }); on_press("ArrowDown", _ => { - move_player(0, 1); + move(0, 1); }); on_press(" ", _ => { diff --git a/js/world.mjs b/js/world.mjs index bcaf6e7..2feee01 100644 --- a/js/world.mjs +++ b/js/world.mjs @@ -34,3 +34,7 @@ export function exit_box () { } return false; } + +export function get_root () { + return world; +}