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);
}
}
+2 -2
View File
@@ -24,7 +24,7 @@ fn main() {
.expect("Failed to create window");
let mut renderer = Renderer::new(&window);
let mut game = Game::new(&mut renderer);
let mut game = Game::new();
// Stats
let mut fps_timer = std::time::Instant::now();
@@ -73,7 +73,7 @@ fn main() {
Event::MainEventsCleared => {
// Update
let delta = delta_timer.elapsed().as_secs_f32();
game.update_camera(delta, &mut renderer.camera);
game.update(delta, &mut renderer);
delta_timer = std::time::Instant::now();
// Redraw if running
+4
View File
@@ -31,6 +31,7 @@ impl Camera {
pub trait CameraTransform {
fn update(&mut self, position: Point3<f32>, forward: Vector3<f32>, up: Vector3<f32>);
fn get_position(&self) -> Point3<f32>;
}
impl CameraTransform for Camera {
@@ -38,4 +39,7 @@ impl CameraTransform for Camera {
self.position = position;
self.view_matrix = Matrix4::look_to_rh((0.0,0.0,0.0).into(), forward, up);
}
fn get_position(&self) -> Point3<f32> {
self.position
}
}
+6
View File
@@ -1,9 +1,11 @@
use crate::renderer::camera::CameraTransform;
use crate::Renderer;
use crate::game::world::block::WorldBlock;
use cgmath::Point3;
pub trait ContentView {
fn write(&mut self, block_pos: &Point3<u32>, block: &WorldBlock);
fn get_camera_transform(&mut self) -> &mut CameraTransform;
}
impl ContentView for Renderer {
@@ -11,4 +13,8 @@ impl ContentView for Renderer {
fn write(&mut self, block_pos: &Point3<u32>, block: &WorldBlock) {
self.buffers.content.write(&self.queue, block_pos, block)
}
fn get_camera_transform(&mut self) -> &mut CameraTransform {
&mut self.camera
}
}
Binary file not shown.
+4 -4
View File
@@ -60,14 +60,14 @@ vec3 solve()
//col += 0.25 * vec3(0.8,0.4,0.2) * pow(sun, 4.0 );
// Gamma
col = pow(clamp(col*1.1-0.02,0.0,1.0), vec3(0.4545));
//col = pow(clamp(col*1.1-0.02,0.0,1.0), vec3(0.4545));
// Contrast
col = col*col*(3.0-2.0*col);
//col = col*col*(3.0-2.0*col);
// Color grade
col = pow( col, vec3(1.0,0.92,1.0) ); // soft green
col *= vec3(1.02,0.99,0.99); // tint red
//col = pow( col, vec3(1.0,0.92,1.0) ); // soft green
//col *= vec3(1.02,0.99,0.99); // tint red
//col.z = (col.z+0.1)/1.1; // bias blue
// Fog