Permalink
Browse files

ADD extract DrawQueue from Player

and add path simplification
  • Loading branch information...
oelmekki committed Mar 25, 2012
1 parent 68ccf8f commit f843942e5d81a1cd0e25c7943366fc2523b43592
View
@@ -56,7 +56,11 @@
return this.change();
},
change: (function() {
- return this.fireEvent('change', [Object.values(this.players)]);
+ return this.fireEvent('change', [
+ Object.values(this.players).map(function(player) {
+ return player.toJson();
+ })
+ ]);
}).protect(),
noCollision: (function(current_id, current_player) {
var collide,
@@ -0,0 +1,29 @@
+(function() {
+
+ exports.DrawQueue = new Class({
+ initialize: function() {
+ this.queue = [];
+ return this;
+ },
+ addPosition: function(new_position) {
+ this.queue.push(new_position);
+ return this.simplify();
+ },
+ reset: function() {
+ return this.queue = [];
+ },
+ simplify: (function() {
+ var positions, start_index;
+ if (this.queue.length > 2) {
+ start_index = this.queue.length - 3;
+ positions = this.queue.slice(start_index, start_index + 3);
+ if (positions.length > 2) {
+ if ((positions[2].x === positions[1].x && positions[2].x === positions[0].x) || (positions[2].y === positions[1].y && positions[2].y === positions[0].y)) {
+ return this.queue.splice(start_index, 3, positions[0], positions[2]);
+ }
+ }
+ }
+ }).protect()
+ });
+
+}).call(this);
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -8,6 +8,7 @@ describe 'Board', ->
create: true
findNewPosition: true
move: true
+ toJson: true
}
@board = new Board( dependencies: { Player: @Player })
@@ -0,0 +1,33 @@
+DrawQueue = require( '../src/DrawQueue' ).DrawQueue
+
+describe 'DrawQueue', ->
+ beforeEach ->
+ @draw_queue = new DrawQueue()
+
+ describe 'addPosition()', ->
+ describe 'with empty list', ->
+ beforeEach ->
+ @draw_queue.addPosition( x: 10, y: 20 )
+
+ it 'should add new position', ->
+ expect( @draw_queue.queue.getLast() ).toEqual( x: 10, y: 20 )
+
+
+ describe 'with already populated list', ->
+ beforeEach ->
+ @draw_queue.queue = [ { x: 10, y: 20 }, { x: 10, y: 25 } ]
+ @draw_queue.addPosition( x: 10, y: 30 )
+
+ it 'should simplify paths', ->
+ expect( @draw_queue.queue ).toEqual( [ { x: 10, y: 20 }, { x: 10, y: 30 } ] )
+
+ describe 'reset()', ->
+ beforeEach ->
+ @draw_queue.queue = [ { x: 10, y: 20 }, { x: 20, y: 30 } ]
+ @draw_queue.reset()
+
+ it 'should empty position list', ->
+ expect( @draw_queue.queue ).toEqual( [] )
+
+
+
@@ -1,8 +1,15 @@
-Player = require( '../src/Player' ).Player
+Player = require( '../src/Player' ).Player
+DrawQueue = require( '../src/DrawQueue' ).DrawQueue
describe 'Player', ->
beforeEach ->
- @player = new Player()
+ @DrawQueue = getMock 'DrawQueue', {
+ initialize: true
+ addPosition: true
+ reset: true
+ }
+
+ @player = new Player( dependencies: { DrawQueue: @DrawQueue } )
describe 'positionateRandom()', ->
beforeEach ->
@@ -23,7 +30,7 @@ describe 'Player', ->
expect( @player.color ).toMatch( /rgb\( \d+, \d+, \d+ \)/ )
it "should create draw queue", ->
- expect( @player.drawn ).toEqual( [] )
+ expect( @DrawQueue.prototype.initialize.used ).toEqual( 1 )
describe 'findNewPosition()', ->
@@ -41,6 +48,9 @@ describe 'Player', ->
describe 'move()', ->
+ beforeEach ->
+ @player.draw_queue = new @DrawQueue()
+
describe 'moving along the edges', ->
describe 'when not drawing', ->
beforeEach ->
@@ -58,15 +68,14 @@ describe 'Player', ->
@player.x = 0
@player.y = 1
@player.drawing = true
- @player.drawn = [ { x: 0, y: 4 }, { x: 0, y: 3 }, { x: 0, y: 2 } ]
@player.move x: 0, y: 0
it 'should move player', ->
expect( @player.x ).toBe( 0 )
expect( @player.y ).toBe( 0 )
it 'should reset draw queue', ->
- expect( @player.drawn ).toEqual( [] )
+ expect( @player.draw_queue.reset.used ).toBe( 1 )
describe 'moving somewhere else than edges', ->
@@ -86,16 +95,14 @@ describe 'Player', ->
@player.x = 0
@player.y = 1
@player.drawing = true
- @player.drawn = []
@player.move x: 1, y: 1
it 'should move player', ->
expect( @player.x ).toBe( 1 )
expect( @player.y ).toBe( 1 )
it 'should add new position to draw queue', ->
- expect( @player.drawn[0].x ).toBe( 1 )
- expect( @player.drawn[0].y ).toBe( 1 )
+ expect( @player.draw_queue.addPosition.used ).toBe( 1 )
describe 'changeDirection()', ->
@@ -56,7 +56,7 @@ exports.Board = new Class {
change: ( ->
- @fireEvent 'change', [ Object.values( @players ) ]
+ @fireEvent 'change', [ Object.values( @players ).map( ( player )-> ( player.toJson() ) ) ]
).protect()
@@ -0,0 +1,29 @@
+exports.DrawQueue = new Class {
+ initialize: ->
+ @queue = []
+ @
+
+
+ addPosition: ( new_position ) ->
+ @queue.push( new_position )
+ @simplify()
+
+
+ reset: ->
+ @queue = []
+
+
+ # Protected
+
+
+ simplify: ( ->
+ if @queue.length > 2
+ start_index = @queue.length - 3
+ positions = @queue.slice( start_index, start_index + 3 )
+
+ if positions.length > 2
+ if ( positions[2].x == positions[1].x and positions[2].x == positions[0].x ) or ( positions[2].y == positions[1].y and positions[2].y == positions[0].y )
+ @queue.splice( start_index, 3, positions[0], positions[2] )
+
+ ).protect()
+}
@@ -1,7 +1,15 @@
exports.Player = new Class {
- initialize: ->
- @step = configuration.step
- @run_step = configuration.run_step
+ Extends: require( './Base' ).Base
+
+ options:
+ dependencies:
+ DrawQueue: require( './DrawQueue' ).DrawQueue
+
+ initialize: ( options ) ->
+ @parent( options )
+
+ @step = configuration.step
+ @run_step = configuration.run_step
for own attr, value of configuration.initial
@[ attr ] = value
@@ -23,8 +31,8 @@ exports.Player = new Class {
create: ->
- @color = 'rgb( ' + Number.random( 10, 255 ) + ', ' + Number.random( 10, 255 ) + ', ' + Number.random( 10, 255 ) + ' )'
- @drawn = []
+ @color = 'rgb( ' + Number.random( 10, 255 ) + ', ' + Number.random( 10, 255 ) + ', ' + Number.random( 10, 255 ) + ' )'
+ @draw_queue = new @DrawQueue()
findNewPosition: ->
@@ -58,10 +66,12 @@ exports.Player = new Class {
if @isOnSafeZone new_position
@x = new_position.x
@y = new_position.y
- @drawn = [] if @drawing
+ if @drawing
+ @draw_queue.reset()
+ @drawing = false
else if @drawing
- @drawn.push({ x: new_position.x, y: new_position.y })
+ @draw_queue.addPosition( x: new_position.x, y: new_position.y )
@x = new_position.x
@y = new_position.y
@@ -73,6 +83,15 @@ exports.Player = new Class {
@drawing = true if options.draw
+ toJson: ->
+ resp =
+ x: @x
+ y: @y
+ width: @width
+ height: @height
+ drawn: @draw_queue.queue
+
+
# Protected

0 comments on commit f843942

Please sign in to comment.