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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#[cfg(feature = "default")]
#[cfg(not(feature = "3d-audio"))]
use audio::Audio;
#[cfg(feature = "3d-audio")]
use audio::{Ambisonic, AmbisonicBuilder};
use imgui::Ui;
use input::Input;
use renderer::{Draw, Renderer};
#[cfg(feature = "nphysics")]
use types::PhysicsType;
use types::{Camera, RenderItem, TextItem};

#[cfg(feature = "nphysics")]
use nalgebra::Translation3;
#[cfg(feature = "nphysics")]
use nalgebra::Vector3 as nVector3;
#[cfg(feature = "nphysics")]
use ncollide::shape::{Cuboid, ShapeHandle};
#[cfg(feature = "nphysics")]
use nphysics3d::object::{BodyHandle, BodyStatus, ColliderDesc, RigidBodyDesc};
#[cfg(feature = "nphysics")]
use nphysics3d::world::World;

use glium::glutin::event::{Event, StartCause};
use glium::glutin::event_loop::{ControlFlow, EventLoop};

//use std::slice::IterMut;
use std::time::{Duration, Instant};

use rayon::prelude::*;
use rayon::slice::IterMut;

/// The divisor for the physics space to align with render space
#[cfg(feature = "nphysics")]
const PHYSICS_DIVISOR: f32 = 2f32;

/// Enum for update to return status
pub enum UpdateStatus {
    /// Contune to update
    Continue,
    /// Finish/Exit the game
    Finish,
}

/// Struct for storing physics handles and associated RenderItem
#[cfg(feature = "nphysics")]
struct PhysicsHandle {
    render_item: (usize, usize),
    body_handle: BodyHandle,
}

/// Struct for creating an instance of a game with all systems and items contained
pub struct Game<T: Default> {
    /// The input system for the game
    pub input: Input,
    /// The render system for the game
    pub renderer: Renderer,
    /// The physics system
    #[cfg(feature = "nphysics")]
    pub physics: World<f32>,
    /// The audio system
    #[cfg(feature = "default")]
    #[cfg(not(feature = "3d-audio"))]
    pub audio: Audio,
    /// The 3d audio system
    #[cfg(feature = "3d-audio")]
    pub audio: Ambisonic,
    /// Simple struct for camera data
    pub cams: Vec<Camera>,
    /// All of the mesh items to be rendered in the game
    render_items: Vec<RenderItem<T>>,
    /// All the text items to be rendered in the game
    text_items: Vec<TextItem>,
    /// All the body handles for physics items
    #[cfg(feature = "nphysics")]
    physics_items: Vec<PhysicsHandle>,
    /// The delta time for each frame
    pub delta: f32,
}

impl<T: Default> Game<T> {
    /// Creates a new instance of a game
    pub fn new() -> (Game<T>, EventLoop<()>) {
        // init physics
        #[cfg(feature = "nphysics")]
        let mut physics = World::new();
        #[cfg(feature = "nphysics")]
        physics.set_gravity(nVector3::new(0.0, -9.81, 0.0));

        //cam state
        let cam = Camera {
            pos: (0.0f32, 0.0, 0.0),
            euler_rot: (0.0f32, 0.0, 0.0),
        };

        let event_loop = EventLoop::new();

        let renderer = Renderer::new("caper window".to_string(), &event_loop);

        #[cfg(feature = "default")]
        #[cfg(not(feature = "3d-audio"))]
        let audio = Audio::new();
        #[cfg(feature = "3d-audio")]
        let audio = AmbisonicBuilder::default().build();

        (
            Game {
                input: Input::new(),
                renderer,
                #[cfg(feature = "nphysics")]
                physics,
                #[cfg(feature = "default")]
                #[cfg(not(feature = "3d-audio"))]
                audio: audio,
                #[cfg(feature = "3d-audio")]
                audio: audio,
                cams: vec![cam],
                render_items: Vec::new(),
                text_items: Vec::new(),
                #[cfg(feature = "nphysics")]
                physics_items: Vec::new(),
                delta: 0.016_666_667f32,
            },
            event_loop,
        )
    }
}

/// Default trait implementation from Game
impl<T: Default> Default for Game<T> {
    /// Returns a default instance of Game
    fn default() -> Self {
        Self::new().0
    }
}

/// Trait for operations on RenderItem
pub trait RenderItems {
    /// RenderItem utype associated type
    type T: Default;
    /// Get the len of render_items
    fn render_items_len(&self) -> usize;
    /// Get an IterMut of the RenderItem
    fn render_items_iter_mut(&mut self) -> IterMut<RenderItem<Self::T>>;
    /// Get a ref to a render item
    fn get_render_item(&mut self, index: usize) -> &mut RenderItem<Self::T>;
    /// Get a ref to a render item from its name, returning the first found
    fn get_render_item_by_name(&mut self, name: &str) -> Option<&mut RenderItem<Self::T>>;
    /// Add a render item to the game
    fn add_render_item(&mut self, render_item: RenderItem<Self::T>);
}

