Compare commits
4 Commits
ed99df295a
...
811b27da46
Author | SHA1 | Date |
---|---|---|
transoptimal | 811b27da46 | |
transoptimal | f2aa5ede1b | |
transoptimal | b389e3b401 | |
transoptimal | 3e4e58e2a2 |
|
@ -37,8 +37,7 @@ export function draw_world (box, start_x = 0, start_y = 0, scale = BASE_SCALE) {
|
|||
|
||||
draw_floor(start_x, start_y, scale, !checkerboard(start_x, start_y));
|
||||
|
||||
iter_2d(range(0, world.BOX_SIZE - 1), (x, y) => {
|
||||
const tile = world.get_tile(box, x, y);
|
||||
world.for_each_tile(box, (x, y, tile) => {
|
||||
draw_tile(tile, x * scale + start_x, y * scale + start_y, scale);
|
||||
});
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import * as graphics from "./graphics.mjs";
|
||||
import { BOX_SIZE, get_tile } from "./world.mjs";
|
||||
import { BOX_SIZE, get_tile, get_root } from "./world.mjs";
|
||||
import { get_player_box } from "./player.mjs";
|
||||
import { save_in_browser, load_from_browser } from "./save_load.mjs";
|
||||
import { tick } from "/js/tile_update.mjs";
|
||||
|
@ -21,7 +21,7 @@ function main (timestamp) {
|
|||
const delta = dt_ms / 1000;
|
||||
|
||||
render(delta);
|
||||
tick(delta);
|
||||
tick(get_root(), delta);
|
||||
|
||||
requestAnimationFrame(main);
|
||||
}
|
||||
|
|
|
@ -33,9 +33,7 @@ on_press(" ", _ => {
|
|||
});
|
||||
|
||||
function clear_tile (x, y) {
|
||||
world.set_tile(player.box, x, y, {
|
||||
type: "empty",
|
||||
});
|
||||
world.clear_tile(player.box, x, y);
|
||||
}
|
||||
|
||||
on_press("Backspace", _ => {
|
||||
|
|
|
@ -6,8 +6,7 @@ import * as random from "/js/utils/random.mjs";
|
|||
|
||||
// lots of this code is very hacky temporary for testing things out & getting started with tile updates.
|
||||
|
||||
export function tick (delta_time) {
|
||||
const box = get_player_box();
|
||||
export function tick (box, delta_time) {
|
||||
world.for_each_tile(box, (x, y, tile) => {
|
||||
if (tile_tickers.has(tile.type)) {
|
||||
tile_tickers.get(tile.type)(box, x, y, delta_time);
|
||||
|
@ -24,15 +23,25 @@ const dirs = [
|
|||
[ 0, 1 ],
|
||||
];
|
||||
|
||||
// this might become terrible for performance eventually.
|
||||
tile_tickers.set("box", (box, x, y, delta_time) => {
|
||||
tick(world.get_tile(box, x, y).box, delta_time);
|
||||
});
|
||||
|
||||
tile_tickers.set("grass", (box, x, y, delta_time) => {
|
||||
const tile = world.get_tile(box, x, y);
|
||||
if (Math.random() > 0.9 ** delta_time) {
|
||||
if (Math.random() > 0.8 ** delta_time) {
|
||||
const dir = random.item(dirs);
|
||||
const [ d_x, d_y ] = dir;
|
||||
if (world.get_tile(box, x + d_x, y + d_y).type === "empty") {
|
||||
world.set_tile(box, x + d_x, y + d_y, {
|
||||
type: "grass",
|
||||
});
|
||||
const spaces = random.shuffle(dirs)
|
||||
.map(([d_x, d_y]) => [x + d_x, y + d_y]);
|
||||
|
||||
for (const [new_x, new_y] of spaces) {
|
||||
if (world.get_tile(box, new_x, new_y).type === "empty") {
|
||||
world.set_tile(box, new_x, new_y, {
|
||||
type: "grass",
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
|
|
@ -9,3 +9,13 @@ export function integer (min, max) {
|
|||
export function item (array) {
|
||||
return array[integer(0, array.length)];
|
||||
}
|
||||
|
||||
export function shuffle (array) {
|
||||
const copy = [ ...array ];
|
||||
const result = [];
|
||||
while (copy.length > 0) {
|
||||
const index = integer(0, copy.length);
|
||||
result.push(copy.splice(index, 1)[0]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
17
js/world.mjs
17
js/world.mjs
|
@ -7,12 +7,18 @@ export const CENTER = Math.floor(BOX_SIZE / 2);
|
|||
|
||||
function create_world (size) {
|
||||
return {
|
||||
tiles: Array(size).fill(0).map(_ => Array(size).fill(0)),
|
||||
tiles: Array(size).fill(0).map(_ =>
|
||||
Array(size).fill(0).map(_ => empty_tile())
|
||||
),
|
||||
depth: 0,
|
||||
};
|
||||
}
|
||||
const world = create_world(BOX_SIZE);
|
||||
|
||||
export function empty_tile () {
|
||||
return { type: "empty" };
|
||||
}
|
||||
|
||||
export function create_new () {
|
||||
return create_world(BOX_SIZE);
|
||||
}
|
||||
|
@ -27,6 +33,10 @@ export function get_tile (world, x, y) {
|
|||
return world.tiles[x][y];
|
||||
}
|
||||
|
||||
export function clear_tile (box, x, y) {
|
||||
set_tile(box, x, y, empty_tile());
|
||||
}
|
||||
|
||||
export function create_box (parent) {
|
||||
return {
|
||||
...create_world(BOX_SIZE),
|
||||
|
@ -73,7 +83,10 @@ function reconstruct_parent (world, parent) {
|
|||
}
|
||||
|
||||
export function from_json (json) {
|
||||
const result = JSON.parse(json);
|
||||
const result = JSON.parse(json, (key, value) => {
|
||||
if (value === undefined || value === 0 || value === null) return empty_tile();
|
||||
return value;
|
||||
});
|
||||
|
||||
for_each_tile(result, (x, y, tile) => {
|
||||
if (tile.type === "box") {
|
||||
|
|
Loading…
Reference in New Issue