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