Skip to content

Commit

Permalink
Grid is now updating with dynamic points
Browse files Browse the repository at this point in the history
  • Loading branch information
serbanghita committed Oct 21, 2024
1 parent c1faf85 commit 4d1e64b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
6 changes: 5 additions & 1 deletion packages/demos/tiled-rendering/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ Example of rendering individual tiles:
![Basic rendering of layers](resources/demo1.png)

Example of rendering collision tiles:
![Basic rendering of layers](resources/demo2.png)
![Basic rendering of layers](resources/demo2.png)

Example of random points moving on a static grid
interacting with themselves and colliding with static tiles:
![Basic rendering of layers](resources/demo3.png)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion packages/demos/tiled-rendering/src/MoveSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@ export default class MoveSystem extends System {
let futureX = tile.properties.x + randomInt(-1, 1);
let futureY = tile.properties.y + randomInt(-1, 1);

const currentTile = tile.properties.tile;
const futureTile = getTileFromCoordinates(futureX, futureY, matrixComponent.properties);
if (matrix[futureTile] === 0) {
if (currentTile === futureTile || matrix[futureTile] === 0) {
tile.properties.x = futureX;
tile.properties.y = futureY;
tile.properties.point.x = futureX;
tile.properties.point.y = futureY;
tile.properties.tile = futureTile;

// Update the tile on the "matrix".
matrix[currentTile] = 0;
matrix[futureTile] = 2;
}
});
}
Expand Down
17 changes: 14 additions & 3 deletions packages/demos/tiled-rendering/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { PreRenderTiledMapSystem } from "@serbanghita-gamedev/renderer";
import { IsTiledMap, Renderable } from "@serbanghita-gamedev/component";
import { loadSprites } from "./assets";
import { Point, Rectangle } from "@serbanghita-gamedev/geometry";
import { randomInt } from "./helpers";
import IsMatrix from "./IsMatrix";
import { TiledMap } from "@serbanghita-gamedev/tiled";
import { getTileCoordinates } from "@serbanghita-gamedev/matrix";
import { getTileCoordinates, getTileFromCoordinates } from "@serbanghita-gamedev/matrix";
import { QuadTree } from "@serbanghita-gamedev/quadtree";
import QuadTreeSystem from "./QuadTreeSystem";
import IsCollisionTile from "./IsCollisionTile";
Expand Down Expand Up @@ -84,10 +83,22 @@ async function setup() {
const x = event.clientX | 0;
const y = event.clientY | 0;

const map = world.getEntity("map");
if (!map) {
throw new Error(`Map entity has not been defined yet.`);
}
const mapMatrix = map.getComponent(IsMatrix);
const tile = getTileFromCoordinates(x, y, mapMatrix.properties);

if (mapMatrix.properties.matrix[tile] !== 0) {
return;
}

const playerId = `player-${x}-${y}`;
const player = world.createEntity(playerId);
const point = new Point(x, y, playerId);
player.addComponent(IsCollisionTile, { x, y, point });

player.addComponent(IsCollisionTile, { x, y, point, tile });
player.addComponent(IsRenderedInForeground);
player.addComponent(IsPlayer);
quadtree.addPoint(point);
Expand Down

0 comments on commit 4d1e64b

Please sign in to comment.