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