rust/lib-hedgewars-engine/src/world.rs
author Wuzzy <Wuzzy2@mail.ru>
Thu, 14 Nov 2019 17:25:48 +0100
changeset 15510 d94c42c174a1
parent 15280 66c987015f2d
permissions -rw-r--r--
ASA moon02: Tweak messages and blue hog face dir
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
     1
use fpnum::{fp, FPNum, FPPoint};
15280
66c987015f2d replace time with milliseconds
alfadur
parents: 15279
diff changeset
     2
use hwphysics::{
66c987015f2d replace time with milliseconds
alfadur
parents: 15279
diff changeset
     3
    self as hwp,
66c987015f2d replace time with milliseconds
alfadur
parents: 15279
diff changeset
     4
    common::{GearId, Millis},
66c987015f2d replace time with milliseconds
alfadur
parents: 15279
diff changeset
     5
};
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
     6
use integral_geometry::{Point, Rect, Size};
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
     7
use land2d::Land2D;
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
     8
use landgen::{
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14709
diff changeset
     9
    outline_template::OutlineTemplate, template_based::TemplatedLandGenerator,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14709
diff changeset
    10
    LandGenerationParameters, LandGenerator,
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    11
};
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    12
use lfprng::LaggedFibonacciPRNG;
14300
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
    13
15195
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14723
diff changeset
    14
use crate::render::{camera::Camera, GearRenderer, MapRenderer};
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14378
diff changeset
    15
14300
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
    16
struct GameState {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
    17
    land: Land2D<u32>,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
    18
    physics: hwp::World,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
    19
}
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
    20
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
    21
