create entity module and refactor player into entity.
This commit is contained in:
parent
f7caf8a3a8
commit
fbf0960e7e
|
@ -0,0 +1,27 @@
|
|||
import * as world from "./world.mjs";
|
||||
|
||||
|
||||
|
||||
const entities = [];
|
||||
|
||||
export function create (x, y, color = "#fff") {
|
||||
const entity = {
|
||||
id: entities.length,
|
||||
x,
|
||||
y,
|
||||
color,
|
||||
};
|
||||
entities.push(entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
export function destroy (id) {
|
||||
// returns the destroyed entity, idk if that makes sense.
|
||||
return entities.splice(id, 1)[0];
|
||||
}
|
||||
|
||||
export function for_each (callback) {
|
||||
entities.forEach(entity => {
|
||||
callback(entity, entity.id);
|
||||
});
|
||||
}
|
|
@ -2,6 +2,7 @@ import * as graphics from "./graphics_core.mjs";
|
|||
export * from "./graphics_core.mjs";
|
||||
|
||||
import { range, iter_2d } from "./utils/range.mjs";
|
||||
import * as entity from "./entity.mjs";
|
||||
|
||||
|
||||
|
||||
|
@ -9,7 +10,7 @@ const canvas = document.getElementById("canvas");
|
|||
const canvas_context = canvas.getContext("2d");
|
||||
graphics.set_size(canvas.width, canvas.height);
|
||||
|
||||
const SCALE = 64;
|
||||
const SCALE = 81;
|
||||
|
||||
export function draw_tile (x, y) {
|
||||
graphics.fill_rect(x * SCALE, y * SCALE, SCALE, SCALE);
|
||||
|
@ -34,11 +35,6 @@ export function draw_world (box_size, get_tile) {
|
|||
const tile = get_tile(x, y);
|
||||
|
||||
switch (tile.type) {
|
||||
case "player": {
|
||||
graphics.set_color("#0f0");
|
||||
draw_tile(x, y);
|
||||
break;
|
||||
}
|
||||
case "box": {
|
||||
graphics.set_color("#ff0");
|
||||
draw_tile(x, y);
|
||||
|
@ -46,6 +42,11 @@ export function draw_world (box_size, get_tile) {
|
|||
}
|
||||
}
|
||||
});
|
||||
|
||||
entity.for_each((entity, id) => {
|
||||
graphics.set_color(entity.color);
|
||||
draw_tile(entity.x, entity.y);
|
||||
});
|
||||
}
|
||||
|
||||
export function render () {
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
import * as world from "./world.mjs";
|
||||
import { on_press } from "./input.mjs";
|
||||
import * as entity from "./entity.mjs";
|
||||
|
||||
|
||||
|
||||
const player = {
|
||||
x: 2,
|
||||
y: 1,
|
||||
};
|
||||
const player = entity.create(2, 1, "#0f0");
|
||||
world.set_tile(player.x, player.y, { type: "player" });
|
||||
|
||||
function out_of_bounds (x, y) {
|
||||
|
@ -20,25 +18,21 @@ export function set_player_pos (x, y) {
|
|||
const previous_y = player.y;
|
||||
|
||||
if (out_of_bounds(x, y)) {
|
||||
world.set_tile(previous_x, previous_y, 0);
|
||||
const could_exit = world.exit_box();
|
||||
if (could_exit) {
|
||||
set_player_pos(0, 0);
|
||||
} else {
|
||||
world.set_tile(x, y, { type: "player" });
|
||||
set_player_pos(previous_x, previous_y);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (world.get_tile(x, y)?.type === "box") {
|
||||
world.set_tile(previous_x, previous_y, 0);
|
||||
world.enter_box(world.get_tile(x, y).box);
|
||||
set_player_pos(world.CENTER, world.CENTER);
|
||||
return;
|
||||
}
|
||||
|
||||
world.set_tile(previous_x, previous_y, 0);
|
||||
world.set_tile(x, y, { type: "player" });
|
||||
player.x = x;
|
||||
player.y = y;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue