First commit

This commit is contained in:
David Masad
2021-01-27 15:30:48 -05:00
commit dc8d36b4de
6 changed files with 569 additions and 0 deletions

43
storymanager.js Normal file
View File

@ -0,0 +1,43 @@
// Choose one of an array
var randomChoice = function(vals) {
return vals[Math.floor(Math.random() * vals.length)];
};
// Set up the general narrative manager
// -----------------------------------------------------------------------
var NarrativeManager = {meta: []};
NarrativeManager.getAllStorylets = function() {
let allStorylets = [];
for (let key in this.storyletMakers) {
let storylets = this.storyletMakers[key]();
for (let i in storylets) allStorylets.push(storylets[i]);
}
return allStorylets;
}
NarrativeManager.getNStorylets = function(n) {
/*
Get N storylets, prioritizing the highest-priority ones first.
For now assume that priorities are integers, but it would be nice
to be more flexible.
*/
let allStorylets = this.getAllStorylets();
n = Math.min(n, allStorylets.length);
if (n == 0) return [];
let selectedStorylets = [];
// First sort by ascending priority:
allStorylets.sort((a, b) => b.priority - a.priority);
let currentPriority = allStorylets[0].priority;
while (selectedStorylets.length < n) {
let priorityStorylets = allStorylets.filter(s => s.priority==currentPriority);
priorityStorylets.sort((a, b) => (Math.random() - Math.random()));
let nAdd = Math.min(n - selectedStorylets.length, priorityStorylets.length);
for (let i=0; i<nAdd; i++) selectedStorylets.push(priorityStorylets.pop());
currentPriority--;
}
return selectedStorylets;
}
window.NM = NarrativeManager;