Skip to content

Commit

Permalink
feat: improved setting scene method, mouse motion & button example
Browse files Browse the repository at this point in the history
  • Loading branch information
load1n9 committed Dec 10, 2021
1 parent 9424125 commit badaa00
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 10 deletions.
28 changes: 28 additions & 0 deletions examples/button.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { World, Scene, Image, Button} from '../mod.ts';


class Game extends Scene {
public test = new Button(this, new Image(this, "assets/caviar.png", 200, 100, 414, 197));

public setup() {
this.addChild(this.test);
this.test.onClick = () => {
this.test.setX(Math.round(Math.random() * this.world.params.width));
}
}
}

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

await test.start();
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type {
Frame,
KeyEvent,
MouseDownEvent,
MouseMotionEvent,
PixelTexture,
RGBA,
spriteConfig,
Expand Down
3 changes: 2 additions & 1 deletion src/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import {
import { hexToRGBA } from "./utils/mod.ts";

export class Renderer {
constructor(public world: World) {}

constructor(public world: World) {}

public render(entity: Entity) {
if (entity instanceof Rectangle) {
this.world.setDrawColor(
Expand Down
25 changes: 20 additions & 5 deletions src/World.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Canvas } from "../deps.ts";
import { Scene, Renderer, Entity } from "../mod.ts"
import type { KeyEvent, MouseDownEvent, WorldOptions } from "./types.ts";
import { Scene, Renderer } from "../mod.ts"
import type { KeyEvent, MouseDownEvent, MouseMotionEvent, WorldOptions } from "./types.ts";

export class World extends Canvas {
public FPS = 100;
public FPS = 500;
public params: WorldOptions;
public scenes: Array<typeof Scene>;
public currentScene: Scene;
Expand All @@ -20,6 +20,9 @@ export class World extends Canvas {
this.setup();
for await (const event of this) {
switch (event.type) {
case "mouse_motion":
this._mouseMotion(event);
break;
case "mouse_button_down":
this._mouseDown(event);
break;
Expand Down Expand Up @@ -70,13 +73,25 @@ export class World extends Canvas {
this.currentScene.keyDown(e);
}

public setScene(scene: number): void {
this.currentScene = new this.scenes[scene](this);
public setScene(scene: number | string): void {
if (typeof scene === 'string') {
for (const s of this.scenes) {
if (s.name === scene) {
this.currentScene = new s(this);
break;
}
}
} else {
this.currentScene = new this.scenes[scene](this);
}
this.setup();
}
private _mouseDown(e: MouseDownEvent) {
this.currentScene._mouseDown(e);
}
private _mouseMotion(e: MouseMotionEvent) {
this.currentScene._mouseMotion(e);
}
public setup(): void {
this.currentScene.setup();

Expand Down
6 changes: 3 additions & 3 deletions src/entities/containers/Button.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Entity, Sprite, Scene } from '../../../mod.ts';
import { Entity, Sprite, Rectangle, Image, Scene } from '../../../mod.ts';

export class Button extends Entity {
public child: Sprite;
public child: Sprite | Image | Rectangle;
public scene: Scene;

constructor(scene: Scene, child: Sprite) {
constructor(scene: Scene, child: Sprite | Image | Rectangle) {
super(child.x,child.y);
this.child = child;
this.scene = scene;
Expand Down
6 changes: 5 additions & 1 deletion src/scenes/Scene.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Entity, World, Button } from "../../mod.ts";
import type { MouseDownEvent, KeyEvent } from "../types.ts";
import type { MouseDownEvent, MouseMotionEvent, KeyEvent } from "../types.ts";
export class Scene {
public entities: Array<Entity> = [];

Expand Down Expand Up @@ -32,7 +32,11 @@ export class Scene {
}
this.mouseDown(e);
}
public _mouseMotion(e: MouseMotionEvent) {
this.mouseMotion(e)
}
public mouseDown(_e: MouseDownEvent): void {}
public mouseMotion(_e: MouseMotionEvent): void {}
public setup(): void {}
public draw(): void {}
public keyDown(_e: KeyEvent): void {}
Expand Down
11 changes: 11 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface WorldPhysicsOptions {
gravity: number,
}



export type RGBA = [number, number, number, number];

export interface KeyEvent {
Expand All @@ -32,6 +34,15 @@ export interface MouseDownEvent {
button: number;
}

export interface MouseMotionEvent {
which: number;
x: number;
y: number;
xrel: number;
yrel: number;
state: number;
}

export interface Frame {
x: number,
y: number,
Expand Down

0 comments on commit badaa00

Please sign in to comment.