@ -2,42 +2,53 @@
const yargs = require ( 'yargs/yargs' )
const { hideBin } = require ( 'yargs/helpers' )
const argv = yargs ( hideBin ( process . argv ) ) . argv
const Charlatan = require ( 'charlatan' ) ;
const fs = require ( "fs" ) ;
const generator = require ( "project-name-generator" ) ;
const dungeonFile = ".dungeonfile.txt" ;
const gameFile = ".gamefile.txt" ;
const dateFile = ".datefile.txt" ;
const itemsFile = ".itemsfile.txt" ;
const descriptionsFile = ".nonKittenItems.txt" ;
const verbsFile = ".verbs.txt" ;
const inventoryFile = ".inventory.txt" ;
const placesFile = ".places.txt" ;
//globals
const width = 20 ;
const height = 5 ;
const movesInADay = 24 ;
let dungeon ;
let roomItems = [ ] ;
let today ;
let itemDescriptions = [ ] ;
let verbs = [ ] ;
let inventory = [ ] ;
let places = [ ] ;
let player = { "position" : {
"x" : null ,
"y" : null ,
} ,
"avatar" : "@" ,
"movedToday" : false
"movedToday" : false ,
"life" : null
} ;
let parseArgs = ( ) => {
if ( argv . new ) {
console . log ( 'new game' ) ;
createDungeon ( ) ;
choosePlayer ( ) ;
placePlayer ( ) ;
createPlayer ( ) ;
loadTextFiles ( ) ;
createItems ( ) ;
createPlaces ( ) ;
createGraves ( ) ;
} else {
loadDungeon ( ) ;
loadItems ( ) ;
loadPlaces ( ) ;
loadPlayer ( ) ;
loadInventory ( ) ;
grab ( ) ;
@ -71,6 +82,9 @@ let loadPlayer = () => {
const playerFile = fs . readFileSync ( gameFile , 'utf8' ) ;
player = JSON . parse ( playerFile ) ;
player . life -- ;
console . log ( 'life: ' + player . life ) ;
}
let loadInventory = ( ) => {
@ -79,12 +93,18 @@ let loadInventory = () => {
inventory = JSON . parse ( invFile ) ;
}
let loadPlaces = ( ) => {
const placesStr = fs . readFileSync ( placesFile , 'utf8' ) ;
places = JSON . parse ( placesStr )
//console.log(roomItems);
}
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 . life > 0 ) { //check to see if player did not move yet
if ( argv . r || argv . right ) {
if ( player . position . x < width - 1 ) {
@ -119,15 +139,16 @@ let movePlayer = () => {
console . log ( "can't go that way" ) ;
}
}
//} else { //you already moved
// console.log("already moved today");
//}
} else { //you are asleep
console . log ( "you've drifted off asleep for the night and will wake tomorrow refreshed." ) ;
}
}
}
let checkCollision = ( ) => {
//check Collision with Items
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);
@ -135,6 +156,14 @@ let checkCollision = () => {
}
}
//check Collision with Places
for ( let i = 0 ; i < places . length ; i ++ ) {
if ( ( player . position . x == places [ i ] . position . x ) && ( player . position . y == places [ i ] . position . y ) ) {
console . log ( places [ i ] . name ) ; //player is standing at this place
}
}
}
let grab = ( ) => {
@ -169,38 +198,26 @@ let createDungeon = () => {
}
//---------------CREATE PLAYER ----------------------------------
let c hoos ePlayer = ( ) => {
let c reat ePlayer = ( ) => {
const avatars = [ '👳' , '👶' , '🧛' , '🕵' , '👲' , '🧕' , '👵' , '👧' , '🧔' , '👸' , '🤠' , '@ ' ]
let chooseAvatar = Math . floor ( Math . random ( ) * avatars . length ) ;
player . avatar = avatars [ chooseAvatar ] ;
}
let placePlayer = ( ) => {
player . life = movesInADay ;
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 loadTextFiles = ( ) => {
itemDescriptions = fs . readFileSync ( descriptionsFile ) . toString ( ) . split ( "\n" ) ;
verbs = fs . readFileSync ( verbsFile ) . toString ( ) . split ( "\n" ) ;
}
let createItems = ( ) => {
@ -225,7 +242,7 @@ let createItems = () => {
roomItems . push (
{
"name" : " random name ",
"name" : " it ",
"symbol" : items [ whichItem ] ,
"description" : itemDescrip ,
"position" :
@ -243,6 +260,71 @@ let createItems = () => {
//console.log(roomItems);
}
let capitalize = ( 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 ) . toUpperCase ( ) ) ;
}
return arr . join ( ' ' ) ;
}
let createPlaces = ( ) => {
let numToSpawn = Math . round ( Math . random ( ) * 3 ) + 3
for ( let i = 0 ; i < numToSpawn ; i ++ ) {
const buildings = [ '🏛️' , '⛺' , '🏚️' ]
const placeTypes = [ 'Village' , 'Village' , 'Village' , 'House' , 'House' , 'Market' , 'Market' , 'Market' , 'Crossroads' , 'Place' , 'Outpost' , 'Trading Post' , 'House' , 'Shack' , 'Meeting Place' , 'Saloon' , 'Watering Hole' , 'Stall' , 'Hideout' , 'Cart' , 'Camp' , 'Camp' , 'Camp' , 'Camp' , '' , '' , '' , 'Zone of Ill Repute' ]
let whichBuilding = buildings [ Math . floor ( Math . random ( ) * 3 ) ] ;
let loc = generator . generate ( ) . spaced ;
let suffix = placeTypes [ Math . floor ( Math . random ( ) * placeTypes . length ) ] ;
let locName = capitalize ( loc + ' ' + suffix ) ;
places . push (
{
"name" : locName ,
"symbol" : whichBuilding ,
"position" :
{
"x" : Math . floor ( Math . random ( ) * width ) ,
"y" : Math . floor ( Math . random ( ) * height )
}
}
)
}
//let's also create some tombstones?
//console.log(places)
}
let createGraves = ( ) => {
let prefix = [ "Here lies" , "RIP" , "" , "" , "" , "Resting place of " , "Beloved" ]
let reason = [ "Made an enemy" , "Wasn't afraid to be" , "Tried" , "Died while" , "Passed while performing" , "Tried out" , "Dissapeared investingating" , "" , "" , "" , "" , "" , "" , "" , "Wandered off while looking for" , "Last seen" , "Loved" , "Adored" , "A lifelong fan of" , "Our favorite at" , "The best at" , "Always in our hearts" , "Keep" , "Always be" , "Always" , "Just" , "Tried" , "Couldn't stop" , "Only ever found" , "Died" , "Passed while" , "Couldn't stop" , "Forgot to try" , "Never stopped" , "We'll always think of you when we're" , "It's not the same" ]
let name = Charlatan . Name . name ( ) ;
let epitaph = prefix [ Math . floor ( Math . random ( ) * prefix . length ) ] + name + "\n" + reason [ Math . floor ( Math . random ( ) * reason . length ) ] + " " + verbs [ Math . floor ( Math . random ( ) * verbs . length ) ] ;
places . push (
{
"name" : epitaph ,
"symbol" : "⚰️" ,
"position" :
{
"x" : Math . floor ( Math . random ( ) * width ) ,
"y" : Math . floor ( Math . random ( ) * height )
}
}
)
}
//-----------------PEOPLE----------------------------------------
let people =
[
@ -281,9 +363,6 @@ let people =
//----------------create zone---------------
let buildings = [ '🏛️' , '⛺' ]
let landscapes = [ '🏔️' , '🌿' , '🌱' , '🌾' , '🌻' , '🌵' ]
let plants = [ '🌹' , '🌺' , '🌻' , '🌼' , '🌷' , '🎋' ]
@ -324,16 +403,7 @@ let createTerrain = () => {
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 ;
}
}
}
}
@ -341,18 +411,19 @@ let createTerrain = () => {
let dungeonWithItemsToStrings = ( ) => {
//specify item location
//specify item location s
for ( let i = 0 ; i < roomItems . length ; i ++ ) {
dungeon [ roomItems [ i ] . position . y ] [ roomItems [ i ] . position . x ] = roomItems [ i ] . symbol ;
}
//specify places location
for ( let i = 0 ; i < places . length ; i ++ ) {
dungeon [ places [ i ] . position . y ] [ places [ i ] . position . x ] = places [ i ] . symbol ;
}
//specify player location
dungeon [ player . position . y ] [ player . position . x ] = player . avatar ;
//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" ) ;
@ -367,6 +438,8 @@ let drawDungeon = dungeonStr => {
let writeToFile = dungeonStr => { //save to disk
//this is very inefficient, just loop it swoop it
//write board to file, although it's all empty save for .....dots
fs . writeFileSync ( dungeonFile , dungeonStr ) ;
@ -382,10 +455,13 @@ let writeToFile = dungeonStr => { //save to disk
fs . writeFileSync ( itemsFile , itemsData ) ;
//list of items saved to player's inventory
//
console . log ( inventory )
console . log ( 'inventory: ' + inventory )
let inventoryData = JSON . stringify ( inventory ) ;
fs . writeFileSync ( inventoryFile , inventoryData ) ;
//save places to file
let placesData = JSON . stringify ( places ) ;
fs . writeFileSync ( placesFile , placesData ) ;
}
let checkDay = ( ) => {
@ -403,6 +479,7 @@ let checkDay = () => {
//console.log('already moved today');
} else {
player . movedToday = false
player . life = movesInADay ;
}
}