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

View File

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

View File

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

View File

@ -1,8 +1,11 @@
import State from "./state";
export default class Item { export default class Item {
constructor() { constructor() {
this.id = "item"; this.id = "item";
this.name = "An item"; this.name = "An item";
this.description = "You see nothing special about this item"; this.description = "You see nothing special about this item";
this.state = new State();
this.usable = true; this.usable = true;
this.takeable = true; this.takeable = true;
this.useCallback = null; this.useCallback = null;
@ -42,4 +45,12 @@ export default class Item {
addTickCallback(callback) { addTickCallback(callback) {
this.tickCallback = callback.bind(this); 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 = { const saveobj = {
state: this.context.state.serialize(), state: this.context.state.serialize(),
itemLocations: this.serializeItemLocations(), itemLocations: this.serializeItemLocations(),
itemStates: this.serializeItemStates(),
player: { player: {
currentRoom: this.context.player.currentRoom, currentRoom: this.context.player.currentRoom,
inventory: this.context.player.inventory inventory: this.context.player.inventory
@ -24,6 +25,7 @@ export default class Serialization {
const loadobj = JSON.parse(localStorage.getItem("save")); const loadobj = JSON.parse(localStorage.getItem("save"));
this.context.state.deserialize(loadobj.state); this.context.state.deserialize(loadobj.state);
this.deserializeItemLocations(loadobj.itemLocations); this.deserializeItemLocations(loadobj.itemLocations);
this.deserializeItemStates(loadobj.itemStates);
this.deserializePlayer(loadobj.player); this.deserializePlayer(loadobj.player);
this.context.output.sound.setSFXVolume(loadobj.volumes.sfx); this.context.output.sound.setSFXVolume(loadobj.volumes.sfx);
this.context.output.sound.setMusicVolume(loadobj.volumes.music); 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) { deserializePlayer(player) {
this.context.move(player.currentRoom); this.context.move(player.currentRoom);
this.context.player.inventory = player.inventory; 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() { constructor() {
this.states = new Map(); this.states = new Map();
} }
@ -34,5 +34,3 @@ class State {
this.states = new Map(data); this.states = new Map(data);
} }
} }
export default new State();