From 91d386107236d4ec2eb986e49683ca336e772178 Mon Sep 17 00:00:00 2001 From: florisdh Date: Thu, 11 Jul 2019 23:40:31 +0200 Subject: [PATCH] Working package --- src/baseState.ts | 32 +++++++-------- src/iState.ts | 19 +++++---- src/index.ts | 6 +-- src/stateManager.ts | 99 ++++++++++++++++++++++----------------------- 4 files changed, 76 insertions(+), 80 deletions(-) diff --git a/src/baseState.ts b/src/baseState.ts index 73f375e..d7e8f24 100644 --- a/src/baseState.ts +++ b/src/baseState.ts @@ -1,20 +1,18 @@ -import * as PIXI from "pixi.js"; -import {IState} from "./iState"; -import {StateManager} from "./stateManager"; +module PixiScenes { + export class BaseState extends PIXI.Container implements IState { -export class BaseState extends PIXI.Container implements IState { - - public app: PIXI.Application|null; - public states: StateManager|null; - - constructor() { - super(); - this.app = null; - this.states = null; + public app: PIXI.Application|null; + public states: StateManager|null; + + constructor() { + super(); + this.app = null; + this.states = null; + } + + public init(): void {} + public start(): void {} + public stop(): void {} + public update(delta: number): void {} } - - public init(): void {} - public start(): void {} - public stop(): void {} - public update(delta: number): void {} } diff --git a/src/iState.ts b/src/iState.ts index a5d63ce..78ae3ef 100644 --- a/src/iState.ts +++ b/src/iState.ts @@ -1,11 +1,10 @@ -import * as PIXI from "pixi.js"; -import {StateManager} from "./StateManager"; - -export interface IState extends PIXI.Container { - app: PIXI.Application|null; - states: StateManager|null; - init(): void; - start(): void; - stop(): void; - update(delta: number): void; +module PixiScenes { + export interface IState extends PIXI.Container { + app: PIXI.Application|null; + states: StateManager|null; + init(): void; + start(): void; + stop(): void; + update(delta: number): void; + } } diff --git a/src/index.ts b/src/index.ts index f8c7398..f711c7d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,3 @@ -export {IState} from "./iState"; -export {BaseState} from "./baseState"; -export {StateManager} from "./stateManager"; +declare module 'pixi-scenes' { + export = PixiScenes; +} diff --git a/src/stateManager.ts b/src/stateManager.ts index ff97ba4..68c85ae 100644 --- a/src/stateManager.ts +++ b/src/stateManager.ts @@ -1,62 +1,61 @@ -import * as PIXI from "pixi.js"; -import {IState} from "./iState"; - -export class StateManager { - - private app: PIXI.Application; - private states: {[name: string]: IState}; - private current: string|null; - - constructor(app: PIXI.Application) { - this.app = app; - this.states = {}; - this.current = null; - // Listen for animate update - app.ticker.add(this.update.bind(this)); - } - - private update(delta: number): void { - let active: IState|null = this.active; - if (active) { - active.update(delta); +module PixiScenes { + export class StateManager { + + private app: PIXI.Application; + private states: {[name: string]: IState}; + private current: string|null; + + constructor(app: PIXI.Application) { + this.app = app; + this.states = {}; + this.current = null; + // Listen for animate update + app.ticker.add(this.update.bind(this)); } - } - public add(name: string, state: IState): void { - if (this.contains(name)) { - return; + private update(delta: number): void { + let active: IState|null = this.active; + if (active) { + active.update(delta); + } } - this.states[name] = state; - //state.app = this.app; - //state.states = this; - state.init(); - } - - public contains(name: string): boolean { - return !!this.states[name]; - } - public start(name: string): void { - if (!this.contains(name) || name === this.current) { - return; + public add(name: string, state: IState): void { + if (this.contains(name)) { + return; + } + this.states[name] = state; + //state.app = this.app; + //state.states = this; + state.init(); } - // Stop current - let active: IState|null = this.active; - if (active) { - active.stop(); - this.app.stage.removeChild(active); + public contains(name: string): boolean { + return !!this.states[name]; } - // Start new - this.current = name; - if (active = this.active) { - this.app.stage.addChild(active); - active.start(); + public start(name: string): void { + if (!this.contains(name) || name === this.current) { + return; + } + + // Stop current + let active: IState|null = this.active; + if (active) { + active.stop(); + this.app.stage.removeChild(active); + } + + // Start new + this.current = name; + if (active = this.active) { + this.app.stage.addChild(active); + active.start(); + } } - } - public get active(): IState|null { - return this.current ? this.states[this.current] : null; + public get active(): IState|null { + return this.current ? this.states[this.current] : null; + } } }