added drop and eat functions
This commit is contained in:
parent
537cbb08c9
commit
4dd0ea745b
125
village.js
125
village.js
|
@ -29,6 +29,7 @@ const movesInADay = 24;
|
|||
let dungeon;
|
||||
let roomItems = [];
|
||||
let today;
|
||||
let eating = false, dropping = false;
|
||||
let itemDescriptions = [];
|
||||
let verbs = [];
|
||||
let inventory = [];
|
||||
|
@ -74,7 +75,11 @@ function loop(){
|
|||
|
||||
resetScreen();
|
||||
|
||||
checkKeys(key);
|
||||
if (!eating && !dropping){
|
||||
checkKeys(key);
|
||||
} else {
|
||||
eatOrDrop(key);
|
||||
}
|
||||
|
||||
|
||||
updateDungeon();
|
||||
|
@ -131,21 +136,36 @@ function checkKeys(key){
|
|||
//program.write('list inventory');
|
||||
inv();
|
||||
break;
|
||||
case 'e': //eat
|
||||
eat();
|
||||
break;
|
||||
case 'd': //drop
|
||||
drop();
|
||||
break;
|
||||
default: //else
|
||||
program.write('hit another key');
|
||||
program.setx(0)
|
||||
program.down(10);
|
||||
program.write('Unknown action. Press another key');
|
||||
|
||||
program.up(10);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
function pickup(){
|
||||
for (let i = 0; i < roomItems.length; i++){
|
||||
if ((player.position.x == roomItems[i].position.x) && (player.position.y == roomItems[i].position.y)){
|
||||
console.log("you take "+roomItems[i].name);
|
||||
if (roomItems.length>10){
|
||||
console.log("You take "+roomItems[i].name);
|
||||
|
||||
//add to player inventory
|
||||
inventory.push(roomItems[i]);
|
||||
|
||||
//now remove from room since owned by player now
|
||||
roomItems.splice(i, 1);
|
||||
} else {
|
||||
console.log("Your backpack is already full");
|
||||
}
|
||||
}
|
||||
}
|
||||
//save to inventory file
|
||||
|
@ -156,15 +176,100 @@ function pickup(){
|
|||
fs.writeFileSync(itemsFile, itemsData);
|
||||
}
|
||||
|
||||
function inv(){
|
||||
program.setx(1)
|
||||
function eat(){
|
||||
program.setx(0)
|
||||
program.down(9);
|
||||
program.write("Eat which item?: ");
|
||||
|
||||
for (let i = 0; i < inventory.length; i++){
|
||||
program.setx(0);
|
||||
program.down(2);
|
||||
program.down(1);
|
||||
program.write(i+": "+inventory[i].symbol+" "+inventory[i].description);
|
||||
}
|
||||
|
||||
program.setx(0)
|
||||
program.sety(0);
|
||||
|
||||
eating = true;
|
||||
}
|
||||
|
||||
function drop(){
|
||||
program.setx(0)
|
||||
program.down(9);
|
||||
program.write("Drop which item?: ");
|
||||
|
||||
for (let i = 0; i < inventory.length; i++){
|
||||
program.setx(0);
|
||||
program.down(1);
|
||||
program.write(i+": "+inventory[i].symbol+" "+inventory[i].description);
|
||||
}
|
||||
|
||||
program.setx(0)
|
||||
program.sety(0);
|
||||
|
||||
dropping = true;
|
||||
}
|
||||
|
||||
function inv(){
|
||||
|
||||
program.setx(0)
|
||||
program.down(9);
|
||||
program.write("In your backpack: ");
|
||||
|
||||
for (let i = 0; i < inventory.length; i++){
|
||||
program.setx(0);
|
||||
program.down(1);
|
||||
program.write(inventory[i].symbol+": "+inventory[i].description);
|
||||
}
|
||||
|
||||
program.setx(0)
|
||||
program.sety(0);
|
||||
}
|
||||
|
||||
function eatOrDrop(key){
|
||||
|
||||
if (key.full>=0 && key.full<=9){
|
||||
if (inventory[key.full]){
|
||||
|
||||
if (eating){
|
||||
console.log("Delicious, you ate "+lowerFirst(inventory[key.full].description));
|
||||
} else if (dropping){
|
||||
console.log("You dropped"+lowerFirst(inventory[key.full].description));
|
||||
//add back to room
|
||||
//
|
||||
//
|
||||
roomItems.push(
|
||||
{
|
||||
"name": "",
|
||||
"symbol": inventory[key.full].symbol,
|
||||
"description": inventory[key.full].description,
|
||||
"position":
|
||||
{
|
||||
"x": inventory[key.full].position.x,
|
||||
"y": inventory[key.full].position.y
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
}
|
||||
|
||||
inventory.splice(key.full, 1);
|
||||
|
||||
//save to inventory file
|
||||
let inventoryData = JSON.stringify(inventory);
|
||||
fs.writeFileSync(inventoryFile, inventoryData);
|
||||
|
||||
} else {
|
||||
console.log("You don't have that");
|
||||
}
|
||||
} else {
|
||||
console.log("You don't have that");
|
||||
}
|
||||
|
||||
eating = false
|
||||
dropping = false
|
||||
|
||||
}
|
||||
|
||||
function checkCollision() {
|
||||
|
@ -425,6 +530,14 @@ let capitalize = (str) => {
|
|||
return arr.join(' ');
|
||||
}
|
||||
|
||||
let lowerFirst = (str) => {
|
||||
let arr = str.split(' ');
|
||||
for(let i = 0; i < arr.length; i++ ) {
|
||||
arr[i] = arr[i].replace(arr[i].charAt(0), arr[i].charAt(0).toLowerCase());
|
||||
}
|
||||
return arr.join(' ');
|
||||
}
|
||||
|
||||
let createPlaces = () => {
|
||||
|
||||
let numToSpawn = Math.round(Math.random() * 3) + 4
|
||||
|
|
Loading…
Reference in New Issue