-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Points on a grid, collision detection with solid obstacles is grid based
- Loading branch information
1 parent
5693acb
commit c1faf85
Showing
17 changed files
with
296 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Component } from "@serbanghita-gamedev/ecs"; | ||
import { Point } from "@serbanghita-gamedev/geometry"; | ||
|
||
export interface IsCollisionTileProps { | ||
x: number; | ||
y: number; | ||
point: Point; | ||
// Tile index. | ||
tile: number; | ||
} | ||
|
||
export default class IsCollisionTile extends Component { | ||
constructor(public properties: IsCollisionTileProps) { | ||
super(properties); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Component } from "@serbanghita-gamedev/ecs"; | ||
|
||
export interface IsMatrixProps { | ||
// Flat array matrix with all the possible entities, obstacles, etc. | ||
matrix: number[]; | ||
// Width in tiles. | ||
width: number; | ||
// Height in tiles. | ||
height: number; | ||
// Size of the tile in pixels. | ||
tileSize: number; | ||
} | ||
|
||
export default class IsMatrix extends Component { | ||
constructor(public properties: IsMatrixProps) { | ||
super(properties); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Component } from "@serbanghita-gamedev/ecs"; | ||
|
||
export default class IsPlayer extends Component { | ||
constructor(public properties: Record<string, never>) { | ||
super(properties); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Component } from "@serbanghita-gamedev/ecs"; | ||
|
||
export default class IsPreRendered extends Component { | ||
constructor(public properties: Record<string, never>) { | ||
super(properties); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Component } from "@serbanghita-gamedev/ecs"; | ||
|
||
export default class IsRenderedInForeground extends Component { | ||
constructor(public properties: Record<string, never>) { | ||
super(properties); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { System, Query, World, Entity } from "@serbanghita-gamedev/ecs"; | ||
import { QuadTree } from "@serbanghita-gamedev/quadtree"; | ||
import PhysicsBody from "./PhysicsBody"; | ||
import { randomInt } from "./helpers"; | ||
import IsCollisionTile from "./IsCollisionTile"; | ||
import IsMatrix from "./IsMatrix"; | ||
import { getTileFromCoordinates } from "@serbanghita-gamedev/matrix"; | ||
|
||
export default class MoveSystem extends System { | ||
public constructor( | ||
public world: World, | ||
public query: Query, | ||
) { | ||
super(world, query); | ||
} | ||
|
||
public update(now) { | ||
const map = this.world.getEntity("map"); | ||
if (!map) { | ||
throw new Error(`Map entity is not defined.`); | ||
} | ||
const matrixComponent = map.getComponent(IsMatrix); | ||
const matrix = matrixComponent.properties.matrix; | ||
|
||
this.query.execute().forEach((entity) => { | ||
const tile = entity.getComponent(IsCollisionTile); | ||
|
||
let futureX = tile.properties.x + randomInt(-1, 1); | ||
let futureY = tile.properties.y + randomInt(-1, 1); | ||
|
||
const futureTile = getTileFromCoordinates(futureX, futureY, matrixComponent.properties); | ||
if (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; | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Component } from "@serbanghita-gamedev/ecs"; | ||
import { Rectangle, Point } from "@serbanghita-gamedev/geometry"; | ||
|
||
export interface PhysicsBodyProps { | ||
width: number; | ||
height: number; | ||
x: number; | ||
y: number; | ||
rectangle: Rectangle; | ||
point: Point; | ||
} | ||
|
||
export default class PhysicsBody extends Component { | ||
constructor(public properties: PhysicsBodyProps) { | ||
super(properties); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/demos/tiled-rendering/src/PreRenderCollisionTilesSystem.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { System, Query, World, Entity } from "@serbanghita-gamedev/ecs"; | ||
import IsCollisionTile from "./IsCollisionTile"; | ||
import { dot } from "@serbanghita-gamedev/renderer"; | ||
|
||
export default class PreRenderCollisionTilesSystem extends System { | ||
public constructor( | ||
public world: World, | ||
public query: Query, | ||
public ctx: CanvasRenderingContext2D, | ||
) { | ||
super(world, query); | ||
} | ||
|
||
public update(now: number): void { | ||
this.query.execute().forEach((entity) => { | ||
const tile = entity.getComponent(IsCollisionTile); | ||
const point = tile.properties.point; | ||
dot(this.ctx, point.x, point.y, "rgb(255,0,0)", 2); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { System, Query, World } from "@serbanghita-gamedev/ecs"; | ||
import { QuadTree } from "@serbanghita-gamedev/quadtree"; | ||
import IsCollisionTile from "./IsCollisionTile"; | ||
|
||
export default class QuadTreeSystem extends System { | ||
public constructor( | ||
public world: World, | ||
public query: Query, | ||
public quadtree: QuadTree, | ||
) { | ||
super(world, query); | ||
} | ||
public update(now: number): void { | ||
this.preUpdate(); | ||
|
||
if (this.isPaused) { | ||
return; | ||
} | ||
|
||
this.quadtree.clear(); | ||
|
||
this.query.execute().forEach((entity) => { | ||
const tile = entity.getComponent(IsCollisionTile); | ||
this.quadtree.addPoint(tile.properties.point); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { System, Query, World, Entity } from "@serbanghita-gamedev/ecs"; | ||
import IsCollisionTile from "./IsCollisionTile"; | ||
import { dot, rectangle, text } from "@serbanghita-gamedev/renderer"; | ||
import { QuadTree } from "@serbanghita-gamedev/quadtree"; | ||
|
||
export default class RenderingSystem extends System { | ||
public constructor( | ||
public world: World, | ||
public query: Query, | ||
public ctx: CanvasRenderingContext2D, | ||
public quadtree: QuadTree, | ||
) { | ||
super(world, query); | ||
} | ||
|
||
private renderQuadTree(quadtree: QuadTree) { | ||
rectangle(this.ctx, quadtree.area.topLeftX, quadtree.area.topLeftY, quadtree.area.width, quadtree.area.height, "rgba(0,0,0,0.5)"); | ||
|
||
Object.values(quadtree.quadrants).forEach((subQuadtree) => { | ||
this.renderQuadTree(subQuadtree); | ||
}); | ||
} | ||
|
||
public update(now: number): void { | ||
this.ctx.clearRect(0, 0, 640, 480); | ||
|
||
this.query.execute().forEach((entity) => { | ||
const tile = entity.getComponent(IsCollisionTile); | ||
const point = tile.properties.point; | ||
dot(this.ctx, point.x, point.y, "rgb(0,255,0)", 6); | ||
text(this.ctx, `${tile.properties.tile}`, point.x, point.y, "10", "arial", "", "black"); | ||
}); | ||
|
||
this.renderQuadTree(this.quadtree); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function randomInt(min: number, max: number): number { | ||
return Math.floor(Math.random() * (max - min + 1)) + min; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.