1 pub mod collision; |
1 pub mod collision; |
2 pub mod common; |
2 pub mod common; |
3 mod data; |
3 mod data; |
4 mod grid; |
4 mod grid; |
5 pub mod physics; |
5 pub mod physics; |
6 pub mod time; |
|
7 |
6 |
8 use integral_geometry::Size; |
7 use integral_geometry::Size; |
9 use land2d::Land2D; |
8 use land2d::Land2D; |
10 |
9 |
11 use crate::{ |
10 use crate::{ |
12 collision::CollisionProcessor, |
11 collision::CollisionProcessor, |
13 common::{GearAllocator, GearId, Millis}, |
12 common::{GearAllocator, GearId, Millis}, |
14 data::{DataIterator, GearDataManager, TypeIter}, |
13 data::{DataIterator, GearDataManager, TypeIter}, |
15 physics::PhysicsProcessor, |
14 physics::PhysicsProcessor, |
16 time::TimeProcessor, |
|
17 }; |
15 }; |
18 |
16 |
19 pub struct World { |
17 pub struct World { |
20 allocator: GearAllocator, |
18 allocator: GearAllocator, |
21 data: GearDataManager, |
19 data: GearDataManager, |
22 physics: PhysicsProcessor, |
20 physics: PhysicsProcessor, |
23 collision: CollisionProcessor, |
21 collision: CollisionProcessor, |
24 time: TimeProcessor, |
|
25 } |
22 } |
26 |
23 |
27 impl World { |
24 impl World { |
28 pub fn new(world_size: Size) -> Self { |
25 pub fn new(world_size: Size) -> Self { |
29 let mut data = GearDataManager::new(); |
26 let mut data = GearDataManager::new(); |
33 Self { |
30 Self { |
34 data, |
31 data, |
35 allocator: GearAllocator::new(), |
32 allocator: GearAllocator::new(), |
36 physics: PhysicsProcessor::new(), |
33 physics: PhysicsProcessor::new(), |
37 collision: CollisionProcessor::new(world_size), |
34 collision: CollisionProcessor::new(world_size), |
38 time: TimeProcessor::new(), |
|
39 } |
35 } |
40 } |
36 } |
41 |
37 |
42 #[inline] |
38 #[inline] |
43 pub fn new_gear(&mut self) -> Option<GearId> { |
39 pub fn new_gear(&mut self) -> Option<GearId> { |
46 |
42 |
47 #[inline] |
43 #[inline] |
48 pub fn delete_gear(&mut self, gear_id: GearId) { |
44 pub fn delete_gear(&mut self, gear_id: GearId) { |
49 self.data.remove_all(gear_id); |
45 self.data.remove_all(gear_id); |
50 self.collision.remove(gear_id); |
46 self.collision.remove(gear_id); |
51 self.time.cancel_all(gear_id); |
|
52 self.allocator.free(gear_id) |
47 self.allocator.free(gear_id) |
53 } |
48 } |
54 |
49 |
55 pub fn step(&mut self, time_step: Millis, land: &Land2D<u32>) { |
50 pub fn step(&mut self, time_step: Millis, land: &Land2D<u32>) { |
56 let updates = if time_step == Millis::new(1) { |
51 let updates = if time_step == Millis::new(1) { |
58 } else { |
53 } else { |
59 self.physics |
54 self.physics |
60 .process_multiple_ticks(&mut self.data, time_step) |
55 .process_multiple_ticks(&mut self.data, time_step) |
61 }; |
56 }; |
62 let collisions = self.collision.process(land, &updates); |
57 let collisions = self.collision.process(land, &updates); |
63 let events = self.time.process(time_step); |
|
64 } |
58 } |
65 |
59 |
66 #[inline] |
60 #[inline] |
67 pub fn add_gear_data<T: Clone + 'static>(&mut self, gear_id: GearId, data: &T) { |
61 pub fn add_gear_data<T: Clone + 'static>(&mut self, gear_id: GearId, data: &T) { |
68 self.data.add(gear_id, data); |
62 self.data.add(gear_id, data); |