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