author | nemo |
Tue, 24 Dec 2019 12:46:23 -0500 | |
changeset 15524 | fbcee515b946 |
parent 15383 | 701ad89a9f2a |
child 15762 | 84c07aa94b61 |
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; |
15270 | 6 |
pub mod time; |
14144 | 7 |
|
14179 | 8 |
use integral_geometry::Size; |
14144 | 9 |
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
|
10 |
|
14178 | 11 |
use crate::{ |
15381 | 12 |
collision::CollisionProcessor, |
15380 | 13 |
common::{GearAllocator, GearId, Millis}, |
14 |
data::GearDataManager, |
|
15 |
physics::PhysicsProcessor, |
|
15270 | 16 |
time::TimeProcessor, |
14178 | 17 |
}; |
14059
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 { |
15274 | 20 |
allocator: GearAllocator, |
15380 | 21 |
data: GearDataManager, |
14178 | 22 |
physics: PhysicsProcessor, |
23 |
collision: CollisionProcessor, |
|
15270 | 24 |
time: TimeProcessor, |
14059
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 { |
14179 | 28 |
pub fn new(world_size: Size) -> Self { |
15380 | 29 |
let mut data = GearDataManager::new(); |
30 |
PhysicsProcessor::register_components(&mut data); |
|
31 |
CollisionProcessor::register_components(&mut data); |
|
32 |
||
14179 | 33 |
Self { |
15380 | 34 |
data, |
15274 | 35 |
allocator: GearAllocator::new(), |
14179 | 36 |
physics: PhysicsProcessor::new(), |
14716 | 37 |
collision: CollisionProcessor::new(world_size), |
15270 | 38 |
time: TimeProcessor::new(), |
14179 | 39 |
} |
40 |
} |
|
41 |
||
15274 | 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) { |
|
15380 | 49 |
self.data.remove_all(gear_id); |
15274 | 50 |
self.collision.remove(gear_id); |
51 |
self.time.cancel(gear_id); |
|
52 |
self.allocator.free(gear_id) |
|
53 |
} |
|
54 |
||
15275 | 55 |
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
|
56 |
let updates = if time_step == Millis::new(1) { |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15381
diff
changeset
|
57 |
self.physics.process_single_tick(&mut self.data) |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15381
diff
changeset
|
58 |
} else { |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15381
diff
changeset
|
59 |
self.physics |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15381
diff
changeset
|
60 |
.process_multiple_ticks(&mut self.data, time_step) |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15381
diff
changeset
|
61 |
}; |
15380 | 62 |
let collisions = self.collision.process(land, &updates); |
15270 | 63 |
let events = self.time.process(time_step); |
14144 | 64 |
} |
65 |
||
15380 | 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); |
|
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 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
72 |
#[cfg(test)] |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
73 |
mod tests { |
14179 | 74 |
use crate::{ |
14716 | 75 |
collision::{CircleBounds, CollisionData}, |
15353 | 76 |
common::Millis, |
15380 | 77 |
physics::{PositionData, VelocityData}, |
14716 | 78 |
World, |
14179 | 79 |
}; |
14716 | 80 |
use fpnum::{fp, FPNum, FPPoint}; |
14179 | 81 |
use integral_geometry::Size; |
82 |
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
|
83 |
|
14179 | 84 |
#[test] |
85 |
fn data_flow() { |
|
86 |
let world_size = Size::new(2048, 2048); |
|
87 |
||
88 |
let mut world = World::new(world_size); |
|
15380 | 89 |
let gear_id = world.new_gear().unwrap(); |
90 |
||
91 |
world.add_gear_data(gear_id, &PositionData(FPPoint::zero())); |
|
92 |
world.add_gear_data(gear_id, &VelocityData(FPPoint::unit_y())); |
|
14179 | 93 |
|
14716 | 94 |
world.add_gear_data( |
95 |
gear_id, |
|
15380 | 96 |
&CollisionData { |
14716 | 97 |
bounds: CircleBounds { |
98 |
center: FPPoint::zero(), |
|
99 |
radius: fp!(10), |
|
100 |
}, |
|
101 |
}, |
|
102 |
); |
|
14179 | 103 |
|
104 |
let land = Land2D::new(Size::new(world_size.width - 2, world_size.height - 2), 0); |
|
105 |
||
15353 | 106 |
world.step(Millis::new(1), &land); |
14179 | 107 |
} |
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
108 |
} |