Skip to content

Commit

Permalink
feat: audio entities
Browse files Browse the repository at this point in the history
  • Loading branch information
load1n9 committed Dec 8, 2021
1 parent 8908d3e commit 9745bfa
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
3 changes: 2 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export {
Arne16,
Atlas,
AtlasSprite,
Audio,
Button,
C64,
CGA,
Entity,
Expand All @@ -18,7 +20,6 @@ export {
Sprite,
Text,
TextureSprite,
Button
} from "./src/entities/mod.ts";
export { GravityForce, NormalForce } from "./src/physics/mod.ts";
export { Vector } from "./src/math/mod.ts";
Expand Down
30 changes: 22 additions & 8 deletions src/World.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Canvas } from "../deps.ts";
import type { KeyEvent, MouseDownEvent, WorldOptions } from "./types.ts";
import {
Animation,
AtlasSprite,
Audio,
Button,
Entity,
Group,
Image,
Expand All @@ -10,8 +13,6 @@ import {
Sprite,
Text,
TextureSprite,
Animation,
Button
} from "../mod.ts";
import { hexToRGBA } from "./utils/mod.ts";

Expand Down Expand Up @@ -75,9 +76,9 @@ export abstract class World extends Canvas {
start = performance.now();
frames = 0;
}

Deno.sleepSync(1 / this.FPS * 1000);
}
};
}
private _render(entity: Entity) {
if (entity instanceof Rectangle) {
Expand Down Expand Up @@ -156,8 +157,16 @@ export abstract class World extends Canvas {
},
);
} else if (entity instanceof Animation) {
this._render(new AtlasSprite(this, entity.x, entity.y, entity.atlas, entity.frames[entity.currentFrame]));
entity.currentFrame = (entity.currentFrame + 1) % entity.frames.length;
this._render(
new AtlasSprite(
this,
entity.x,
entity.y,
entity.atlas,
entity.frames[entity.currentFrame],
),
);
entity.currentFrame = (entity.currentFrame + 1) % entity.frames.length;
} else if (entity instanceof TextureSprite) {
for (let y = 0; y < entity.data.length; y++) {
const row = entity.data[y];
Expand All @@ -184,10 +193,15 @@ export abstract class World extends Canvas {
}
} else if (entity instanceof Group) {
for (const child of entity.children) {
this._render(child);
this._render(child);
}
} else if (entity instanceof Button) {
this._render(entity.child)
this._render(entity.child);
} else if (entity instanceof Audio) {
if (!entity.playing) {
this.playMusic(entity.src);
entity.playing = true;
}
}
}
public keyDown(_e: KeyEvent): void {
Expand Down
11 changes: 11 additions & 0 deletions src/entities/audio/Audio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Entity } from "../mod.ts";


export class Audio extends Entity {
public playing = false;
constructor( public src: string) {
super(0,0);

}

}
1 change: 1 addition & 0 deletions src/entities/audio/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Audio } from './Audio.ts';
3 changes: 2 additions & 1 deletion src/entities/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export { Entity } from "./Entity.ts";
export { Line, Point, Rectangle } from "./geometry/mod.ts";
export { Animation, Atlas, AtlasSprite, Image, Sprite } from "./sprites/mod.ts";
export { Text } from "./text/mod.ts";
export { Group, Button } from "./containers/mod.ts";
export { Audio } from "./audio/mod.ts";
export { Button, Group } from "./containers/mod.ts";
export * from "./textures/mod.ts";

0 comments on commit 9745bfa

Please sign in to comment.