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
mod background;
mod vehicle;

use agents::{
    app::{App, State},
    simulation::{Simulation, Worker},
};
use vehicle::{Bounds, Vehicle};

use anyhow::Result;
use draw2d::{
    camera::{default_camera_controls, OrthoCamera},
    graphics::{
        layer::{Batch, LayerHandle},
        Graphics,
    },
};
use std::time::Duration;
use triple_buffer::Input;

struct VehicleWorld {
    vehicles: Vec<Vehicle>,
}

impl VehicleWorld {
    fn new() -> Self {
        Self { vehicles: vec![] }
    }

    fn flush(&self, sync: &mut Input<Vec<Vehicle>>) {
        sync.input_buffer().clear();
        sync.input_buffer().extend_from_slice(&self.vehicles);
        sync.publish();
    }
}

impl Simulation for VehicleWorld {
    type SyncState = Vec<Vehicle>;

    fn setup(&mut self, sync: &mut Input<Self::SyncState>) {
        let max = 10000;
        for i in 0..max {
            let norm = i as f32 / max as f32;
            let angle = norm * std::f32::consts::TAU;
            self.vehicles.push(Vehicle::new(
                [angle.cos() * 10.0, angle.sin() * 10.0],
                [angle.cos() * 2.0, angle.sin() * 2.0],
            ));
        }
        self.flush(sync);
    }

    fn tick(&mut self, sync: &mut Input<Self::SyncState>, _: Duration) {
        let bounds = Bounds {
            left: -20.0,
            right: 20.0,
            bottom: -20.0,
            top: 20.0,
            margin: 0.5,
        };

        let dt = Self::TICK_THROTTLE.as_secs_f32();
        for vehicle in &mut self.vehicles {
            vehicle.enforce_bounds(&bounds);
            vehicle.integrate(dt);
        }
        self.flush(sync);
    }
}

struct Demo {
    background_layer: LayerHandle,
    foreground_layer: LayerHandle,
    camera: OrthoCamera,
    sim: Worker<VehicleWorld>,
}

impl Demo {
    fn new(window: &mut glfw::Window, graphics: &mut Graphics) -> Result<Self> {
        let (w, h) = window.get_size();
        Ok(Self {
            background_layer: graphics.add_layer_to_bottom(),
            foreground_layer: graphics.add_layer_to_top(),
            camera: OrthoCamera::with_viewport(20.0, w as f32 / h as f32),
            sim: Worker::new(VehicleWorld::new)?,
        })
    }

    fn update_projection(&self, graphics: &mut Graphics) {
        let matrix = self.camera.as_matrix();
        graphics
            .get_layer_mut(&self.background_layer)
            .set_projection(matrix);
        graphics
            .get_layer_mut(&self.foreground_layer)
            .set_projection(matrix);
    }
}

impl State for Demo {
    fn init(
        &mut self,
        _window: &mut glfw::Window,
        graphics: &mut Graphics,
    ) -> Result<()> {
        self.update_projection(graphics);
        let background = self.build_background(graphics)?;
        graphics
            .get_layer_mut(&self.background_layer)
            .push_batch(background);
        Ok(())
    }

    fn update(
        &mut self,
        _window: &mut glfw::Window,
        graphics: &mut Graphics,
        _update_duration: Duration,
    ) -> Result<()> {
        let vehicles = self.sim.state();

        let mut vehicle_batch = Batch::empty();
        vehicle_batch.vertices.reserve(vehicles.len() * 3);

        for vehicle in self.sim.state() {
            vehicle_batch.vertices.extend_from_slice(&vehicle.draw());
        }

        let layer = graphics.get_layer_mut(&self.foreground_layer);
        layer.clear();
        layer.push_batch(vehicle_batch);

        Ok(())
    }

    fn handle_event(
        &mut self,
        window_event: &glfw::WindowEvent,
        _window: &mut glfw::Window,
        graphics: &mut Graphics,
    ) -> Result<()> {
        if default_camera_controls(&mut self.camera, &window_event) {
            self.update_projection(graphics);
        }
        Ok(())
    }
}

fn main() -> Result<()> {
    App::new(Demo::new)?.main_loop()
}