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/Player.ts | |
parent | aa08a8943a9a2d4a0e51893eebe6900bca7a7251 (diff) | |
download | the-abstraction-engine-a8d07a790395e14fe7aedd3ba638db466f9c0842.tar.gz the-abstraction-engine-a8d07a790395e14fe7aedd3ba638db466f9c0842.zip |
boundingbox + draw player
Diffstat (limited to 'src/engine/entities/Player.ts')
-rw-r--r-- | src/engine/entities/Player.ts | 58 |
1 files changed, 58 insertions, 0 deletions
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 + } +} |