author | alfadur |
Thu, 12 Nov 2020 00:24:58 +0300 | |
changeset 15781 | c929e25a7da2 |
parent 15780 | 6c939e6de981 |
child 15784 | 84c07aa94b61 |
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; |
14316 | 14 |
|
15211 | 15 |
use crate::render::{camera::Camera, GearRenderer, MapRenderer}; |
14723 | 16 |
|
14316 | 17 |
struct GameState { |
18 |
land: Land2D<u32>, |
|
19 |
physics: hwp::World, |
|
20 |
} |
|
21 |
||
22 |
impl GameState { |
|
23 |
fn new(land: Land2D<u32>, physics: hwp::World) -> Self { |
|
14726 | 24 |
Self { land, physics } |
14316 | 25 |
} |
26 |
} |
|
14175
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
27 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
28 |
pub struct World { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
29 |
random_numbers_gen: LaggedFibonacciPRNG, |
14394
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
30 |
preview: Option<Land2D<u8>>, |
14316 | 31 |
game_state: Option<GameState>, |
15211 | 32 |
map_renderer: Option<MapRenderer>, |
33 |
gear_renderer: Option<GearRenderer>, |
|
14726 | 34 |
camera: Camera, |
14175
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 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
37 |
impl World { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
38 |
pub fn new() -> Self { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
39 |
Self { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
40 |
random_numbers_gen: LaggedFibonacciPRNG::new(&[]), |
14394
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
41 |
preview: None, |
14316 | 42 |
game_state: None, |
15211 | 43 |
map_renderer: None, |
44 |
gear_renderer: None, |
|
14726 | 45 |
camera: Camera::new(), |
14175
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 |
} |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
48 |
|
14726 | 49 |
pub fn create_renderer(&mut self, width: u16, height: u16) { |
14735 | 50 |
let land_tile_size = Size::square(512); |
15211 | 51 |
self.map_renderer = Some(MapRenderer::new(land_tile_size)); |
52 |
self.gear_renderer = Some(GearRenderer::new()); |
|
14726 | 53 |
self.camera = Camera::with_size(Size::new(width as usize, height as usize)); |
14729 | 54 |
|
55 |
use mapgen::{theme::Theme, MapGenerator}; |
|
56 |
use std::path::Path; |
|
57 |
||
58 |
if let Some(ref state) = self.game_state { |
|
14732 | 59 |
self.camera.position = state.land.play_box().center(); |
14735 | 60 |
|
14729 | 61 |
let theme = |
62 |
Theme::load(Path::new("../../share/hedgewars/Data/Themes/Cheese/")).unwrap(); |
|
14731 | 63 |
let texture = MapGenerator::new().make_texture(&state.land, &theme); |
15211 | 64 |
if let Some(ref mut renderer) = self.map_renderer { |
14729 | 65 |
renderer.init(&texture); |
66 |
} |
|
67 |
} |
|
14726 | 68 |
} |
69 |
||
14293 | 70 |
pub fn set_seed(&mut self, seed: &[u8]) { |
71 |
self.random_numbers_gen = LaggedFibonacciPRNG::new(seed); |
|
72 |
} |
|
73 |
||
14394
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
74 |
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
|
75 |
&self.preview |
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 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
78 |
pub fn generate_preview(&mut self) { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
79 |
fn template() -> OutlineTemplate { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
80 |
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
|
81 |
template.islands = vec![vec![ |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
82 |
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
|
83 |
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
|
84 |
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
|
85 |
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
|
86 |
]]; |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
87 |
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
|
88 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
89 |
template |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
90 |
} |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
91 |
|
14316 | 92 |
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
|
93 |
let landgen = TemplatedLandGenerator::new(template()); |
14394
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
94 |
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
|
95 |
} |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
96 |
|
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
97 |
pub fn dispose_preview(&mut self) { |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
98 |
self.preview = None |
14175
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
99 |
} |
14316 | 100 |
|
101 |
pub fn init(&mut self, template: OutlineTemplate) { |
|
102 |
let physics = hwp::World::new(template.size); |
|
103 |
||
104 |
let params = LandGenerationParameters::new(0u32, u32::max_value(), 5, false, false); |
|
105 |
let landgen = TemplatedLandGenerator::new(template); |
|
106 |
let land = landgen.generate_land(¶ms, &mut self.random_numbers_gen); |
|
107 |
||
108 |
self.game_state = Some(GameState::new(land, physics)); |
|
109 |
} |
|
110 |
||
14725 | 111 |
pub fn move_camera(&mut self, position_shift: Point, zoom_shift: f32) { |
112 |
self.camera.zoom += zoom_shift; |
|
14730 | 113 |
self.camera.position += Point::new( |
114 |
(position_shift.x as f32 / self.camera.zoom) as i32, |
|
115 |
(position_shift.y as f32 / self.camera.zoom) as i32, |
|
116 |
); |
|
14725 | 117 |
} |
118 |
||
119 |
pub fn render(&mut self) { |
|
15211 | 120 |
if let Some(ref mut renderer) = self.map_renderer { |
14726 | 121 |
unsafe { |
122 |
gl::ClearColor(0.4f32, 0f32, 0.2f32, 1f32); |
|
123 |
gl::Clear(gl::COLOR_BUFFER_BIT); |
|
124 |
} |
|
14725 | 125 |
|
14739 | 126 |
renderer.render(&self.camera); |
14723 | 127 |
} |
15211 | 128 |
if let Some(ref mut renderer) = self.gear_renderer { |
129 |
renderer.render(&self.camera) |
|
130 |
} |
|
14723 | 131 |
} |
132 |
||
14737 | 133 |
fn create_gear(&mut self, position: Point) { |
134 |
if let Some(ref mut state) = self.game_state { |
|
15295 | 135 |
let id = state.physics.new_gear().unwrap(); |
14737 | 136 |
let fp_position = FPPoint::new(position.x.into(), position.y.into()); |
15781 | 137 |
state.physics.add_gear_data(id, &PositionData(fp_position)); |
138 |
state |
|
139 |
.physics |
|
140 |
.add_gear_data(id, &VelocityData(FPPoint::zero())) |
|
14737 | 141 |
} |
142 |
} |
|
143 |
||
14316 | 144 |
pub fn step(&mut self) { |
145 |
if let Some(ref mut state) = self.game_state { |
|
14737 | 146 |
let next = self.random_numbers_gen.next().unwrap(); |
15780 | 147 |
if next % 32 == 0 { |
14737 | 148 |
let position = Point::new( |
149 |
(self.random_numbers_gen.next().unwrap() % state.land.width() as u32) as i32, |
|
150 |
0, |
|
151 |
); |
|
152 |
self.create_gear(position); |
|
15780 | 153 |
} |
14737 | 154 |
} |
155 |
||
156 |
if let Some(ref mut state) = self.game_state { |
|
15296 | 157 |
state.physics.step(Millis::new(1), &state.land); |
14316 | 158 |
} |
159 |
} |
|
14175
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
160 |
} |