Last active
August 29, 2015 14:17
-
-
Save jaredonline/84237bb323f0d1ed2b60 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#![feature(core)] | |
extern crate rand; | |
extern crate core; | |
use std::sync::{Arc, Mutex}; | |
use std::thread; | |
use std::fmt::{Display, Formatter, Result}; | |
use core::ops::Deref; | |
use self::rand::{thread_rng}; | |
use self::rand::distributions::{IndependentSample, Range}; | |
trait UpdateComponent { | |
fn update(&mut self, &Point) -> Point; | |
} | |
struct RandomUpdateComponent; | |
impl RandomUpdateComponent { | |
fn new() -> RandomUpdateComponent { | |
RandomUpdateComponent | |
} | |
} | |
impl UpdateComponent for RandomUpdateComponent { | |
fn update(&mut self, start: &Point) -> Point { | |
let between = Range::new(0, 10u8); | |
let mut rng = thread_rng(); | |
Point::new( | |
start.x + between.ind_sample(&mut rng), | |
start.y + between.ind_sample(&mut rng) | |
) | |
} | |
} | |
struct Point { | |
x: u8, | |
y: u8 | |
} | |
impl Point { | |
fn new(x: u8, y: u8) -> Point { | |
Point { | |
x: x, | |
y: y | |
} | |
} | |
} | |
impl Display for Point { | |
fn fmt(&self, fmt: &mut Formatter) -> Result { | |
format!("({},\t{})", self.x, self.y).fmt(fmt) | |
} | |
} | |
unsafe impl Send for Point {} | |
struct Actor { | |
location: Point, | |
movement: Box<UpdateComponent + 'static>, | |
label: usize | |
} | |
impl Actor { | |
fn mutex(label: usize) -> Arc<Mutex<Actor>> { | |
Arc::new(Mutex::new(Actor::new(label))) | |
} | |
fn new(label: usize) -> Actor { | |
let point = Point::new(0, 0); | |
let compo = Box::new(RandomUpdateComponent::new()); | |
Actor { | |
location: point, | |
movement: compo, | |
label: label | |
} | |
} | |
fn update(&mut self) { | |
let location = self.movement.update(&self.location); | |
self.location = location; | |
} | |
} | |
impl Display for Actor { | |
fn fmt(&self, fmt: &mut Formatter) -> Result { | |
format!("{}:\tActor at {}", self.label, self.location).fmt(fmt) | |
} | |
} | |
unsafe impl Send for Actor {} | |
struct Renderer; | |
impl Renderer { | |
fn new() -> Renderer { Renderer } | |
fn render(&mut self, actor: Arc<Mutex<Actor>>) { | |
println!("{}", actor.lock().unwrap().deref()); | |
} | |
} | |
fn update(data: &Vec<Arc<Mutex<Actor>>>) { | |
let _ : Vec<_> = data.iter().map(|a| { | |
let actor = a.clone(); | |
thread::spawn(move || { | |
actor.lock().unwrap().update(); | |
}) | |
}).collect(); | |
} | |
fn render(data: &Vec<Arc<Mutex<Actor>>>, renderer: &mut Renderer) { | |
for actor in data { | |
let actor = actor.clone(); | |
renderer.render(actor); | |
} | |
} | |
fn main() { | |
let mut renderer = Renderer::new(); | |
let data = vec![ | |
Actor::mutex(1), Actor::mutex(2), | |
Actor::mutex(3), Actor::mutex(4), | |
Actor::mutex(5), Actor::mutex(6), | |
Actor::mutex(7), Actor::mutex(8), | |
Actor::mutex(9), Actor::mutex(10) | |
]; | |
// game loop | |
for _ in 0..3 { | |
// update | |
update(&data); | |
// render | |
render(&data, &mut renderer); | |
println!("next loop!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment