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