Wednesday, September 4, 2013

Coroutines, Conversations and Camera Control

In the quest to build an actual level, I have kind of been bouncing all over the place with what I'm working on.  On one hand, it feels a little random and undirected.  On the other hand, there hasn't really been any effort that I would call wasted.  And if I had a third hand, I would say, at least I'm having fun.  :)

In any case, before you can really build a level editor, you need to know what data it is you want to edit.  While trying to rough in a basic level by hand (i.e. editing text attribute files), I realized there are a couple of fairly key components I'm missing.

The first one is character dialogue.  RPG-style conversations and dialogue are one of the key things I want in the game.  I had this working in Lua months ago, but I was using coroutines, so it doesn't exactly port easily to C++.

Basically, I would like to write a script like this:

say("Luke: You've picked one up...watch it!" )
say("Biggs: I can't see it! He's on me tight, I can't shake him.")
say("Luke: I'll be right there.")
set_nav_point(0)

The call to say(...) shouldn't actually return until you're done displaying the dialogue, which will take multiple frames.  With coroutines this is easy - just run the whole script in a coroutine.  Call resume() during the game's update and yield() in the say() function after setting up the frame's dialogue.

There are ways to get coroutines working in C++.  Getting a stack frame allocated for your coroutine seems to be a bit of a messy business though, so I didn't bother with that and just did it in the stupidest way possible:  Each coroutine just creates a new thread two thread event handles.  The thread waits on one of the handles.  The coroutine's resume() function just signals the thread to continue and waits on the other handle.  The yield() function just signals the main thread to continue and waits on the first handle again.

So, that took very little time to write and seems to work great. 

The second thing I need, as hinted above, is some sort of navigation system.  Because the other part of the game is basically a scrolling shooter, I need a good way to get the camera to move around the world.  I've been going back and forth on whether the camera should navigate the world, or if it should be fixed near the origin and have "the world come to the player," and I'm currently leaning towards having it navigate through the world.

I had this working in Lua too, though I wasn't entirely happy with it - following a Bezier curve works, but it's too easy to make corners that feel kind of abrupt and unnatural.  I'm going try looking at using some of the guidance control ideas for navigating between points under constant angular acceleration to see if that looks/feels better.
 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.