Skip to content

Commit

Permalink
Working package
Browse files Browse the repository at this point in the history
  • Loading branch information
florisdh committed Jul 11, 2019
1 parent df37008 commit 91d3861
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 80 deletions.
32 changes: 15 additions & 17 deletions src/baseState.ts
Original file line number Diff line number Diff line change
@@ -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 {}
}
19 changes: 9 additions & 10 deletions src/iState.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export {IState} from "./iState";
export {BaseState} from "./baseState";
export {StateManager} from "./stateManager";
declare module 'pixi-scenes' {
export = PixiScenes;
}
99 changes: 49 additions & 50 deletions src/stateManager.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 91d3861

Please sign in to comment.