Endless world, still some bugs to figure out
This commit is contained in:
+62
-44
@@ -24,74 +24,92 @@ pub struct Game {
|
||||
}
|
||||
|
||||
impl Game {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
pub fn new(renderer: &mut impl RendererView) -> Self {
|
||||
let mut instance = Self {
|
||||
state: GameState::Paused,
|
||||
player: Player::new(),
|
||||
world: World::init("hello".to_string()),
|
||||
prev_pos: None,
|
||||
prev_chunk_pos: None,
|
||||
};
|
||||
|
||||
// Preload chunks around player
|
||||
for x in -2..2 {
|
||||
for z in -2..2 {
|
||||
let pos = Vector3{x, y: 00, z};
|
||||
if instance.world.load_chunk(pos) {
|
||||
let chunk = instance.world.get_chunk(&pos).unwrap();
|
||||
chunk.write_to(renderer, pos * world::chunk::SIZE as i32);
|
||||
println!("Writing chunk to {:?}", pos * world::chunk::SIZE as i32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// // 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());
|
||||
// }
|
||||
// Return generated instance
|
||||
instance
|
||||
}
|
||||
|
||||
pub fn update(&mut self, delta: f32, renderer: &mut impl RendererView) {
|
||||
// Update camera controller
|
||||
// Update camera
|
||||
let camera = renderer.get_camera_transform();
|
||||
self.player.camera_controller.update(delta, camera);
|
||||
|
||||
// Get positions
|
||||
let pos = camera.get_position();
|
||||
let pos = Vector3{x: pos.x as i32, y: pos.y as i32, z: pos.z as i32};
|
||||
let mut chunk_pos = pos / world::chunk::SIZE as i32;
|
||||
if pos.x < 0 { chunk_pos.x -= 1; }
|
||||
if pos.y < 0 { chunk_pos.y -= 1; }
|
||||
if pos.z < 0 { chunk_pos.z -= 1; }
|
||||
let cam_pos = camera.get_position();
|
||||
let cam_pos = Vector3{x: cam_pos.x as i32, y: cam_pos.y as i32, z: cam_pos.z as i32};
|
||||
let mut chunk_pos = cam_pos / world::chunk::SIZE as i32;
|
||||
if cam_pos.x < 0 { chunk_pos.x -= 1; }
|
||||
if cam_pos.y < 0 { chunk_pos.y -= 1; }
|
||||
if cam_pos.z < 0 { chunk_pos.z -= 1; }
|
||||
|
||||
// Update pointing direction
|
||||
let yaw = self.player.camera_controller.get_yaw();
|
||||
let mut yaw_str = "";
|
||||
if yaw >= 315.0 || yaw <= 45.0 { yaw_str = "+z"; }
|
||||
else if yaw >= 45.0 && yaw < 135.0 { yaw_str = "+x"; }
|
||||
else if yaw >= 135.0 && yaw < 225.0 { yaw_str = "-z"; }
|
||||
else if yaw >= 225.0 && yaw < 315.0 { yaw_str = "-x"; }
|
||||
renderer.get_ui().set_text("World", 0, format!("Direction: {}", yaw_str));
|
||||
|
||||
// Block position
|
||||
if self.prev_pos == None || self.prev_pos != Some(pos) {
|
||||
renderer.get_ui().set_text("World", 0, format!("Position: {}, {}, {}", pos.x, pos.y, pos.z));
|
||||
self.prev_pos = Some(pos);
|
||||
if self.prev_pos == None || self.prev_pos != Some(cam_pos) {
|
||||
renderer.get_ui().set_text("World", 1, format!("Position: {}, {}, {}", cam_pos.x, cam_pos.y, cam_pos.z));
|
||||
self.prev_pos = Some(cam_pos);
|
||||
}
|
||||
|
||||
// Chunk position
|
||||
if self.prev_chunk_pos == None || self.prev_chunk_pos != Some(chunk_pos) {
|
||||
renderer.get_ui().set_text("World", 1, format!("Chunk: {}, {}, {}", chunk_pos.x, chunk_pos.y, chunk_pos.z));
|
||||
renderer.get_ui().set_text("World", 2, format!("Chunk: {}, {}, {}", chunk_pos.x, chunk_pos.y, chunk_pos.z));
|
||||
|
||||
// Shift world
|
||||
let dir = chunk_pos - self.prev_chunk_pos.unwrap_or(chunk_pos);
|
||||
renderer.shift(&(dir * world::chunk::SIZE as i32));
|
||||
let dir = chunk_pos - self.prev_chunk_pos.unwrap_or(chunk_pos); //TODO: clamp to -1,1
|
||||
renderer.shift(dir * world::chunk::SIZE as i32);
|
||||
|
||||
// Load chunk
|
||||
if self.world.load_chunk(chunk_pos) {
|
||||
let chunk = self.world.get_chunk(&chunk_pos).unwrap();
|
||||
chunk.write_to(renderer, chunk_pos * world::chunk::SIZE as i32);
|
||||
println!("Writing chunk to {:?}", chunk_pos * world::chunk::SIZE as i32);
|
||||
// Load chunks at the edge
|
||||
let center = chunk_pos.clone() + 2*dir;
|
||||
let mask = Vector3{x: dir.x.abs(), y: dir.y.abs(), z: dir.z.abs()};
|
||||
println!("Load at {:?}", center);
|
||||
|
||||
for a in -2..3 {
|
||||
let pos = center + Vector3{x: mask.z*a, y: 0, z: mask.x*a};
|
||||
|
||||
self.world.load_chunk(pos);
|
||||
let chunk = self.world.get_chunk(&pos).unwrap();
|
||||
chunk.write_to(renderer, pos * world::chunk::SIZE as i32);
|
||||
}
|
||||
|
||||
//println!("Writing chunk to {:?}", pos * world::chunk::SIZE as i32);
|
||||
|
||||
// for i in -2..2 {
|
||||
|
||||
// }
|
||||
|
||||
|
||||
// if self.world.load_chunk(chunk_pos) {
|
||||
// let chunk = self.world.get_chunk(&chunk_pos).unwrap();
|
||||
// chunk.write_to(renderer, chunk_pos * world::chunk::SIZE as i32);
|
||||
// println!("Writing chunk to {:?}", chunk_pos * world::chunk::SIZE as i32);
|
||||
// }
|
||||
|
||||
self.prev_chunk_pos = Some(chunk_pos);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user