author | Wuzzy <Wuzzy2@mail.ru> |
Sun, 05 Jul 2020 22:40:11 +0200 | |
changeset 15682 | 842385b5e0f6 |
parent 15275 | 66c987015f2d |
child 15755 | 2eb3469a28a0 |
permissions | -rw-r--r-- |
14716 | 1 |
use fpnum::{fp, FPNum, FPPoint}; |
15275 | 2 |
use hwphysics::{ |
3 |
self as hwp, |
|
4 |
common::{GearId, Millis}, |
|
5 |
}; |
|
14154
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::{ |
14705 | 9 |
outline_template::OutlineTemplate, template_based::TemplatedLandGenerator, |
10 |
LandGenerationParameters, LandGenerator, |
|
14154
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; |
14295 | 13 |
|
15190 | 14 |
use crate::render::{camera::Camera, GearRenderer, MapRenderer}; |
14702 | 15 |
|
14295 | 16 |
struct GameState { |
17 |
land: Land2D<u32>, |
|
18 |
physics: hwp::World, |
|
19 |
} |
|
20 |
||
21 |
impl GameState { |
|
22 |
fn new(land: Land2D<u32>, physics: hwp::World) -> Self { |
|
14705 | 23 |
Self { land, physics } |
14295 | 24 |
} |
25 |
} |
|
14154
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, |
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
29 |
preview: Option<Land2D<u8>>, |
14295 | 30 |
game_state: Option<GameState>, |
15190 | 31 |
map_renderer: Option<MapRenderer>, |
32 |
gear_renderer: Option<GearRenderer>, |
|
14705 | 33 |
camera: Camera, |
14154
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(&[]), |
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
40 |
preview: None, |
14295 | 41 |
game_state: None, |
15190 | 42 |
map_renderer: None, |
43 |
gear_renderer: None, |
|
14705 | 44 |
camera: Camera::new(), |
14154
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 |
|
14705 | 48 |
pub fn create_renderer(&mut self, width: u16, height: u16) { |
14714 | 49 |
let land_tile_size = Size::square(512); |
15190 | 50 |
self.map_renderer = Some(MapRenderer::new(land_tile_size)); |
51 |
self.gear_renderer = Some(GearRenderer::new()); |
|
14705 | 52 |
self.camera = Camera::with_size(Size::new(width as usize, height as usize)); |
14708 | 53 |
|
54 |
use mapgen::{theme::Theme, MapGenerator}; |
|
55 |
use std::path::Path; |
|
56 |
||
57 |
if let Some(ref state) = self.game_state { |
|
14711 | 58 |
self.camera.position = state.land.play_box().center(); |
14714 | 59 |
|
14708 | 60 |
let theme = |
61 |
Theme::load(Path::new("../../share/hedgewars/Data/Themes/Cheese/")).unwrap(); |
|
14710 | 62 |
let texture = MapGenerator::new().make_texture(&state.land, &theme); |
15190 | 63 |
if let Some(ref mut renderer) = self.map_renderer { |
14708 | 64 |
renderer.init(&texture); |
65 |
} |
|
66 |
} |
|
14705 | 67 |
} |
68 |
||
14272 | 69 |
pub fn set_seed(&mut self, seed: &[u8]) { |
70 |
self.random_numbers_gen = LaggedFibonacciPRNG::new(seed); |
|
71 |
} |
|
72 |
||
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
73 |
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
|
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 |
|
14295 | 91 |
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
|
92 |
let landgen = TemplatedLandGenerator::new(template()); |
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
93 |
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
|
94 |
} |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
95 |
|
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
96 |
pub fn dispose_preview(&mut self) { |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
97 |
self.preview = None |
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
98 |
} |
14295 | 99 |
|
100 |
pub fn init(&mut self, template: OutlineTemplate) { |
|
101 |
let physics = hwp::World::new(template.size); |
|
102 |
||
103 |
let params = LandGenerationParameters::new(0u32, u32::max_value(), 5, false, false); |
|
104 |
let landgen = TemplatedLandGenerator::new(template); |
|
105 |
let land = landgen.generate_land(¶ms, &mut self.random_numbers_gen); |
|
106 |
||
107 |
self.game_state = Some(GameState::new(land, physics)); |
|
108 |
} |
|
109 |
||
14704 | 110 |
pub fn move_camera(&mut self, position_shift: Point, zoom_shift: f32) { |
111 |
self.camera.zoom += zoom_shift; |
|
14709 | 112 |
self.camera.position += Point::new( |
113 |
(position_shift.x as f32 / self.camera.zoom) as i32, |
|
114 |
(position_shift.y as f32 / self.camera.zoom) as i32, |
|
115 |
); |
|
14704 | 116 |
} |
117 |
||
118 |
pub fn render(&mut self) { |
|
15190 | 119 |
if let Some(ref mut renderer) = self.map_renderer { |
14705 | 120 |
unsafe { |
121 |
gl::ClearColor(0.4f32, 0f32, 0.2f32, 1f32); |
|
122 |
gl::Clear(gl::COLOR_BUFFER_BIT); |
|
123 |
} |
|
14704 | 124 |
|
14718 | 125 |
renderer.render(&self.camera); |
14702 | 126 |
} |
15190 | 127 |
if let Some(ref mut renderer) = self.gear_renderer { |
128 |
renderer.render(&self.camera) |
|
129 |
} |
|
14702 | 130 |
} |
131 |
||
14716 | 132 |
fn create_gear(&mut self, position: Point) { |
133 |
if let Some(ref mut state) = self.game_state { |
|
15274 | 134 |
let id = state.physics.new_gear().unwrap(); |
14716 | 135 |
let fp_position = FPPoint::new(position.x.into(), position.y.into()); |
136 |
state.physics.add_gear_data( |
|
137 |
id, |
|
138 |
hwp::physics::PhysicsData::new(fp_position, FPPoint::zero()), |
|
139 |
) |
|
140 |
} |
|
141 |
} |
|
142 |
||
14295 | 143 |
pub fn step(&mut self) { |
144 |
if let Some(ref mut state) = self.game_state { |
|
14716 | 145 |
let next = self.random_numbers_gen.next().unwrap(); |
146 |
if next % 32 == 0 { |
|
147 |
let position = Point::new( |
|
148 |
(self.random_numbers_gen.next().unwrap() % state.land.width() as u32) as i32, |
|
149 |
0, |
|
150 |
); |
|
151 |
self.create_gear(position); |
|
152 |
} |
|
153 |
} |
|
154 |
||
155 |
if let Some(ref mut state) = self.game_state { |
|
15275 | 156 |
state.physics.step(Millis::new(1), &state.land); |
14295 | 157 |
} |
158 |
} |
|
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
159 |
} |