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