Skip to content

Commit

Permalink
implement saving and loading for Pong
Browse files Browse the repository at this point in the history
  • Loading branch information
minecrawler committed Dec 3, 2020
1 parent b8ecb99 commit 877a156
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/pong/src/app/frame-transition-handlers.ts
@@ -1,4 +1,4 @@
import {GameStore} from "./game-store";
import {GameStore} from "../models/game-store";
import {ITransitionActions} from "../ecs";
import {MenuState} from "../states/menu";

Expand Down
10 changes: 6 additions & 4 deletions examples/pong/src/main.ts
Expand Up @@ -2,7 +2,7 @@ import {ECS, IWorld} from "./ecs";
import {PaddleSystem} from "./systems/paddle";
import {afterFrameHandler, beforeFrameHandler} from "./app/frame-transition-handlers";
import {InputSystem} from "./systems/input";
import {GameStore} from "./app/game-store";
import {GameStore} from "./models/game-store";
import {MenuState} from "./states/menu";
import {UIItem} from "./components/ui-item";
import {MenuSystem} from "./systems/menu";
Expand All @@ -15,6 +15,7 @@ import {BallSystem} from "./systems/ball";
import {ScoreSystem} from "./systems/score";
import {GameItem} from "./components/game-item";
import {ScoreBoardSystem} from "./systems/score-board";
import {Direction} from "./components/direction";


