diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-03-01 18:56:58 -0700 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-03-01 18:56:58 -0700 |
commit | a8d07a790395e14fe7aedd3ba638db466f9c0842 (patch) | |
tree | 644f60a6bca79ceb55f24fcab7bdb3dee52c2d6e /src/engine/entities | |
parent | aa08a8943a9a2d4a0e51893eebe6900bca7a7251 (diff) | |
download | the-abstraction-engine-a8d07a790395e14fe7aedd3ba638db466f9c0842.tar.gz the-abstraction-engine-a8d07a790395e14fe7aedd3ba638db466f9c0842.zip |
boundingbox + draw player
Diffstat (limited to 'src/engine/entities')
-rw-r--r-- | src/engine/entities/Entity.ts | 6 | ||||
-rw-r--r-- | src/engine/entities/EntityNames.ts | 2 | ||||
-rw-r--r-- | src/engine/entities/Player.ts | 58 | ||||
-rw-r--r-- | src/engine/entities/index.ts | 1 |
4 files changed, 62 insertions, 5 deletions
diff --git a/src/engine/entities/Entity.ts b/src/engine/entities/Entity.ts index 18ee5d0..2cc2ac3 100644 --- a/src/engine/entities/Entity.ts +++ b/src/engine/entities/Entity.ts @@ -1,13 +1,13 @@ import { type Component } from "../components"; -const randomId = () => (Math.random() * 1_000_000_000).toString(); - export abstract class Entity { + static Id = 0; + public id: string; public components: Map<string, Component>; public name: string; - constructor(name: string, id: string = randomId()) { + constructor(name: string, id: string = (Entity.Id++).toString()) { this.name = name; this.id = id; this.components = new Map(); diff --git a/src/engine/entities/EntityNames.ts b/src/engine/entities/EntityNames.ts index 59010fc..e2f642a 100644 --- a/src/engine/entities/EntityNames.ts +++ b/src/engine/entities/EntityNames.ts @@ -1,5 +1,3 @@ export namespace EntityNames { export const Player = "Player"; - export const Wall = "Wall"; - export const Ball = "Ball"; } diff --git a/src/engine/entities/Player.ts b/src/engine/entities/Player.ts new file mode 100644 index 0000000..f25730c --- /dev/null +++ b/src/engine/entities/Player.ts @@ -0,0 +1,58 @@ +import { Entity, EntityNames } from "."; +import { IMAGES, SPRITE_SPECS, Sprites, type SpriteSpec } from "../config"; +import { + FacingDirection, + Sprite, + GridPosition, + BoundingBox, +} from "../components"; +import { Direction } from "../interfaces/"; + +export class Player extends Entity { + private static spriteSpec: SpriteSpec = SPRITE_SPECS.get( + Sprites.PLAYER, + ) as SpriteSpec; + + constructor() { + super(EntityNames.Player); + + this.addComponent( + new BoundingBox( + { + x: 0, + y: 0, + }, + { width: Player.spriteSpec.width, height: Player.spriteSpec.height }, + 0, + ), + ); + + this.addComponent(new GridPosition(0, 0)); + this.addFacingDirectionComponents(); + } + + private addFacingDirectionComponents() { + const facingDirectionComponent = new FacingDirection(); + [ + Direction.NONE, + Direction.LEFT, + Direction.RIGHT, + Direction.UP, + Direction.DOWN, + ].forEach((direction) => { + const sprite = new Sprite( + IMAGES.get(Player.spriteSpec.states!.get(direction)!.sheet!)!, + { x: 0, y: 0 }, + { width: Player.spriteSpec.width, height: Player.spriteSpec.height }, + Player.spriteSpec.msPerFrame, + Player.spriteSpec.frames, + ); + facingDirectionComponent.directionSprites.set(direction, sprite); + }); + + this.addComponent(facingDirectionComponent); + this.addComponent( + facingDirectionComponent.directionSprites.get(Direction.NONE)!, + ); // face no direction by default + } +} diff --git a/src/engine/entities/index.ts b/src/engine/entities/index.ts index ee26a63..13dd57a 100644 --- a/src/engine/entities/index.ts +++ b/src/engine/entities/index.ts @@ -1,2 +1,3 @@ export * from "./Entity"; export * from "./EntityNames"; +export * from "./Player"; |