author | alfadur |
Thu, 27 Jan 2022 03:51:13 +0300 | |
changeset 15828 | 44b49f255e31 |
parent 15809 | c3971b38bbfa |
child 15890 | 8a6a2d931bae |
permissions | -rw-r--r-- |
14716 | 1 |
use fpnum::{fp, FPNum, FPPoint}; |
15275 | 2 |
use hwphysics::{ |
3 |
self as hwp, |
|
4 |
common::{GearId, Millis}, |
|
15755 | 5 |
physics::{PositionData, VelocityData}, |
15275 | 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::{ |
14705 | 10 |
outline_template::OutlineTemplate, template_based::TemplatedLandGenerator, |
11 |
LandGenerationParameters, LandGenerator, |
|
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
12 |
}; |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
13 |
use lfprng::LaggedFibonacciPRNG; |
14295 | 14 |
|
15762 | 15 |
use crate::render::{camera::Camera, GearEntry, GearRenderer, MapRenderer}; |
14702 | 16 |
|
14295 | 17 |
struct GameState { |
18 |
land: Land2D<u32>, |
|
19 |
physics: hwp::World, |
|
20 |
} |
|
21 |
||
22 |
impl GameState { |
|
23 |
fn new(land: Land2D<u32>, physics: hwp::World) -> Self { |
|
14705 | 24 |
Self { land, physics } |
14295 | 25 |
} |
26 |
} |
|
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
27 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
28 |
pub struct World { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
29 |
random_numbers_gen: LaggedFibonacciPRNG, |
15809
c3971b38bbfa
hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents:
15764
diff
changeset
|
30 |
feature_size: u8, |
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
31 |
preview: Option<Land2D<u8>>, |
14295 | 32 |
game_state: Option<GameState>, |
15190 | 33 |
map_renderer: Option<MapRenderer>, |
34 |
gear_renderer: Option<GearRenderer>, |
|
14705 | 35 |
camera: Camera, |
15762 | 36 |
gear_entries: Vec<GearEntry>, |
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
37 |
} |
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 |
impl World { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
40 |
pub fn new() -> Self { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
41 |
Self { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
42 |
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
|
43 |
feature_size: 5, |
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
44 |
preview: None, |
14295 | 45 |
game_state: None, |
15190 | 46 |
map_renderer: None, |
47 |
gear_renderer: None, |
|
14705 | 48 |
camera: Camera::new(), |
15762 | 49 |
gear_entries: vec![], |
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
50 |
} |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
51 |
} |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
52 |
|
14705 | 53 |
pub fn create_renderer(&mut self, width: u16, height: u16) { |
14714 | 54 |
let land_tile_size = Size::square(512); |
15190 | 55 |
self.map_renderer = Some(MapRenderer::new(land_tile_size)); |
56 |
self.gear_renderer = Some(GearRenderer::new()); |
|
14705 | 57 |
self.camera = Camera::with_size(Size::new(width as usize, height as usize)); |
14708 | 58 |
|
59 |
use mapgen::{theme::Theme, MapGenerator}; |
|
60 |
use std::path::Path; |
|
61 |
||
62 |
if let Some(ref state) = self.game_state { |
|
14711 | 63 |
self.camera.position = state.land.play_box().center(); |
14714 | 64 |
|
14708 | 65 |
let theme = |
66 |
Theme::load(Path::new("../../share/hedgewars/Data/Themes/Cheese/")).unwrap(); |
|
14710 | 67 |
let texture = MapGenerator::new().make_texture(&state.land, &theme); |
15190 | 68 |
if let Some(ref mut renderer) = self.map_renderer { |
14708 | 69 |
renderer.init(&texture); |
70 |
} |
|
71 |
} |
|
14705 | 72 |
} |
73 |
||
14272 | 74 |
pub fn set_seed(&mut self, seed: &[u8]) { |
75 |
self.random_numbers_gen = LaggedFibonacciPRNG::new(seed); |
|
76 |
} |
|
77 |
||
15809
c3971b38bbfa
hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents:
15764
diff
changeset
|
78 |
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
|
79 |
self.feature_size = feature_size; |
c3971b38bbfa
hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents:
15764
diff
changeset
|
80 |
} |
c3971b38bbfa
hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents:
15764
diff
changeset
|
81 |
|
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
82 |
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
|
83 |
&self.preview |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
84 |
} |
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 |
pub fn generate_preview(&mut self) { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
87 |
fn template() -> OutlineTemplate { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
88 |
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
|
89 |
template.islands = vec![vec![ |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
90 |
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
|
91 |
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
|
92 |
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
|
93 |
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
|
94 |
]]; |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
95 |
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
|
96 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
97 |
template |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
98 |
} |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
99 |
|
15809
c3971b38bbfa
hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents:
15764
diff
changeset
|
100 |
// 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
|
101 |
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
|
102 |
|
15828 | 103 |
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
|
104 |
let landgen = TemplatedLandGenerator::new(template()); |
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
105 |
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
|
106 |
} |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
107 |
|
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
108 |
pub fn dispose_preview(&mut self) { |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
109 |
self.preview = None |
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
110 |
} |
14295 | 111 |
|
112 |
pub fn init(&mut self, template: OutlineTemplate) { |
|
15828 | 113 |
let params = LandGenerationParameters::new(0u32, u32::MAX, 5, false, false); |
14295 | 114 |
let landgen = TemplatedLandGenerator::new(template); |
115 |
let land = landgen.generate_land(¶ms, &mut self.random_numbers_gen); |
|
116 |
||
15828 | 117 |
let physics = hwp::World::new(land.size()); |
118 |
||
14295 | 119 |
self.game_state = Some(GameState::new(land, physics)); |
120 |
} |
|
121 |
||
14704 | 122 |
pub fn move_camera(&mut self, position_shift: Point, zoom_shift: f32) { |
123 |
self.camera.zoom += zoom_shift; |
|
14709 | 124 |
self.camera.position += Point::new( |
125 |
(position_shift.x as f32 / self.camera.zoom) as i32, |
|
126 |
(position_shift.y as f32 / self.camera.zoom) as i32, |
|
127 |
); |
|
14704 | 128 |
} |
129 |
||
130 |
pub fn render(&mut self) { |
|
15190 | 131 |
if let Some(ref mut renderer) = self.map_renderer { |
14705 | 132 |
unsafe { |
133 |
gl::ClearColor(0.4f32, 0f32, 0.2f32, 1f32); |
|
134 |
gl::Clear(gl::COLOR_BUFFER_BIT); |
|
135 |
} |
|
14704 | 136 |
|
14718 | 137 |
renderer.render(&self.camera); |
14702 | 138 |
} |
15762 | 139 |
|
140 |
self.gear_entries.clear(); |
|
141 |
let mut gear_entries = std::mem::take(&mut self.gear_entries); |
|
142 |
||
15190 | 143 |
if let Some(ref mut renderer) = self.gear_renderer { |
15762 | 144 |
if let Some(ref mut state) = self.game_state { |
145 |
state |
|
146 |
.physics |
|
147 |
.iter_data() |
|
148 |
.run(|(pos,): (&mut PositionData,)| { |
|
149 |
gear_entries.push(GearEntry::new( |
|
150 |
f64::from(pos.0.x()) as f32, |
|
151 |
f64::from(pos.0.y()) as f32, |
|
15764 | 152 |
Size::square(256), |
15762 | 153 |
)) |
154 |
}); |
|
155 |
} |
|
156 |
renderer.render(&self.camera, &gear_entries); |
|
15190 | 157 |
} |
15762 | 158 |
self.gear_entries = gear_entries; |
14702 | 159 |
} |
160 |
||
14716 | 161 |
fn create_gear(&mut self, position: Point) { |
162 |
if let Some(ref mut state) = self.game_state { |
|
15274 | 163 |
let id = state.physics.new_gear().unwrap(); |
14716 | 164 |
let fp_position = FPPoint::new(position.x.into(), position.y.into()); |
15759 | 165 |
state.physics.add_gear_data(id, &PositionData(fp_position)); |
166 |
state |
|
167 |
.physics |
|
168 |
.add_gear_data(id, &VelocityData(FPPoint::zero())) |
|
14716 | 169 |
} |
170 |
} |
|
171 |
||
14295 | 172 |
pub fn step(&mut self) { |
173 |
if let Some(ref mut state) = self.game_state { |
|
14716 | 174 |
let next = self.random_numbers_gen.next().unwrap(); |
15758 | 175 |
if next % 32 == 0 { |
14716 | 176 |
let position = Point::new( |
177 |
(self.random_numbers_gen.next().unwrap() % state.land.width() as u32) as i32, |
|
178 |
0, |
|
179 |
); |
|
180 |
self.create_gear(position); |
|
15758 | 181 |
} |
14716 | 182 |
} |
183 |
||
184 |
if let Some(ref mut state) = self.game_state { |
|
15275 | 185 |
state.physics.step(Millis::new(1), &state.land); |
14295 | 186 |
} |
187 |
} |
|
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
188 |
} |