Skip to content

Commit

Permalink
feat: sprite groups
Browse files Browse the repository at this point in the history
  • Loading branch information
load1n9 committed Dec 6, 2021
1 parent 8e2b5d5 commit 93d4fbb
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 100 deletions.
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export {
Sprite,
Text,
TextureSprite,
Group
} from "./src/entities/mod.ts";
export {
GravityForce,
Expand Down
209 changes: 109 additions & 100 deletions src/World.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { KeyEvent, MouseDownEvent, WorldOptions } from "./types.ts";
import {
AtlasSprite,
Entity,
Group,
Image,
Line,
Rectangle,
Expand Down Expand Up @@ -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;
}
Expand Down
23 changes: 23 additions & 0 deletions src/entities/containers/Group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Entity, World } from '../../../mod.ts';

export class Group extends Entity {
public children: Array<Entity> = [];
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 = [];
}
}
1 change: 1 addition & 0 deletions src/entities/containers/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Group } from './Group.ts';
1 change: 1 addition & 0 deletions src/entities/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

0 comments on commit 93d4fbb

Please sign in to comment.