summaryrefslogtreecommitdiff
path: root/src/sim/sim_manager/mod.rs
blob: b6d2528c0780d3d16c48d1dbf1858deb3ebe4147 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use std::time::Instant;

use fxhash::FxHashMap;
use glam::IVec2;

use crate::{
    Config,
    config::{PHYSICS_DELTA_TIME, PHYSICS_FPS, SIM_FPS},
    sim::{
        cell::{cell::Cell, materials::MaterialId},
        cell_manager::manager::CellManager,
        entity::{Entity, EntityDef},
        particle_manager::ParticleManager,
        rb_manager::RbManager,
        sim_manager::utils::write_entity_to_world,
    },
};

mod utils;

pub struct SimManager {
    // timing
    pub paused: bool,
    pub ignore_pause_next_tick: bool,
    pub last_cell_update: Instant,
    pub cell_updates_due: f32,
    pub last_physics_update: Instant,
    pub physics_updates_due: f32,

    // systems
    pub cell_manager: CellManager,
    pub rb_manager: RbManager,
    pub particle_manager: ParticleManager,

    // entities
    // TODO entity manager?
    next_entity_id: u32,
    pub entities: FxHashMap<u32, Entity>,
}

impl SimManager {
    pub fn create_entity(&mut self, def: EntityDef) -> u32 {
        let (rb_h, collider_h) = if let Some(rb) = def.rb {
            let rb_h = self.rb_manager.physics_manager.rigid_body_set.insert(rb);

            if let Some(collider) = def.collider {
                let collider_h = self
                    .rb_manager
                    .physics_manager
                    .collider_set
                    .insert_with_parent(
                        collider,
                        rb_h,
                        &mut self.rb_manager.physics_manager.rigid_body_set,
                    );
                (Some(rb_h), Some(collider_h))
            } else {
                (Some(rb_h), None)
            }
        } else if let Some(collider) = def.collider {
            let collider_h = self
                .rb_manager
                .physics_manager
                .collider_set
                .insert(collider);
            (None, Some(collider_h))
        } else {
            (None, None)
        };

        let entity = Entity::new(
            self.next_entity_id,
            rb_h,
            collider_h,
            def.cells,
            def.behaviour,
        );

        let id = self.next_entity_id;
        self.entities.insert(id, entity);
        self.next_entity_id += 1;
        id
    }

    pub fn destroy_entity(&mut self, id: u32) {
        if let Some(entity) = self.entities.get(&id) {
            if let Some(rb_h) = entity.rb_h {
                self.rb_manager.physics_manager.rigid_body_set.remove(
                    rb_h,
                    &mut self.rb_manager.physics_manager.island_manager,
                    &mut self.rb_manager.physics_manager.collider_set,
                    &mut self.rb_manager.physics_manager.impulse_joint_set,
                    &mut self.rb_manager.physics_manager.multibody_joint_set,
                    true,
                );
            } else if let Some(collider_h) = entity.collider_h {
                self.rb_manager.physics_manager.collider_set.remove(
                    collider_h,
                    &mut self.rb_manager.physics_manager.island_manager,
                    &mut self.rb_manager.physics_manager.rigid_body_set,
                    true,
                );
            }

            self.entities.remove(&id);
        }
    }

