assassin-bug/src/engine/index.js

153 lines
4.2 KiB
JavaScript
Raw Normal View History

2021-11-04 19:58:37 +00:00
import State from './state';
import Room from './room';
import Player from './player';
import Output from './output';
import Input from './input';
import Commands from './commands';
2021-11-05 19:02:20 +00:00
import Serialization from './serialization';
2021-11-04 22:55:56 +00:00
2021-11-04 19:58:37 +00:00
export default class Game {
2021-11-05 22:22:28 +00:00
constructor(newGame = true) {
this.newGame = newGame;
2021-11-04 19:58:37 +00:00
this.player = new Player();
this.state = State;
this.rooms = [];
this.items = [];
this.output = new Output();
this.commandHandler = new Commands(this);
2021-11-04 21:47:09 +00:00
this.input = new Input(this.commandHandler, this.output);
2021-11-04 19:58:37 +00:00
this.visitedRooms = new Map();
2021-11-04 22:55:56 +00:00
this.interval = null;
2021-11-05 19:02:20 +00:00
this.Serialization = new Serialization(this);
2021-11-04 19:58:37 +00:00
}
print(string) {
this.output.say(string);
}
init(data) {
this.rooms = data.rooms.map((room) => {
room.context = this;
return room;
});
this.items = data.items.map((item) => {
item.context = this;
return item;
});
2021-11-05 13:28:01 +00:00
this.state = data.state || State;
2021-11-04 19:58:37 +00:00
this.commandHandler.addCommands(data.commands);
this.player = new Player();
2021-11-04 21:47:09 +00:00
this.player.context = this;
2021-11-05 22:22:28 +00:00
if (this.newGame) {
this.move(this.player.currentRoom);
} else {
this.Serialization.load();
}
2021-11-04 22:55:56 +00:00
this.start();
}
advanceTick() {
this.items.forEach((item) => item.onTick());
this.rooms.forEach((room) => room.onTick());
}
start() {
this.interval = setInterval(() => this.advanceTick(), 1000);
}
2021-11-05 13:28:01 +00:00
2021-11-04 22:55:56 +00:00
stop() {
clearInterval(this.interval);
this.interval = null;
2021-11-04 19:58:37 +00:00
}
examineRoom() {
const room = this.getRoom(this.player.currentRoom);
this.output.say(room.title);
if (!this.visitedRooms.get(this.player.currentRoom) && room.firstDescription != "") {
this.output.say(room.firstDescription);
} else {
this.output.say(room.description);
}
this.examineItems();
this.examineExits();
}
examineItems() {
const room = this.getRoom(this.player.currentRoom);
const items = room.getItems();
2021-11-05 13:28:01 +00:00
if (items.length < 1) return;
let itemDescription = `You see `;
items.forEach((item, index) => {
if (index < items.length - 2) {
itemDescription += `${item.name}, `;
} else if (index < items.length - 1) {
itemDescription += `${item.name} and `;
} else {
itemDescription += item.name
}
});
this.output.say(itemDescription + ".");
2021-11-04 19:58:37 +00:00
}
examineExits() {
const room = this.getRoom(this.player.currentRoom);
2021-11-05 13:28:01 +00:00
let exits = [];
2021-11-04 19:58:37 +00:00
let exitDescription = "You can go ";
2021-11-05 13:28:01 +00:00
const exitKeys = room.exits.keys();
for (let exit of exitKeys) {
exits.push(exit);
2021-11-04 19:58:37 +00:00
}
2021-11-05 13:28:01 +00:00
exits.forEach((item, index) => {
if (index < exits.length - 2) {
exitDescription += `${item}, `;
} else if (index < exits.length - 1) {
exitDescription += `${item} and `;
} else {
exitDescription += item
}
});
this.output.say(exitDescription + ".");
2021-11-04 19:58:37 +00:00
}
getRoom(id) {
return this.rooms.find((room) => room.id == id);
}
getItem(id) {
return this.items.find((item) => item.id == id);
}
wait(ms) {
return new Promise((resolve, reject) => {
setTimeout(resolve, ms);
});
}
async move(roomID) {
const currentRoom = this.getRoom(this.player.currentRoom);
const newRoom = this.getRoom(roomID);
if (currentRoom.canExit() && newRoom.canEnter()) {
await currentRoom.onExit();
await newRoom.onEnter();
this.player.currentRoom = roomID;
this.examineRoom();
this.visitedRooms.set(roomID, true);
}
}
2021-11-04 21:19:50 +00:00
enableCommandInput(value) {
this.commandHandler.enabled = value;
}
2021-11-04 21:51:50 +00:00
setInputEcho(value) {
this.input.setEcho(value);
}
2021-11-05 19:02:20 +00:00
save() {
this.Serialization.save();
}
load() {
this.Serialization.load();
}
2021-11-04 19:58:37 +00:00
}