move/
main.rs

1use ecs_rust::world::World;
2use ecs_rust::entity_manager::{EntityIdAccessor, EntityManager};
3use ecs_rust::component::Component;
4use ecs_rust::system::System;
5
6struct Namable {
7	name: &'static str
8}
9
10struct Position {
11	x: f32,
12	y: f32
13}
14
15struct Velocity {
16	x: f32,
17	y: f32
18}
19
20struct Step {
21	num: u32
22}
23
24struct PrintStepSystem;
25struct MoveSystem;
26struct PrintPositionSystem;
27
28impl Component for Namable {
29}
30
31impl Component for Position {
32}
33
34impl Component for Velocity {
35}
36
37impl Component for Step {
38}
39
40impl System for PrintStepSystem {
41	fn update(&mut self, manager: &mut EntityManager, _accessor: &mut EntityIdAccessor) {
42		let steps = manager.borrow_components_mut::<Step>().unwrap();
43		for step in steps.iter_mut() {
44			step.num += 1;
45			println!("Step {}", step.num);
46		}
47	}
48}
49
50impl System for MoveSystem {
51	fn update(&mut self, manager: &mut EntityManager, accessor: &mut EntityIdAccessor) {
52		let entity_ids = accessor.borrow_ids_for_pair::<Velocity, Position>(manager).unwrap();
53		for id in entity_ids.iter() {
54			let (velocity, mut position) = manager.borrow_component_pair_mut::<Velocity, Position>(*id).unwrap();
55			position.x += velocity.x;
56			position.y += velocity.y;
57		}
58	}
59}
60
61impl System for PrintPositionSystem {
62	fn update(&mut self, manager: &mut EntityManager, accessor: &mut EntityIdAccessor) {
63		let entity_ids = accessor.borrow_ids_for_pair::<Namable, Position>(manager).unwrap();
64		for id in entity_ids.iter() {
65			let name = manager.borrow_component::<Namable>(*id).unwrap();
66			let position = manager.borrow_component::<Position>(*id).unwrap();
67			println!("{} is at ({}, {})", name.name, position.x, position.y);
68		}
69	}
70}
71
72fn main() {
73	let mut world = World::new();
74
75	world
76		.register_component::<Step>()
77		.register_component::<Namable>()
78		.register_component::<Position>()
79		.register_component::<Velocity>();
80
81	{
82		let entity_id = world.create_entity();
83		world
84			.add_component_to_entity(entity_id, Step {num: 0});
85	}
86
87	{
88		let entity_id = world.create_entity();
89		world
90			.add_component_to_entity(entity_id, Namable {name: "Alice"})
91			.add_component_to_entity(entity_id, Position {x: 0.0, y: 0.0})
92			.add_component_to_entity(entity_id, Velocity {x: 1.0, y: 2.0});
93	}
94
95	{
96		let entity_id = world.create_entity();
97		world
98			.add_component_to_entity(entity_id, Namable {name: "Bob"})
99			.add_component_to_entity(entity_id, Position {x: 0.0, y: 0.0})
100			.add_component_to_entity(entity_id, Velocity {x: -2.0, y: 1.0});
101	}
102
103	{
104		// Unmovable
105		let entity_id = world.create_entity();
106		world
107			.add_component_to_entity(entity_id, Namable {name: "Rock"})
108			.add_component_to_entity(entity_id, Position {x: 0.0, y: 0.0});
109	}
110
111	world
112		.add_system(PrintStepSystem {})
113		.add_system(MoveSystem {})
114		.add_system(PrintPositionSystem {});
115
116	for _i in 0..3 {
117		world.update();
118	}
119}