impl GameState {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
    22
    fn new(land: Land2D<u32>, physics: hwp::World) -> Self {
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14709
diff changeset
    23
        Self { land, physics }
14300
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
    24
    }
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
    25
}
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    26
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    27
pub struct World {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    28
    random_numbers_gen: LaggedFibonacciPRNG,
14378
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14300
diff changeset
    29
    preview: Option<Land2D<u8>>,
14300
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
    30
    game_state: Option<GameState>,
15195
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14723
diff changeset
    31
    map_renderer: Option<MapRenderer>,
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14723
diff changeset
    32
    gear_renderer: Option<GearRenderer>,
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14709
diff changeset
    33
    camera: Camera,
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    34
}
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    35
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    36
impl World {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    37
    pub fn new() -> Self {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    38
        Self {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    39
            random_numbers_gen: LaggedFibonacciPRNG::new(&[]),
14378
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14300
diff changeset
    40
            preview: None,
14300
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
    41
            game_state: None,
15195
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14723
diff changeset
    42
            map_renderer: None,
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14723
diff changeset
    43
            gear_renderer: None,
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14709
diff changeset
    44
            camera: Camera::new(),
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    45
        }
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    46
    }
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    47
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14709
diff changeset
    48
    pub fn create_renderer(&mut self, width: u16, height: u16) {
14719
4df1aac5f090 a bit of cleanup
alfadur
parents: 14716
diff changeset
    49
        let land_tile_size = Size::square(512);
15195
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14723
diff changeset
    50
        self.map_renderer = Some(MapRenderer::new(land_tile_size));
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14723
diff changeset
    51
        self.gear_renderer = Some(GearRenderer::new());
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14709
diff changeset
    52
        self.camera = Camera::with_size(Size::new(width as usize, height as usize));
14713
19358c313ebb fix texture initialization
alfadur
parents: 14710
diff changeset
    53
19358c313ebb fix texture initialization
alfadur
parents: 14710
diff changeset
    54
        use mapgen::{theme::Theme, MapGenerator};
19358c313ebb fix texture initialization
alfadur
parents: 14710
diff changeset
    55
        use std::path::Path;
19358c313ebb fix texture initialization
alfadur
parents: 14710
diff changeset
    56
19358c313ebb fix texture initialization
alfadur
parents: 14710
diff changeset
    57
        if let Some(ref state) = self.game_state {
14716
40809bfd44af center camera on land
alfadur
parents: 14715
diff changeset
    58
            self.camera.position = state.land.play_box().center();
14719
4df1aac5f090 a bit of cleanup
alfadur
parents: 14716
diff changeset
    59
14713
19358c313ebb fix texture initialization
alfadur
parents: 14710
diff changeset
    60
            let theme =
19358c313ebb fix texture initialization
alfadur
parents: 14710
diff changeset
    61
                Theme::load(Path::new("../../share/hedgewars/Data/Themes/Cheese/")).unwrap();
14715
946df0bb3b28 collapse mapgen back
alfadur
parents: 14714
diff changeset
    62
            let texture = MapGenerator::new().make_texture(&state.land, &theme);
15195
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14723
diff changeset
    63
            if let Some(ref mut renderer) = self.map_renderer {
14713
19358c313ebb fix texture initialization
alfadur
parents: 14710
diff changeset
    64
                renderer.init(&texture);
19358c313ebb fix texture initialization
alfadur
parents: 14710
diff changeset
    65
            }
19358c313ebb fix texture initialization
alfadur
parents: 14710
diff changeset
    66
        }
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14709
diff changeset
    67
    }
19122a329774 make world renderer optional + fmt
alfadur
parents: 14709
diff changeset
    68
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14171
diff changeset
    69
    pub fn set_seed(&mut self, seed: &[u8]) {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14171
diff changeset
    70
        self.random_numbers_gen = LaggedFibonacciPRNG::new(seed);
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14171
diff changeset
    71
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14171
diff changeset
    72
14378
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14300
diff changeset
    73
    pub fn preview(&self) -> &Option<Land2D<u8>> {
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    74
        &self.preview
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    75
    }
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    76
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    77
    pub fn generate_preview(&mut self) {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    78
        fn template() -> OutlineTemplate {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    79
            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
    80
            template.islands = vec![vec![
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    81
                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
    82
                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
    83
                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
    84
                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
    85
            ]];
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    86
            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
    87
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    88
            template
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    89
        }
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    90
14300
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
    91
        let params = LandGenerationParameters::new(0u8, u8::max_value(), 5, false, false);
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    92
        let landgen = TemplatedLandGenerator::new(template());
14378
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14300
diff changeset
    93
        self.preview = Some(landgen.generate_land(&params, &mut self.random_numbers_gen));
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14300
diff changeset
    94
    }
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14300
diff changeset
    95
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14300
diff changeset
    96
    pub fn dispose_preview(&mut self) {
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14300
diff changeset
    97
        self.preview = None
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    98
    }
14300
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
    99
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
   100
    pub fn init(&mut self, template: OutlineTemplate) {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
   101
        let physics = hwp::World::new(template.size);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
   102
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
   103
        let params = LandGenerationParameters::new(0u32, u32::max_value(), 5, false, false);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
   104
        let landgen = TemplatedLandGenerator::new(template);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
   105
        let land = landgen.generate_land(&params, &mut self.random_numbers_gen);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
   106
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
   107
        self.game_state = Some(GameState::new(land, physics));
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
   108
    }
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
   109
14709
12db7e435ea6 move camera into engine
alfadur
parents: 14707
diff changeset
   110
    pub fn move_camera(&mut self, position_shift: Point, zoom_shift: f32) {
12db7e435ea6 move camera into engine
alfadur
parents: 14707
diff changeset
   111
        self.camera.zoom += zoom_shift;
14714
65c971417780 fix zoom
alfadur
parents: 14713
diff changeset
   112
        self.camera.position += Point::new(
65c971417780 fix zoom
alfadur
parents: 14713
diff changeset
   113
            (position_shift.x as f32 / self.camera.zoom) as i32,
65c971417780 fix zoom
alfadur
parents: 14713
diff changeset
   114
            (position_shift.y as f32 / self.camera.zoom) as i32,
65c971417780 fix zoom
alfadur
parents: 14713
diff changeset
   115
        );
14709
12db7e435ea6 move camera into engine
alfadur
parents: 14707
diff changeset
   116
    }
12db7e435ea6 move camera into engine
alfadur
parents: 14707
diff changeset
   117
12db7e435ea6 move camera into engine
alfadur
parents: 14707
diff changeset
   118
    pub fn render(&mut self) {
15195
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14723
diff changeset
   119
        if let Some(ref mut renderer) = self.map_renderer {
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14709
diff changeset
   120
            unsafe {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14709
diff changeset
   121
                gl::ClearColor(0.4f32, 0f32, 0.2f32, 1f32);
19122a329774 make world renderer optional + fmt
alfadur
parents: 14709
diff changeset
   122
                gl::Clear(gl::COLOR_BUFFER_BIT);
19122a329774 make world renderer optional + fmt
alfadur
parents: 14709
diff changeset
   123
            }
14709
12db7e435ea6 move camera into engine
alfadur
parents: 14707
diff changeset
   124
14723
5915a199cb81 move projection matrix into camera
alfadur
parents: 14721
diff changeset
   125
            renderer.render(&self.camera);
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14378
diff changeset
   126
        }
15195
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14723
diff changeset
   127
        if let Some(ref mut renderer) = self.gear_renderer {
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14723
diff changeset
   128
            renderer.render(&self.camera)
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 14723
diff changeset
   129
        }
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14378
diff changeset
   130
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14378
diff changeset
   131
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   132
    fn create_gear(&mut self, position: Point) {
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   133
        if let Some(ref mut state) = self.game_state {
15279
42b710b0f883 add gear allocator
alfadur
parents: 15268
diff changeset
   134
            let id = state.physics.new_gear().unwrap();
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   135
            let fp_position = FPPoint::new(position.x.into(), position.y.into());
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   136
            state.physics.add_gear_data(
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   137
                id,
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   138
                hwp::physics::PhysicsData::new(fp_position, FPPoint::zero()),
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   139
            )
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   140
        }
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   141
    }
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   142
14300
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
   143
    pub fn step(&mut self) {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
   144
        if let Some(ref mut state) = self.game_state {
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   145
            let next = self.random_numbers_gen.next().unwrap();
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   146
            if next % 32 == 0 {
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   147
                let position = Point::new(
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   148
                    (self.random_numbers_gen.next().unwrap() % state.land.width() as u32) as i32,
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   149
                    0,
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   150
                );
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   151
                self.create_gear(position);
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   152
            }
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   153
        }
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   154
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14720
diff changeset
   155
        if let Some(ref mut state) = self.game_state {
15280
66c987015f2d replace time with milliseconds
alfadur
parents: 15279
diff changeset
   156
            state.physics.step(Millis::new(1), &state.land);
14300
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
   157
        }
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14277
diff changeset
   158
    }
14159
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
   159
}