    fn cell_update(&mut self, config: &Config) {
        // before we tick, write all the entities into the sim world
        // TODO optimize
        let entity_ids: Vec<u32> = self.entities.keys().copied().collect();
        let mut cells_written_by_entity: Vec<(u32, Vec<(u8, u8, i32, i32)>)> = Vec::new();

        for entity_id in entity_ids {
            let cells_written = write_entity_to_world(self, entity_id, self.cell_manager.seqno);
            cells_written_by_entity.push((entity_id, cells_written));
        }

        self.cell_manager.tick(config.use_threading);

        // after we tick, remove the written entity cells and update the entities
        // TODO optimize
        for (entity_id, cells_written) in cells_written_by_entity {
            let entity = self.entities.get_mut(&entity_id).unwrap();
            for (lx, ly, x, y) in cells_written {
                // update the entity
                // TODO we should skip cells that weren't changed?
                // TODO optimize
                let new_local_cell = self.cell_manager.get_cell_from_game_position(x, y).unwrap();
                if new_local_cell.material != MaterialId::Void && !new_local_cell.entity() {
                    panic!(
                        "Someone swapped into this entity's cell! ({x}, {y}, {m:#?}, {f})",
                        m = new_local_cell.material,
                        f = new_local_cell.flags
                    );
                }
                if let Some(cells) = &mut entity.cells {
                    cells.set_cell_at_local_position(
                        IVec2::new(lx as i32, ly as i32),
                        new_local_cell,
                    );
                }
                // update the world
                self.cell_manager
                    .set_cell_from_game_position(x, y, Cell::void(), false);
            }
        }
    }

    fn physics_update(&mut self, delta_time: f32) {
        // before we move the rigidbodies, upsert the current terrain state
        // TODO use the chunk sleeping, and make this range dynamic
        for cx in -5..5 {
            for cy in -5..5 {
                if let Some(chunk) = self
                    .cell_manager
                    .chunk_position_to_chunk_idx
                    .get(&(cx, cy))
                    .map(|&idx| &self.cell_manager.chunks[idx])
                    && !chunk.sleeping
                {
                    self.rb_manager.upsert_chunk_collider(cx, cy, chunk);
                }
            }
        }

        // move the rbs
        self.rb_manager.tick(delta_time);

        // move the particles
        self.particle_manager
            .tick(&mut self.cell_manager, delta_time);
    }

    pub fn update(&mut self, config: &Config, _delta_time: f32) -> () {
        let now = Instant::now();
        let secs_since_last_cell_update = (now - self.last_cell_update).as_secs_f32();
        let expected_secs_since_last_cell_update = 1.0 / SIM_FPS as f32;

        self.last_cell_update = now;
        if self.paused && self.ignore_pause_next_tick {
            self.cell_update(config);
        } else if !self.paused {
            self.cell_updates_due +=
                secs_since_last_cell_update / expected_secs_since_last_cell_update;
            let mut cell_updates_done = 0;
            // don't ever update more than 3 times per frame, or else we can get a pseudo deadlock
            while self.cell_updates_due >= 1.0 && cell_updates_done < 3 {
                self.cell_update(config);
                self.cell_updates_due -= 1.0;
                cell_updates_done += 1;
            }
            self.cell_updates_due = self.cell_updates_due.min(3.0);
        }

        // PHYSICS UPDATE
        let secs_since_last_physics_update = (now - self.last_physics_update).as_secs_f32();
        let expected_secs_since_last_physics_update = 1.0 / PHYSICS_FPS as f32;

        self.last_physics_update = now;
        if self.paused && self.ignore_pause_next_tick {
            self.physics_update(PHYSICS_DELTA_TIME);
        } else if !self.paused {
            self.physics_updates_due +=
                secs_since_last_physics_update / expected_secs_since_last_physics_update;
            let mut updates_done = 0;
            // don't ever update more than 3 times per frame, or else we can get a pseudo deadlock
            while self.physics_updates_due >= 1.0 && updates_done < 3 {
                self.physics_update(PHYSICS_DELTA_TIME);
                self.physics_updates_due -= 1.0;
                updates_done += 1;
            }
            self.physics_updates_due = self.physics_updates_due.min(3.0);
        }

        self.ignore_pause_next_tick = false;
    }

    pub fn new() -> Self {
        SimManager {
            paused: false,
            ignore_pause_next_tick: false,
            last_cell_update: Instant::now(),
            cell_updates_due: 0.0,
            last_physics_update: Instant::now(),
            physics_updates_due: 0.0,

            cell_manager: CellManager::from_default_size(),
            rb_manager: RbManager::new(),
            particle_manager: ParticleManager::new(),

            next_entity_id: 0,
            entities: FxHashMap::default(),
        }
    }
}