rust/lib-hedgewars-engine/src/world.rs
changeset 15762 84c07aa94b61
parent 15759 c929e25a7da2
child 15764 b10bbfb2b354
equal deleted inserted replaced
15761:e7eb0cd5b0e4 15762:84c07aa94b61
    10     outline_template::OutlineTemplate, template_based::TemplatedLandGenerator,
    10     outline_template::OutlineTemplate, template_based::TemplatedLandGenerator,
    11     LandGenerationParameters, LandGenerator,
    11     LandGenerationParameters, LandGenerator,
    12 };
    12 };
    13 use lfprng::LaggedFibonacciPRNG;
    13 use lfprng::LaggedFibonacciPRNG;
    14 
    14 
    15 use crate::render::{camera::Camera, GearRenderer, MapRenderer};
    15 use crate::render::{camera::Camera, GearEntry, GearRenderer, MapRenderer};
    16 
    16 
    17 struct GameState {
    17 struct GameState {
    18     land: Land2D<u32>,
    18     land: Land2D<u32>,
    19     physics: hwp::World,
    19     physics: hwp::World,
    20 }
    20 }
    30     preview: Option<Land2D<u8>>,
    30     preview: Option<Land2D<u8>>,
    31     game_state: Option<GameState>,
    31     game_state: Option<GameState>,
    32     map_renderer: Option<MapRenderer>,
    32     map_renderer: Option<MapRenderer>,
    33     gear_renderer: Option<GearRenderer>,
    33     gear_renderer: Option<GearRenderer>,
    34     camera: Camera,
    34     camera: Camera,
       
    35     gear_entries: Vec<GearEntry>,
    35 }
    36 }
    36 
    37 
    37 impl World {
    38 impl World {
    38     pub fn new() -> Self {
    39     pub fn new() -> Self {
    39         Self {
    40         Self {
    41             preview: None,
    42             preview: None,
    42             game_state: None,
    43             game_state: None,
    43             map_renderer: None,
    44             map_renderer: None,
    44             gear_renderer: None,
    45             gear_renderer: None,
    45             camera: Camera::new(),
    46             camera: Camera::new(),
       
    47             gear_entries: vec![],
    46         }
    48         }
    47     }
    49     }
    48 
    50 
    49     pub fn create_renderer(&mut self, width: u16, height: u16) {
    51     pub fn create_renderer(&mut self, width: u16, height: u16) {
    50         let land_tile_size = Size::square(512);
    52         let land_tile_size = Size::square(512);
   123                 gl::Clear(gl::COLOR_BUFFER_BIT);
   125                 gl::Clear(gl::COLOR_BUFFER_BIT);
   124             }
   126             }
   125 
   127 
   126             renderer.render(&self.camera);
   128             renderer.render(&self.camera);
   127         }
   129         }
       
   130 
       
   131         self.gear_entries.clear();
       
   132         let mut gear_entries = std::mem::take(&mut self.gear_entries);
       
   133 
   128         if let Some(ref mut renderer) = self.gear_renderer {
   134         if let Some(ref mut renderer) = self.gear_renderer {
   129             renderer.render(&self.camera)
   135             if let Some(ref mut state) = self.game_state {
       
   136                 state
       
   137                     .physics
       
   138                     .iter_data()
       
   139                     .run(|(pos,): (&mut PositionData,)| {
       
   140                         gear_entries.push(GearEntry::new(
       
   141                             f64::from(pos.0.x()) as f32,
       
   142                             f64::from(pos.0.y()) as f32,
       
   143                             Size::square(128),
       
   144                         ))
       
   145                     });
       
   146             }
       
   147             renderer.render(&self.camera, &gear_entries);
   130         }
   148         }
       
   149         self.gear_entries = gear_entries;
   131     }
   150     }
   132 
   151 
   133     fn create_gear(&mut self, position: Point) {
   152     fn create_gear(&mut self, position: Point) {
   134         if let Some(ref mut state) = self.game_state {
   153         if let Some(ref mut state) = self.game_state {
   135             let id = state.physics.new_gear().unwrap();
   154             let id = state.physics.new_gear().unwrap();