Created
April 16, 2015 20:29
-
-
Save iofjuupasli/edd6652975b8c174f716 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global React, flyd, R*/ | |
(function () { | |
'use strict'; | |
var Player = function (control) { | |
return function (players) { | |
var player = {}; | |
var possibleNewPosition = flyd.stream([control.move], function () { | |
return (player.position() || 0) + control.move(); | |
}); | |
var canMove = flyd.stream([possibleNewPosition], function () { | |
return !players().some(function (pl) { | |
return pl.position() === possibleNewPosition(); | |
}) || undefined; | |
}); | |
player.position = flyd.stream([canMove], function (position) { | |
return possibleNewPosition(); | |
}); | |
return player; | |
} | |
} | |
var moveFirst = flyd.stream(); | |
var moveSecond = flyd.stream(); | |
var playerJoin = flyd.stream(); | |
var players = flyd.stream([playerJoin], function (players) { | |
return (players() || []).concat(playerJoin()); | |
}); | |
var playerFirst = Player({ | |
move: moveFirst | |
})(players); | |
playerJoin(playerFirst); | |
var playerSecond = Player({ | |
move: moveSecond | |
})(players); | |
playerJoin(playerSecond); | |
moveFirst(1); | |
moveSecond(4); | |
moveFirst(3); | |
moveSecond(-3); | |
console.assert(players()[0].position() === 1); | |
console.assert(players()[1].position() === 4); | |
var Sheep = function () { | |
} | |
var Game = function () { | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment