rust/lib-hedgewars-engine/src/world.rs
changeset 14707 29dbe9ce8b7d
parent 14378 4409344db447
child 14709 12db7e435ea6
equal deleted inserted replaced
14706:5e2c892b0222 14707:29dbe9ce8b7d
     7     LandGenerationParameters,
     7     LandGenerationParameters,
     8     LandGenerator,
     8     LandGenerator,
     9 };
     9 };
    10 use lfprng::LaggedFibonacciPRNG;
    10 use lfprng::LaggedFibonacciPRNG;
    11 use hwphysics as hwp;
    11 use hwphysics as hwp;
       
    12 
       
    13 use crate::render::MapRenderer;
    12 
    14 
    13 struct GameState {
    15 struct GameState {
    14     land: Land2D<u32>,
    16     land: Land2D<u32>,
    15     physics: hwp::World,
    17     physics: hwp::World,
    16 }
    18 }
    26 
    28 
    27 pub struct World {
    29 pub struct World {
    28     random_numbers_gen: LaggedFibonacciPRNG,
    30     random_numbers_gen: LaggedFibonacciPRNG,
    29     preview: Option<Land2D<u8>>,
    31     preview: Option<Land2D<u8>>,
    30     game_state: Option<GameState>,
    32     game_state: Option<GameState>,
       
    33     renderer: MapRenderer,
    31 }
    34 }
    32 
    35 
    33 impl World {
    36 impl World {
    34     pub fn new() -> Self {
    37     pub fn new() -> Self {
    35         Self {
    38         Self {
    36             random_numbers_gen: LaggedFibonacciPRNG::new(&[]),
    39             random_numbers_gen: LaggedFibonacciPRNG::new(&[]),
    37             preview: None,
    40             preview: None,
    38             game_state: None,
    41             game_state: None,
       
    42             renderer: MapRenderer::new(512, 512),
    39         }
    43         }
    40     }
    44     }
    41 
    45 
    42     pub fn set_seed(&mut self, seed: &[u8]) {
    46     pub fn set_seed(&mut self, seed: &[u8]) {
    43         self.random_numbers_gen = LaggedFibonacciPRNG::new(seed);
    47         self.random_numbers_gen = LaggedFibonacciPRNG::new(seed);
    75 
    79 
    76         let params = LandGenerationParameters::new(0u32, u32::max_value(), 5, false, false);
    80         let params = LandGenerationParameters::new(0u32, u32::max_value(), 5, false, false);
    77         let landgen = TemplatedLandGenerator::new(template);
    81         let landgen = TemplatedLandGenerator::new(template);
    78         let land = landgen.generate_land(&params, &mut self.random_numbers_gen);
    82         let land = landgen.generate_land(&params, &mut self.random_numbers_gen);
    79 
    83 
       
    84         use mapgen::{
       
    85             MapGenerator,
       
    86             theme::{Theme, slice_u32_to_u8}
       
    87         };
       
    88 
       
    89         use std::path::Path;
       
    90         
       
    91         let theme = Theme::load(Path::new("../../share/hedgewars/Data/Themes/Cheese/")).unwrap();
       
    92         let texture = MapGenerator::new().make_texture32(&land, &theme);
       
    93         self.renderer.init(&texture);
       
    94         
    80         self.game_state = Some(GameState::new(land, physics));
    95         self.game_state = Some(GameState::new(land, physics));
       
    96     }
       
    97 
       
    98     pub fn render(&mut self, x: f32, y: f32, w: f32, h: f32) {
       
    99         unsafe {
       
   100             gl::ClearColor(0.4f32, 0f32, 0.2f32, 1f32);
       
   101             gl::Clear(gl::COLOR_BUFFER_BIT);
       
   102         }
       
   103 
       
   104         self.renderer.render(Rect::new(
       
   105             Point::new(x as _, y as _),
       
   106             Point::new((x + w) as _, (y + h) as _),
       
   107         ));
    81     }
   108     }
    82 
   109 
    83     pub fn step(&mut self) {
   110     pub fn step(&mut self) {
    84         if let Some(ref mut state) = self.game_state {
   111         if let Some(ref mut state) = self.game_state {
    85             state.physics.step(fp!(1), &state.land);
   112             state.physics.step(fp!(1), &state.land);
    86         }
   113         }
    87     }
   114     }
    88 }
   115 }
       
   116 
       
   117 
       
   118