rust/lib-hedgewars-engine/src/world.rs
author unC0Rr
Wed, 28 Aug 2024 15:31:51 +0200
branchtransitional_engine
changeset 16021 6a3dc15b78b9
parent 15924 9502611bffc1
child 16022 2003b466b279
permissions -rw-r--r--
Add corrosion as a subdirectory, CMake fixes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14715
diff changeset
     1
use fpnum::{fp, FPNum, FPPoint};
15275
66c987015f2d replace time with milliseconds
alfadur
parents: 15274
diff changeset
     2
use hwphysics::{
66c987015f2d replace time with milliseconds
alfadur
parents: 15274
diff changeset
     3
    self as hwp,
66c987015f2d replace time with milliseconds
alfadur
parents: 15274
diff changeset
     4
    common::{GearId, Millis},
15755
2eb3469a28a0 trying to build
nemo
parents: 15275
diff changeset
     5
    physics::{PositionData, VelocityData},
15275
66c987015f2d replace time with milliseconds
alfadur
parents: 15274
diff changeset
     6
};
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
     7
use integral_geometry::{Point, Rect, Size};
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
     8
use land2d::Land2D;
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
     9
use landgen::{
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15891
diff changeset
    10
    outline_template_based::outline_template::OutlineTemplate,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15891
diff changeset
    11
    outline_template_based::template_based::TemplatedLandGenerator, LandGenerationParameters,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15891
diff changeset
    12
    LandGenerator,
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    13
};
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    14
use lfprng::LaggedFibonacciPRNG;
15890
8a6a2d931bae Implement data path
unc0rr
parents: 15828
diff changeset
    15
use std::path::{Path, PathBuf};
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    16
15762
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
    17
