-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up ECS and render a player on screen.
- Loading branch information
Showing
7 changed files
with
137 additions
and
6 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
"vite": "^4.3.9" | ||
}, | ||
"dependencies": { | ||
"malwoden": "^0.5.0" | ||
"malwoden": "^0.5.0", | ||
"ecsy": "^0.4.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,35 @@ | ||
import { Component, ComponentSchema, Types } from 'ecsy'; | ||
import { Terminal } from 'malwoden'; | ||
|
||
/** | ||
* Position component | ||
* @param x x-coordinate | ||
* @param y y-coordinate | ||
*/ | ||
export class Position extends Component<Position> { | ||
x = 0; | ||
y = 0; | ||
|
||
static schema: ComponentSchema = { | ||
x: { type: Types.Number }, | ||
y: { type: Types.Number }, | ||
}; | ||
} | ||
|
||
/** | ||
* Renderable component | ||
* @param glyph `Terminal.Glyph` for rendering on the canvas | ||
*/ | ||
export class Renderable extends Component<Renderable> { | ||
// glyph can never be undefined | ||
glyph!: Terminal.Glyph; | ||
|
||
static schema: ComponentSchema = { | ||
glyph: { type: Types.Ref }, | ||
}; | ||
} | ||
|
||
/** | ||
* Player component | ||
*/ | ||
export class Player extends Component<Player> {} |
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 @@ | ||
export { RenderSystem } from './render'; |
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,44 @@ | ||
import { World, System, SystemQueries } from 'ecsy'; | ||
|
||
import { Game } from '../main'; | ||
import * as Components from '../components'; | ||
|
||
/** | ||
* Rendering system | ||
*/ | ||
export class RenderSystem extends System { | ||
game: Game; | ||
|
||
constructor(world: World, game: Game) { | ||
super(world, game); | ||
this.game = game; | ||
} | ||
|
||
static queries: SystemQueries = { | ||
renderables: { | ||
components: [Components.Renderable, Components.Position], | ||
}, | ||
}; | ||
|
||
/** | ||
* Render any entities that have Position and Renderable components. | ||
*/ | ||
execute() { | ||
// run the query | ||
const { results } = this.queries.renderables; | ||
|
||
// clear before the render pass | ||
this.game.terminal.clear(); | ||
|
||
// draw each entity | ||
for (const entity of results) { | ||
const position = entity.getComponent(Components.Position)!; | ||
const renderable = entity.getComponent(Components.Renderable)!; | ||
|
||
this.game.terminal.drawGlyph(position, renderable.glyph) | ||
} | ||
|
||
// render to canvas | ||
this.game.terminal.render(); | ||
} | ||
} |