-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RuggeroVisintin/issue20 #21
Changes from all commits
a0034c8
5fb7cef
e6440b0
ba3acfe
f757ccb
325ecb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<html> | ||
|
||
<body> | ||
<canvas id="canvas" style="width: 100%; height: 100%"></canvas> | ||
<script type="text/javascript" src="../../dist/spark-engine-web.js"></script> | ||
<script> | ||
const context = document.getElementById('canvas').getContext('2d') | ||
const device = new SparkEngine.CanvasDevice(); | ||
|
||
const renderer = new SparkEngine.Renderer(device); | ||
const renderSystem = new SparkEngine.RenderSystem(renderer); | ||
|
||
const firstRect = new SparkEngine.ShapeComponent(); | ||
firstRect.transform.size = { width: 50, height: 25 }; | ||
renderSystem.registerComponent(firstRect); | ||
|
||
let pos = 0; | ||
|
||
const drawFrame = () => { | ||
pos++; | ||
pos = pos % 60; | ||
|
||
firstRect.transform.position = { x: pos, y: pos }; | ||
|
||
|
||
renderSystem.update(); | ||
renderer.endFrame(context); | ||
} | ||
|
||
setInterval(drawFrame, 33); | ||
</script> | ||
</body> | ||
|
||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,28 @@ | ||
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 defaultTransform = new TransformComponent(); | ||
|
||
public get transform(): TransformComponent { | ||
return this.defaultTransform; | ||
} | ||
|
||
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] | ||
)); | ||
} | ||
} |
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; | ||
} | ||
} |
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)); | ||
} | ||
} |
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'); | ||
}) | ||
}) |
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', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add more test cases with different positions and sizes |
||
shapeComponent.draw(renderer); | ||
|
||
expect(renderer.commandBuffer).toEqual([new DrawPrimitiveCommand( | ||
PrimitiveType.Rectangle, | ||
[0, 0], | ||
[0, 0], | ||
)]); | ||
}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { RenderSystem, ShapeComponent } from "../../../../src" | ||
import { CanvasDevice, RenderSystem, Renderer, ShapeComponent } from "../../../../src"; | ||
|
||
describe('systems/RenderSystem', () => { | ||
describe('.registerComponent()', () => { | ||
const renderSystem = new RenderSystem(); | ||
const renderSystem = new RenderSystem(new Renderer(new CanvasDevice())); | ||
const myTestShape = new ShapeComponent(); | ||
|
||
it('Should register the component into the RenderSystem components list', () => { | ||
|
@@ -11,4 +11,23 @@ describe('systems/RenderSystem', () => { | |
expect(renderSystem.components).toContain(myTestShape); | ||
}) | ||
}) | ||
|
||
describe('.update()', () => { | ||
const renderSystem = new RenderSystem(new Renderer(new CanvasDevice())); | ||
|
||
it('Should draw renderable object', () => { | ||
const drawSpy = jest.spyOn(ShapeComponent.prototype, 'draw'); | ||
|
||
renderSystem.registerComponent(new ShapeComponent()); | ||
renderSystem.registerComponent(new ShapeComponent()); | ||
|
||
renderSystem.update(); | ||
|
||
expect(drawSpy).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it.todo('Should render objects based on their depthIndex in reverse order (0 rendered last)') | ||
|
||
it.todo('Should render object based on their transparency (transparent last)') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add another test case like it('Should render transparency objects as last but ordered by depth index) |
||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pass this.shapeType instead