begin refactoring movement logic out of player code into entity code.
This commit is contained in:
parent
96678ea5a2
commit
c8e8d7eb70
|
@ -1,12 +1,14 @@
|
||||||
import * as world from "./world.mjs";
|
import * as world from "./world.mjs";
|
||||||
|
const BOX_SIZE = world.BOX_SIZE;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const entities = [];
|
const entities = [];
|
||||||
|
|
||||||
export function create (x, y, color = "#fff") {
|
export function create (box, x = 0, y = 0, color = "#fff") {
|
||||||
const entity = {
|
const entity = {
|
||||||
id: entities.length,
|
id: entities.length,
|
||||||
|
box,
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
color,
|
color,
|
||||||
|
@ -25,3 +27,45 @@ export function for_each (callback) {
|
||||||
callback(entity, entity.id);
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -4,53 +4,25 @@ import * as entity from "./entity.mjs";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const player = entity.create(2, 1, "#0f0");
|
const player = entity.create(world.get_root(), 2, 1, "#0f0");
|
||||||
world.set_tile(player.x, player.y, { type: "player" });
|
|
||||||
|
|
||||||
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)) {
|
export function move (d_x, d_y) {
|
||||||
const could_exit = world.exit_box();
|
entity.move(player, d_x, d_y);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
on_press("ArrowLeft", _ => {
|
on_press("ArrowLeft", _ => {
|
||||||
move_player(-1, 0);
|
move(-1, 0);
|
||||||
});
|
});
|
||||||
on_press("ArrowRight", _ => {
|
on_press("ArrowRight", _ => {
|
||||||
move_player(1, 0);
|
move(1, 0);
|
||||||
});
|
});
|
||||||
on_press("ArrowUp", _ => {
|
on_press("ArrowUp", _ => {
|
||||||
move_player(0, -1);
|
move(0, -1);
|
||||||
});
|
});
|
||||||
on_press("ArrowDown", _ => {
|
on_press("ArrowDown", _ => {
|
||||||
move_player(0, 1);
|
move(0, 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
on_press(" ", _ => {
|
on_press(" ", _ => {
|
||||||
|
|
|
@ -34,3 +34,7 @@ export function exit_box () {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function get_root () {
|
||||||
|
return world;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue