-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rendering): implement rendering system
add rendering system implementation + ShapeComponent draw logic
- Loading branch information
1 parent
a0034c8
commit 5fb7cef
Showing
9 changed files
with
81 additions
and
14 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 |
---|---|---|
@@ -1,9 +1,26 @@ | ||
import { PrimitiveType } from "../../renderer/RenderCommand"; | ||
import { Renderer } from "../../renderer"; | ||
import { DrawPrimitiveCommand, PrimitiveType } from "../../renderer/RenderCommand"; | ||
import { BaseComponent } from "./BaseComponent"; | ||
import { TransformComponent } from "./TransformComponent"; | ||
|
||
/** | ||
* Represents a primitive Shape like rectangle, circle, etc | ||
*/ | ||
export class ShapeComponent extends BaseComponent { | ||
public shapeType: PrimitiveType = PrimitiveType.Rectangle; | ||
|
||
private get transform(): TransformComponent { | ||
return new TransformComponent(); | ||
} | ||
|
||
public draw(renderer: Renderer): void { | ||
const position = this.transform.position; | ||
const size = this.transform.size; | ||
|
||
renderer.pushRenderCommand(new DrawPrimitiveCommand( | ||
PrimitiveType.Rectangle, | ||
[position.x, position.y], | ||
[size.width, size.height] | ||
)); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { BaseComponent } from "../components"; | ||
import { BaseComponent, IComponent } from "../components"; | ||
import { IEntity } from "./IEntity"; | ||
|
||
export class BaseEntity implements IEntity { | ||
private components: Map<string, BaseComponent> = new Map(); | ||
private components: Map<string, IComponent> = new Map(); | ||
|
||
public addComponent<Component extends BaseComponent>(key: string, component: Component): void { | ||
public addComponent(key: string, component: IComponent): void { | ||
this.components.set(key, component); | ||
} | ||
|
||
public getComponent<Component extends BaseComponent>(key: string): Component | undefined { | ||
public getComponent<Component extends IComponent>(key: string): Component | undefined { | ||
return this.components.get(key) as Component; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,14 +1,19 @@ | ||
import { Renderer } from "../../renderer"; | ||
import { ShapeComponent } from "../components"; | ||
import { ISystem } from "./ISystem"; | ||
|
||
export class RenderSystem implements ISystem { | ||
public readonly components: ShapeComponent[] = [] | ||
public readonly components: ShapeComponent[] = []; | ||
|
||
constructor( | ||
private readonly renderer: Renderer | ||
) {} | ||
|
||
public registerComponent(component: ShapeComponent): void { | ||
this.components.push(component); | ||
} | ||
|
||
public update(): void { | ||
throw new Error('NotImplemented'); | ||
this.components.forEach(component => component.draw(this.renderer)); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import { BaseComponent } from "../../../../src"; | ||
|
||
describe('ecs/components/BaseComponent', () => { | ||
it('Should construct', () => { | ||
new BaseComponent(); | ||
describe('.getContainer()', () => { | ||
it.todo('Should get the container entity of the component'); | ||
it.todo('Should return null if the component is not attached to an entity'); | ||
}) | ||
|
||
describe('.setConatiner', () => { | ||
it.todo('Should set the container entity to the component'); | ||
}) | ||
}) |
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 { CanvasDevice, DrawPrimitiveCommand, PrimitiveType, Renderer, ShapeComponent } from "../../../../src"; | ||
|
||
describe('ecs/components/ShapeComponent', () => { | ||
describe('.draw()', () => { | ||
const shapeComponent = new ShapeComponent(); | ||
const renderer = new Renderer(new CanvasDevice()); | ||
|
||
it('Should push the right draw command to the renderer', () => { | ||
shapeComponent.draw(renderer); | ||
|
||
expect(renderer.commandBuffer).toEqual([new DrawPrimitiveCommand( | ||
PrimitiveType.Rectangle, | ||
[0, 0], | ||
[0, 0], | ||
)]); | ||
}) | ||
}) | ||
}) |
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