impl<T: Default> RenderItems for Game<T> {
    /// Associated type for RenderItems
    type T = T;
    /// Get the len of render_items
    fn render_items_len(&self) -> usize {
        self.render_items.len()
    }

    /// Get an IterMut of the RenderItem
    fn render_items_iter_mut(&mut self) -> IterMut<RenderItem<T>> {
        self.render_items.par_iter_mut()
    }

    /// Get a ref to a render item
    fn get_render_item(&mut self, index: usize) -> &mut RenderItem<T> {
        &mut self.render_items[index]
    }

    /// Get a ref to a render item from its name, returning the first found
    fn get_render_item_by_name(&mut self, name: &str) -> Option<&mut RenderItem<T>> {
        self.render_items.iter_mut().find(|item| item.name == name)
    }

    /// Add a render item to the game
    fn add_render_item(&mut self, render_item: RenderItem<T>) {
        // add the render item
        self.render_items.push(render_item);

        // the index of the newly added item
        let i = self.render_items.len() - 1;

        // setup the physics for the item
        #[cfg(feature = "nphysics")]
        self.add_physics(i);
    }
}

/// Trait for physics operations
#[cfg(feature = "nphysics")]
pub trait Physics {
    /// Initalise physics depending on PhysicsType
    fn add_physics(&mut self, i: usize);
    /// Update physics
    fn update_physics(&mut self);
}

#[cfg(feature = "nphysics")]
impl<T: Default> Physics for Game<T> {
    /// Initalise physics depending on PhysicsType
    fn add_physics(&mut self, i: usize) {
        // add the rigid body if needed
        match self.render_items[i].physics_type {
            PhysicsType::Static => {
                for j in 0..self.render_items[i].instance_transforms.len() {
                    let ri_trans = self.render_items[i].instance_transforms[j];

                    let geom = ShapeHandle::new(Cuboid::new(nVector3::new(
                        ri_trans.scale.0,
                        ri_trans.scale.1,
                        ri_trans.scale.2,
                    )));
                    let collider_desc = ColliderDesc::new(geom).density(1.0);

                    let mut rb_desc = RigidBodyDesc::new().collider(&collider_desc);

                    let pos = nVector3::new(
                        ri_trans.pos.0 * PHYSICS_DIVISOR,
                        ri_trans.pos.1 * PHYSICS_DIVISOR,
                        ri_trans.pos.2 * PHYSICS_DIVISOR,
                    );

                    let rb = rb_desc
                        .set_translation(pos)
                        .set_status(BodyStatus::Static)
                        .build(&mut self.physics);

                    let physics_handle = PhysicsHandle {
                        render_item: (i, j),
                        body_handle: rb.handle(),
                    };
                    self.physics_items.push(physics_handle);
                }
            }
            PhysicsType::Dynamic => {
                for j in 0..self.render_items[i].instance_transforms.len() {
                    let ri_trans = self.render_items[i].instance_transforms[j];

                    let geom = ShapeHandle::new(Cuboid::new(nVector3::new(
                        ri_trans.scale.0,
                        ri_trans.scale.1,
                        ri_trans.scale.2,
                    )));
                    let collider_desc = ColliderDesc::new(geom).density(1.0);

                    let mut rb_desc = RigidBodyDesc::new().collider(&collider_desc);

                    let pos = nVector3::new(
                        ri_trans.pos.0 * PHYSICS_DIVISOR,
                        ri_trans.pos.1 * PHYSICS_DIVISOR,
                        ri_trans.pos.2 * PHYSICS_DIVISOR,
                    );

                    let rb = rb_desc.set_translation(pos).build(&mut self.physics);

                    let physics_handle = PhysicsHandle {
                        render_item: (i, j),
                        body_handle: rb.handle(),
                    };
                    self.physics_items.push(physics_handle);
                }
            }
            PhysicsType::None => {}
        }
    }

    /// Update the physics engine
    fn update_physics(&mut self) {
        // update the new positions back to rb
        {
            for ph in self.physics_items.iter() {
                let (ri_i, ri_it_i) = ph.render_item;

                // check if it actually exists, if it doesn't remove
                if self.render_items.len() > ri_i
                    && self.render_items[ri_i].instance_transforms.len() > ri_it_i
                {
                    // update the rb transform pos
                    let rb = self.physics.rigid_body_mut(ph.body_handle).unwrap();
                    let ri_pos = self.render_items[ri_i].instance_transforms[ri_it_i].pos;

                    let mut rb_pos = rb.position().clone();
                    rb_pos.translation = Translation3::new(
                        ri_pos.0 * PHYSICS_DIVISOR,
                        ri_pos.1 * PHYSICS_DIVISOR,
                        ri_pos.2 * PHYSICS_DIVISOR,
                    );
                    rb.set_position(rb_pos);
                }
            }
        }

        // block for updating physics
        {
            // update all the physics items
            self.physics.step();

            for ph in self.physics_items.iter() {
                let rb = self.physics.rigid_body_mut(ph.body_handle).unwrap();

                // update the RenderItem transform pos
                let trans = rb.position().translation.vector;
                let prot = rb.position().rotation;
                let rot = prot.coords.data.as_slice();

                let (ri_i, ri_it_i) = ph.render_item;

                if self.render_items.len() > ri_i
                    && self.render_items[ri_i].instance_transforms.len() > ri_it_i
                {
                    self.render_items[ri_i].instance_transforms[ri_it_i].pos = (
                        trans.x / PHYSICS_DIVISOR,
                        trans.y / PHYSICS_DIVISOR,
                        trans.z / PHYSICS_DIVISOR,
                    );
                    self.render_items[ri_i].instance_transforms[ri_it_i].rot =
                        (rot[0], rot[1], rot[2], rot[3]);
                }
            }
        }
    }
}

