author | alfadur |
Sat, 16 Oct 2021 02:39:22 +0300 | |
changeset 15822 | 4ede5e84278a |
parent 15780 | f4b563a9ac5e |
child 15828 | 44b49f255e31 |
permissions | -rw-r--r-- |
14716 | 1 |
pub mod collision; |
2 |
pub mod common; |
|
15305 | 3 |
mod data; |
14178 | 4 |
mod grid; |
14716 | 5 |
pub mod physics; |
14144 | 6 |
|
14179 | 7 |
use integral_geometry::Size; |
14144 | 8 |
use land2d::Land2D; |
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
9 |
|
14178 | 10 |
use crate::{ |
15381 | 11 |
collision::CollisionProcessor, |
15380 | 12 |
common::{GearAllocator, GearId, Millis}, |
15762 | 13 |
data::{DataIterator, GearDataManager, TypeIter}, |
15380 | 14 |
physics::PhysicsProcessor, |
14178 | 15 |
}; |
14059
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 { |
15274 | 18 |
allocator: GearAllocator, |
15380 | 19 |
data: GearDataManager, |
14178 | 20 |
physics: PhysicsProcessor, |
21 |
collision: CollisionProcessor, |
|
14059
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 { |
14179 | 25 |
pub fn new(world_size: Size) -> Self { |
15380 | 26 |
let mut data = GearDataManager::new(); |
27 |
PhysicsProcessor::register_components(&mut data); |
|
28 |
CollisionProcessor::register_components(&mut data); |
|
29 |
||
14179 | 30 |
Self { |
15380 | 31 |
data, |
15274 | 32 |
allocator: GearAllocator::new(), |
14179 | 33 |
physics: PhysicsProcessor::new(), |
14716 | 34 |
collision: CollisionProcessor::new(world_size), |
14179 | 35 |
} |
36 |
} |
|
37 |
||
15274 | 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) { |
|
15380 | 45 |
self.data.remove_all(gear_id); |
15274 | 46 |
self.collision.remove(gear_id); |
47 |
self.allocator.free(gear_id) |
|
48 |
} |
|
49 |
||
15275 | 50 |
pub fn step(&mut self, time_step: Millis, land: &Land2D<u32>) { |
15780 | 51 |
let updates = self.physics.process(&mut self.data, time_step); |
15380 | 52 |
let collisions = self.collision.process(land, &updates); |
14144 | 53 |
} |
54 |
||
15380 | 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); |
|
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
58 |
} |
15762 | 59 |
|
60 |
#[inline] |
|
61 |
pub fn iter_data<T: TypeIter + 'static>(&mut self) -> DataIterator<T> { |
|
62 |
self.data.iter() |
|
63 |
} |
|
14059
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 { |
14179 | 68 |
use crate::{ |
14716 | 69 |
collision::{CircleBounds, CollisionData}, |
15353 | 70 |
common::Millis, |
15380 | 71 |
physics::{PositionData, VelocityData}, |
14716 | 72 |
World, |
14179 | 73 |
}; |
14716 | 74 |
use fpnum::{fp, FPNum, FPPoint}; |
14179 | 75 |
use integral_geometry::Size; |
76 |
use land2d::Land2D; |
|
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
77 |
|
14179 | 78 |
#[test] |
79 |
fn data_flow() { |
|
80 |
let world_size = Size::new(2048, 2048); |
|
81 |
||
82 |
let mut world = World::new(world_size); |
|
15380 | 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())); |
|
14179 | 87 |
|
14716 | 88 |
world.add_gear_data( |
89 |
gear_id, |
|
15380 | 90 |
&CollisionData { |
14716 | 91 |
bounds: CircleBounds { |
92 |
center: FPPoint::zero(), |
|
93 |
radius: fp!(10), |
|
94 |
}, |
|
95 |
}, |
|
96 |
); |
|
14179 | 97 |
|
98 |
let land = Land2D::new(Size::new(world_size.width - 2, world_size.height - 2), 0); |
|
99 |
||
15353 | 100 |
world.step(Millis::new(1), &land); |
14179 | 101 |
} |
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
102 |
} |