author | alfadur |
Thu, 07 Jan 2021 17:13:32 +0300 | |
changeset 15775 | 95402fa4e191 |
parent 15768 | 704f00889f3a |
child 15780 | f4b563a9ac5e |
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>) { |
15383
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15381
diff
changeset
|
51 |
let updates = if time_step == Millis::new(1) { |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15381
diff
changeset
|
52 |
self.physics.process_single_tick(&mut self.data) |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15381
diff
changeset
|
53 |
} else { |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15381
diff
changeset
|
54 |
self.physics |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15381
diff
changeset
|
55 |
.process_multiple_ticks(&mut self.data, time_step) |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15381
diff
changeset
|
56 |
}; |
15380 | 57 |
let collisions = self.collision.process(land, &updates); |
14144 | 58 |
} |
59 |
||
15380 | 60 |
#[inline] |
61 |
pub fn add_gear_data<T: Clone + 'static>(&mut self, gear_id: GearId, data: &T) { |
|
62 |
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
|
63 |
} |
15762 | 64 |
|
65 |
#[inline] |
|
66 |
pub fn iter_data<T: TypeIter + 'static>(&mut self) -> DataIterator<T> { |
|
67 |
self.data.iter() |
|
68 |
} |
|
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
69 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
70 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
71 |
#[cfg(test)] |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
72 |
mod tests { |
14179 | 73 |
use crate::{ |
14716 | 74 |
collision::{CircleBounds, CollisionData}, |
15353 | 75 |
common::Millis, |
15380 | 76 |
physics::{PositionData, VelocityData}, |
14716 | 77 |
World, |
14179 | 78 |
}; |
14716 | 79 |
use fpnum::{fp, FPNum, FPPoint}; |
14179 | 80 |
use integral_geometry::Size; |
81 |
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
|
82 |
|
14179 | 83 |
#[test] |
84 |
fn data_flow() { |
|
85 |
let world_size = Size::new(2048, 2048); |
|
86 |
||
87 |
let mut world = World::new(world_size); |
|
15380 | 88 |
let gear_id = world.new_gear().unwrap(); |
89 |
||
90 |
world.add_gear_data(gear_id, &PositionData(FPPoint::zero())); |
|
91 |
world.add_gear_data(gear_id, &VelocityData(FPPoint::unit_y())); |
|
14179 | 92 |
|
14716 | 93 |
world.add_gear_data( |
94 |
gear_id, |
|
15380 | 95 |
&CollisionData { |
14716 | 96 |
bounds: CircleBounds { |
97 |
center: FPPoint::zero(), |
|
98 |
radius: fp!(10), |
|
99 |
}, |
|
100 |
}, |
|
101 |
); |
|
14179 | 102 |
|
103 |
let land = Land2D::new(Size::new(world_size.width - 2, world_size.height - 2), 0); |
|
104 |
||
15353 | 105 |
world.step(Millis::new(1), &land); |
14179 | 106 |
} |
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
107 |
} |