author | Wuzzy <Wuzzy2@mail.ru> |
Wed, 15 May 2019 03:56:00 +0200 | |
changeset 14973 | 8e4e508d153c |
parent 14739 | 5915a199cb81 |
child 15211 | e2adb40c7988 |
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 |
|
14726 | 11 |
use crate::render::{camera::Camera, 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>, |
14726 | 28 |
renderer: Option<MapRenderer>, |
29 |
camera: Camera, |
|
14737 | 30 |
last_gear_id: GearId, |
14175
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(&[]), |
14394
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
37 |
preview: None, |
14316 | 38 |
game_state: None, |
14726 | 39 |
renderer: None, |
40 |
camera: Camera::new(), |
|
14737 | 41 |
last_gear_id: GearId::default(), |
14175
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 |
|
14726 | 45 |
pub fn create_renderer(&mut self, width: u16, height: u16) { |
14735 | 46 |
let land_tile_size = Size::square(512); |
47 |
self.renderer = Some(MapRenderer::new(land_tile_size)); |
|
14726 | 48 |
self.camera = Camera::with_size(Size::new(width as usize, height as usize)); |
14729 | 49 |
|
50 |
use mapgen::{theme::Theme, MapGenerator}; |
|
51 |
use std::path::Path; |
|
52 |
||
53 |
if let Some(ref state) = self.game_state { |
|
14732 | 54 |
self.camera.position = state.land.play_box().center(); |
14735 | 55 |
|
14729 | 56 |
let theme = |
57 |
Theme::load(Path::new("../../share/hedgewars/Data/Themes/Cheese/")).unwrap(); |
|
14731 | 58 |
let texture = MapGenerator::new().make_texture(&state.land, &theme); |
14729 | 59 |
if let Some(ref mut renderer) = self.renderer { |
60 |
renderer.init(&texture); |
|
61 |
} |
|
62 |
} |
|
14726 | 63 |
} |
64 |
||
14293 | 65 |
pub fn set_seed(&mut self, seed: &[u8]) { |
66 |
self.random_numbers_gen = LaggedFibonacciPRNG::new(seed); |
|
67 |
} |
|
68 |
||
14394
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
69 |
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
|
70 |
&self.preview |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
71 |
} |
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 |
pub fn generate_preview(&mut self) { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
74 |
fn template() -> OutlineTemplate { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
75 |
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
|
76 |
template.islands = vec![vec![ |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
77 |
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
|
78 |
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
|
79 |
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
|
80 |
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
|
81 |
]]; |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
82 |
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
|
83 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
84 |
template |
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 |
|
14316 | 87 |
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
|
88 |
let landgen = TemplatedLandGenerator::new(template()); |
14394
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
89 |
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
|
90 |
} |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
91 |
|
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
92 |
pub fn dispose_preview(&mut self) { |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14316
diff
changeset
|
93 |
self.preview = None |
14175
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
94 |
} |
14316 | 95 |
|
96 |
pub fn init(&mut self, template: OutlineTemplate) { |
|
97 |
let physics = hwp::World::new(template.size); |
|
98 |
||
99 |
let params = LandGenerationParameters::new(0u32, u32::max_value(), 5, false, false); |
|
100 |
let landgen = TemplatedLandGenerator::new(template); |
|
101 |
let land = landgen.generate_land(¶ms, &mut self.random_numbers_gen); |
|
102 |
||
103 |
self.game_state = Some(GameState::new(land, physics)); |
|
104 |
} |
|
105 |
||
14725 | 106 |
pub fn move_camera(&mut self, position_shift: Point, zoom_shift: f32) { |
107 |
self.camera.zoom += zoom_shift; |
|
14730 | 108 |
self.camera.position += Point::new( |
109 |
(position_shift.x as f32 / self.camera.zoom) as i32, |
|
110 |
(position_shift.y as f32 / self.camera.zoom) as i32, |
|
111 |
); |
|
14725 | 112 |
} |
113 |
||
114 |
pub fn render(&mut self) { |
|
14726 | 115 |
if let Some(ref mut renderer) = self.renderer { |
116 |
unsafe { |
|
117 |
gl::ClearColor(0.4f32, 0f32, 0.2f32, 1f32); |
|
118 |
gl::Clear(gl::COLOR_BUFFER_BIT); |
|
119 |
} |
|
14725 | 120 |
|
14739 | 121 |
renderer.render(&self.camera); |
14723 | 122 |
} |
123 |
} |
|
124 |
||
14737 | 125 |
fn get_unused_gear_id(&mut self) -> GearId { |
126 |
let id = self.last_gear_id; |
|
127 |
self.last_gear_id += 1; |
|
128 |
id |
|
129 |
} |
|
130 |
||
131 |
fn create_gear(&mut self, position: Point) { |
|
132 |
let id = self.get_unused_gear_id(); |
|
133 |
if let Some(ref mut state) = self.game_state { |
|
134 |
let fp_position = FPPoint::new(position.x.into(), position.y.into()); |
|
135 |
state.physics.add_gear_data( |
|
136 |
id, |
|
137 |
hwp::physics::PhysicsData::new(fp_position, FPPoint::zero()), |
|
138 |
) |
|
139 |
} |
|
140 |
} |
|
141 |
||
14316 | 142 |
pub fn step(&mut self) { |
143 |
if let Some(ref mut state) = self.game_state { |
|
14737 | 144 |
let next = self.random_numbers_gen.next().unwrap(); |
145 |
if next % 32 == 0 { |
|
146 |
let position = Point::new( |
|
147 |
(self.random_numbers_gen.next().unwrap() % state.land.width() as u32) as i32, |
|
148 |
0, |
|
149 |
); |
|
150 |
self.create_gear(position); |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
if let Some(ref mut state) = self.game_state { |
|
14316 | 155 |
state.physics.step(fp!(1), &state.land); |
156 |
} |
|
157 |
} |
|
14175
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
158 |
} |