author | alfadur |
Thu, 25 Jul 2019 22:31:24 +0300 | |
changeset 15274 | 42b710b0f883 |
parent 15263 | 24828281c9c5 |
child 15275 | 66c987015f2d |
permissions | -rw-r--r-- |
14716 | 1 |
use fpnum::{fp, FPNum, FPPoint}; |
2 |
use hwphysics::{self as hwp, common::GearId}; |
|
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
3 |
use integral_geometry::{Point, Rect, Size}; |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
4 |
use land2d::Land2D; |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
5 |
use landgen::{ |
14705 | 6 |
outline_template::OutlineTemplate, template_based::TemplatedLandGenerator, |
7 |
LandGenerationParameters, LandGenerator, |
|
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
8 |
}; |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
9 |
use lfprng::LaggedFibonacciPRNG; |
14295 | 10 |
|
15190 | 11 |
use crate::render::{camera::Camera, GearRenderer, MapRenderer}; |
14702 | 12 |
|
14295 | 13 |
struct GameState { |
14 |
land: Land2D<u32>, |
|
15 |
physics: hwp::World, |
|
16 |
} |
|
17 |
||
18 |
impl GameState { |
|
19 |
fn new(land: Land2D<u32>, physics: hwp::World) -> Self { |
|
14705 | 20 |
Self { land, physics } |
14295 | 21 |
} |
22 |
} |
|
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
23 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
24 |
pub struct World { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
25 |
random_numbers_gen: LaggedFibonacciPRNG, |
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
26 |
preview: Option<Land2D<u8>>, |
14295 | 27 |
game_state: Option<GameState>, |
15190 | 28 |
map_renderer: Option<MapRenderer>, |
29 |
gear_renderer: Option<GearRenderer>, |
|
14705 | 30 |
camera: Camera, |
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
31 |
} |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
32 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
33 |
impl World { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
34 |
pub fn new() -> Self { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
35 |
Self { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
36 |
random_numbers_gen: LaggedFibonacciPRNG::new(&[]), |
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
37 |
preview: None, |
14295 | 38 |
game_state: None, |
15190 | 39 |
map_renderer: None, |
40 |
gear_renderer: None, |
|
14705 | 41 |
camera: Camera::new(), |
14154
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 |
} |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
44 |
|
14705 | 45 |
pub fn create_renderer(&mut self, width: u16, height: u16) { |
14714 | 46 |
let land_tile_size = Size::square(512); |
15190 | 47 |
self.map_renderer = Some(MapRenderer::new(land_tile_size)); |
48 |
self.gear_renderer = Some(GearRenderer::new()); |
|
14705 | 49 |
self.camera = Camera::with_size(Size::new(width as usize, height as usize)); |
14708 | 50 |
|
51 |
use mapgen::{theme::Theme, MapGenerator}; |
|
52 |
use std::path::Path; |
|
53 |
||
54 |
if let Some(ref state) = self.game_state { |
|
14711 | 55 |
self.camera.position = state.land.play_box().center(); |
14714 | 56 |
|
14708 | 57 |
let theme = |
58 |
Theme::load(Path::new("../../share/hedgewars/Data/Themes/Cheese/")).unwrap(); |
|
14710 | 59 |
let texture = MapGenerator::new().make_texture(&state.land, &theme); |
15190 | 60 |
if let Some(ref mut renderer) = self.map_renderer { |
14708 | 61 |
renderer.init(&texture); |
62 |
} |
|
63 |
} |
|
14705 | 64 |
} |
65 |
||
14272 | 66 |
pub fn set_seed(&mut self, seed: &[u8]) { |
67 |
self.random_numbers_gen = LaggedFibonacciPRNG::new(seed); |
|
68 |
} |
|
69 |
||
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
70 |
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
|
71 |
&self.preview |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
72 |
} |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
73 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
74 |
pub fn generate_preview(&mut self) { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
75 |
fn template() -> OutlineTemplate { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
76 |
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
|
77 |
template.islands = vec![vec![ |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
78 |
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
|
79 |
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
|
80 |
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
|
81 |
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
|
82 |
]]; |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
83 |
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
|
84 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
85 |
template |
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 |
|
14295 | 88 |
let params = LandGenerationParameters::new(0u8, u8::max_value(), 5, false, false); |
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
89 |
let landgen = TemplatedLandGenerator::new(template()); |
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
90 |
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
|
91 |
} |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
92 |
|
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
93 |
pub fn dispose_preview(&mut self) { |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
94 |
self.preview = None |
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
95 |
} |
14295 | 96 |
|
97 |
pub fn init(&mut self, template: OutlineTemplate) { |
|
98 |
let physics = hwp::World::new(template.size); |
|
99 |
||
100 |
let params = LandGenerationParameters::new(0u32, u32::max_value(), 5, false, false); |
|
101 |
let landgen = TemplatedLandGenerator::new(template); |
|
102 |
let land = landgen.generate_land(¶ms, &mut self.random_numbers_gen); |
|
103 |
||
104 |
self.game_state = Some(GameState::new(land, physics)); |
|
105 |
} |
|
106 |
||
14704 | 107 |
pub fn move_camera(&mut self, position_shift: Point, zoom_shift: f32) { |
108 |
self.camera.zoom += zoom_shift; |
|
14709 | 109 |
self.camera.position += Point::new( |
110 |
(position_shift.x as f32 / self.camera.zoom) as i32, |
|
111 |
(position_shift.y as f32 / self.camera.zoom) as i32, |
|
112 |
); |
|
14704 | 113 |
} |
114 |
||
115 |
pub fn render(&mut self) { |
|
15190 | 116 |
if let Some(ref mut renderer) = self.map_renderer { |
14705 | 117 |
unsafe { |
118 |
gl::ClearColor(0.4f32, 0f32, 0.2f32, 1f32); |
|
119 |
gl::Clear(gl::COLOR_BUFFER_BIT); |
|
120 |
} |
|
14704 | 121 |
|
14718 | 122 |
renderer.render(&self.camera); |
14702 | 123 |
} |
15190 | 124 |
if let Some(ref mut renderer) = self.gear_renderer { |
125 |
renderer.render(&self.camera) |
|
126 |
} |
|
14702 | 127 |
} |
128 |
||
14716 | 129 |
fn create_gear(&mut self, position: Point) { |
130 |
if let Some(ref mut state) = self.game_state { |
|
15274 | 131 |
let id = state.physics.new_gear().unwrap(); |
14716 | 132 |
let fp_position = FPPoint::new(position.x.into(), position.y.into()); |
133 |
state.physics.add_gear_data( |
|
134 |
id, |
|
135 |
hwp::physics::PhysicsData::new(fp_position, FPPoint::zero()), |
|
136 |
) |
|
137 |
} |
|
138 |
} |
|
139 |
||
14295 | 140 |
pub fn step(&mut self) { |
141 |
if let Some(ref mut state) = self.game_state { |
|
14716 | 142 |
let next = self.random_numbers_gen.next().unwrap(); |
143 |
if next % 32 == 0 { |
|
144 |
let position = Point::new( |
|
145 |
(self.random_numbers_gen.next().unwrap() % state.land.width() as u32) as i32, |
|
146 |
0, |
|
147 |
); |
|
148 |
self.create_gear(position); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
if let Some(ref mut state) = self.game_state { |
|
14295 | 153 |
state.physics.step(fp!(1), &state.land); |
154 |
} |
|
155 |
} |
|
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
156 |
} |