dungeon-of-the-day/dotd.js

333 lines
7.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
const argv = yargs(hideBin(process.argv)).argv
const fs = require("fs");
const dungeonFile = "dungeonfile.txt";
const gameFile = "gamefile.txt";
const dateFile = "datefile.txt";
//globals
const width = 20;
const height = 5;
let dungeon;
let roomItems = [];
let today;
let player = {"position": {
"x": null,
"y": null,
},
"avatar": "@",
"movedToday": false
};
let parseArgs = () => {
if (argv.new){
console.log('new game');
createDungeon();
choosePlayer();
placePlayer();
createItems();
} else {
loadDungeon();
//loadItems();
loadPlayer();
movePlayer();
}
}
let loadDungeon = () => {
const dungeonInput = fs.readFileSync(dungeonFile,'utf8')
let dungeonRows = dungeonInput.split("\n");
dungeon = Array.from(Array(height), () => new Array(width))
for (let y = 0; y < dungeonRows.length-1; y++){
for (x = 0; x < dungeonRows[0].length; x++){ //length of any string (width of dungeon)
let dungeonRowArr = dungeonRows[y].split("");
dungeon[y][x] = dungeonRowArr[x];
}
}
}
let loadPlayer = () => {
const playerFile = fs.readFileSync(gameFile,'utf8')
player = JSON.parse(playerFile);
}
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 (argv.r || argv.right){
if (player.position.x < width-1){
console.log('moved right');
player.position.x++;
player.movedToday = true;
} else {
console.log("can't go that way");
}
} else if (argv.l || argv.left){
if (player.position.x > 0){
console.log('moved left');
player.position.x--;
player.movedToday = true;
} else {
console.log("can't go that way");
}
} else if (argv.u || argv.up){
if (player.position.y>0){
console.log('moved up');
player.position.y--;
player.movedToday = true;
} else {
console.log("can't go that way");
}
} else if (argv.d || argv.down){
if (player.position.y<height-1){
console.log('moved down');
player.position.y++;
player.movedToday = true;
} else {
console.log("can't go that way");
}
}
} else { //you already moved
console.log("already moved today");
}
}
}
//---------------CREATE DUNGEON -----------------------------------
let createDungeon = () => {
dungeon = Array.from(Array(height), () => new Array(width))
for (let y = 0; y < height; y++){
for (let x = 0; x < width; x++){
dungeon[y][x] = ".";
}
}
}
//---------------CREATE PLAYER ----------------------------------
let choosePlayer = () => {
const avatars = ['👳','👶','🧛','🤺','🕵','👲','🧕','👵','👧','🧔','👸','🤠','']
let chooseAvatar = Math.floor(Math.random() * avatars.length);
player.avatar = avatars[chooseAvatar];
}
let placePlayer = () => {
player.position.x = Math.floor(Math.random()*width)
player.position.y = Math.floor(Math.random()*height)
//dungeon[player.position.y][player.position.x] = "@"
}
//-------------CREATE ITEMS---------------------------------------
items = [
{
"name":"blanket",
"symbol":"b",
"description":"a wide flat blue striped and draped or bunched thing"
},
{
"name":"blanket",
"symbol":"b",
"description":"a wide flat blue striped and draped or bunched thing"
}
]
let createItems = () => {
let numToSpawn = Math.round(Math.random() * 2)
let currentItemsInRoom = [];
for (let i = 0; i < numToSpawn; i++){
const items = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,.:;!?"'`^~ ̄_&#%+-*=<>()[]{}⦅⦆|¦/\¬$£¢₩¥".split("");
//make sure it wasn't chosen previously in this board
do {
whichItem = Math.floor(Math.random()*items.length);
} while (currentItemsInRoom.includes(items[whichItem]))
currentItemsInRoom.push(items[whichItem]);
// console.log(items[whichItem]);
roomItems.push(
{
"name": "random name",
"symbol": items[whichItem],
"position":
{
"x":Math.floor(Math.random()*width),
"y":Math.floor(Math.random()*height)
}
}
)
//roomItems[i].char = items[whichItem];
//roomItems[i].position.x = Math.floor(Math.random()*width)
//roomItems[i].position.y = Math.floor(Math.random()*height)
}
//console.log(roomItems);
}
//-----------------PEOPLE----------------------------------------
let people =
[
{
"name": "wizard",
"emoji": "🧙",
"life": 3
},
{
"name": "woman",
"emoji": "🧝",
"life": 3
},
{
"name": "man",
"emoji": "🧔",
"life": 3
},
{
"name": "woman",
"emoji": "👵",
"life": 3
},
{
"name": "man",
"emoji": "👴",
"life": 3
},
{
"name": "person",
"emoji": "🧓",
"life": 3
}
]
//----------------create zone---------------
let buildings = ['🏛️','⛺']
let landscapes = ['🏔️','🌿','🌱','🌾','🌻','🌵']
let plants = ['🌹','🌺','🌻','🌼','🌷','🎋']
let shrines = ['⛩️','🗿']
let emojiitems = ['🍄','🌰','🦴','🧵','🧶','👓','🕶','🥽','👞','👟','🥾','🥿','👑','💼']
let debug = () => {
//console.log(dungeon)
console.log("terminal width: "+process.stdout.columns+" height: "+process.stdout.rows);
console.log("game width: "+width+" height: "+height);
console.log("x: "+player.position.x+" y: "+player.position.y)
}
let dungeonToStrings = () => {
let dungeonStr = "";
for (let i = 0; i < height; i++){
dungeonStr+=(dungeon[i].join("")+"\n");
}
return dungeonStr;
}
let createTerrain = () => {
let forestTerrain = ['🎄', '🌳','🌲']
for (let y = 0; y < height; y++){
for (let x = 0; x < width; x++){
if (dungeon[y][x] === "."){
let whichTerrain = forestTerrain[Math.floor(Math.random()*forestTerrain.length)];
dungeon[y][x] = whichTerrain;
}
}
}
}
let dungeonWithItemsToStrings = () => {
//specify player location
dungeon[player.position.y][player.position.x] = player.avatar;
//specify item location
for (let i = 0; i < roomItems.length; i++){
dungeon[roomItems[i].position.y][roomItems[i].position.x] = roomItems[i].symbol;
}
//for (let i = 0; i < items.length; i++){
// dungeon[item[i].position.y][item[i].position.x] = item[i].icon;
//}
let dungeonStr = "";
for (let i = 0; i < height; i++){
dungeonStr+=(dungeon[i].join("")+"\n");
}
return dungeonStr;
}
let drawDungeon = dungeonStr => {
console.log(dungeonStr); //display
}
let writeToFile = dungeonStr => { //save to disk
fs.writeFileSync(dungeonFile, dungeonStr);
let playerData = JSON.stringify(player);
fs.writeFileSync(gameFile, playerData);
fs.appendFileSync(dateFile, today+'\n');
}
let checkDay = () => {
const savedDays = fs.readFileSync(dateFile).toString().split("\n");;
const lastDay = savedDays[savedDays.length-2]; //loads day last played
console.log("Last movement: "+lastDay);
const fullDate = new Date();
today = fullDate.getFullYear()+"-"+fullDate.getMonth()+"-"+fullDate.getDate();
console.log("Today: "+today);
if (lastDay === today){
//console.log('already moved today');
} else {
player.movedToday = false
}
}
let main = () => {
checkDay();
parseArgs();
let dungeonStr = dungeonToStrings();
writeToFile(dungeonStr);
createTerrain(); //do this after writing to file
let dungeonWithItems = dungeonWithItemsToStrings();
drawDungeon(dungeonWithItems);
//debug();
}
main()