Skip to content

Instantly share code, notes, and snippets.

@igul222
Created October 10, 2014 02:31
Show Gist options
  • Save igul222/8dd22f18e31c85f9db62 to your computer and use it in GitHub Desktop.
Save igul222/8dd22f18e31c85f9db62 to your computer and use it in GitHub Desktop.
Moves =
start: () ->
{type: 'start'}
class Players
constructor: (@game) ->
@teams: [
active: true
players: []
,
active: false
players: []
]
add: (id, name) ->
if @teams[0].players.length <= @teams[1].players.length
team = @teams[0]
else
team = @teams[1]
team.players.push {
id: id,
name: name,
active: false
dots: []
}
kill: (id) ->
for team in @teams
for player in team.players
if player.id == id
for dot in player.dots
dot.alive = false
@advanceTurn() if dot.active
remove: (id) ->
for team in @state.teams
team.players = _.reject(team.players, (p) -> p.id == id)
# Return the player with the given id, or undefined if none exists.
get: (id) ->
players = _.flatten(_.pluck(@teams, 'players'))
_.find(players, (p) -> p.id == id)
class Game
@new: (rand) ->
LobbyGame.new(rand)
constructor: (old, rand) ->
if old instanceof Game
@rand = old.rand
@players = old.players
else
@rand = rand
@players = new Players(this)
tick: ->
handleMove: (move) ->
switch move.type
when 'sendMessage'
@_sendMessage(move)
else
return this
class LobbyGame extends Game
@new: (rand) ->
new LobbyGame(rand)
constructor: (rand) ->
super(null, rand)
tick: ->
handleMove: (move) ->
switch move.type
when 'addPlayer'
@players.add(move.playerId, move.playerName)
return this
when 'removePlayer'
@players.remove(move.playerId)
return this
when 'start'
if !move.agentId? and @players.get(move.playerId)
return StartedGame.new(this)
else
return this
else
return super(move)
class StartedGame extends Game
@XMax: 25
@YMax: 15
@DotsPerPlayer: 2
@DotRadius: 1
@ObstacleCount: 10
@ObstacleRadiusMin: 0
@ObstacleRadiusMax: 5
@ObstaclePathResolution: 0.5
@AntiobstacleRadius: 1
@new: (old) ->
TypingFunctionGame.new(old)
constructor: (old) ->
super(old)
@_generateInitialPositions()
handleMove: (move) ->
@state = @state.handleMove(move) or
switch move.type
when 'removePlayer'
@game.players.kill(move.playerId)
return this
when 'fire'
return FiringGame.new(this, move)
else
return super(move)
class TypingFunctionGame extends StartedGame
@TurnTime: 60000 # ms
@new: (old) ->
new TypingFunctionGame(old)
constructor: (old) ->
super(old)
class FiringGame extends StartedGame
@FunctionAnimationSpeed: 0.005 # graph units per ms
@new: (old, fireMove) ->
new FiringGame(old, fireMove)
constructor: (old, fireMove) ->
super(old)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment