refactor world code to no longer assume the player entity:s pov.

This commit is contained in:
HyperOnion 2023-06-24 10:03:32 +02:00 committed by transoptimal
parent c8e8d7eb70
commit 68c93726f0
5 changed files with 29 additions and 35 deletions

View File

@ -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) {

View File

@ -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": {

View File

@ -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);

View File

@ -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;
}

View File

@ -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;