working on terrain - mid-step

This commit is contained in:
lee2sman 2021-03-09 03:52:39 -05:00
parent 7be7685ea2
commit 7da47ff1b1

58
dotd.js
View File

@ -9,6 +9,7 @@ const gameFile = "gamefile.txt";
const dateFile = "datefile.txt";
const itemsFile = "itemsfile.txt";
const descriptionsFile = "nonKittenItems.txt";
const inventoryFile = "inventory.txt";
//globals
const width = 20;
@ -17,6 +18,7 @@ let dungeon;
let roomItems = [];
let today;
let itemDescriptions = [];
let inventory = [];
let player = {"position": {
"x": null,
"y": null,
@ -37,6 +39,8 @@ let parseArgs = () => {
loadDungeon();
loadItems();
loadPlayer();
loadInventory();
grab();
movePlayer();
checkCollision();
}
@ -57,18 +61,25 @@ let loadDungeon = () => {
}
let loadItems = () => {
const itemsStr = fs.readFileSync(itemsFile, 'utf8')
const itemsStr = fs.readFileSync(itemsFile, 'utf8');
roomItems = JSON.parse(itemsStr)
//console.log(roomItems);
}
let loadPlayer = () => {
const playerFile = fs.readFileSync(gameFile,'utf8')
const playerFile = fs.readFileSync(gameFile,'utf8');
player = JSON.parse(playerFile);
}
let loadInventory = () => {
const invFile = fs.readFileSync(inventoryFile,'utf8');
inventory = JSON.parse(invFile);
}
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
@ -126,6 +137,25 @@ let checkCollision = () => {
}
let grab = () => {
if (argv.g){
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);
//add to player inventory
inventory.push(roomItems[i]);
//now remove from room since owned by player now
roomItems.splice(i, 1);
}
}
}
}
//---------------CREATE DUNGEON -----------------------------------
let createDungeon = () => {
@ -281,12 +311,28 @@ let dungeonToStrings = () => {
let createTerrain = () => {
let forestTerrain = ['🎄', '🌳','🌲']
let buildings = ['🏛️','⛺']
let landscapes = ['🏔️','🌵']
let plants = ['🌹','🌺','🌻','🌼','🌷','🎋','🍄','🌰','🌿','🌱','🌾']
let shrines = ['⛩️','🗿']
for (let y = 0; y < height; y++){
for (let x = 0; x < width; x++){
if (dungeon[y][x] === "."){
//instead of below!, create objects and save to dungeonfile!
//
if (dungeon[y][x] === "."){ //empty space = place forest terrain
let whichTerrain = forestTerrain[Math.floor(Math.random()*forestTerrain.length)];
dungeon[y][x] = whichTerrain;
} else if (dungeon[y][x] === "*"){ //flower
let whichPlant = plants[Math.floor(Math.random()*plants.length)];
dungeon[y][x] = whichPlant;
} else if (dungeon[y][x] === "#"){ //building
let whichBuilding = buildings[Math.floor(Math.random()*buildings.length)];
dungeon[y][x] = whichBuilding;
} else if (dungeon[y][x] === "+"){ //shrine
let whichShrine = shrines[Math.floor(Math.random()*shrines.length)];
dungeon[y][x] = whichShrine;
}
}
}
@ -334,6 +380,12 @@ let writeToFile = dungeonStr => { //save to disk
//write items in current dungeon to file
let itemsData = JSON.stringify(roomItems);
fs.writeFileSync(itemsFile, itemsData);
//list of items saved to player's inventory
//
console.log(inventory)
let inventoryData = JSON.stringify(inventory);
fs.writeFileSync(inventoryFile, inventoryData);
}
let checkDay = () => {