author | alfadur |
Fri, 13 Nov 2020 20:54:00 +0300 | |
changeset 15784 | 84c07aa94b61 |
parent 15404 | 701ad89a9f2a |
child 15790 | 704f00889f3a |
permissions | -rw-r--r-- |
14737 | 1 |
pub mod collision; |
2 |
pub mod common; |
|
15326 | 3 |
mod data; |
14199 | 4 |
mod grid; |
14737 | 5 |
pub mod physics; |
15291 | 6 |
pub mod time; |
14165 | 7 |
|
14200 | 8 |
use integral_geometry::Size; |
14165 | 9 |
use land2d::Land2D; |
14080
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
10 |
|
14199 | 11 |
use crate::{ |
15402 | 12 |
collision::CollisionProcessor, |
15401 | 13 |
common::{GearAllocator, GearId, Millis}, |
15784 | 14 |
data::{DataIterator, GearDataManager, TypeIter}, |
15401 | 15 |
physics::PhysicsProcessor, |
15291 | 16 |
time::TimeProcessor, |
14199 | 17 |
}; |
14080
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
18 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
19 |
pub struct World { |
15295 | 20 |
allocator: GearAllocator, |
15401 | 21 |
data: GearDataManager, |
14199 | 22 |
physics: PhysicsProcessor, |
23 |
collision: CollisionProcessor, |
|
15291 | 24 |
time: TimeProcessor, |
14080
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
25 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
26 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
27 |
impl World { |
14200 | 28 |
pub fn new(world_size: Size) -> Self { |
15401 | 29 |
let mut data = GearDataManager::new(); |
30 |
PhysicsProcessor::register_components(&mut data); |
|
31 |
CollisionProcessor::register_components(&mut data); |
|
32 |
||
14200 | 33 |
Self { |
15401 | 34 |
data, |
15295 | 35 |
allocator: GearAllocator::new(), |
14200 | 36 |
physics: PhysicsProcessor::new(), |
14737 | 37 |
collision: CollisionProcessor::new(world_size), |
15291 | 38 |
time: TimeProcessor::new(), |
14200 | 39 |
} |
40 |
} |
|
41 |
||
15295 | 42 |
#[inline] |
43 |
pub fn new_gear(&mut self) -> Option<GearId> { |
|
44 |
self.allocator.alloc() |
|
45 |
} |
|
46 |
||
47 |
#[inline] |
|
48 |
pub fn delete_gear(&mut self, gear_id: GearId) { |
|
15401 | 49 |
self.data.remove_all(gear_id); |
15295 | 50 |
self.collision.remove(gear_id); |
51 |
self.time.cancel(gear_id); |
|
52 |
self.allocator.free(gear_id) |
|
53 |
} |
|
54 |
||
15296 | 55 |
pub fn step(&mut self, time_step: Millis, land: &Land2D<u32>) { |
15404
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15402
diff
changeset
|
56 |
let updates = if time_step == Millis::new(1) { |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15402
diff
changeset
|
57 |
self.physics.process_single_tick(&mut self.data) |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15402
diff
changeset
|
58 |
} else { |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15402
diff
changeset
|
59 |
self.physics |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15402
diff
changeset
|
60 |
.process_multiple_ticks(&mut self.data, time_step) |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15402
diff
changeset
|
61 |
}; |
15401 | 62 |
let collisions = self.collision.process(land, &updates); |
15291 | 63 |
let events = self.time.process(time_step); |
14165 | 64 |
} |
65 |
||
15401 | 66 |
#[inline] |
67 |
pub fn add_gear_data<T: Clone + 'static>(&mut self, gear_id: GearId, data: &T) { |
|
68 |
self.data.add(gear_id, data); |
|
14080
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
69 |
} |
15784 | 70 |
|
71 |
#[inline] |
|
72 |
pub fn iter_data<T: TypeIter + 'static>(&mut self) -> DataIterator<T> { |
|
73 |
self.data.iter() |
|
74 |
} |
|
14080
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
75 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
76 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
77 |
#[cfg(test)] |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
78 |
mod tests { |
14200 | 79 |
use crate::{ |
14737 | 80 |
collision::{CircleBounds, CollisionData}, |
15374 | 81 |
common::Millis, |
15401 | 82 |
physics::{PositionData, VelocityData}, |
14737 | 83 |
World, |
14200 | 84 |
}; |
14737 | 85 |
use fpnum::{fp, FPNum, FPPoint}; |
14200 | 86 |
use integral_geometry::Size; |
87 |
use land2d::Land2D; |
|
14080
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
88 |
|
14200 | 89 |
#[test] |
90 |
fn data_flow() { |
|
91 |
let world_size = Size::new(2048, 2048); |
|
92 |
||
93 |
let mut world = World::new(world_size); |
|
15401 | 94 |
let gear_id = world.new_gear().unwrap(); |
95 |
||
96 |
world.add_gear_data(gear_id, &PositionData(FPPoint::zero())); |
|
97 |
world.add_gear_data(gear_id, &VelocityData(FPPoint::unit_y())); |
|
14200 | 98 |
|
14737 | 99 |
world.add_gear_data( |
100 |
gear_id, |
|
15401 | 101 |
&CollisionData { |
14737 | 102 |
bounds: CircleBounds { |
103 |
center: FPPoint::zero(), |
|
104 |
radius: fp!(10), |
|
105 |
}, |
|
106 |
}, |
|
107 |
); |
|
14200 | 108 |
|
109 |
let land = Land2D::new(Size::new(world_size.width - 2, world_size.height - 2), 0); |
|
110 |
||
15374 | 111 |
world.step(Millis::new(1), &land); |
14200 | 112 |
} |
14080
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
113 |
} |