const cleanup = () => {
Expand All @@ -37,18 +38,19 @@ const createWorld = () => {
.withSystem(MenuSystem, [InputSystem])
.withSystem(PaddleSystem, [InputSystem])
.withSystem(PauseSystem, [InputSystem])
.withSystem(ScoreBoardSystem) // todo: throw if a system was not registered but is supposed to run
.withSystem(ScoreBoardSystem)
.withSystem(ScoreSystem, [InputSystem])
.withComponents([
.withComponents(
Ball,
Direction,
GameItem,
MenuItem,
Paddle,
PauseItem,
Position,
ScoreItem,
UIItem,
])
)
.build();
};

Expand Down
Expand Up @@ -9,6 +9,7 @@ export enum EMovement {

export class GameStore {
backToMenu = false
continue = false
ctx!: CanvasRenderingContext2D
currentState?: IState
exit = false
Expand Down
13 changes: 12 additions & 1 deletion examples/pong/src/prefabs/menu.ts
Expand Up @@ -44,6 +44,17 @@ export const menuPrefab: TPrefab = [
top: 270,
},
},
{
MenuItem: {},
UIItem: {
action: EActions.Continue,
color: '#ddd',
caption: 'Continue',
fontSize: 32,
left: 120,
top: 320,
},
},
{
MenuItem: {},
UIItem: {
Expand All @@ -52,7 +63,7 @@ export const menuPrefab: TPrefab = [
caption: 'Exit',
fontSize: 32,
left: 120,
top: 320,
top: 370,
},
},
];
12 changes: 10 additions & 2 deletions examples/pong/src/states/game.ts
@@ -1,12 +1,12 @@
import {ITransitionActions, State, TPrefabHandle, With} from "../ecs";
import {ECS, ITransitionActions, State, TPrefabHandle, With} from "../ecs";
import {InputSystem} from "../systems/input";
import {PauseSystem} from "../systems/pause";
import {PaddleSystem} from "../systems/paddle";
import {gamePrefab} from "../prefabs/game";
import {Paddle} from "../components/paddle";
import {Position} from "../components/position";
import {Ball} from "../components/ball";
import {GameStore} from "../app/game-store";
import {GameStore} from "../models/game-store";
import {BallSystem} from "../systems/ball";
import {Direction} from "../components/direction";
import {ScoreSystem} from "../systems/score";
Expand All @@ -18,6 +18,14 @@ export class GameState extends State {
create(actions: ITransitionActions) {
const gameStore = actions.getResource(GameStore);

if (gameStore.continue) {
actions.fromJSON(localStorage.getItem('save')!);
actions.maintain();
actions.getResource(GameStore).pointsLeft = parseInt(localStorage.getItem('savePointsLeft')!);
actions.getResource(GameStore).pointsRight = parseInt(localStorage.getItem('savePointsRight')!);
return;
}

this.prefabHandle = actions.loadPrefab(gamePrefab);
for (const entity of actions.getEntities([With(Paddle)])) {
entity.addComponent(new Position());
Expand Down
5 changes: 4 additions & 1 deletion examples/pong/src/states/pause.ts
Expand Up @@ -3,7 +3,7 @@ import {InputSystem} from "../systems/input";
import {PauseSystem} from "../systems/pause";
import {pausePrefab} from "../prefabs/pause";
import {PaddleSystem} from "../systems/paddle";
import {GameStore} from "../app/game-store";
import {GameStore} from "../models/game-store";
import {BallSystem} from "../systems/ball";

export class PauseState extends State {
Expand All @@ -13,6 +13,9 @@ export class PauseState extends State {

activate(actions: ITransitionActions): void | Promise<void> {
actions.getResource(GameStore).pause = true;
localStorage.setItem('save', actions.toJSON());
localStorage.setItem('savePointsLeft', actions.getResource(GameStore).pointsLeft.toString());
localStorage.setItem('savePointsRight', actions.getResource(GameStore).pointsRight.toString());
}

create(actions: ITransitionActions) {
Expand Down
2 changes: 1 addition & 1 deletion examples/pong/src/states/score-board.ts
Expand Up @@ -2,7 +2,7 @@ import {ITransitionActions, State, TPrefabHandle, With} from "../ecs";
import {scorePrefab} from "../prefabs/score";
import {ScoreItem} from "../components/_markers";
import {UIItem} from "../components/ui-item";
import {GameStore} from "../app/game-store";
import {GameStore} from "../models/game-store";
import {ScoreBoardSystem} from "../systems/score-board";

export class ScoreBoardState extends State {
Expand Down
2 changes: 1 addition & 1 deletion examples/pong/src/systems/ball.ts
@@ -1,5 +1,5 @@
import {ISystemActions, Read, System, SystemData, Write} from "../ecs";
import {GameStore} from "../app/game-store";
import {GameStore} from "../models/game-store";
import {Position} from "../components/position";
import {Ball} from "../components/ball";
import {Direction} from "../components/direction";
Expand Down
2 changes: 1 addition & 1 deletion examples/pong/src/systems/input.ts
@@ -1,5 +1,5 @@
import {ISystemActions, NoData, System} from "../ecs";
import {EMovement, GameStore} from "../app/game-store";
import {EMovement, GameStore} from "../models/game-store";

export enum EKeyState {
Down,
Expand Down
34 changes: 29 additions & 5 deletions examples/pong/src/systems/menu.ts
@@ -1,6 +1,6 @@
import {ISystemActions, Read, System, SystemData, With} from "../ecs";
import {UIItem} from "../components/ui-item";
import {EMovement, GameStore} from "../app/game-store";
import {EMovement, GameStore} from "../models/game-store";
import {EActions} from "../app/actions";
import {GameState} from "../states/game";
import {MenuItem} from "../components/_markers";
Expand All @@ -20,10 +20,26 @@ export class MenuSystem extends System<Data> {
}

run(dataSet: Set<Data>) {
if (this.gameStore.input.actions.menuMovement != EMovement.halt) {
this.menuAction = this.menuAction == EActions.Play
? EActions.Exit
: EActions.Play;
// todo: use index
if (this.gameStore.input.actions.menuMovement == EMovement.down) {
switch (this.menuAction) {
case EActions.Play: this.menuAction = EActions.Continue; break;
case EActions.Continue: this.menuAction = EActions.Exit; break;
case EActions.Exit: this.menuAction = EActions.Play; break;
default: {
throw new Error(`Action ${this.menuAction} not implemented!`);
}
}
}
else if (this.gameStore.input.actions.menuMovement == EMovement.up) {
switch (this.menuAction) {
case EActions.Play: this.menuAction = EActions.Exit; break;
case EActions.Continue: this.menuAction = EActions.Play; break;
case EActions.Exit: this.menuAction = EActions.Continue; break;
default: {
throw new Error(`Action ${this.menuAction} not implemented!`);
}
}
}

for (const {uiItem} of dataSet) {
Expand All @@ -34,6 +50,14 @@ export class MenuSystem extends System<Data> {
if (this.menuAction == EActions.Play) {
this.gameStore.PushState = GameState;
}
else if (this.menuAction == EActions.Continue) {
if (localStorage.getItem('save') == null) {
return;
}

this.gameStore.continue = true;
this.gameStore.PushState = GameState;
}
else {
this.gameStore.exit = true;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/pong/src/systems/paddle.ts
@@ -1,5 +1,5 @@
import {ISystemActions, Read, System, SystemData, Write} from "../ecs";
import {EMovement, GameStore} from "../app/game-store";
import {EMovement, GameStore} from "../models/game-store";
import {EPaddleSide, Paddle} from "../components/paddle";
import {Position} from "../components/position";

Expand Down
2 changes: 1 addition & 1 deletion examples/pong/src/systems/pause.ts
@@ -1,5 +1,5 @@
import {ISystemActions, Read, System, SystemData, With} from "../ecs";
import {GameStore} from "../app/game-store";
import {GameStore} from "../models/game-store";
import {GameState} from "../states/game";
import {PauseState} from "../states/pause";
import {PauseItem} from "../components/_markers";
Expand Down
2 changes: 1 addition & 1 deletion examples/pong/src/systems/score-board.ts
@@ -1,7 +1,7 @@
import {ISystemActions, Read, System, SystemData, With} from "../ecs";
import {ScoreItem} from "../components/_markers";
import {UIItem} from "../components/ui-item";
import {GameStore} from "../app/game-store";
import {GameStore} from "../models/game-store";

export class Data extends SystemData {
_scoreItem = With(ScoreItem)
Expand Down
2 changes: 1 addition & 1 deletion examples/pong/src/systems/score.ts
Expand Up @@ -3,7 +3,7 @@ import {Position} from "../components/position";
import {Ball} from "../components/ball";
import {Direction} from "../components/direction";
import {EPaddleSide, Paddle} from "../components/paddle";
import {GameStore} from "../app/game-store";
import {GameStore} from "../models/game-store";
import {ScoreBoardState} from "../states/score-board";
import {GameItem} from "../components/game-item";

Expand Down

0 comments on commit 877a156

Please sign in to comment.