Skip to content

Instantly share code, notes, and snippets.

@igul222
Created October 8, 2014 20:11
Show Gist options
  • Save igul222/6f561208cd2a2085f9c5 to your computer and use it in GitHub Desktop.
Save igul222/6f561208cd2a2085f9c5 to your computer and use it in GitHub Desktop.
class Game
@XMax: 25
@YMax: 15
@DotsPerPlayer: 2
@FunctionAnimationSpeed: 0.005 # graph units per ms
@DotRadius: 1
@TurnTime: 60000 # ms
@ObstacleCount: 10
@ObstacleRadiusMin: 0
@ObstacleRadiusMax: 5
@ObstaclePathResolution: 0.5
@AntiobstacleRadius: 1
constructor: (@data) ->
@data or=
rand: Math.random()
moves: []
pushMove: (move, agentId, time) ->
move.t = time || Math.round(Date.now())
move.agentId = agentId
@data.moves.push(move)
# Add a player (with given id and name) to the team with fewer players.
# Only the server can issue this move.
@addPlayer: (playerId, playerName) ->
return {type: 'addPlayer', playerId: playerId, playerName: playerName}
# Remove the player with the given id from the game if he exists.
# Players can only remove themselves.
@removePlayer: (playerId) ->
return {type: 'removePlayer', playerId: playerId}
# Start the game on behalf of the player with the given id.
# Only the server can issue this move, and only on behalf of a player
# in the game. The game must not already have been started.
@start: (playerId)->
return {type: 'start', playerId: playerId}
# Fire the given function from the currently active dot. Only the currently
# active player can issue this move.
@fire: (expression) ->
return {type: 'fire', expression: expression}
# Set the location of dot at the given path. For testing use only.
@setDotLocation: (dotPath, location) ->
return {type: 'setDotLocation', location: location, dotPath: dotPath}
# Send chat message from player
# Any player can make this move at anytime
@sendMessage: (message) ->
return {type: 'sendMessage', message: message}
class GameStateGenerator
constructor: (@data) ->
@time = (@data.moves[0].t - 1)
@nextMoveIndex = 0
@nextMoveTime = @data.moves[0].t
@state = new GameState(@data.rand)
# only ever call this with monotonically increasing time values!
generateAtTime: (time) ->
while @time < time
@time++
@state.tick()
if @time == @nextMoveTime
@state.handleMove(@data.moves[@nextMoveIndex])
if @nextMoveIndex + 1 < @data.moves.length
@nextMoveIndex++
@nextMoveTime = @data.moves[@state.nextMove].t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment