Skip to content

Commit

Permalink
feat: button entity
Browse files Browse the repository at this point in the history
  • Loading branch information
load1n9 committed Dec 8, 2021
1 parent bb106eb commit 8908d3e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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
20 changes: 19 additions & 1 deletion src/World.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Text,
TextureSprite,
Animation,
Button
} from "../mod.ts";
import { hexToRGBA } from "./utils/mod.ts";

Expand All @@ -28,7 +29,7 @@ export abstract class World extends Canvas {
for await (const event of this) {
switch (event.type) {
case "mouse_button_down":
this.mouseDown(event);
this._mouseDown(event);
break;
case "draw":
this._draw();
Expand Down Expand Up @@ -185,11 +186,28 @@ export abstract class World extends Canvas {
for (const child of entity.children) {
this._render(child);
}
} else if (entity instanceof Button) {
this._render(entity.child)
}
}
public keyDown(_e: KeyEvent): void {
return;
}
private _mouseDown(e: MouseDownEvent) {
for (const entity of this.entities) {
if (entity instanceof Button) {
if (
e.x >= entity.x &&
e.x <= entity.child.x + entity.child.width &&
e.y >= entity.child.y &&
e.y <= entity.child.y + entity.child.height
) {
entity.onClick();
}
}
}
this.mouseDown(e);
}
public mouseDown(_e: MouseDownEvent): void {
return;
}
Expand Down
22 changes: 22 additions & 0 deletions src/entities/containers/Button.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Entity, Sprite, World } from '../../../mod.ts';

export class Button extends Entity {
public child: Sprite;
public world: World;

constructor(world: World, child: Sprite) {
super(child.x,child.y);
this.child = child;
this.world = world;
}

public setX(x: number) {
this.child.x = x;
}
public setY(y: number) {
this.child.y = y;
}
public onClick() {

}
}
3 changes: 2 additions & 1 deletion src/entities/containers/mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { Group } from './Group.ts';
export { Group } from './Group.ts';
export { Button } from './Button.ts';
2 changes: 1 addition & 1 deletion src/entities/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ 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 { Group, Button } from "./containers/mod.ts";
export * from "./textures/mod.ts";

0 comments on commit 8908d3e

Please sign in to comment.