preparing or auto loading chunks around player

This commit is contained in:
Piotrek
2021-05-11 14:46:11 +02:00
parent 6acdd51b05
commit 4acf8ea816
6 changed files with 60 additions and 36 deletions
+44 -30
View File
@@ -18,47 +18,65 @@ pub enum GameState {
pub struct Game {
state: GameState,
player: Player,
world: World
world: World,
prev_pos: Point3<i32>
}
impl Game {
pub fn new(content: &mut dyn ContentView) -> Self {
pub fn new() -> Self {
let mut app = Self {
state: GameState::Paused,
player: Player::new(),
world: World::new("default".to_string())
world: World::new("default".to_string()),
prev_pos: Point3{ x:0, y:0, z:0 }
};
// Try to load world from file
let timer = Instant::now();
if app.world.load() {
for (pos, block) in app.world.all_blocks() {
content.write(&pos, block);
}
println!("Loaded in {}", timer.elapsed().as_secs_f32());
}
// Generate new world
else {
let timer = Instant::now();
for x in 0..32 {
for z in 0..32 {
let pos = &Point3{x,y:0,z};
let block = app.world.gen_block(pos);
content.write(pos, block);
}
}
println!("Generated in {}", timer.elapsed().as_secs_f32());
// // Try to load world from file
// let timer = Instant::now();
// if app.world.load() {
// for (pos, block) in app.world.all_blocks() {
// content.write(&pos, block);
// }
// println!("Loaded in {}", timer.elapsed().as_secs_f32());
// }
// // Generate new world
// else {
// let timer = Instant::now();
// for x in 0..32 {
// for z in 0..32 {
// let pos = &Point3{x,y:0,z};
// let block = app.world.gen_block(pos);
// content.write(pos, block);
// }
// }
// println!("Generated in {}", timer.elapsed().as_secs_f32());
let timer = Instant::now();
app.world.save();
println!("Saved in {}", timer.elapsed().as_secs_f32());
}
// let timer = Instant::now();
// app.world.save();
// println!("Saved in {}", timer.elapsed().as_secs_f32());
// }
println!("App initialized");
app
}
pub fn update(&mut self, delta: f32, content: &mut impl ContentView) {
// Update camera controller
let camera = content.get_camera_transform();
self.player.camera_controller.update(delta, camera);
// Update chunks
let pos = camera.get_position();
let pos = Point3{x: pos.x as i32, y: pos.y as i32, z: pos.z as i32};
if pos != self.prev_pos {
self.prev_pos = pos;
// Load sphere of chunks around player
}
}
pub fn input(&mut self, event: &Event<()>) {
// Update camera controller
self.player.camera_controller.handle_input(event);
@@ -82,8 +100,4 @@ impl Game {
pub fn get_state(&self) -> &GameState {
&self.state
}
pub fn update_camera(&mut self, delta: f32, camera_transform: &mut dyn CameraTransform) {
self.player.camera_controller.update(delta, camera_transform);
}
}