Refactor scene management with store
This commit is contained in:
parent
f9079e4823
commit
871b4116ff
10 changed files with 69 additions and 59 deletions
|
|
@ -1,7 +1,6 @@
|
|||
import { mp5 } from '../../main';
|
||||
import { SceneManager } from './SceneManager';
|
||||
import store from '../store';
|
||||
import { Scenes } from './scenes';
|
||||
|
||||
export class LegacyScene {
|
||||
constructor() {}
|
||||
|
||||
|
|
@ -9,8 +8,8 @@ export class LegacyScene {
|
|||
mp5.background(100);
|
||||
}
|
||||
|
||||
onSceneClick(sm: SceneManager) {
|
||||
onSceneClick() {
|
||||
console.log('Click on legacy scene');
|
||||
sm.changeSceneTo(Scenes.OVERVIEW);
|
||||
store.setState({ currentScene: Scenes.OVERVIEW });
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import { mp5 } from '../../main';
|
||||
import { Player } from '../Player';
|
||||
import { Player } from '../sketchObjects/Player';
|
||||
import { colors } from '../constants/colors';
|
||||
import { Edge } from '../Edge';
|
||||
import { SceneManager } from './SceneManager';
|
||||
import { Edge } from '../sketchObjects/Edge';
|
||||
import { Scenes } from './scenes';
|
||||
import store from '../store';
|
||||
|
||||
export class OverviewScene {
|
||||
player: Player;
|
||||
|
|
@ -28,11 +28,11 @@ export class OverviewScene {
|
|||
this.player.move();
|
||||
}
|
||||
|
||||
public onSceneClick(sm: SceneManager) {
|
||||
public onSceneClick() {
|
||||
this.edgeData.forEach((edge, i) => {
|
||||
const dist = mp5.dist(mp5.mouseX, mp5.mouseY, edge.x, edge.y);
|
||||
if (dist < edge.r) {
|
||||
sm.changeSceneTo(edge.scene);
|
||||
store.setState({ currentScene: edge.scene });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,39 +0,0 @@
|
|||
import { LegacyScene } from './LegacyScene';
|
||||
import { OverviewScene } from './OverviewScene';
|
||||
import { Scenes } from './scenes';
|
||||
|
||||
export class SceneManager {
|
||||
currentScene: Scenes;
|
||||
|
||||
// Scenes
|
||||
overviewScene: OverviewScene;
|
||||
legacyScene: LegacyScene;
|
||||
|
||||
constructor() {
|
||||
this.currentScene = Scenes.OVERVIEW;
|
||||
|
||||
// Scenes
|
||||
this.overviewScene = new OverviewScene();
|
||||
this.legacyScene = new LegacyScene();
|
||||
}
|
||||
|
||||
public draw() {
|
||||
if (this.currentScene === Scenes.OVERVIEW) {
|
||||
this.overviewScene.draw();
|
||||
} else if (this.currentScene === Scenes.LEGACY) {
|
||||
this.legacyScene.draw();
|
||||
}
|
||||
}
|
||||
|
||||
public changeSceneTo(newScene: Scenes) {
|
||||
this.currentScene = newScene;
|
||||
}
|
||||
|
||||
public handleClick() {
|
||||
if (this.currentScene === Scenes.OVERVIEW) {
|
||||
this.overviewScene.onSceneClick(this);
|
||||
} else if (this.currentScene === Scenes.LEGACY) {
|
||||
this.legacyScene.onSceneClick(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in a new issue