Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/ludiek/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @123ishatest/ludiek

## 0.9.5

### Patch Changes

- Improve game startup flow

## 0.9.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/ludiek/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@123ishatest/ludiek",
"private": false,
"version": "0.9.4",
"version": "0.9.5",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
"types": "dist/index.d.ts",
Expand Down
25 changes: 20 additions & 5 deletions packages/ludiek/src/engine/LudiekGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ export class LudiekGame<Engine extends DependencyEngine<LudiekDependencies>> {
this._nextSave = this.config.saveInterval;
}

public start(): void {
public start(loadFromStorage: boolean = true): void {
// TODO(@Isha): Improve game loop, make sure you can only start once (i.e. add resume after pause)
this.engine.initialize();

if (loadFromStorage) {
this.loadFromStorage();
}

this.engine.start();

this._engine.logger.info('Starting game');
Expand All @@ -49,9 +54,7 @@ export class LudiekGame<Engine extends DependencyEngine<LudiekDependencies>> {

this._nextSave -= delta;
if (this._nextSave <= 0) {
const data = this.save();
LudiekLocalStorage.store(this.config.saveKey, data, this.saveEncoder);

this.save();
this._nextSave = this.config.saveInterval;
}

Expand All @@ -62,7 +65,19 @@ export class LudiekGame<Engine extends DependencyEngine<LudiekDependencies>> {
return this._engine;
}

public save(): LudiekSaveData {
/**
* Serialize the game and write it to local storage
*/
public save(): void {
const data = this.serialize();
LudiekLocalStorage.store(this.config.saveKey, data, this.saveEncoder);
}

public deleteSave(): void {
LudiekLocalStorage.delete(this.config.saveKey);
}

public serialize(): LudiekSaveData {
return {
engine: this._engine.save(),
game: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export class AchievementPlugin extends LudiekPlugin {
* Loops through all achievements and see if you can earn them
*/
public checkAchievements(): void {
this.engine.logger.info('Checking achievements');
Object.keys(this._achievements).forEach((id) => {
this.tryEarnAchievement(id as string);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/ludiek/tests/consumer/smoke.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ it('saves features and plugins', () => {
for (let i = 0; i < TICKS; i++) {
game.tick(1);
}
const save = game.save();
const save = game.serialize();

// Assert
expect(save).toStrictEqual({
Expand Down
Loading