From 68c93726f05e680b6ff381618dcc9d18431e950a Mon Sep 17 00:00:00 2001 From: HyperOnion <> Date: Sat, 24 Jun 2023 10:03:32 +0200 Subject: [PATCH] refactor world code to no longer assume the player entity:s pov. --- js/entity.mjs | 12 +++++------- js/graphics.mjs | 9 +++++---- js/main.mjs | 4 ++-- js/player.mjs | 6 +++++- js/world.mjs | 33 ++++++++++++--------------------- 5 files changed, 29 insertions(+), 35 deletions(-) diff --git a/js/entity.mjs b/js/entity.mjs index 857caf8..ff54b66 100644 --- a/js/entity.mjs +++ b/js/entity.mjs @@ -40,8 +40,9 @@ export function set_pos (entity, x, y) { entity.x = x; entity.y = y; - /* if (out_of_bounds(entity)) { + clamp_pos(entity); + /* const could_exit = world.exit_box(); if (could_exit) { set_player_pos(0, 0); @@ -49,15 +50,12 @@ export function set_pos (entity, x, y) { 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; + if (world.get_tile(entity.box, entity.x, entity.y)?.type === "box") { + // TODO } - */ - clamp_pos(entity); } export function move (entity, d_x, d_y) { diff --git a/js/graphics.mjs b/js/graphics.mjs index dda7296..ce68868 100644 --- a/js/graphics.mjs +++ b/js/graphics.mjs @@ -3,6 +3,7 @@ export * from "./graphics_core.mjs"; import { range, iter_2d } from "./utils/range.mjs"; import * as entity from "./entity.mjs"; +import * as world from "./world.mjs"; @@ -28,11 +29,11 @@ export function draw_floor (box_size) { }); } -export function draw_world (box_size, get_tile) { - draw_floor(box_size); +export function draw_world (box) { + draw_floor(world.BOX_SIZE); - iter_2d(range(0, box_size - 1), (x, y) => { - const tile = get_tile(x, y); + iter_2d(range(0, world.BOX_SIZE - 1), (x, y) => { + const tile = world.get_tile(box, x, y); switch (tile.type) { case "box": { diff --git a/js/main.mjs b/js/main.mjs index defb484..8cff8e6 100644 --- a/js/main.mjs +++ b/js/main.mjs @@ -1,12 +1,12 @@ import * as graphics from "./graphics.mjs"; import { BOX_SIZE, get_tile } from "./world.mjs"; -import "./player.mjs"; +import { get_player_box } from "./player.mjs"; function render (timestamp) { graphics.clear(); - graphics.draw_world(BOX_SIZE, get_tile); + graphics.draw_world(get_player_box()); graphics.render(); requestAnimationFrame(render); diff --git a/js/player.mjs b/js/player.mjs index cab9b5d..99ff134 100644 --- a/js/player.mjs +++ b/js/player.mjs @@ -26,8 +26,12 @@ on_press("ArrowDown", _ => { }); on_press(" ", _ => { - world.set_tile(player.x + 1, player.y, { + world.set_tile(player.box, player.x + 1, player.y, { type: "box", box: world.create_box(), }); }); + +export function get_player_box () { + return player.box; +} diff --git a/js/world.mjs b/js/world.mjs index 2feee01..f5e0191 100644 --- a/js/world.mjs +++ b/js/world.mjs @@ -4,36 +4,27 @@ export const CENTER = Math.floor(BOX_SIZE / 2); function create_world (size) { - return Array(BOX_SIZE).fill(0).map(_ => Array(BOX_SIZE).fill(0)); + return { + tiles: Array(BOX_SIZE).fill(0).map(_ => Array(BOX_SIZE).fill(0)), + }; } const world = create_world(BOX_SIZE); -const parent_stack = []; -let current_world = world; -export function set_tile (x, y, tile) { - current_world[x][y] = tile; +export function set_tile (world, x, y, tile) { + world.tiles[x][y] = tile; } -export function get_tile (x, y) { - return current_world[x][y]; +export function get_tile (world, x, y) { + return world.tiles[x][y]; } -export function create_box () { - return create_world(BOX_SIZE); +export function create_box (parent) { + return { + ...create_world(BOX_SIZE), + parent, + }; } -export function enter_box (box) { - parent_stack.push(current_world); - current_world = box; -} - -export function exit_box () { - if (parent_stack.length > 0) { - current_world = parent_stack.pop(); - return true; - } - return false; -} export function get_root () { return world;