author | Wuzzy <Wuzzy2@mail.ru> |
Thu, 14 Nov 2019 17:25:48 +0100 | |
changeset 15510 | d94c42c174a1 |
parent 15280 | 66c987015f2d |
permissions | -rw-r--r-- |
14721 | 1 |
use fpnum::{fp, FPNum, FPPoint}; |
15280 | 2 |
use hwphysics::{ |
3 |
self as hwp, |
|
4 |
common::{GearId, Millis}, |
|
5 |
}; |
|
14159
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::{ |
14710 | 9 |
outline_template::OutlineTemplate, template_based::TemplatedLandGenerator, |
10 |
LandGenerationParameters, LandGenerator, |
|
14159
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; |
14300 | 13 |
|
15195 | 14 |
use crate::render::{camera::Camera, GearRenderer, MapRenderer}; |
14707 | 15 |
|
14300 | 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 { |
|
14710 | 23 |
Self { land, physics } |
14300 | 24 |
} |
25 |
} |
|
14159
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, |
14378
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14300
diff
changeset
|
29 |
preview: Option<Land2D<u8>>, |
14300 | 30 |
game_state: Option<GameState>, |
15195 | 31 |
map_renderer: Option<MapRenderer>, |
32 |
gear_renderer: Option<GearRenderer>, |
|
14710 | 33 |
camera: Camera, |
14159
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(&[]), |
14378
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14300
diff
changeset
|
40 |
preview: None, |
14300 | 41 |
game_state: None, |
15195 | 42 |
map_renderer: None, |
43 |
gear_renderer: None, |
|
14710 | 44 |
camera: Camera::new(), |
14159
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 |
|
14710 | 48 |
pub fn create_renderer(&mut self, width: u16, height: u16) { |
14719 | 49 |
let land_tile_size = Size::square(512); |
15195 | 50 |
self.map_renderer = Some(MapRenderer::new(land_tile_size)); |
51 |
self.gear_renderer = Some(GearRenderer::new()); |
|
14710 | 52 |
self.camera = Camera::with_size(Size::new(width as usize, height as usize)); |
14713 | 53 |
|
54 |
use mapgen::{theme::Theme, MapGenerator}; |
|
55 |
use std::path::Path; |
|
56 |
||
57 |
if let Some(ref state) = self.game_state { |
|
14716 | 58 |
self.camera.position = state.land.play_box().center(); |
14719 | 59 |
|
14713 | 60 |
let theme = |
61 |
Theme::load(Path::new("../../share/hedgewars/Data/Themes/Cheese/")).unwrap(); |
|
14715 | 62 |
let texture = MapGenerator::new().make_texture(&state.land, &theme); |
15195 | 63 |
if let Some(ref mut renderer) = self.map_renderer { |
14713 | 64 |
renderer.init(&texture); |
65 |
} |
|
66 |
} |
|
14710 | 67 |
} |
68 |
||
14277 | 69 |
pub fn set_seed(&mut self, seed: &[u8]) { |
70 |
self.random_numbers_gen = LaggedFibonacciPRNG::new(seed); |
|
71 |
} |
|
72 |
||
14378
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14300
diff
changeset
|
73 |
pub fn preview(&self) -> &Option<Land2D<u8>> { |
14159
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 |
|
14300 | 91 |
let params = LandGenerationParameters::new(0u8, u8::max_value(), 5, false, false); |
14159
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
92 |
let landgen = TemplatedLandGenerator::new(template()); |
14378
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14300
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:
14300
diff
changeset
|
94 |
} |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14300
diff
changeset
|
95 |
|
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14300
diff
changeset
|
96 |
pub fn dispose_preview(&mut self) { |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14300
diff
changeset
|
97 |
self.preview = None |
14159
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
98 |
} |
14300 | 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 |
||
14709 | 110 |
pub fn move_camera(&mut self, position_shift: Point, zoom_shift: f32) { |
111 |
self.camera.zoom += zoom_shift; |
|
14714 | 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 |
); |
|
14709 | 116 |
} |
117 |
||
118 |
pub fn render(&mut self) { |
|
15195 | 119 |
if let Some(ref mut renderer) = self.map_renderer { |
14710 | 120 |
unsafe { |
121 |
gl::ClearColor(0.4f32, 0f32, 0.2f32, 1f32); |
|
122 |
gl::Clear(gl::COLOR_BUFFER_BIT); |
|
123 |
} |
|
14709 | 124 |
|
14723 | 125 |
renderer.render(&self.camera); |
14707 | 126 |
} |
15195 | 127 |
if let Some(ref mut renderer) = self.gear_renderer { |
128 |
renderer.render(&self.camera) |
|
129 |
} |
|
14707 | 130 |
} |
131 |
||
14721 | 132 |
fn create_gear(&mut self, position: Point) { |
133 |
if let Some(ref mut state) = self.game_state { |
|
15279 | 134 |
let id = state.physics.new_gear().unwrap(); |
14721 | 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 |
||
14300 | 143 |
pub fn step(&mut self) { |
144 |
if let Some(ref mut state) = self.game_state { |
|
14721 | 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 { |
|
15280 | 156 |
state.physics.step(Millis::new(1), &state.land); |
14300 | 157 |
} |
158 |
} |
|
14159
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
159 |
} |