diff --git a/mod.ts b/mod.ts index e4084a2..7a6d577 100644 --- a/mod.ts +++ b/mod.ts @@ -16,6 +16,7 @@ export { Sprite, Text, TextureSprite, + Group } from "./src/entities/mod.ts"; export { GravityForce, diff --git a/src/World.ts b/src/World.ts index 110c5a6..dbc2b27 100644 --- a/src/World.ts +++ b/src/World.ts @@ -3,6 +3,7 @@ import type { KeyEvent, MouseDownEvent, WorldOptions } from "./types.ts"; import { AtlasSprite, Entity, + Group, Image, Line, Rectangle, @@ -54,111 +55,119 @@ export abstract class World extends Canvas { this.setDrawColor(0, 0, 0, 255); this.clear(); for (const entity of this.entities) { - if (entity instanceof Rectangle) { - this.setDrawColor( - entity.fill[0], - entity.fill[1], - entity.fill[2], - entity.fill[3], - ); - this.fillRect(entity.x, entity.y, entity.width, entity.height); - } else if (entity instanceof Line) { - this.drawLine(entity.p1, entity.p2); - } else if (entity instanceof Image) { - this.copy( - entity.texture, - { - x: 0, - y: 0, - width: entity.width, - height: entity.height, - }, - { - x: entity.x, - y: entity.y, - width: entity.width, - height: entity.height, - }, - ); - } else if (entity instanceof Sprite) { - this.copy( - entity.texture, - { - x: Math.round(entity.frame.x), - y: Math.round(entity.frame.y), - width: Math.round(entity.frame.width), - height: Math.round(entity.frame.height), - }, - { - x: Math.round(entity.x), - y: Math.round(entity.y), - width: Math.round(entity.frame.width), - height: Math.round(entity.frame.height), - }, - ); - } else if (entity instanceof Text) { - entity.render(this); - this.copy( - entity.texture, - { - x: 0, - y: 0, - width: entity.width, - height: entity.height, - }, - { - x: entity.x, - y: entity.y, - width: entity.width, - height: entity.height, - }, - ); - } else if (entity instanceof AtlasSprite) { - this.copy( - entity.texture, - { - x: Math.round(entity.frame.x), - y: Math.round(entity.frame.y), - width: Math.round(entity.frame.width), - height: Math.round(entity.frame.height), - }, - { - x: Math.round(entity.x), - y: Math.round(entity.y), - width: Math.round(entity.frame.width), - height: Math.round(entity.frame.height), - }, - ); - } else if (entity instanceof TextureSprite) { - for (let y = 0; y < entity.data.length; y++) { - const row = entity.data[y]; - for (let x = 0; x < row.length; x++) { - const d: string = row[x]; - if (d !== "." && d !== " ") { - const pixelColor = hexToRGBA( - entity.palette[parseInt("0x" + d.toUpperCase())], - ); - this.setDrawColor( - pixelColor[0], - pixelColor[1], - pixelColor[2], - pixelColor[3], - ); - this.fillRect( - (x * entity.pixelWidth) + entity.x, - (y * entity.pixelHeight) + entity.y, - entity.pixelWidth, - entity.pixelHeight, - ); - } - } - } - } + this._render(entity); } this.draw(); this.present(); Deno.sleepSync(10); } + + private _render(entity: Entity) { + if (entity instanceof Rectangle) { + this.setDrawColor( + entity.fill[0], + entity.fill[1], + entity.fill[2], + entity.fill[3], + ); + this.fillRect(entity.x, entity.y, entity.width, entity.height); + } else if (entity instanceof Line) { + this.drawLine(entity.p1, entity.p2); + } else if (entity instanceof Image) { + this.copy( + entity.texture, + { + x: 0, + y: 0, + width: entity.width, + height: entity.height, + }, + { + x: entity.x, + y: entity.y, + width: entity.width, + height: entity.height, + }, + ); + } else if (entity instanceof Sprite) { + this.copy( + entity.texture, + { + x: Math.round(entity.frame.x), + y: Math.round(entity.frame.y), + width: Math.round(entity.frame.width), + height: Math.round(entity.frame.height), + }, + { + x: Math.round(entity.x), + y: Math.round(entity.y), + width: Math.round(entity.frame.width), + height: Math.round(entity.frame.height), + }, + ); + } else if (entity instanceof Text) { + entity.render(this); + this.copy( + entity.texture, + { + x: 0, + y: 0, + width: entity.width, + height: entity.height, + }, + { + x: entity.x, + y: entity.y, + width: entity.width, + height: entity.height, + }, + ); + } else if (entity instanceof AtlasSprite) { + this.copy( + entity.texture, + { + x: Math.round(entity.frame.x), + y: Math.round(entity.frame.y), + width: Math.round(entity.frame.width), + height: Math.round(entity.frame.height), + }, + { + x: Math.round(entity.x), + y: Math.round(entity.y), + width: Math.round(entity.frame.width), + height: Math.round(entity.frame.height), + }, + ); + } else if (entity instanceof TextureSprite) { + for (let y = 0; y < entity.data.length; y++) { + const row = entity.data[y]; + for (let x = 0; x < row.length; x++) { + const d: string = row[x]; + if (d !== "." && d !== " ") { + const pixelColor = hexToRGBA( + entity.palette[parseInt("0x" + d.toUpperCase())], + ); + this.setDrawColor( + pixelColor[0], + pixelColor[1], + pixelColor[2], + pixelColor[3], + ); + this.fillRect( + (x * entity.pixelWidth) + entity.x, + (y * entity.pixelHeight) + entity.y, + entity.pixelWidth, + entity.pixelHeight, + ); + } + } + } + } else if (entity instanceof Group) { + for (const child of entity.children) { + this._render(child); + } + } + } public keyDown(_e: KeyEvent): void { return; } diff --git a/src/entities/containers/Group.ts b/src/entities/containers/Group.ts new file mode 100644 index 0000000..f2546cf --- /dev/null +++ b/src/entities/containers/Group.ts @@ -0,0 +1,23 @@ +import { Entity, World } from '../../../mod.ts'; + +export class Group extends Entity { + public children: Array = []; + public world: World; + + constructor(world: World, x: number, y: number) { + super(x,y); + this.world = world; + } + + public addChild(child: Entity) { + this.children.push(child); + } + + public killChild(child: Entity) { + this.children.splice(this.children.indexOf(child), 1); + } + + public killAllChildren() { + this.children = []; + } +} \ No newline at end of file diff --git a/src/entities/containers/mod.ts b/src/entities/containers/mod.ts new file mode 100644 index 0000000..60e0576 --- /dev/null +++ b/src/entities/containers/mod.ts @@ -0,0 +1 @@ +export { Group } from './Group.ts'; \ No newline at end of file diff --git a/src/entities/mod.ts b/src/entities/mod.ts index c0d240f..ac7817b 100644 --- a/src/entities/mod.ts +++ b/src/entities/mod.ts @@ -2,4 +2,5 @@ export { Entity } from './Entity.ts'; export { Rectangle, Point, Line } from './geometry/mod.ts'; export { Sprite, Image, Atlas, AtlasSprite } from './sprites/mod.ts'; export { Text } from "./text/mod.ts"; +export { Group } from './containers/mod.ts'; export * from "./textures/mod.ts"; \ No newline at end of file