Creating a Framework for OOC Events in a Mobile Text Adventure

Coding Begins: GSoC at Systers

Cady Holmes
3 min readMay 29, 2018

I’ve started working on a class to handle story sequences for our game, PowerUp.

Hey, this is an intro sequence!

I think of it as more of a design challenge than a coding challenge. I want to make sure it’s easy to use, and has minimal impact on the rest of the app functionality. I want other people to think “Hey that’s not so bad. I could create content for this and add it.”

So here’s the checklist:

  • It should be able to be added to the view hierarchy anywhere in the app, and it should be as simple as possible to do so.
  • Sequences should be customizable, but the data model should be simple to read and design around.
  • The heavy lifting will be done by the class itself. Another developer shouldn’t need a three-page wiki to implement a sequence.

This isn’t that big of a deal, but it needs to be done right. I absolutely hate complicated code patterns that render cool tools bulky and unusable.

1 In order to make it easy-to-use and immediately familiar to other iOS developers, StorySequencePlayer is a subclass of UIView. It doesn’t need to be a UIViewController, even though the class is a controller. The plan is to simplify its lifecycle by presenting the view as an overlay, and using a delegate to handle dismissal when it’s finished presenting content.

Externally, the only code to add to existing view controllers should look pretty familiar:

// get a model, initialize, and add to the viewfunc startSequence() {
guard let model = getTheModel() else { return }
let view = StorySequencePlayer(delegate: self, model: model)
self.view.addSubview(view)
}
// this delegate method is called when the last step is dismissedfunc sequenceDidFinish(sender: StorySequencePlayer) {
sender.removeFromSuperview()
}

2 I want to make sure these story sequences can have some character. They should be customizable, but in a way that is still easy to create content. I’m managing this with defined structs for Events and Steps. A Sequence is a dictionary of these structs, and it’s the model to pass to StorySequencePlayer.

// An Event is four values: text, image, position, and animation.Event(txt: "Hey, this is an intro sequence!",
img: testChar.happy,
pos: pos.near,
ani: ani.shake)
// A Step is two Events: left and right.Step(lftEvent, rgtEvent)// And a Sequence is a dictionary of Steps.Sequence([
0: Step(lftEvent, rgtEvent),
1: Step(lftEvent, nil)
])

Change the values between steps, and the scene will update.

3 StorySequencePlayer handles the rest. It creates its own full-window subview and programmatically lays out the rest of the subviews. It adds a blurred background and starts stepping through the Sequence once added to a superview. It handles its own interactions, and the model passes all the media information. It mostly handles its own animations, but it uses a dependency class I’m writing to help with animations in the app. I’ll share that soon.

I’m off to a running start here. I’ve got this and the controller for popup events stable, and I think it’s all coming along. Moving forward, I’m sure I’ll think of more optimizations. I’ll almost certainly make some design changes.

You can check out some code at this gist.

--

--