Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
maartene committed Feb 16, 2021
1 parent 95ed91e commit 4eaecc7
Show file tree
Hide file tree
Showing 10 changed files with 848 additions and 12 deletions.
17 changes: 15 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import PackageDescription

let package = Package(
name: "InkSwift",
platforms: [
.macOS(.v10_15),
.iOS(.v13),
.tvOS(.v13)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
Expand All @@ -20,9 +25,17 @@ let package = Package(
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "InkSwift",
dependencies: []),
dependencies: [],
resources: [
.process("ink.js")]
),
.testTarget(
name: "InkSwiftTests",
dependencies: ["InkSwift"]),
dependencies: ["InkSwift"],
resources: [
.process("test.ink.json"),
.process("compare.json")
]
)
]
)
97 changes: 96 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,98 @@
# InkSwift
Swift wrapper for the Ink narrative scripting language. Based on InkJS. Requires JavaScriptCore (so no Linux support).

A description of this package.
## Supported features
* Loading Ink stories `loadStory(json: String)`;
* Basic flow: continue story `continueStory()` and choices `chooseChoiceIndex(_ index: Int)`;
* Moving to knots/stitches `moveToKnitStitch(_ knot: String, stitch: String? = nil)`;
* Tag support. Read `currentTags` variable;
* Setting and getting variable values (supports strings, 32-bit integers and doubles);
* Loading and saving state `stateToJSON()` and `loadState(_ jsonDataString: String)`;
* Combine integration (subscribe to state changes, observe variables).

## Limitations
* InkSwift uses JavascriptCore. This means that only Apple platforms are supported. I'm working on Linux support using [SwiftJS](https://github.com/SusanDoggie/SwiftJS), but there is a [bug](https://github.com/SusanDoggie/SwiftJS/issues/1) that makes it unusable at this time.

## Getting started
### Regular XCode project


### Using SwiftPM
Add InkSwift as a dependency to `Package.swift`:




## Usage
Start by creating a InkStory
```
let story = InkStory()
```

Then load a story from a Ink JSON (you can use Inklecate or Inky to convert an .Ink file to a .json file.):
let storyJSON = ... //
story.loadStory(json: storyJSON)

A very cool





## Using Combine/SwiftUI
InkStory conforms to the `ObservableObject` protocol. This makes using it in Combine possible and SwiftUI very easy. A simple example SwiftUI view that can play an Ink story would contain:

### Import the SwiftInk package
Add
```
import SwiftInk
```

to ContentView.swift

### The ink story as a @StateObject
Add the following property to your ContentView:
`@StateObject var story = InkStory()`

### Add a function that loads the Ink story:
Note: change the filename to load to your own JSON file. Don't forget to add it to the project.

```
func loadStory() {
guard let url = Bundle.main.url(forResource: "test.ink", withExtension: "json") else {
fatalError("Could not find ink story file.")
}
guard let storyJSON = try? String(contentsOf: url) else {
fatalError("Could not load story file.")
}
story.loadStory(json: storyJSON)
}
```

### Create the body property
```
var body: some View {
VStack {
Text(story.currentText)
if story.canContinue {
Button("Continue") {
story.continueStory()
}
}
ForEach(story.options, id: \.index) { option in
Button(option.text) {
story.chooseChoiceIndex(option.index)
}
}
}.padding()
.onAppear {
loadStory()
}
}
```


## Licenced content
* The Ink runtime uses the official Ink Javascript port [InkJS](https://github.com/y-lohse/inkjs)
Loading

0 comments on commit 4eaecc7

Please sign in to comment.