Skip to content
Merged
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
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"configurations": [
{
"type": "chrome",
"name": "Editor",
"request": "launch",
"url": "http://127.0.0.1:3000/editor/index.html"
},
{
"type": "chrome",
"name": "Player",
"request": "launch",
"url": "http://127.0.0.1:3000/player/index.html"
}
]
}
89 changes: 56 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,58 +1,81 @@
# TextAdventureJS

A text based adventure engine written in Javascript.
This repo comes with a player that uses the engine as well as an editor to create games for it.
Allows you to create your own text adventure games and embed them into your website or share and play them with included demo player.

- Games are json files, that follow the textAdventureGameDatabase schema (tadb).
- Editor folder contains an editor that allows to create games with a GUI.
- Player folder contains a demo player that shows how to integreate the engine

The library and the player are written in pure JavaScript.
The editor uses jquery.
The editor uses jQuery.

## Player
<img src="./docs/player.gif" width="400" alt="Animated demo for the textAdventureJS player" />
<img src="./shared/player.gif" width="400" alt="Animated demo for the textAdventureJS player" />

Try it here: https://dak0r.github.io/TextAdventureJS/player/

## Editor

This repo also provides a full editor including debugger functionality for creating your own games:

<img src="./docs/editor.jpg" width="600" alt="Animated demo for the textAdventureJS player" />
<img src="./shared/editor.jpg" width="600" alt="Animated demo for the textAdventureJS player" />

Try it here: https://dak0r.github.io/TextAdventureJS/editor/

## Usage

A barebones example that uses jquery:

Usage is simple: the engine needs to be initilized with a JS functions that allows the engine to write output and to clear all written output. Then any compatible game file can be loaded:
```js
// Defines where to write output
function witeLine(outputLine) {
$("#gameLog").append(outputLine + "<br />");
}
// Clears written output from the area
function clearArea() {
$("#gameLog").html("");
}
// Read user input
function readInput() {
const inputText = $("#inputField").val().trim();
writeLine(inputText);
textAdv.input(inputText);
$("#inputField").val("");
}

// Add event handler for reading user input:
$("#submit").click(function() { readInput(); });

// Init textAdventureJS and load a game
var textAdvEngine = new textAdventureEngine(witeLine, clearArea);
textAdvEngine.loadDatabaseFromFile("game.json");
var textAdvEngine = new textAdventureEngine(writeLine, clearArea);
textAdvEngine.loadDatabaseFromFile(
"https://dak0r.github.io/TextAdventureJS/games/new_project.tadb.json"
);
```
with this html elements:
A minimalistic working example, which uses jquery to keep it short:

```html
<div id="gameLog"></div>
<input id="inputField" type="text"/>
<button id="submit">Submit</button>
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://dak0r.github.io/TextAdventureJS/textAdventure.js"></script>
<script>
$(document).ready(function () {
// Defines where to write output
function writeLine(outputLine) {
$("#gameLog").append(outputLine + "<br />");
}
// Clears written output from the area
function clearArea() {
$("#gameLog").html("");
}
// Read user input
function readInput() {
const inputText = $("#inputField").val().trim();
writeLine(inputText);
textAdvEngine.input(inputText);
$("#inputField").val("");
}

// Add event handler for reading user input:
$("#submitButton").click(function () {
readInput();
});

// Init textAdventureJS and load a game
var textAdvEngine = new textAdventureEngine(writeLine, clearArea);
textAdvEngine.loadDatabaseFromFile(
"https://dak0r.github.io/TextAdventureJS/games/new_project.tadb.json"
);
});
</script>
</head>
<body>
<div id="gameLog" style="width: 600px; height: 500px"></div>
<input id="inputField" style="width: 500px" type="text" />
<button id="submitButton">Submit</button>
</body>
</html>
```

See [player/index.html](./player/index.html) for a more complex example.
Expand Down
189 changes: 0 additions & 189 deletions Templates/new_project.tadb.json

This file was deleted.

14 changes: 7 additions & 7 deletions __tests__/textAdventureEngine.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ const path = require("path");

let sampleGame;
let found = false;
const p = path.join(__dirname, "..", "templates", "new_project.tadb.json");
const p = path.join(__dirname, "..", "games", "new_project.tadb.json");
if (fs.existsSync(p)) {
sampleGame = JSON.parse(fs.readFileSync(p, "utf8"));
found = true;
} else {
throw new Error("Could not find templates/new_project.tadb.json.");
throw new Error("Could not find sample game at " + p);
}
const TextAdventureEngine = require("../textAdventure.js");

Expand Down Expand Up @@ -52,18 +52,18 @@ describe("textAdventureEngine parser & action integration tests (using template)
test("take object moves it to inventory and removes from location", () => {
engine.input("take object");
const gs = engine.devGetGameState();
expect(gs.inventory).toContain("template_object_pickedUp");
expect(gs.inventory).toContain("demo_object_pickedUp");
const objects = gs.locations[gs.currentLocation].objects;
expect(objects).not.toContain("template_object");
expect(objects).not.toContain("demo_object");
});

test("drop object returns it to location", () => {
engine.input("take object");
engine.input("drop object");
const gs = engine.devGetGameState();
expect(gs.inventory).not.toContain("template_object_pickedUp");
expect(gs.inventory).not.toContain("demo_object_pickedUp");
expect(gs.locations[gs.currentLocation].objects).toContain(
"template_object"
"demo_object"
);
});

Expand Down Expand Up @@ -156,7 +156,7 @@ describe("textAdventureEngine parser & action integration tests (using template)
);
engine2.loadDatabaseFromObject(sampleGame);
expect(engine2.devGetGameState().inventory).toContain(
"template_object_pickedUp"
"demo_object_pickedUp"
);
});
});
22 changes: 0 additions & 22 deletions css/channel-shift.css

This file was deleted.

Loading