added simple item spawn and detection proof of concept

This commit is contained in:
lee2sman 2021-03-09 01:48:36 -05:00
parent 7d089edbf1
commit ab80f1557e

46
dotd.js
View File

@ -7,6 +7,7 @@ const fs = require("fs");
const dungeonFile = "dungeonfile.txt";
const gameFile = "gamefile.txt";
const dateFile = "datefile.txt";
const itemsFile = "itemsfile.txt";
//globals
const width = 20;
@ -22,6 +23,16 @@ let player = {"position": {
"movedToday": false
};
var nonKittenItems = [
"\"50 Years Among the Non-Kitten Items\", by Ann Droyd.",
"\"Blup, blup, blup\", says the mud pot.",
"\"Dear robot, you may have already won our 10 MILLION DOLLAR prize...\"",
"\"I pity the fool who mistakes me for kitten!\", sez Mr. T.",
"\"Kibo was here\"",
]
console.log(nonKittenItems[4])
let parseArgs = () => {
if (argv.new){
console.log('new game');
@ -31,9 +42,10 @@ let parseArgs = () => {
createItems();
} else {
loadDungeon();
//loadItems();
loadItems();
loadPlayer();
movePlayer();
checkCollision();
}
}
@ -51,6 +63,13 @@ let loadDungeon = () => {
}
}
let loadItems = () => {
const itemsStr = fs.readFileSync(itemsFile, 'utf8')
roomItems = JSON.parse(itemsStr)
console.log(roomItems);
}
let loadPlayer = () => {
const playerFile = fs.readFileSync(gameFile,'utf8')
@ -61,7 +80,7 @@ let movePlayer = () => {
//if moving
if (argv.r || argv.right || argv.l || argv.left || argv.u || argv.up || argv.d || argv.down) { //check to see if moving first
if(!player.movedToday){ //check to see if player did not move yet
// if(!player.movedToday){ //check to see if player did not move yet
if (argv.r || argv.right){
if (player.position.x < width-1){
@ -96,8 +115,18 @@ let movePlayer = () => {
console.log("can't go that way");
}
}
} else { //you already moved
console.log("already moved today");
//} else { //you already moved
// console.log("already moved today");
//}
}
}
let checkCollision = () => {
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 collided with "+roomItems[i].name);
}
}
@ -289,12 +318,20 @@ let drawDungeon = dungeonStr => {
}
let writeToFile = dungeonStr => { //save to disk
//write board to file, although it's all empty save for .....dots
fs.writeFileSync(dungeonFile, dungeonStr);
//save player info to file
let playerData = JSON.stringify(player);
fs.writeFileSync(gameFile, playerData);
//write dates to dateFile
fs.appendFileSync(dateFile, today+'\n');
//write items in current dungeon to file
let itemsData = JSON.stringify(roomItems);
fs.writeFileSync(itemsFile, itemsData);
}
let checkDay = () => {
@ -307,6 +344,7 @@ let checkDay = () => {
console.log("Today: "+today);
//TURN OFF RESTRICTION
if (lastDay === today){
//console.log('already moved today');
} else {