Skip to content

Instantly share code, notes, and snippets.

@ulve
Last active June 7, 2018 11:31
Show Gist options
  • Save ulve/997f4a73a3320e8d26670cffbd39d5eb to your computer and use it in GitHub Desktop.
Save ulve/997f4a73a3320e8d26670cffbd39d5eb to your computer and use it in GitHub Desktop.
import { Map, List } from "immutable";
// createStory :: String -> Option -> Option -> Option -> Artifact -> Story
const createStory = (
storyText,
option1,
option2,
option3,
requiredArtifact = null
) =>
Map({
text: storyText,
one: option1,
two: option2,
three: option3,
requiredArtifact: requiredArtifact
});
// createChoice :: String -> (GameState -> (String, GameState)) -> String -> Choice
const createChoice = (text, fn, resolutionText) =>
Map({ text: text, fn: fn, resolutionText: resolutionText });
// addStory :: Story -> GameState -> GameState
const addStory = (story, gameState) =>
gameState.updateIn(["stories"], l => l.push(story));
// addArtifact :: Artifact -> GameState -> GameState
const addArtifact = (artifact, gameState) =>
gameState.updateIn(["artifacts"], l => l.push(artifact));
// hasArtifact :: Artifact -> GameState -> Bool
const hasArtifact = (artifact, gameState) =>
gameState.get("artifacts").includes(artifact);
// initGameState :: () -> GameState
const initGameState = () => new Map({ stories: List(), artifacts: List() });
// nextStory :: GameState -> (Story, GameState)
const nextStory = gameState => {
const filtered = gameState.get("stories").filter(s => {
const ra = s.get("requiredArtifact");
return ra === null || gameState.get("artifacts").includes(ra);
});
const index = Math.floor(Math.random() * filtered.size);
const selected = filtered.get(index);
const newGameState = gameState.updateIn(["stories"], l => l.delete(index));
return { selected, newGameState };
};
const s1 = createStory(
"Du befinner dig i templet. Du hör hur munkarna kommer. Du har bara tid att plocka en av sakerna. Vilken väljer du?",
createChoice("Du tar kniven", s => {
const s2 = addArtifact("Knivs", s);
return {
resolution: "Du stoppar kiven inannför västen, känns som ett bra val",
gameState: s2
};
}),
createChoice("Du tar bägaren", s => {
const s2 = addArtifact("Bägare", s);
return {
resolution: "vad skall man ens med en sådan här till?",
gameState: s2
};
}),
createChoice("Du springer", s => {
return { resolution: "Mmmm fegis", gameState: s };
})
);
const s2 = createStory(
"Med kniven i handen har du all makt över hennes liv vad gör du?",
createChoice("Skär av henne håret", s => {
const s2 = addArtifact("Jagad", s);
return {
resolution:
"Du hör hur någon bankar in dörren. Du hinner just ut men du känner dig inte trygg. Vad har du gjort",
gameState: s2
};
}),
createChoice("Skär henne i foten", s => {
const s2 = addArtifact("Fot", s);
return { resolution: "Utan fötter kan man inte göra något", gameState: s2 };
}),
createChoice("Skär henne i tungan", s => {
const s2 = addArtifact("Tunga", s);
const s3 = addStory(sTunga, s2);
return { resolution: "Ännu ett smart val!", gameState: s3 };
}),
"Kniv"
);
const sTunga = createStory(
"Opium och torkad tunga. Det är tidens melodi",
createChoice("Väääässs", s => ({
resolution: "Du känner giftet i blodet",
gameState: s
})),
createChoice("Vyyys", s => ({
resolution: "Du känner kraften i blodet",
gameState: s
})),
createChoice("MMmppfff", s => ({
resolution: "Du har inget blod",
gameState: s
}))
);
const s3 = createStory(
"Du fumlar genom mörkret men vad är det du ser?",
createChoice("En kentaur", s => ({
resolution: "Dom här kan man slåss med",
gameState: s
})),
createChoice("En orm", s => ({ resolution: "Dom här ger en styrka", s })),
createChoice("En man i blått", s => {
if (hasArtifact("Jagad", s)) {
const s2 = addArtifact("Död", s);
return { resolution: "Mannen skjuter dig i huvudet", gameState: s2 };
} else {
return { resolution: "Ingen du känner", gameState: s };
}
})
);
// printStory :: Story -> ()
const printStory = s => {
console.log(s.get("text"));
console.log("1: " + s.get("one").text);
console.log("2: " + s.get("two").text);
console.log("3: " + s.get("three").text);
};
// printResolution :: String -> ()
const printResolution = resolution => console.log(resolution);
const gs1 = initGameState();
const gs2 = addStory(s1, gs1);
const gs3 = addStory(s2, gs2);
const gs4 = addStory(s3, gs3);
// Game loop
// Select story
const { selected: s, newGameState: gs5 } = nextStory(gs4);
// Print
printStory(s);
// Select and resolve
const { resolution: r, gameState: gs6 } = s.get("two").get("fn")(gs5);
// print resolution
printResolution(r);
// Kör igen med gs6 som gamestate istälet för gs4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment