Skip to content

Commit

Permalink
Merge pull request #10 from RuggeroVisintin/RuggeroVisintin/issue2
Browse files Browse the repository at this point in the history
RuggeroVisintin/issue2
  • Loading branch information
RuggeroVisintin authored Dec 3, 2023
2 parents 10760a7 + fbca6d1 commit 2262f50
Show file tree
Hide file tree
Showing 21 changed files with 162 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../core';
Empty file added src/ecs/components/.gitkeep
Empty file.
8 changes: 8 additions & 0 deletions src/ecs/components/BaseComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IEntity } from "../entities";
import { IComponent } from "./IComponent";

export class BaseComponent implements IComponent {
public getContainer(): IEntity {
throw new Error("Method not implemented.");
}
}
5 changes: 5 additions & 0 deletions src/ecs/components/IComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IEntity } from "../entities";

export interface IComponent {
getContainer(): IEntity;
}
12 changes: 12 additions & 0 deletions src/ecs/components/ShapeComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { BaseComponent } from "./BaseComponent";

export enum ShapeType {
Rectangle = 0
}

/**
* Represents a primitive Shape like rectangle, circle, etc
*/
export class ShapeComponent extends BaseComponent {
public shapeType: ShapeType = ShapeType.Rectangle;
}
16 changes: 16 additions & 0 deletions src/ecs/components/TransformComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { BaseComponent } from "./BaseComponent";

interface Position2D {
x: number;
y: number;
}

interface Size2D {
width: number;
height: number;
}

export class TransformComponent extends BaseComponent {
public position: Position2D = { x: 0, y: 0 };
public size: Size2D = { width: 0, height: 0 };
}
4 changes: 4 additions & 0 deletions src/ecs/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './IComponent';
export * from './BaseComponent';
export * from './TransformComponent';
export * from './ShapeComponent';
Empty file added src/ecs/entities/.gitkeep
Empty file.
14 changes: 14 additions & 0 deletions src/ecs/entities/BaseEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { BaseComponent } from "../components";
import { IEntity } from "./IEntity";

export class BaseEntity implements IEntity {
private components: Map<string, BaseComponent> = new Map();

public addComponent<Component extends BaseComponent>(key: string, component: Component): void {
this.components.set(key, component);
}

public getComponent<Component extends BaseComponent>(key: string): Component | undefined {
return this.components.get(key) as Component;
}
}
6 changes: 6 additions & 0 deletions src/ecs/entities/IEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IComponent } from "../components";

export interface IEntity {
addComponent(key: string, component: IComponent): void;
getComponent(key: string): IComponent | undefined;
}
2 changes: 2 additions & 0 deletions src/ecs/entities/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './BaseEntity';
export * from './IEntity';
3 changes: 3 additions & 0 deletions src/ecs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './entities';
export * from './components';
export * from './systems';
Empty file added src/ecs/systems/.gitkeep
Empty file.
6 changes: 6 additions & 0 deletions src/ecs/systems/ISystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { BaseComponent } from "../components";

export interface ISystem {
registerComponent(component: BaseComponent): void;
update(): void;
}
14 changes: 14 additions & 0 deletions src/ecs/systems/RenderSystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ShapeComponent } from "../components";
import { ISystem } from "./ISystem";

export class RenderSystem implements ISystem {
public readonly components: ShapeComponent[] = []

public registerComponent(component: ShapeComponent): void {
this.components.push(component);
}

public update(): void {
throw new Error('NotImplemented');
}
}
2 changes: 2 additions & 0 deletions src/ecs/systems/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './ISystem';
export * from './RenderSystem';
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
console.log('RUN')
export * from './core';
export * from './ecs';
7 changes: 7 additions & 0 deletions test/ecs/components/BaseComponent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BaseComponent } from "../../../src";

describe('ecs/components/BaseComponent', () => {
it('Should construct', () => {
new BaseComponent();
})
})
23 changes: 23 additions & 0 deletions test/ecs/components/TransformComponent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { TransformComponent } from "../../../src"

describe('ecs/components/TransformComponent', () => {
describe('constructor', () => {
it('Should default position to 0,0 if not defined', () => {
const transformComponent = new TransformComponent();

expect(transformComponent.position).toEqual({
x: 0,
y: 0
})
})

it('Should default size to 0,0 if not defiend', () => {
const transformComponent = new TransformComponent();

expect(transformComponent.size).toEqual({
width: 0,
height: 0
})
})
})
})
23 changes: 23 additions & 0 deletions test/ecs/entities/BaseEntity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { BaseComponent, BaseEntity } from "../../../src";

describe('ecs/entities/BaseEntity', () => {
const baseEntity = new BaseEntity();

describe('.addComponent()', () => {
it('Should add component to entity', () => {
const testComponent = new BaseComponent();

baseEntity.addComponent<BaseComponent>('testComponent', testComponent);
expect(baseEntity.getComponent<BaseComponent>('testComponent')).toEqual(testComponent);
});
})

describe('.getComponent()', () => {
it('Should retrieve a specific component given a specific key', () => {
const testComponent = new BaseComponent();
baseEntity.addComponent<BaseComponent>('testComponent', testComponent);

expect(baseEntity.getComponent<BaseComponent>('testComponent')).toEqual(testComponent);
})
})
})
14 changes: 14 additions & 0 deletions test/ecs/systems/RenderSystem.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { RenderSystem, ShapeComponent } from "../../../src"

describe('systems/RenderSystem', () => {
describe('.registerComponent()', () => {
const renderSystem = new RenderSystem();
const myTestShape = new ShapeComponent();

it('Should register the component into the RenderSystem components list', () => {
renderSystem.registerComponent(myTestShape);

expect(renderSystem.components).toContain(myTestShape);
})
})
})

0 comments on commit 2262f50

Please sign in to comment.