Skip to content

Commit

Permalink
feat: fps built in & animation entity
Browse files Browse the repository at this point in the history
  • Loading branch information
load1n9 committed Dec 7, 2021
1 parent 93d4fbb commit c836287
Show file tree
Hide file tree
Showing 11 changed files with 1,922 additions and 40 deletions.
1,826 changes: 1,826 additions & 0 deletions assets/klutzy.json

Large diffs are not rendered by default.

Binary file added assets/klutzy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions examples/atlas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { World, Animation, Atlas } from '../mod.ts';


class Game extends World {
public test = new Atlas('assets/klutzy.json');
public setup() {
const test = new Animation(this, 45, 45, this.test, [
'sprite1',
'sprite2',
'sprite3',
'sprite4',
'sprite5',
'sprite6',
'sprite7',
'sprite8'
]);
this.addChild(test);
}
public draw() {

}
}

const test = new Game({
title: "test",
width: 800,
height: 600,
centered: true,
fullscreen: false,
hidden: false,
resizable: true,
minimized: false,
maximized: false,
flags: null,
});

await test.start();
1 change: 1 addition & 0 deletions examples/sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { World, Sprite } from '../mod.ts';


class Game extends World {
public FPS = 5;
public test = new Sprite(this, "assets/sample.png", 200, 100, 307, 127,{rows:1,cols:4});

public setup() {
Expand Down
30 changes: 14 additions & 16 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
export { World } from "./src/World.ts";
export {
Animation,
Arne16,
Atlas,
AtlasSprite,
C64,
CGA,
JMP,
MSX,
Entity,
Group,
Image,
JMP,
Line,
MSX,
PICO8,
Point,
Rectangle,
Sprite,
Text,
TextureSprite,
Group
} from "./src/entities/mod.ts";
export {
GravityForce,
NormalForce
} from "./src/physics/mod.ts"
export { GravityForce, NormalForce } from "./src/physics/mod.ts";
export { Vector } from "./src/math/mod.ts";
export { Keys } from "./src/utils/Keycodes.ts";
export type {
KeyEvent,
MouseDownEvent,
PixelTexture,
spriteConfig,
Frame,
RGBA,
WorldOptions,
WorldPhysicsOptions
export type {
Frame,
KeyEvent,
MouseDownEvent,
PixelTexture,
RGBA,
spriteConfig,
WorldOptions,
WorldPhysicsOptions,
} from "./src/types.ts";
19 changes: 19 additions & 0 deletions src/World.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import {
Sprite,
Text,
TextureSprite,
Animation,
} from "../mod.ts";
import { hexToRGBA } from "./utils/mod.ts";

export abstract class World extends Canvas {
public FPS = 100;
public entities: Array<Entity> = [];
public params: WorldOptions;
constructor(params: WorldOptions) {
Expand Down Expand Up @@ -52,6 +54,7 @@ export abstract class World extends Canvas {
this.entities.splice(index, 1);
}
private _draw() {
this._fps()();
this.setDrawColor(0, 0, 0, 255);
this.clear();
for (const entity of this.entities) {
Expand All @@ -62,6 +65,19 @@ export abstract class World extends Canvas {
Deno.sleepSync(10);
}

public _fps() {
let start = performance.now();
let frames = 0;
return () => {
frames++;
if ((performance.now() - start) >= 1000) {
start = performance.now();
frames = 0;
}

Deno.sleepSync(1 / this.FPS * 1000);
}
}
private _render(entity: Entity) {
if (entity instanceof Rectangle) {
this.setDrawColor(
Expand Down Expand Up @@ -138,6 +154,9 @@ export abstract class World extends Canvas {
height: Math.round(entity.frame.height),
},
);
} 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;
} else if (entity instanceof TextureSprite) {
for (let y = 0; y < entity.data.length; y++) {
const row = entity.data[y];
Expand Down
10 changes: 5 additions & 5 deletions src/entities/mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { Entity } from './Entity.ts';
export { Rectangle, Point, Line } from './geometry/mod.ts';
export { Sprite, Image, Atlas, AtlasSprite } from './sprites/mod.ts';
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 } from './containers/mod.ts';
export * from "./textures/mod.ts";
export { Group } from "./containers/mod.ts";
export * from "./textures/mod.ts";
15 changes: 15 additions & 0 deletions src/entities/sprites/Animation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Entity, Atlas } from "../mod.ts";
import { World } from '../../../mod.ts';

export class Animation extends Entity {

public atlas: Atlas;
public frames: Array<string>;
public currentFrame = 0;

constructor(_world: World, x: number, y: number, atlas: Atlas, frames: Array<string>) {
super(x, y);
this.atlas = atlas;
this.frames = frames;
}
}
9 changes: 5 additions & 4 deletions src/entities/sprites/mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { Image } from './Image.ts';
export { Sprite } from './Sprite.ts';
export { Atlas } from './Atlas.ts';
export { AtlasSprite } from './AtlasSprite.ts';
export { Image } from "./Image.ts";
export { Sprite } from "./Sprite.ts";
export { Atlas } from "./Atlas.ts";
export { AtlasSprite } from "./AtlasSprite.ts";
export { Animation } from "./Animation.ts";
14 changes: 0 additions & 14 deletions src/utils/FPS.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/utils/mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { FPS } from "./FPS.ts";
export { Keys } from "./Keycodes.ts";
export { atlas, PhaserAtlas } from "./atlas/mod.ts";
export { hexToRGBA } from "./HexToRGBA.ts";
Expand Down

0 comments on commit c836287

Please sign in to comment.