Last active
December 30, 2016 06:29
-
-
Save ympbyc/f9a7b339e8fa7f4f236b to your computer and use it in GitHub Desktop.
Pull-based Async Objects -- Demo
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
//1 dimentional, constant speed car | |
//private | |
function _Car (vision, address, speed, id) { | |
this.visual = {id: id, brake_light: false, address: address, _speed: speed + "mph", _car: this}; | |
this._vision = vision; | |
this._speed = speed; | |
this._brake = function () { | |
this.visual.brake_light = true; | |
}; | |
this._accelerate = function () { | |
this.visual.brake_light = false; | |
this.visual.address ++; | |
}; | |
this._too_close = function (carsp) { | |
var diff = carsp.address - this.visual.address; | |
return diff < 30 && diff > 0; | |
}; | |
this._ai = function () { | |
var self = this; | |
setInterval(function () { | |
var close_cars = self._vision; //cars in front | |
if (close_cars.some(function (carsp) { return carsp.brake_light; })) //see break light | |
self._brake(); | |
else if (close_cars.filter(self._too_close.bind(self)).length) //really close | |
self._brake(); | |
else self._accelerate(); | |
}, 3600 / (speed * 100) * 100); | |
}; | |
this._ai(); | |
} | |
//public | |
//vision :: [CarVisual] | |
//address :: Int | |
//speed :: Int -- miles/hour | |
//id :: String | |
function CarVisual (vision, address, speed, id) { | |
return new _Car(vision, address, speed, id).visual; | |
} | |
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
//cars :: [CarVisual] | |
function Road (cars) { | |
this.cars = cars; | |
this._init = function () { console.log("init road"); | |
var self = this; | |
function car_address (car) { return car.address; } | |
setInterval(function () { | |
var cars = _.sortBy(self.cars, function (c) { return - c.address; }); | |
for (var i in cars) { | |
for (var j in cars) { | |
if (i == j) continue; | |
var car_infront = _.max([cars[i], cars[j]], car_address); | |
var car_rear = _.min([cars[i], cars[j]], car_address); | |
var distance = car_infront.address - car_rear.address; | |
if (distance < 100 && distance > 0) | |
car_rear._car._vision.unshift(car_infront); | |
else { | |
car_rear._car._vision = _.without(car_rear.vision, car_infront); | |
} | |
} | |
} | |
}, 1000); | |
}; | |
this._init(); | |
} |
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
new Road([ | |
new CarVisual([], 300, 125, "A"), | |
new CarVisual([], 270, 70, "B"), | |
new CarVisual([], 200, 80, "C"), | |
new CarVisual([], 100, 100, "D") | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment