author | alfadur |
Thu, 27 Jan 2022 03:51:13 +0300 | |
changeset 15850 | 44b49f255e31 |
parent 15802 | f4b563a9ac5e |
child 15851 | d5e6c8c92d87 |
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; |
14165 | 6 |
|
15850 | 7 |
use integral_geometry::PotSize; |
14165 | 8 |
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
|
9 |
|
14199 | 10 |
use crate::{ |
15402 | 11 |
collision::CollisionProcessor, |
15401 | 12 |
common::{GearAllocator, GearId, Millis}, |
15784 | 13 |
data::{DataIterator, GearDataManager, TypeIter}, |
15401 | 14 |
physics::PhysicsProcessor, |
14199 | 15 |
}; |
14080
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
16 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
17 |
pub struct World { |
15295 | 18 |
allocator: GearAllocator, |
15401 | 19 |
data: GearDataManager, |
14199 | 20 |
physics: PhysicsProcessor, |
21 |
collision: CollisionProcessor, |
|
14080
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
22 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
23 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
24 |
impl World { |
15850 | 25 |
pub fn new(world_size: PotSize) -> Self { |
15401 | 26 |
let mut data = GearDataManager::new(); |
27 |
PhysicsProcessor::register_components(&mut data); |
|
28 |
CollisionProcessor::register_components(&mut data); |
|
29 |
||
14200 | 30 |
Self { |
15401 | 31 |
data, |
15295 | 32 |
allocator: GearAllocator::new(), |
14200 | 33 |
physics: PhysicsProcessor::new(), |
14737 | 34 |
collision: CollisionProcessor::new(world_size), |
14200 | 35 |
} |
36 |
} |
|
37 |
||
15295 | 38 |
#[inline] |
39 |
pub fn new_gear(&mut self) -> Option<GearId> { |
|
40 |
self.allocator.alloc() |
|
41 |
} |
|
42 |
||
43 |
#[inline] |
|
44 |
pub fn delete_gear(&mut self, gear_id: GearId) { |
|
15401 | 45 |
self.data.remove_all(gear_id); |
15295 | 46 |
self.collision.remove(gear_id); |
47 |
self.allocator.free(gear_id) |
|
48 |
} |
|
49 |
||
15296 | 50 |
pub fn step(&mut self, time_step: Millis, land: &Land2D<u32>) { |
15802 | 51 |
let updates = self.physics.process(&mut self.data, time_step); |
15401 | 52 |
let collisions = self.collision.process(land, &updates); |
14165 | 53 |
} |
54 |
||
15401 | 55 |
#[inline] |
56 |
pub fn add_gear_data<T: Clone + 'static>(&mut self, gear_id: GearId, data: &T) { |
|
57 |
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
|
58 |
} |
15784 | 59 |
|
60 |
#[inline] |
|
61 |
pub fn iter_data<T: TypeIter + 'static>(&mut self) -> DataIterator<T> { |
|
62 |
self.data.iter() |
|
63 |
} |
|
14080
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
64 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
65 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
66 |
#[cfg(test)] |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
67 |
mod tests { |
14200 | 68 |
use crate::{ |
14737 | 69 |
collision::{CircleBounds, CollisionData}, |
15374 | 70 |
common::Millis, |
15401 | 71 |
physics::{PositionData, VelocityData}, |
14737 | 72 |
World, |
14200 | 73 |
}; |
14737 | 74 |
use fpnum::{fp, FPNum, FPPoint}; |
15850 | 75 |
use integral_geometry::PotSize; |
14200 | 76 |
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
|
77 |
|
14200 | 78 |
#[test] |
79 |
fn data_flow() { |
|
15850 | 80 |
let world_size = PotSize::new(2048, 2048).unwrap; |
14200 | 81 |
|
82 |
let mut world = World::new(world_size); |
|
15401 | 83 |
let gear_id = world.new_gear().unwrap(); |
84 |
||
85 |
world.add_gear_data(gear_id, &PositionData(FPPoint::zero())); |
|
86 |
world.add_gear_data(gear_id, &VelocityData(FPPoint::unit_y())); |
|
14200 | 87 |
|
14737 | 88 |
world.add_gear_data( |
89 |
gear_id, |
|
15401 | 90 |
&CollisionData { |
14737 | 91 |
bounds: CircleBounds { |
92 |
center: FPPoint::zero(), |
|
93 |
radius: fp!(10), |
|
94 |
}, |
|
95 |
}, |
|
96 |
); |
|
14200 | 97 |
|
98 |
let land = Land2D::new(Size::new(world_size.width - 2, world_size.height - 2), 0); |
|
99 |
||
15374 | 100 |
world.step(Millis::new(1), &land); |
14200 | 101 |
} |
14080
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
102 |
} |