Skip to content

Commit

Permalink
add remove object feature
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshurajora committed Dec 4, 2023
1 parent ca00767 commit 15531ee
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 25 deletions.
3 changes: 3 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Local Examples

These are some local example and do not meant to be hosted in production (but they may be).
32 changes: 18 additions & 14 deletions examples/delta-time/script.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Engine, Point } from '../../lib';
import { Engine } from '../../lib';

const engine = new Engine(
document.getElementById('canvas') as HTMLCanvasElement,
Expand All @@ -10,7 +10,7 @@ const engine = new Engine(
},
);

const { renderer, input } = engine;
const { renderer } = engine;

const random = () => {
return Math.random();
Expand All @@ -22,17 +22,21 @@ document.getElementById('checkbox')?.addEventListener('change', (e) => {
engine.options.engineOptions!.clearEachFrame = (e.target as any).checked;
});

engine.update = (engine, deltaTime) => {
const point = renderer.point({
show: true,
x: Math.random() * engine.width,
y: Math.random() * engine.height,
color: `rgb(${random() * 255}, ${random() * 255}, ${random() * 255})`,
radius: random() * 10,
});
var lastTime = Date.now();

point.update = (_) => {
point.options.x += speed * deltaTime;
point.options.x += speed * deltaTime;
};
engine.update = (engine, deltaTime) => {
var currentTime = Date.now();
if (currentTime - lastTime > 50) {
const point = renderer.point({
show: true,
x: Math.random() * engine.width,
y: Math.random() * engine.height,
color: `rgb(${random() * 255}, ${random() * 255}, ${random() * 255})`,
radius: random() * 10,
});
setTimeout(() => {
renderer.removeObject(point);
}, 200);
lastTime = Date.now();
}
};
1 change: 1 addition & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ <h2>We got examples for you</h2>
<li><a href="/mouse-inputs/">Mouse Inputs</a></li>
<li><a href="/keyboard-inputs/">Keyboard Inputs</a></li>
<li><a href="/update/">Update</a></li>
<li><a href="/delta-time/">Delta Time</a></li>
</ul>
<script type="module" src="/src/main.ts"></script>
</body>
Expand Down
7 changes: 4 additions & 3 deletions lib/render/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ export class Engine {

let lowLimit = 1;
let lastRenderTime = Date.now();
let deltaTime = 1;
const render = () => {
let deltaTime = Date.now() - lastRenderTime;
if (deltaTime < lowLimit) deltaTime = lowLimit;

this.update(this, deltaTime);
if (options.engineOptions!.clearEachFrame) {
this.context?.clearRect(0, 0, this.width, this.height);
}
this.renderer.render(deltaTime);
this.resetEvent();
deltaTime = Date.now() - lastRenderTime;
lastRenderTime = Date.now();
requestAnimationFrame(render);
};
Expand All @@ -104,7 +104,8 @@ export class Engine {
_engine: Engine,
_deltaTime: number,
) => {};
start: (engine: Engine) => void = (_engine: Engine) => {};

start: (_engine: Engine) => void = (_engine: Engine) => {};

// reset single frame events
resetEvent() {
Expand Down
20 changes: 15 additions & 5 deletions lib/render/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { forEach } from 'lodash';
import _, { forEach } from 'lodash';
import { DefaultPointOptions } from '../constants';
import { BaseObjectInterface, BaseRenderer, PointOptions } from '../types';
import { Point } from './objects';
Expand All @@ -19,7 +19,9 @@ export class Renderer extends BaseRenderer {

render(deltaTime: number) {
forEach(this.objects, (object) => {
object.checkDrawConditionAndDraw(deltaTime);
if (object) {
object.checkDrawConditionAndDraw(deltaTime);
}
});
}

Expand All @@ -29,15 +31,23 @@ export class Renderer extends BaseRenderer {
* @param {BaseObjectInterface<T>} object
* @returns
*/
addObject<T>(object: T): T {
this.objects.push(object as BaseObjectInterface<T>);
addObject<T extends BaseObjectInterface>(object: T): T {
this.objects[object.id] = object as BaseObjectInterface<T>;
(object as BaseObjectInterface<T>).renderer = this;
return object;
}

removeObject<T extends BaseObjectInterface>(object: T) {
delete this.objects[object.id];
}

getObject(id: string) {
return this.objects[id];
}

point(options: PointOptions = DefaultPointOptions) {
const point = new Point(options, this);
this.objects.push(point);
this.objects[point.id] = point;
return point;
}
}
4 changes: 2 additions & 2 deletions lib/types/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import { BaseObjectInterface } from './shapes';

export abstract class BaseRenderer {
context: CanvasRenderingContext2D;
objects: BaseObjectInterface[];
objects: Record<string, BaseObjectInterface>;

constructor(context: CanvasRenderingContext2D) {
this.context = context;
this.objects = [];
this.objects = {};
}
abstract render(deltaTime: number): void;
}
2 changes: 1 addition & 1 deletion lib/types/shapes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface BaseObjectInterface<T = unknown> {
/**
* A Unique Id that will help us identify each object in the Engine
*/
id?: string;
id: string;
renderer: Renderer;
}

Expand Down

0 comments on commit 15531ee

Please sign in to comment.