vimtronner
Version:
A multi-player Vim trainer.
175 lines (153 loc) • 5.54 kB
text/coffeescript
require '../define_property'
playerAttributes = require './player_attributes'
directions = require './directions'
Wall = require './wall'
CYCLE_STATES = {
RACING: 0
EXPLODING: 1
DEAD: 2
WINNER: 3
INSERTING: 4
}
DIRECTIONS_TO_WALL_TYPES = {}
DIRECTIONS_TO_WALL_TYPES[directions.UP] = {}
DIRECTIONS_TO_WALL_TYPES[directions.UP][directions.UP] = Wall.WALL_TYPES.NORTH_SOUTH
DIRECTIONS_TO_WALL_TYPES[directions.UP][directions.DOWN] = Wall.WALL_TYPES.NORTH_SOUTH
DIRECTIONS_TO_WALL_TYPES[directions.UP][directions.LEFT] = Wall.WALL_TYPES.NORTH_EAST
DIRECTIONS_TO_WALL_TYPES[directions.UP][directions.RIGHT] = Wall.WALL_TYPES.NORTH_WEST
DIRECTIONS_TO_WALL_TYPES[directions.DOWN] = {}
DIRECTIONS_TO_WALL_TYPES[directions.DOWN][directions.UP] = Wall.WALL_TYPES.NORTH_SOUTH
DIRECTIONS_TO_WALL_TYPES[directions.DOWN][directions.DOWN] = Wall.WALL_TYPES.NORTH_SOUTH
DIRECTIONS_TO_WALL_TYPES[directions.DOWN][directions.LEFT] = Wall.WALL_TYPES.SOUTH_EAST
DIRECTIONS_TO_WALL_TYPES[directions.DOWN][directions.RIGHT] = Wall.WALL_TYPES.SOUTH_WEST
DIRECTIONS_TO_WALL_TYPES[directions.LEFT] = {}
DIRECTIONS_TO_WALL_TYPES[directions.LEFT][directions.UP] = Wall.WALL_TYPES.SOUTH_WEST
DIRECTIONS_TO_WALL_TYPES[directions.LEFT][directions.DOWN] = Wall.WALL_TYPES.NORTH_WEST
DIRECTIONS_TO_WALL_TYPES[directions.LEFT][directions.LEFT] = Wall.WALL_TYPES.EAST_WEST
DIRECTIONS_TO_WALL_TYPES[directions.LEFT][directions.RIGHT] = Wall.WALL_TYPES.EAST_WEST
DIRECTIONS_TO_WALL_TYPES[directions.RIGHT] = {}
DIRECTIONS_TO_WALL_TYPES[directions.RIGHT][directions.UP] = Wall.WALL_TYPES.SOUTH_EAST
DIRECTIONS_TO_WALL_TYPES[directions.RIGHT][directions.DOWN] = Wall.WALL_TYPES.NORTH_EAST
DIRECTIONS_TO_WALL_TYPES[directions.RIGHT][directions.LEFT] = Wall.WALL_TYPES.EAST_WEST
DIRECTIONS_TO_WALL_TYPES[directions.RIGHT][directions.RIGHT] = Wall.WALL_TYPES.EAST_WEST
class Cycle
: CYCLE_STATES
constructor: (attributes={})->
= attributes.player ? playerAttributes[0]
= 0
= 0
= attributes.direction ? attributes.player?.direction ? directions.LEFT
= attributes.number ? attributes.player?.number ? 1
= attributes.color ? attributes.player?.color ? 1
= CYCLE_STATES.RACING
= attributes.game
= 0
= false
= if attributes.walls?
(new Wall(wall) for wall in attributes.walls)
else
[]
'inserting', get: ->
== CYCLE_STATES.INSERTING
'racing', get: ->
== CYCLE_STATES.RACING
'active', get: ->
or
navigate: (movement) ->
if and .isStarted
.touch()
switch movement
when 27
= CYCLE_STATES.RACING if
when 105
= CYCLE_STATES.INSERTING if
when 106
unless
when 107
unless
when 104
unless
when 108
unless
else if .isWaiting or .isRestarting or .isFinished
.touch()
switch movement
when 27
= false
when 105
= true
= CYCLE_STATES.RACING
step: ->
switch
when CYCLE_STATES.EXPLODING
when CYCLE_STATES.DEAD
return
else
movingStep: ->
if
switch
when directions.UP
-= 1 unless == 0
when directions.DOWN
+= 1 unless == (.height - 1)
when directions.LEFT
-= 1 unless == 0
when directions.RIGHT
+= 1 unless == (.width - 1)
addWall: ->
.push new Wall({
x:
y:
type:
direction:
})
explosionStep: =>
if <= 10
++
else
= CYCLE_STATES.DEAD
checkCollisionWith: (object)->
== object.x and == object.y
checkCollisions: (cycles)->
if == CYCLE_STATES.RACING or == CYCLE_STATES.INSERTING
bottomWallY = (.height - 1)
rightWallX = (.width - 1)
if ( <= 0 or <= 0 or >= bottomWallY or >= rightWallX)
return
for cycle in cycles
unless cycle is @
if
return
for wall in cycle.walls
if
return
triggerCollision: ->
= CYCLE_STATES.EXPLODING
.length = 0
nextWallType: ->
lastWallDirection = [.length - 1]?.direction ?
DIRECTIONS_TO_WALL_TYPES[lastWallDirection][]
turnLeft: -> = directions.LEFT unless is directions.RIGHT
turnRight: -> = directions.RIGHT unless is directions.LEFT
turnUp: -> = directions.UP unless is directions.DOWN
turnDown: -> = directions.DOWN unless is directions.UP
makeWinner: ->
= Cycle.STATES.WINNER
toJSON: -> {
number:
x:
y:
color:
state:
direction:
explosionFrame:
walls: (wall.toJSON() for wall in )
ready:
}
module.exports = Cycle