Last active
August 29, 2015 14:27
-
-
Save mizchi/329ed0b6f1cb33ffdb76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Actor | |
constructor: -> | |
@_id = (~~(Math.random() * 1000)).toString() | |
defineAction: -> | |
throw 'override me' | |
class Monster extends Actor | |
defineAction: (env) -> | |
{actionId: 'attack'} | |
class ControllableActor extends Actor | |
defineAction: (env) -> new Promise (done) -> | |
env.registerInputHandler (input) -> | |
done({actionId: 'action'}) | |
class Environment | |
constructor: -> | |
@actors = [ | |
new Monster 0 | |
new Monster 1 | |
new ControllableActor 1 | |
new Monster 1 | |
] | |
@actorCursor = -1 | |
@turn = 0 | |
@waiting = false | |
registerInputHandler: (f) -> | |
@wainting = true | |
@_inputHandler = (args...) -> | |
@wainting = false | |
f(args...) | |
resolveInput: (input) -> | |
@_inputHandler(input) | |
delete @_inputHandler | |
updateNext: -> | |
throw 'Input locked' if @wainting | |
if @actors[@actorCursor + 1] | |
@actorCursor += 1 | |
else | |
@actorCursor = 0 | |
@turn += 1 | |
current = @actors[@actorCursor] | |
current.defineAction(@) | |
turnEnded: -> | |
@actorIndex is @actors.length - 1 | |
processAction: (input) -> | |
# event handling | |
switch input.actionId | |
when 'attack' | |
console.log('attack') | |
env = new Environment | |
mainloop = -> | |
console.log('loop') | |
ret = env.updateNext() | |
if ret instanceof Promise | |
ret.then (action) -> | |
env.processAction(action) | |
mainloop() | |
else | |
env.processAction(ret) | |
mainloop() | |
do mainloop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment