Last active
November 29, 2019 21:22
-
-
Save oelna/d85427f6704c2904f537bfec62ca79ec to your computer and use it in GitHub Desktop.
A simple script to generate pseudo-random titles for an adventure game, a la One Page Dungeon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* outputs a simple string comprised of strings from a dictionary */ | |
var game = window.game || {}; | |
// inspired by https://watabou.itch.io/one-page-dungeon | |
game.titles = { | |
'typ': ['Temple', 'Prison', 'House', 'Mansion', 'Dungeon', 'World', 'Castle', 'Graveyard', 'Library', 'Water World'], | |
'adjektiv': ['Void', 'Blood', 'Burnt', 'Undead', 'Frozen', 'Night'], | |
'name': ['Queen', 'Prince', 'King', 'Delila'] | |
} | |
game.randomArrayItem = function(arr) { | |
const randomIndex = Math.floor(Math.random() * arr.length); | |
return arr[randomIndex]; | |
} | |
game.generateTitle = function() { | |
const typ = game.randomArrayItem(game.titles.typ); | |
const adjektiv = game.randomArrayItem(game.titles.adjektiv); | |
const name = game.randomArrayItem(game.titles.name); | |
return typ + ' of the ' + adjektiv + ' ' + name; | |
} | |
game.init = function() { | |
const mapTitle = game.generateTitle(); | |
document.querySelector('h1').textContent = mapTitle; | |
} | |
window.addEventListener('DOMContentLoaded', game.init); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* this improves upon v1 with a simple way to expand sentence structure and allow for multiple uses of title variables */ | |
var game = window.game || {}; | |
// inspired by https://watabou.itch.io/one-page-dungeon | |
game.titles = { | |
'type': ['Temple', 'Prison', 'House', 'Mansion', 'Dungeon', 'World', 'Castle', 'Graveyard', 'Library', 'Visage', 'Monument', 'Sin', 'Rise'], | |
'descriptor': ['Void', 'Blood', 'Burnt', 'Undead', 'Frozen', 'Night', 'Drowned', 'Moonlight', 'Sun', 'Poisoned', 'Invisible', 'Ancient'], | |
'actor': ['Queen', 'Prince', 'King', 'Elder', 'Scholar', 'Statue', 'Serpent', 'Slave Master', 'Lord', 'Lady', 'Demon', 'Caretaker'], | |
'sentences': [ | |
'{type} of the {descriptor} {actor}', | |
'The {actor}\'s {descriptor} {type}', | |
'The {actor}\'s {type}', | |
'The {actor} and the {descriptor} {actor}', | |
'The {descriptor} {type}' | |
] | |
} | |
game.randomArrayItem = function(arr) { | |
if (!arr || arr.length === 0) return false; | |
return arr[Math.floor(Math.random() * arr.length)]; | |
} | |
game.extractRandomArrayItem = function(name) { | |
if (!name) return false; | |
const randomIndex = Math.floor(Math.random() * game.titles[name].length); | |
const extractedString = game.titles[name][randomIndex]; | |
// remove the string from the array | |
game.titles[name].splice(randomIndex, 1); | |
return extractedString; | |
} | |
game.generateTitle = function() { | |
let sentence = game.randomArrayItem(game.titles.sentences); | |
for (item of ['type', 'descriptor', 'actor']) { | |
// how many occurrences of this string? | |
let pattern = new RegExp('{'+item+'}', 'g'); | |
const amount = (sentence.match(pattern) || []).length; | |
// replace each occurrence with a unique random string | |
for (let i = 0; i < amount; i += 1) { | |
const word = game.extractRandomArrayItem(item); | |
pattern = new RegExp('{'+item+'}'); | |
sentence = sentence.replace(pattern, word); | |
} | |
} | |
return sentence; | |
} | |
game.init = function() { | |
const mapTitle = game.generateTitle(); | |
document.querySelector('h1').textContent = mapTitle; | |
} | |
window.addEventListener('DOMContentLoaded', game.init); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment