Add item states

master
Kevin Weispfennig 2022-10-26 16:05:20 +02:00
parent f8f4c555b8
commit 8d74237a6d
6 changed files with 377 additions and 348 deletions

2
package-lock.json generated
View File

@ -5,6 +5,7 @@
"requires": true,
"packages": {
"": {
"name": "assassin-bug",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
@ -568,7 +569,6 @@
"dependencies": {
"anymatch": "~3.1.2",
"braces": "~3.0.2",
"fsevents": "~2.3.2",
"glob-parent": "~5.1.2",
"is-binary-path": "~2.1.0",
"is-glob": "~4.0.1",

View File

@ -20,6 +20,11 @@ export default class ItemBuilder {
return this;
}
withState(key, value) {
this.item.setState(key, value);
return this;
}
isUsable(value) {
this.item.usable = value;
return this;

View File

@ -10,7 +10,7 @@ export default class Game {
constructor(newGame = true) {
this.newGame = newGame;
this.player = new Player();
this.state = State;
this.state = new State();
this.rooms = [];
this.items = [];
this.output = new Output();
@ -41,7 +41,7 @@ export default class Game {
item.context = this;
return item;
});
this.state = data.state || State;
this.state = data.state || new State();
this.commandHandler.addCommands(data.commands);
this.player = new Player();
this.player.context = this;

View File

@ -1,8 +1,11 @@
import State from "./state";
export default class Item {
constructor() {
this.id = "item";
this.name = "An item";
this.description = "You see nothing special about this item";
this.state = new State();
this.usable = true;
this.takeable = true;
this.useCallback = null;
@ -42,4 +45,12 @@ export default class Item {
addTickCallback(callback) {
this.tickCallback = callback.bind(this);
}
setState(key, value) {
return this.state.set(key, value);
}
getState(key) {
return this.state.get(key);
}
}

View File

@ -7,6 +7,7 @@ export default class Serialization {
const saveobj = {
state: this.context.state.serialize(),
itemLocations: this.serializeItemLocations(),
itemStates: this.serializeItemStates(),
player: {
currentRoom: this.context.player.currentRoom,
inventory: this.context.player.inventory
@ -24,6 +25,7 @@ export default class Serialization {
const loadobj = JSON.parse(localStorage.getItem("save"));
this.context.state.deserialize(loadobj.state);
this.deserializeItemLocations(loadobj.itemLocations);
this.deserializeItemStates(loadobj.itemStates);
this.deserializePlayer(loadobj.player);
this.context.output.sound.setSFXVolume(loadobj.volumes.sfx);
this.context.output.sound.setMusicVolume(loadobj.volumes.music);
@ -45,8 +47,21 @@ export default class Serialization {
})
}
deserializeItemStates(items) {
items.forEach((item) => {
const obj = this.context.getItem(item[0]);
obj.state.deserialize(item[1]);
})
}
deserializePlayer(player) {
this.context.move(player.currentRoom);
this.context.player.inventory = player.inventory;
}
serializeItemStates() {
return this.context.items.map((item) => {
return [item.id, item.state.serialize()]
});
}
}

View File

@ -1,4 +1,4 @@
class State {
export default class State {
constructor() {
this.states = new Map();
}
@ -34,5 +34,3 @@ class State {
this.states = new Map(data);
}
}
export default new State();