Skip to content

Instantly share code, notes, and snippets.

@demoth
Last active August 29, 2015 14:18
Show Gist options
  • Save demoth/451cb0c7bcee7bd6da1d to your computer and use it in GitHub Desktop.
Save demoth/451cb0c7bcee7bd6da1d to your computer and use it in GitHub Desktop.
some pieces of games
void update(float tpf) {
super.update(tpf)
// lets process a message
ClientMessage mess = messages.poll()
if (mess != null) {
switch (mess.class) {
case ActionRequestMessage:
if (players.get(mess.id).name != null) // if logged in
players.get(mess.id).actions << (mess as ActionRequestMessage)
break
case LoginMessage:
def m = mess as LoginMessage
if (LoginService.authorized(m.username, m.password)) {
players.get(m.id).name = m.username
actors << players.get(m.id)
}
break
}
}
if (actors.isEmpty())
return
time += tpf
if (time >= roundTimeout || notReadyActors.isEmpty()) {
actors.each { it.energy = Math.max(MAX_ENERGY, it.speed + it.energy) }
time = 0
notReadyActors.clear()
notReadyActors += actors
log.info "Round ended: $roundNo"
roundNo++
}
notReadyActors.each { performActions(it) }
notReadyActors.removeAll { it.energy == 0 }
}
def performActions(Actor actor) {
actor.actions.each { action ->
if (actor.energy >= action.type.cost) {
switch (action.type) {
case ActionType.walk:
if (action.target.isNear(actor.pos)
&& terrain[action.target.y][action.target.x] == 1)
actor.pos = action.target
break
case ActionType.wait:
break
}
actor.energy -= action.type.cost
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment