author | unC0Rr |
Sat, 14 Dec 2024 18:07:29 +0100 | |
branch | transitional_engine |
changeset 16077 | 67469603872a |
parent 16051 | 2003b466b279 |
permissions | -rw-r--r-- |
14737 | 1 |
use fpnum::{fp, FPNum, FPPoint}; |
15296 | 2 |
use hwphysics::{ |
3 |
self as hwp, |
|
4 |
common::{GearId, Millis}, |
|
15777 | 5 |
physics::{PositionData, VelocityData}, |
15296 | 6 |
}; |
14175
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::{ |
15954 | 10 |
outline_template_based::outline_template::OutlineTemplate, |
11 |
outline_template_based::template_based::TemplatedLandGenerator, LandGenerationParameters, |
|
12 |
LandGenerator, |
|
14175
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
13 |
}; |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
14 |
use lfprng::LaggedFibonacciPRNG; |
15918 | 15 |
use std::path::{Path, PathBuf}; |
16046
e8afb1bf2779
Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents:
15919
diff
changeset
|
16 |
use log::trace; |
14316 | 17 |
|
15784 | 18 |
use crate::render::{camera::Camera, GearEntry, GearRenderer, MapRenderer}; |
14723 | 19 |
|
14316 | 20 |
struct GameState { |
21 |
land: Land2D<u32>, |
|
22 |
physics: hwp::World, |
|
23 |
} |
|
24 |
||
25 |
impl GameState { |
|
26 |
fn new(land: Land2D<u32>, physics: hwp::World) -> Self { |
|
14726 | 27 |
Self { land, physics } |
14316 | 28 |
} |
29 |
} |
|
14175
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 |
pub struct World { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
32 |
random_numbers_gen: LaggedFibonacciPRNG, |
15831
c3971b38bbfa
hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents:
15786
diff
changeset
|
33 |
feature_size: u8, |
14394
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
34 |
preview: Option<Land2D<u8>>, |
14316 | 35 |
game_state: Option<GameState>, |
15211 | 36 |
map_renderer: Option<MapRenderer>, |
37 |
gear_renderer: Option<GearRenderer>, |
|
14726 | 38 |
camera: Camera, |
15784 | 39 |
gear_entries: Vec<GearEntry>, |
15918 | 40 |
data_path: PathBuf, |
14175
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 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
43 |
impl World { |
15919 | 44 |
pub fn new(data_path: &Path) -> Self { |
14175
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
45 |
Self { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
46 |
random_numbers_gen: LaggedFibonacciPRNG::new(&[]), |
15831
c3971b38bbfa
hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents:
15786
diff
changeset
|
47 |
feature_size: 5, |
14394
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
48 |
preview: None, |
14316 | 49 |
game_state: None, |
15211 | 50 |
map_renderer: None, |
51 |
gear_renderer: None, |
|
14726 | 52 |
camera: Camera::new(), |
15784 | 53 |
gear_entries: vec![], |
15919 | 54 |
data_path: data_path.to_owned(), |
14175
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 |
} |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
57 |
|
14726 | 58 |
pub fn create_renderer(&mut self, width: u16, height: u16) { |
14735 | 59 |
let land_tile_size = Size::square(512); |
15211 | 60 |
self.map_renderer = Some(MapRenderer::new(land_tile_size)); |
15918 | 61 |
self.gear_renderer = Some(GearRenderer::new(&self.data_path.as_path())); |
14726 | 62 |
self.camera = Camera::with_size(Size::new(width as usize, height as usize)); |
14729 | 63 |
|
16046
e8afb1bf2779
Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents:
15919
diff
changeset
|
64 |
if let Some(ref state) = self.game_state { |
e8afb1bf2779
Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents:
15919
diff
changeset
|
65 |
self.camera.position = state.land.play_box().center(); |
e8afb1bf2779
Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents:
15919
diff
changeset
|
66 |
} |
e8afb1bf2779
Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents:
15919
diff
changeset
|
67 |
} |
e8afb1bf2779
Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents:
15919
diff
changeset
|
68 |
|
e8afb1bf2779
Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents:
15919
diff
changeset
|
69 |
pub fn init_renderer(&mut self) { |
14729 | 70 |
use mapgen::{theme::Theme, MapGenerator}; |
71 |
||
72 |
if let Some(ref state) = self.game_state { |
|
14732 | 73 |
self.camera.position = state.land.play_box().center(); |
14735 | 74 |
|
15954 | 75 |
let parameters = LandGenerationParameters::new(0u32, 0x8000u32, 0, false, false); |
14729 | 76 |
let theme = |
15918 | 77 |
Theme::load(self.data_path.join(Path::new("Themes/Cheese/")).as_path()).unwrap(); |
15954 | 78 |
let texture = MapGenerator::<OutlineTemplate>::new().make_texture( |
79 |
&state.land, |
|
80 |
¶meters, |
|
81 |
&theme, |
|
82 |
); |
|
15211 | 83 |
if let Some(ref mut renderer) = self.map_renderer { |
14729 | 84 |
renderer.init(&texture); |
85 |
} |
|
86 |
} |
|
14726 | 87 |
} |
88 |
||
14293 | 89 |
pub fn set_seed(&mut self, seed: &[u8]) { |
90 |
self.random_numbers_gen = LaggedFibonacciPRNG::new(seed); |
|
91 |
} |
|
92 |
||
15831
c3971b38bbfa
hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents:
15786
diff
changeset
|
93 |
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:
15786
diff
changeset
|
94 |
self.feature_size = feature_size; |
c3971b38bbfa
hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents:
15786
diff
changeset
|
95 |
} |
c3971b38bbfa
hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents:
15786
diff
changeset
|
96 |
|
14394
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
97 |
pub fn preview(&self) -> &Option<Land2D<u8>> { |
14175
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
98 |
&self.preview |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
99 |
} |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
100 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
101 |
pub fn generate_preview(&mut self) { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
102 |
fn template() -> OutlineTemplate { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
103 |
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
|
104 |
template.islands = vec![vec![ |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
105 |
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
|
106 |
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
|
107 |
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
|
108 |
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
|
109 |
]]; |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
110 |
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
|
111 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
112 |
template |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
113 |
} |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
114 |
|
15831
c3971b38bbfa
hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents:
15786
diff
changeset
|
115 |
// 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:
15786
diff
changeset
|
116 |
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:
15786
diff
changeset
|
117 |
|
15850 | 118 |
let params = LandGenerationParameters::new(0u8, u8::MAX, distance_divisor, false, false); |
14175
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
119 |
let landgen = TemplatedLandGenerator::new(template()); |
14394
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
120 |
self.preview = Some(landgen.generate_land(¶ms, &mut self.random_numbers_gen)); |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
121 |
} |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
122 |
|
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
123 |
pub fn dispose_preview(&mut self) { |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
124 |
self.preview = None |
14175
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
125 |
} |
14316 | 126 |
|
127 |
pub fn init(&mut self, template: OutlineTemplate) { |
|
15850 | 128 |
let params = LandGenerationParameters::new(0u32, u32::MAX, 5, false, false); |
14316 | 129 |
let landgen = TemplatedLandGenerator::new(template); |
130 |
let land = landgen.generate_land(¶ms, &mut self.random_numbers_gen); |
|
131 |
||
15850 | 132 |
let physics = hwp::World::new(land.size()); |
133 |
||
14316 | 134 |
self.game_state = Some(GameState::new(land, physics)); |
135 |
} |
|
136 |
||
14725 | 137 |
pub fn move_camera(&mut self, position_shift: Point, zoom_shift: f32) { |
138 |
self.camera.zoom += zoom_shift; |
|
14730 | 139 |
self.camera.position += Point::new( |
140 |
(position_shift.x as f32 / self.camera.zoom) as i32, |
|
141 |
(position_shift.y as f32 / self.camera.zoom) as i32, |
|
142 |
); |
|
14725 | 143 |
} |
144 |
||
145 |
pub fn render(&mut self) { |
|
15211 | 146 |
if let Some(ref mut renderer) = self.map_renderer { |
14726 | 147 |
unsafe { |
148 |
gl::ClearColor(0.4f32, 0f32, 0.2f32, 1f32); |
|
149 |
gl::Clear(gl::COLOR_BUFFER_BIT); |
|
150 |
} |
|
14725 | 151 |
|
14739 | 152 |
renderer.render(&self.camera); |
14723 | 153 |
} |
15784 | 154 |
|
155 |
self.gear_entries.clear(); |
|
156 |
let mut gear_entries = std::mem::take(&mut self.gear_entries); |
|
157 |
||
15211 | 158 |
if let Some(ref mut renderer) = self.gear_renderer { |
15784 | 159 |
if let Some(ref mut state) = self.game_state { |
160 |
state |
|
161 |
.physics |
|
162 |
.iter_data() |
|
163 |
.run(|(pos,): (&mut PositionData,)| { |
|
164 |
gear_entries.push(GearEntry::new( |
|
165 |
f64::from(pos.0.x()) as f32, |
|
166 |
f64::from(pos.0.y()) as f32, |
|
15786 | 167 |
Size::square(256), |
15784 | 168 |
)) |
169 |
}); |
|
170 |
} |
|
171 |
renderer.render(&self.camera, &gear_entries); |
|
15211 | 172 |
} |
15784 | 173 |
self.gear_entries = gear_entries; |
14723 | 174 |
} |
175 |
||
14737 | 176 |
fn create_gear(&mut self, position: Point) { |
177 |
if let Some(ref mut state) = self.game_state { |
|
15295 | 178 |
let id = state.physics.new_gear().unwrap(); |
14737 | 179 |
let fp_position = FPPoint::new(position.x.into(), position.y.into()); |
15781 | 180 |
state.physics.add_gear_data(id, &PositionData(fp_position)); |
181 |
state |
|
182 |
.physics |
|
183 |
.add_gear_data(id, &VelocityData(FPPoint::zero())) |
|
14737 | 184 |
} |
185 |
} |
|
186 |
||
14316 | 187 |
pub fn step(&mut self) { |
188 |
if let Some(ref mut state) = self.game_state { |
|
14737 | 189 |
let next = self.random_numbers_gen.next().unwrap(); |
15780 | 190 |
if next % 32 == 0 { |
14737 | 191 |
let position = Point::new( |
192 |
(self.random_numbers_gen.next().unwrap() % state.land.width() as u32) as i32, |
|
193 |
0, |
|
194 |
); |
|
195 |
self.create_gear(position); |
|
15780 | 196 |
} |
14737 | 197 |
} |
198 |
||
199 |
if let Some(ref mut state) = self.game_state { |
|
15296 | 200 |
state.physics.step(Millis::new(1), &state.land); |
14316 | 201 |
} |
202 |
} |
|
14175
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
203 |
} |