author | unc0rr |
Fri, 22 Mar 2019 21:29:10 +0100 | |
changeset 14713 | cc6ab1e3f7d5 |
parent 14711 | 40809bfd44af |
child 14715 | e519802076e9 |
permissions | -rw-r--r-- |
14705 | 1 |
use fpnum::{fp, FPNum}; |
2 |
use hwphysics as hwp; |
|
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
3 |
use integral_geometry::{Point, Rect, Size}; |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
4 |
use land2d::Land2D; |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
5 |
use landgen::{ |
14705 | 6 |
outline_template::OutlineTemplate, template_based::TemplatedLandGenerator, |
7 |
LandGenerationParameters, LandGenerator, |
|
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
8 |
}; |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
9 |
use lfprng::LaggedFibonacciPRNG; |
14295 | 10 |
|
14705 | 11 |
use crate::render::{camera::Camera, MapRenderer}; |
14702 | 12 |
|
14295 | 13 |
struct GameState { |
14 |
land: Land2D<u32>, |
|
15 |
physics: hwp::World, |
|
16 |
} |
|
17 |
||
18 |
impl GameState { |
|
19 |
fn new(land: Land2D<u32>, physics: hwp::World) -> Self { |
|
14705 | 20 |
Self { land, physics } |
14295 | 21 |
} |
22 |
} |
|
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
23 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
24 |
pub struct World { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
25 |
random_numbers_gen: LaggedFibonacciPRNG, |
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
26 |
preview: Option<Land2D<u8>>, |
14295 | 27 |
game_state: Option<GameState>, |
14705 | 28 |
renderer: Option<MapRenderer>, |
29 |
camera: Camera, |
|
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
30 |
} |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
31 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
32 |
impl World { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
33 |
pub fn new() -> Self { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
34 |
Self { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
35 |
random_numbers_gen: LaggedFibonacciPRNG::new(&[]), |
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
36 |
preview: None, |
14295 | 37 |
game_state: None, |
14705 | 38 |
renderer: None, |
39 |
camera: Camera::new(), |
|
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 |
|
14705 | 43 |
pub fn create_renderer(&mut self, width: u16, height: u16) { |
44 |
self.renderer = Some(MapRenderer::new(512, 512)); |
|
45 |
self.camera = Camera::with_size(Size::new(width as usize, height as usize)); |
|
14708 | 46 |
|
47 |
use mapgen::{theme::Theme, MapGenerator}; |
|
48 |
use std::path::Path; |
|
49 |
||
50 |
if let Some(ref state) = self.game_state { |
|
14711 | 51 |
self.camera.position = state.land.play_box().center(); |
14713 | 52 |
|
14708 | 53 |
let theme = |
54 |
Theme::load(Path::new("../../share/hedgewars/Data/Themes/Cheese/")).unwrap(); |
|
14710 | 55 |
let texture = MapGenerator::new().make_texture(&state.land, &theme); |
14708 | 56 |
if let Some(ref mut renderer) = self.renderer { |
57 |
renderer.init(&texture); |
|
58 |
} |
|
59 |
} |
|
14705 | 60 |
} |
61 |
||
14272 | 62 |
pub fn set_seed(&mut self, seed: &[u8]) { |
63 |
self.random_numbers_gen = LaggedFibonacciPRNG::new(seed); |
|
64 |
} |
|
65 |
||
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
66 |
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
|
67 |
&self.preview |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
68 |
} |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
69 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
70 |
pub fn generate_preview(&mut self) { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
71 |
fn template() -> OutlineTemplate { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
72 |
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
|
73 |
template.islands = vec![vec![ |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
74 |
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
|
75 |
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
|
76 |
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
|
77 |
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
|
78 |
]]; |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
79 |
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
|
80 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
81 |
template |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
82 |
} |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
83 |
|
14295 | 84 |
let params = LandGenerationParameters::new(0u8, u8::max_value(), 5, false, false); |
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
85 |
let landgen = TemplatedLandGenerator::new(template()); |
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
86 |
self.preview = Some(landgen.generate_land(¶ms, &mut self.random_numbers_gen)); |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
87 |
} |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
88 |
|
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
89 |
pub fn dispose_preview(&mut self) { |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
90 |
self.preview = None |
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
91 |
} |
14295 | 92 |
|
93 |
pub fn init(&mut self, template: OutlineTemplate) { |
|
94 |
let physics = hwp::World::new(template.size); |
|
95 |
||
96 |
let params = LandGenerationParameters::new(0u32, u32::max_value(), 5, false, false); |
|
97 |
let landgen = TemplatedLandGenerator::new(template); |
|
98 |
let land = landgen.generate_land(¶ms, &mut self.random_numbers_gen); |
|
99 |
||
100 |
self.game_state = Some(GameState::new(land, physics)); |
|
101 |
} |
|
102 |
||
14704 | 103 |
pub fn move_camera(&mut self, position_shift: Point, zoom_shift: f32) { |
104 |
self.camera.zoom += zoom_shift; |
|
14709 | 105 |
self.camera.position += Point::new( |
106 |
(position_shift.x as f32 / self.camera.zoom) as i32, |
|
107 |
(position_shift.y as f32 / self.camera.zoom) as i32, |
|
108 |
); |
|
14704 | 109 |
} |
110 |
||
111 |
pub fn render(&mut self) { |
|
14705 | 112 |
if let Some(ref mut renderer) = self.renderer { |
113 |
unsafe { |
|
114 |
gl::ClearColor(0.4f32, 0f32, 0.2f32, 1f32); |
|
115 |
gl::Clear(gl::COLOR_BUFFER_BIT); |
|
116 |
} |
|
14704 | 117 |
|
14705 | 118 |
renderer.render(self.camera.viewport()); |
14702 | 119 |
} |
120 |
} |
|
121 |
||
14295 | 122 |
pub fn step(&mut self) { |
123 |
if let Some(ref mut state) = self.game_state { |
|
124 |
state.physics.step(fp!(1), &state.land); |
|
125 |
} |
|
126 |
} |
|
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
127 |
} |