use crate::render::{camera::Camera, GearEntry, GearRenderer, MapRenderer};
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    18
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    19
struct GameState {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    20
    land: Land2D<u32>,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    21
    physics: hwp::World,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    22
}
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    23
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    24
impl GameState {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    25
    fn new(land: Land2D<u32>, physics: hwp::World) -> Self {
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    26
        Self { land, physics }
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    27
    }
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    28
}
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    29
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    30
pub struct World {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    31
    random_numbers_gen: LaggedFibonacciPRNG,
15809
c3971b38bbfa hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents: 15764
diff changeset
    32
    feature_size: u8,
14373
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14295
diff changeset
    33
    preview: Option<Land2D<u8>>,
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    34
    game_state: Option<GameState>,
15190
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14718
diff changeset
    35
    map_renderer: Option<MapRenderer>,
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14718
diff changeset
    36
    gear_renderer: Option<GearRenderer>,
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    37
    camera: Camera,
15762
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
    38
    gear_entries: Vec<GearEntry>,
15890
8a6a2d931bae Implement data path
unc0rr
parents: 15828
diff changeset
    39
    data_path: PathBuf,
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    40
}
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    41
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    42
impl World {
15891
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 15890
diff changeset
    43
    pub fn new(data_path: &Path) -> Self {
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    44
        Self {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    45
            random_numbers_gen: LaggedFibonacciPRNG::new(&[]),
15809
c3971b38bbfa hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents: 15764
diff changeset
    46
            feature_size: 5,
14373
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14295
diff changeset
    47
            preview: None,
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    48
            game_state: None,
15190
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14718
diff changeset
    49
            map_renderer: None,
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14718
diff changeset
    50
            gear_renderer: None,
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    51
            camera: Camera::new(),
15762
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
    52
            gear_entries: vec![],
15891
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 15890
diff changeset
    53
            data_path: data_path.to_owned(),
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    54
        }
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    55
    }
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    56
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    57
    pub fn create_renderer(&mut self, width: u16, height: u16) {
14714
4df1aac5f090 a bit of cleanup
alfadur
parents: 14711
diff changeset
    58
        let land_tile_size = Size::square(512);
15190
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14718
diff changeset
    59
        self.map_renderer = Some(MapRenderer::new(land_tile_size));
15890
8a6a2d931bae Implement data path
unc0rr
parents: 15828
diff changeset
    60
        self.gear_renderer = Some(GearRenderer::new(&self.data_path.as_path()));
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    61
        self.camera = Camera::with_size(Size::new(width as usize, height as usize));
14708
19358c313ebb fix texture initialization
alfadur
parents: 14705
diff changeset
    62
19358c313ebb fix texture initialization
alfadur
parents: 14705
diff changeset
    63
        use mapgen::{theme::Theme, MapGenerator};
19358c313ebb fix texture initialization
alfadur
parents: 14705
diff changeset
    64
19358c313ebb fix texture initialization
alfadur
parents: 14705
diff changeset
    65
        if let Some(ref state) = self.game_state {
14711
40809bfd44af center camera on land
alfadur
parents: 14710
diff changeset
    66
            self.camera.position = state.land.play_box().center();
14714
4df1aac5f090 a bit of cleanup
alfadur
parents: 14711
diff changeset
    67
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15891
diff changeset
    68
            let parameters = LandGenerationParameters::new(0u32, 0x8000u32, 0, false, false);
14708
19358c313ebb fix texture initialization
alfadur
parents: 14705
diff changeset
    69
            let theme =
15890
8a6a2d931bae Implement data path
unc0rr
parents: 15828
diff changeset
    70
                Theme::load(self.data_path.join(Path::new("Themes/Cheese/")).as_path()).unwrap();
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15891
diff changeset
    71
            let texture = MapGenerator::<OutlineTemplate>::new().make_texture(
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15891
diff changeset
    72
                &state.land,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15891
diff changeset
    73
                &parameters,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15891
diff changeset
    74
                &theme,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15891
diff changeset
    75
            );
15190
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14718
diff changeset
    76
            if let Some(ref mut renderer) = self.map_renderer {
14708
19358c313ebb fix texture initialization
alfadur
parents: 14705
diff changeset
    77
                renderer.init(&texture);
19358c313ebb fix texture initialization
alfadur
parents: 14705
diff changeset
    78
            }
19358c313ebb fix texture initialization
alfadur
parents: 14705
diff changeset
    79
        }
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    80
    }
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    81
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14166
diff changeset
    82
    pub fn set_seed(&mut self, seed: &[u8]) {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14166
diff changeset
    83
        self.random_numbers_gen = LaggedFibonacciPRNG::new(seed);
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14166
diff changeset
    84
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14166
diff changeset
    85
15809
c3971b38bbfa hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents: 15764
diff changeset
    86
    pub fn set_feature_size(&mut self, feature_size: u8) {
c3971b38bbfa hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents: 15764
diff changeset
    87
        self.feature_size = feature_size;
c3971b38bbfa hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents: 15764
diff changeset
    88
    }
c3971b38bbfa hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents: 15764
diff changeset
    89
14373
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14295
diff changeset
    90
    pub fn preview(&self) -> &Option<Land2D<u8>> {
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    91
        &self.preview
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    92
    }
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    93
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    94
    pub fn generate_preview(&mut self) {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    95
        fn template() -> OutlineTemplate {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    96
            let mut template = OutlineTemplate::new(Size::new(4096, 2048));
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    97
            template.islands = vec![vec![
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    98
                Rect::from_size_coords(100, 2050, 1, 1),
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    99
                Rect::from_size_coords(100, 500, 400, 1200),
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
   100
                Rect::from_size_coords(3600, 500, 400, 1200),
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
   101
                Rect::from_size_coords(3900, 2050, 1, 1),
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
   102
            ]];
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
   103
            template.fill_points = vec![Point::new(1, 0)];
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
   104
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
   105
            template
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
   106
        }
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
   107
15809
c3971b38bbfa hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents: 15764
diff changeset
   108
        // based on old engine min_distance... dunno if this is the correct place tho
c3971b38bbfa hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents: 15764
diff changeset
   109
        let distance_divisor = (self.feature_size as u32).pow(2) / 8 + 10;
c3971b38bbfa hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents: 15764
diff changeset
   110
15828
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15809
diff changeset
   111
        let params = LandGenerationParameters::new(0u8, u8::MAX, distance_divisor, false, false);
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
   112
        let landgen = TemplatedLandGenerator::new(template());
14373
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14295
diff changeset
   113
        self.preview = Some(landgen.generate_land(&params, &mut self.random_numbers_gen));
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14295
diff changeset
   114
    }
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14295
diff changeset
   115
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14295
diff changeset
   116
    pub fn dispose_preview(&mut self) {
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14295
diff changeset
   117
        self.preview = None
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
   118
    }
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   119
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   120
    pub fn init(&mut self, template: OutlineTemplate) {
15828
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15809
diff changeset
   121
        let params = LandGenerationParameters::new(0u32, u32::MAX, 5, false, false);
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   122
        let landgen = TemplatedLandGenerator::new(template);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   123
        let land = landgen.generate_land(&params, &mut self.random_numbers_gen);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   124
15828
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15809
diff changeset
   125
        let physics = hwp::World::new(land.size());
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15809
diff changeset
   126
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   127
        self.game_state = Some(GameState::new(land, physics));
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   128
    }
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   129
14704
12db7e435ea6 move camera into engine
alfadur
parents: 14702
diff changeset
   130
    pub fn move_camera(&mut self, position_shift: Point, zoom_shift: f32) {
12db7e435ea6 move camera into engine
alfadur
parents: 14702
diff changeset
   131
        self.camera.zoom += zoom_shift;
14709
65c971417780 fix zoom
alfadur
parents: 14708
diff changeset
   132
        self.camera.position += Point::new(
65c971417780 fix zoom
alfadur
parents: 14708
diff changeset
   133
            (position_shift.x as f32 / self.camera.zoom) as i32,
65c971417780 fix zoom
alfadur
parents: 14708
diff changeset
   134
            (position_shift.y as f32 / self.camera.zoom) as i32,
65c971417780 fix zoom
alfadur
parents: 14708
diff changeset
   135
        );
14704
12db7e435ea6 move camera into engine
alfadur
parents: 14702
diff changeset
   136
    }
12db7e435ea6 move camera into engine
alfadur
parents: 14702
diff changeset
   137
12db7e435ea6 move camera into engine
alfadur
parents: 14702
diff changeset
   138
    pub fn render(&mut self) {
15190
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14718
diff changeset
   139
        if let Some(ref mut renderer) = self.map_renderer {
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
   140
            unsafe {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
   141
                gl::ClearColor(0.4f32, 0f32, 0.2f32, 1f32);
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
   142
                gl::Clear(gl::COLOR_BUFFER_BIT);
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
   143
            }
14704
12db7e435ea6 move camera into engine
alfadur
parents: 14702
diff changeset
   144
14718
5915a199cb81 move projection matrix into camera
alfadur
parents: 14716
diff changeset
   145
            renderer.render(&self.camera);
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
   146
        }
15762
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
   147
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
   148
        self.gear_entries.clear();
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
   149
        let mut gear_entries = std::mem::take(&mut self.gear_entries);
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
   150
15190
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14718
diff changeset
   151
        if let Some(ref mut renderer) = self.gear_renderer {
15762
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
   152
            if let Some(ref mut state) = self.game_state {
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
   153
                state
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
   154
                    .physics
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
   155
                    .iter_data()
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
   156
                    .run(|(pos,): (&mut PositionData,)| {
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
   157
                        gear_entries.push(GearEntry::new(
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
   158
                            f64::from(pos.0.x()) as f32,
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
   159
                            f64::from(pos.0.y()) as f32,
15764
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15762
diff changeset
   160
                            Size::square(256),
15762
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
   161
                        ))
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
   162
                    });
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
   163
            }
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
   164
            renderer.render(&self.camera, &gear_entries);
15190
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14718
diff changeset
   165
        }
15762
84c07aa94b61 start drawing gears
alfadur
parents: 15759
diff changeset
   166
        self.gear_entries = gear_entries;
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
   167
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
   168
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14715
diff changeset
   169
    fn create_gear(&mut self, position: Point) {
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14715
diff changeset
   170
        if let Some(ref mut state) = self.game_state {
15274
42b710b0f883 add gear allocator
alfadur
parents: 15263
diff changeset
   171
            let id = state.physics.new_gear().unwrap();
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14715
diff changeset
   172
            let fp_position = FPPoint::new(position.x.into(), position.y.into());
15759
c929e25a7da2 allow finding sprites
alfadur
parents: 15758
diff changeset
   173
            state.physics.add_gear_data(id, &PositionData(fp_position));
c929e25a7da2 allow finding sprites
alfadur
parents: 15758
diff changeset
   174
            state
c929e25a7da2 allow finding sprites
alfadur
parents: 15758
diff changeset
   175
                .physics
c929e25a7da2 allow finding sprites
alfadur
parents: 15758
diff changeset
   176
                .add_gear_data(id, &VelocityData(FPPoint::zero()))
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14715
diff changeset
   177
        }
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14715
diff changeset
   178
    }
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14715
diff changeset
   179
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   180
    pub fn step(&mut self) {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   181
        if let Some(ref mut state) = self.game_state {
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14715
diff changeset
   182
            let next = self.random_numbers_gen.next().unwrap();
15758
6c939e6de981 restore gear generation
alfadur
parents: 15757
diff changeset
   183
            if next % 32 == 0 {
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14715
diff changeset
   184
                let position = Point::new(
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14715
diff changeset
   185
                    (self.random_numbers_gen.next().unwrap() % state.land.width() as u32) as i32,
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14715
diff changeset
   186
                    0,
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14715
diff changeset
   187
                );
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14715
diff changeset
   188
                self.create_gear(position);
15758
6c939e6de981 restore gear generation
alfadur
parents: 15757
diff changeset
   189
            }
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14715
diff changeset
   190
        }
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14715
diff changeset
   191
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14715
diff changeset
   192
        if let Some(ref mut state) = self.game_state {
15275
66c987015f2d replace time with milliseconds
alfadur
parents: 15274
diff changeset
   193
            state.physics.step(Millis::new(1), &state.land);
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   194
        }
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   195
    }
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
   196
}