/// Trait for operations on TextItem
pub trait TextItems {
    /// Get the len of render_items
    fn text_items_len(&self) -> usize;
    /// Get an IterMut of the TextItem
    fn text_items_iter_mut(&mut self) -> IterMut<TextItem>;
    /// Get a ref to a text item
    fn get_text_item(&mut self, index: usize) -> &mut TextItem;
    /// Get a ref to a text item from its name, returning the first found
    fn get_text_item_by_name(&mut self, name: String) -> Option<&mut TextItem>;
    /// Add a text item to the game
    fn add_text_item(&mut self, text_item: TextItem);
}

impl<T: Default> TextItems for Game<T> {
    /// Get the len of render_items
    fn text_items_len(&self) -> usize {
        self.text_items.len()
    }

    /// Get an IterMut of the TextItem
    fn text_items_iter_mut(&mut self) -> IterMut<TextItem> {
        self.text_items.par_iter_mut()
    }

    /// Get a ref to a text item
    fn get_text_item(&mut self, index: usize) -> &mut TextItem {
        &mut self.text_items[index]
    }

    /// Get a ref to a text item from its name, returning the first found
    fn get_text_item_by_name(&mut self, name: String) -> Option<&mut TextItem> {
        for i in 0..self.text_items.len() {
            if self.text_items[i].name == name {
                return Some(&mut self.text_items[i]);
            }
        }
        None
    }

    /// Add a text item to the game
    fn add_text_item(&mut self, text_item: TextItem) {
        self.text_items.push(text_item);
    }
}

/// Trait with default update definition
pub trait Update {
    /// RenderItem utype associated type
    type T;
    /// Update the per frame engine state
    fn update<F: FnMut(&Ui), U: FnMut(&mut Game<Self::T>) -> UpdateStatus>(
        &mut self,
        render_imgui: F,
        update: U,
        events: &Vec<Event<()>>,
    ) -> UpdateStatus;
    /// Update the per frame inputs
    fn update_inputs(&mut self, events: &Vec<Event<()>>);
}

/// Impl for Update on Game
impl<T: Default> Update for Game<T> {
    /// Associated type for RenderItems
    type T = T;
    /// Default Game implementation to update the engine state
    fn update<F: FnMut(&Ui), U: FnMut(&mut Game<T>) -> UpdateStatus>(
        &mut self,
        mut render_imgui: F,
        mut update: U,
        events: &Vec<Event<()>>,
    ) -> UpdateStatus {
        let frame_start = Instant::now();

        self.update_inputs(events);
        #[cfg(feature = "nphysics")]
        self.update_physics();

        let status = update(self);

        // render the frame
        {
            self.renderer.draw(
                &mut self.cams,
                &mut self.render_items,
                &mut self.text_items,
                &mut render_imgui,
            );
        }

        self.delta = 0.000_000_001f32 * frame_start.elapsed().subsec_nanos() as f32;

        status
    }

    /// Default Game implementation to Update inputs
    fn update_inputs(&mut self, events: &Vec<Event<()>>) {
        {
            // updating and handling the inputs
            let gl_window = self.renderer.display.gl_window();
            let window = gl_window.window();
            self.input.update_inputs(window, events);
        }
        {
            // update the inputs for imgui
            self.renderer.update_imgui_input(&self.input);
        }
    }
}

/// start running a game
pub fn start_loop<F>(event_loop: EventLoop<()>, mut callback: F) -> !
where
    F: 'static + FnMut(&Vec<Event<()>>) -> UpdateStatus,
{
    let mut events_buffer = Vec::new();
    let mut next_frame_time = Instant::now();
    event_loop.run(move |event, _, control_flow| {
        let run_callback = match event.to_static() {
            Some(Event::NewEvents(cause)) => match cause {
                StartCause::ResumeTimeReached { .. } | StartCause::Init => true,
                _ => false,
            },
            Some(event) => {
                events_buffer.push(event);
                false
            }
            None => {
                // Ignore this event.
                false
            }
        };

        let action = if run_callback {
            let action = callback(&events_buffer);
            next_frame_time = Instant::now() + Duration::from_nanos(16666667);
            // TODO: Add back the old accumulator loop in some way

            events_buffer.clear();
            action
        } else {
            UpdateStatus::Continue
        };

        match action {
            UpdateStatus::Continue => {
                *control_flow = ControlFlow::WaitUntil(next_frame_time);
            }
            UpdateStatus::Finish => *control_flow = ControlFlow::Exit,
        }
    })
}