Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion resource/passages/testpassages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
<title>Test Passage 1</title>
<text>
This is a test passage.
[check visited3 eq true]You have visited passage 3.[/check]
[check visited2 eq true]You have visited passage 2.[/check]
</text>
<option link="2">Continue</option>
<option link="3">[check visited3 eq true]Passage 3[/check]</option>
</passage>

<passage id="2">
<title>Test Passage 2</title>
<text>
[set visited2 true]
[set var1 5]
This is another test passage.
</text>
<option link="1">Back</option>
Expand All @@ -19,10 +24,12 @@
<passage id="3">
<title>Test Passage 3</title>
<text>
[set visited3 true]
The third test passage.
[check var1 gte 5]The check passed[/check]
</text>
<option link="1">The first passage.</option>
<option link="2">The second passage.</option>
<option link="3">The thrid passage.</option>
<option link="3">The third passage.</option>
</passage>
</passages>
35 changes: 35 additions & 0 deletions src/js/games/pstcc/gameState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export default class GameState {
constructor() {
this.stateDictionary = {};
}

setValue(key, value) {
this.stateDictionary[key.trim()] = value.trim();
}

checkValue(key, op, check) {
if (op == "eq") {
return this.stateDictionary[key] == check;
}

let checknum = parseInt(check);
let value = parseInt(this.stateDictionary[key]);

if (op == "lt") {
return value < checknum;
}

if (op == "gt") {
console.log(checknum, value);
return value > checknum;
}

if (op == "lte") {
return value <= checknum;
}

if (op == "gte") {
return value >= checknum;
}
}
}
5 changes: 5 additions & 0 deletions src/js/games/pstcc/inputhandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export default class InputHandler{
// }

keyDown(e) {
if(e.keyCode == 32 && e.target == document.body) {
//Prevents spacebar from scrolling page
e.preventDefault();
}

if(this.inputEnabled) {
const key = String(e.key);

Expand Down
113 changes: 107 additions & 6 deletions src/js/games/pstcc/passageController.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import * as PIXI from "pixi.js";
import TextRenderer from "./textrenderer";
import StoryNode from "./storyNode";
import GameState from "./gameState";

export default class PassageController {
constructor(app, input) {
this.textRenderer = new TextRenderer(app);
this.app = app;
this.input = input;
this.state = new GameState();
this.storyNodes = {};
this.currentNode = null;
this.previousNodes = []; // Stack for tracking previous nodes
this.parser = new DOMParser();
this.passageFolder = "../resource/passages";
this.passageFile = "testpassages2.xml";
this.passageFile = "testpassages.xml";
this.endText = "The End"; // Default end text
this.currentPassageText = null;
this.currentPassageOptions = null;
}

async loadPassages() {
Expand Down Expand Up @@ -46,17 +50,37 @@ export default class PassageController {

renderCurrentNode() {
this.textRenderer.clear();


//Read any tags out of the text
let passageText = this.parseTags(this.currentNode.text);
this.currentPassageText = passageText.trim();

//Read check tags out of the options
let parsedOptions = [];
this.currentNode.options.forEach(item => {
let optionText = this.parseTags(item.text);
//If the option is empty after parsing tags, ignore it
if (optionText.length > 0) {
const newOption = {
text: optionText,
id: item.id,
textBroken: optionText.split(' ')
}
parsedOptions.push(newOption);
}
});
this.currentPassageOptions = parsedOptions;

// Display the current passage text and title
if (this.currentNode) {
this.textRenderer.addTextLine(this.currentNode.title);
this.textRenderer.addLineBreak();
this.textRenderer.addTextLine(this.currentNode.text);
this.textRenderer.addTextLine(this.currentPassageText);
this.textRenderer.addLineBreak();

// Display options or end text
if (this.currentNode.options.length > 0) {
this.currentNode.options.forEach(option => {
if (this.currentPassageOptions.length > 0) {
this.currentPassageOptions.forEach(option => {
this.textRenderer.addTextLine(`${option.id}) ${option.text}`);
});
} else {
Expand All @@ -82,6 +106,82 @@ export default class PassageController {
}
}

parseTags(text) {
//Parses and removes 'set' tags from the text
let parsedText = text;
let removedText = [];

for (let i = 0; i < text.length; i++) {
if (text[i] == '[') {
if (i < 0 && text[i - 1] == '\\')
{
//Escape character
continue;
}

if (text.slice(i, i + 6) == "[check") {
let begin = i;
let startIndex = i + 7;
let endIndex = 0;
let words = [];
for (endIndex = startIndex; text[endIndex] != "]"; endIndex++) {
//Pull out the options for the check tag
if (text[endIndex] == ' ') {
words.push(text.slice(startIndex, endIndex));
startIndex = endIndex + 1;
}
}
words.push(text.slice(startIndex, endIndex));
//Words now contains the options for the check

removedText.push(text.slice(begin, endIndex + 1));

//Find the end tag
removedText.push("[/check]")
for (let j = endIndex + 1; j < text.length; j++) {
if (text[j] == '[' && text.slice(j, j + 8) == "[/check]") {
//j marks the start of the end tag
//If the check fails, remove everything between tags
if (!this.state.checkValue(words[0].trim(), words[1].trim(), words[2].trim())) {
removedText.push(text.slice(endIndex + 1, j));
}
break;
}
}
}

if (text.slice(i, i + 4) == "[set") {
let begin = i;
let startIndex = i + 5;
let endIndex = 0;
let words = [];

for (endIndex = startIndex; text[endIndex] != "]"; endIndex++) {
//Pull out the options for the check tag
if (text[endIndex] == ' ') {
words.push(text.slice(startIndex, endIndex));
startIndex = endIndex;
}
}
words.push(text.slice(startIndex, endIndex));
//words now contains the options for the set

removedText.push(text.slice(begin, endIndex + 1));
//There is no end tag for set

this.state.setValue(words[0], words[1]);
}
}
}

//Remove all tags from the output
for (let i = 0; i < removedText.length; i++) {
parsedText = parsedText.replace(removedText[i], "")
}

return parsedText.trim();
}

goBack() {
// Pop the previous node from the stack and set it as the current node
if (this.previousNodes.length > 0) {
Expand Down Expand Up @@ -113,6 +213,7 @@ export default class PassageController {
}

getOptions() {
return this.currentNode.options;
//return this.currentNode.options;
return this.currentPassageOptions;
}
}