Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
y-lohse committed Jul 10, 2016
2 parents 5a804a8 + ce21c7a commit 2a4df33
Show file tree
Hide file tree
Showing 18 changed files with 626 additions and 430 deletions.
4 changes: 3 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
test/
tests/
engine/
Gruntfile.js
Gruntfile.js
.gitattributes
24 changes: 16 additions & 8 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
module.exports = function(grunt) {

var version = 0.1;

var babel = require('rollup-plugin-babel');
var uglify = require('rollup-plugin-uglify');
var exposedFiles = ['engine/Story.js'];

// Project configuration.
Expand All @@ -15,16 +14,25 @@ module.exports = function(grunt) {
rollup: {
options: {
plugins: [
babel({ exclude: 'node_modules/**' }),
babel({
exclude: 'node_modules/**',
presets: ['es2015-rollup'],
}),
uglify(),
],
moduleId: 'inkjs',
moduleName: 'inkjs',
},
// amd: {
// options : { format: 'amd' },
// dest: 'dist/ink.amd.js',
// src: exposedFiles
// },
amd: {
options : { format: 'amd' },
dest: 'dist/ink.amd.js',
src: exposedFiles
},
umd: {
options : { format: 'umd' },
dest: 'dist/ink.umd.js',
src: exposedFiles
},
cjs: {
options : { format: 'cjs' },
dest: 'dist/ink.cjs.js',
Expand Down
109 changes: 88 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,110 @@ This is a javascript port of inkle's [ink](https://github.com/inkle/ink), a scri

inkjs should support pretty much everything the original version does. If you find any bugs, please report them here! The code has zero dependencies and it should work in node and all evergreen browsers.

### How do I use this…
## Getting started *browser version*

#### …in Node?

Install the package using `npm install inkjs`.

#### …in the browser?
### Installation

Grab the most convenient format for you from the [release page](https://github.com/y-lohse/inkjs/releases). If in doubt, use the iife version.

It's also available on bower: `bower install inkjs`

### And then?
### Loading inkjs

Once you grab a hold of the module, it exposes a `Story` class. It should work like [the reference implementation](https://github.com/inkle/ink/blob/master/Documentation/RunningYourInk.md).
If you're using the IIFE version, add the script to your page and it will create aglobal object called `inkjs`. If you're using the AMD/UMD version, you need to require the package using your prefered mechanism.

In node, you would do something like this:
The `inkjs` has a property called `Story`. This is the main class we will interact with.

```
var Story = require('inkjs').Story;
var fs = require('fs');
### Loading a json file

First you need to turn your ink file into a json file [as described here](https://github.com/inkle/ink#using-inklecate-on-the-command-line). You can then load the json file using an ajax request. You can use jquery or a simple fetch:

var inkFile = fs.readFileSync('inkfile.json', 'UTF-8');
var s = new Story(inkFile);
```
fetch('path/to/ink_file.json')
.then(function(response){
return response.text();
})
.then(function(ink){
//ink now contains your ink_file.json
});
```

Please note that if you are viewing your html page using the `file://` protocol (ie. you just double clicked the html file), you may run into [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) problems. In that case, you may want to load it using an html file input or directly embed the json into your page.

### Starting a story

Or in the browser, you'd do:
Bringing it all together, we get something like this:

```
var Story = inkjs.Story;
fetch('inkfile.json')
.then(response => {
fetch('path/to/ink_file.json')
.then(function(response){
return response.text();
})
.then(data => {
var s = new Story(data);
.then(function(ink){
//create a story
var inkStory = new inkjs.Story(ink);
//run it
var content = inkStory.ContinueMaximally();
//add the content on the page
document.querySelector('#display').html = content;
//etc
});
```

After that, just use the API as described in the reference documentation. The functions are nammed exactly the same.
From there on, you can follow [the official guide](https://github.com/inkle/ink/blob/master/Documentation/RunningYourInk.md#getting-started-with-the-runtime-api). All functions are named exactly the same.

For an example implementation, you can refer to the source code of the demo page [here](https://github.com/y-lohse/inkjs/blob/gh-pages/index.html).

## Getting started *node version*

### Installation

Install using npm: `npm install inkjs`.

### Loading inkjs

Require the module: `var Story = require('inkjs').Story;`.

### Loading a json file

You can load the json file using a simple call to `require`:

```
var json = require('./ink_file.json');
```

You can also load it using `fs`. In that case, please note that inklecate outputs a json file encoded **with** BOM, and node isn't very good at handling that.

```
var fs = require('fs');
var json = fs.readFileSync('./ink_file.json', 'UTF-8').replace(/^\uFEFF/, '');//strips the BOM
```

### Starting a story

Now that you have a `Story` object and a json file, it's time to bring it all together:

```
var inkStory = new Story(json);
console.log(inkStory.ContinueMaximally());
//etc
```

From there on, you can follow [the official guide](https://github.com/inkle/ink/blob/master/Documentation/RunningYourInk.md#getting-started-with-the-runtime-api). All functions are named exactly the same.

For an example implementation, you can refer to the source code of the test file [here](https://github.com/y-lohse/inkjs/blob/master/test/simple.js).

## Compatibility with older node and browser versions

Generally speaking, everything should work just fine on any modern-ish browser and in node v4 or above. The only thing you should be careful about is if you're [getting and setting ink variables](https://github.com/inkle/ink/blob/master/Documentation/RunningYourInk.md#settinggetting-ink-variables). In anything that does not [support Proxies](https://kangax.github.io/compat-table/es6/) (basically node v5, IE 11, Safari 9 and everything below), you can't directly read and write variables to the story state. Instead you will have to use the `$` function:

```
_inkStory.variablesState.$("player_health", 100);
var health = _inkStory.variablesState.$("player_health");
```


34 changes: 34 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env node

var spawn = require('child_process').spawn;

var command = '';

switch (process.platform){
case 'darwin':
command = './' + __dirname + '/inklecate';
break;
case 'win32':
command = __dirname + '/inklecate.exe';
break;
default:
command = 'mono ' + __dirname + '/inklecate.exe';
break;
}

var child = spawn(command, process.argv);

process.stdin.pipe(child.stdin);

child.stdout.on('data', function(chunk){
console.log(chunk.toString());
});

child.stderr.on('data', function(chunk){
console.warn(chunk.toString());
process.exit();
});

child.on('exit', function(){
process.exit();
});
Binary file added bin/ink-engine.dll
Binary file not shown.
Loading

0 comments on commit 2a4df33

Please sign in to comment.