1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::any::Any;

trait Component {
    fn as_any(&self) -> &Any;
}

#[derive(Debug, Clone, Copy)]
struct Health {
    hp: i32,
}
impl Component for Health {
    fn as_any(&self) -> &Any {
        self
    }
}

#[derive(Debug, Clone, Copy)]
struct Position {
    x: i32,
    y: i32,
}
impl Component for Position {
    fn as_any(&self) -> &Any {
        self
    }
}
impl Position {
    fn set(&mut self, x: i32, y: i32) {
        self.x = x;
        self.y = y;
    }
}

#[derive(Debug, Clone, Copy)]
struct Motion {
    // velocity
    v_x: f32,
    v_y: f32,
    // acceleration
    a_x: f32,
    a_y: f32,
}
impl Component for Motion {
    fn as_any(&self) -> &Any {
        self
    }
}
impl Motion {
    fn new() -> Motion {
        return Motion {
            v_x: 0.0, v_y: 0.0,
            a_x: 0.0, a_y: 0.0,
        };
    }
    fn set_v(&mut self, a_x: f32, a_y: f32)  {
        self.a_x = a_x;
        self.a_y = a_y;
    }
    fn set_a(&mut self, v_x: f32, v_y: f32) {
        self.v_x = v_x;
        self.v_y = v_y;
    }
}

#[derive(Debug, Clone, Copy)]
struct Draw {

}

fn get_cmpnt<'a, T>(v: &'a Components) -> Option<T>
    where T: Component + Copy + 'static {
    for boxed_cmpnt in v {
        match boxed_cmpnt.as_any().downcast_ref::<T>() {
            Some(t) => return Some(*t),
            None => continue,
        }
    }
    return None;
}

fn set_cmpnt<T>(v: &mut Components, val: T)
    where T: Component + Copy + 'static {
    for boxed_cmpnt in v.iter_mut() {
        if let Some(_) = boxed_cmpnt.as_any().downcast_ref::<T>() {
            *boxed_cmpnt = Box::new(val);
        }
    }
}

type Components = Vec<Box<Component>>;

struct Screen {
    w: i32,
    h: i32,
}

fn moving(entity: &Components) {
    let motion = get_cmpnt::<Motion>(entity);
    let position = get_cmpnt::<Position>(entity);
    if let Some(m) = motion {
        if let Some(p) = position {
            let mut new_p = p;

        }
    }
}

fn rendering(screen: &Screen, entity: &Components) {
    for y in 0..screen.h {
        for x in 0..screen.w {
            let position = get_cmpnt::<Position>(entity);
            if let Some(p) = position {
                if p.x == x && p.y == y {
                    print!(".");
                } else {
                    print!(" ");
                }
            }
        }
        println!("");
    }
}

fn main() {
    let screen = Screen {w: 20, h: 20};
    let mut entity: Components = vec![];
    entity.push(Box::new(Health {hp: 0}));
    entity.push(Box::new(Position {x: 0, y: 1}));
    entity.push(Box::new(Motion::new()));

    let health = get_cmpnt::<Health>(&entity);
    if let Some(h) = health {
        println!("health: {:?}", h);
    }

    if let Some(h) = health {
        let mut new_h = h;
        new_h.hp = 20;
        set_cmpnt::<Health>(&mut entity, new_h);
    }

    let health = get_cmpnt::<Health>(&entity);
    if let Some(h) = health {
        println!("health: {:?}", h);
    }

    for i in 0..20 {
        let position = get_cmpnt::<Position>(&entity);
        if let Some(p) = position {
            println!("position: {:?}", p);

            let mut new_p = p;
            new_p.x += 1;
            set_cmpnt::<Position>(&mut entity, new_p);
        }
    }

    // Systems
    moving(&entity);
    rendering(&screen, &entity);
}