Skip to content

Instantly share code, notes, and snippets.

@iofjuupasli
Created April 16, 2015 20:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iofjuupasli/edd6652975b8c174f716 to your computer and use it in GitHub Desktop.
Save iofjuupasli/edd6652975b8c174f716 to your computer and use it in GitHub Desktop.
/*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