Rooms, items and some player stuff
This commit is contained in:
92
src/engine/index.js
Normal file
92
src/engine/index.js
Normal file
@@ -0,0 +1,92 @@
|
||||
import State from './state';
|
||||
import Room from './room';
|
||||
import Player from './player';
|
||||
import Output from './output';
|
||||
import Input from './input';
|
||||
import Commands from './commands';
|
||||
|
||||
export default class Game {
|
||||
constructor() {
|
||||
this.player = new Player();
|
||||
this.state = State;
|
||||
this.rooms = [];
|
||||
this.items = [];
|
||||
this.output = new Output();
|
||||
this.commandHandler = new Commands(this);
|
||||
this.input = new Input(this.commandHandler);
|
||||
this.visitedRooms = new Map();
|
||||
}
|
||||
|
||||
print(string) {
|
||||
this.output.say(string);
|
||||
}
|
||||
|
||||
init(data) {
|
||||
console.log(data);
|
||||
this.rooms = data.rooms.map((room) => {
|
||||
room.context = this;
|
||||
return room;
|
||||
});
|
||||
this.items = data.items.map((item) => {
|
||||
item.context = this;
|
||||
return item;
|
||||
});
|
||||
this.state = data.state;
|
||||
this.commandHandler.addCommands(data.commands);
|
||||
this.player = new Player();
|
||||
this.move(this.player.currentRoom);
|
||||
}
|
||||
|
||||
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();
|
||||
items.forEach((item) => this.output.say(item.name));
|
||||
}
|
||||
|
||||
examineExits() {
|
||||
const room = this.getRoom(this.player.currentRoom);
|
||||
let exitDescription = "You can go ";
|
||||
for (let exit of room.exits.keys()) {
|
||||
exitDescription += " " + exit;
|
||||
}
|
||||
this.output.say(exitDescription);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user