Saving world to file
This commit is contained in:
Generated
+44
@@ -444,6 +444,26 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "dirs"
|
||||||
|
version = "3.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "30baa043103c9d0c2a57cf537cc2f35623889dc0d405e6c3cccfadbc81c71309"
|
||||||
|
dependencies = [
|
||||||
|
"dirs-sys",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "dirs-sys"
|
||||||
|
version = "0.3.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "03d86534ed367a67548dc68113a0f5db55432fdfbb6e6f9d77704397d95d5780"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"redox_users",
|
||||||
|
"winapi 0.3.9",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "dispatch"
|
name = "dispatch"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
@@ -486,6 +506,18 @@ version = "0.2.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "37ab347416e802de484e4d03c7316c48f1ecb56574dfd4a46a80f173ce1de04d"
|
checksum = "37ab347416e802de484e4d03c7316c48f1ecb56574dfd4a46a80f173ce1de04d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "flate2"
|
||||||
|
version = "1.0.20"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cd3aec53de10fe96d7d8c565eb17f2c687bb5518a2ec453b5b1252964526abe0"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if 1.0.0",
|
||||||
|
"crc32fast",
|
||||||
|
"libc",
|
||||||
|
"miniz_oxide 0.4.4",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fnv"
|
name = "fnv"
|
||||||
version = "1.0.7"
|
version = "1.0.7"
|
||||||
@@ -1710,6 +1742,16 @@ dependencies = [
|
|||||||
"bitflags",
|
"bitflags",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "redox_users"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom 0.2.2",
|
||||||
|
"redox_syscall",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustc-hash"
|
name = "rustc-hash"
|
||||||
version = "1.1.0"
|
version = "1.1.0"
|
||||||
@@ -2239,6 +2281,8 @@ dependencies = [
|
|||||||
"bytemuck",
|
"bytemuck",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"cgmath",
|
"cgmath",
|
||||||
|
"dirs",
|
||||||
|
"flate2",
|
||||||
"fs_extra",
|
"fs_extra",
|
||||||
"futures",
|
"futures",
|
||||||
"glob",
|
"glob",
|
||||||
|
|||||||
+2
-1
@@ -17,7 +17,8 @@ image = "0.23.14"
|
|||||||
cgmath = "0.18"
|
cgmath = "0.18"
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
noise = "0.7"
|
noise = "0.7"
|
||||||
|
dirs = "3.0"
|
||||||
|
flate2 = "1.0"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
|
|||||||
+29
-16
@@ -1,40 +1,53 @@
|
|||||||
mod camera_controller;
|
mod camera_controller;
|
||||||
|
mod player;
|
||||||
pub mod world;
|
pub mod world;
|
||||||
|
use std::time::Instant;
|
||||||
use crate::renderer::content_view::ContentView;
|
use crate::renderer::content_view::ContentView;
|
||||||
use cgmath::{SquareMatrix, Point3, InnerSpace};
|
use cgmath::{SquareMatrix, Point3, InnerSpace};
|
||||||
use winit::event::Event;
|
use winit::event::Event;
|
||||||
use crate::renderer::{camera::CameraTransform};
|
use crate::renderer::{camera::CameraTransform};
|
||||||
use camera_controller::CameraController;
|
use player::Player;
|
||||||
use world::World;
|
use world::World;
|
||||||
|
|
||||||
|
|
||||||
pub enum GameState {
|
pub enum GameState {
|
||||||
Paused,
|
Paused,
|
||||||
Running
|
Running
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
camera_controller: CameraController,
|
player: Player,
|
||||||
world: World
|
world: World
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
pub fn new(content: &mut dyn ContentView) -> Self {
|
pub fn new(content: &mut dyn ContentView) -> Self {
|
||||||
let mut app = Self {
|
let mut app = Self {
|
||||||
camera_controller: CameraController::new(),
|
player: Player::new(),
|
||||||
world: World::new("default".to_string())
|
world: World::new("default".to_string())
|
||||||
};
|
};
|
||||||
|
|
||||||
let pos = &Point3{x:0,y:0,z:0};
|
let timer = Instant::now();
|
||||||
let block = app.world.gen_block(pos);
|
for x in 0..16 {
|
||||||
content.write(pos, block);
|
for z in 0..16 {
|
||||||
|
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 pos = &Point3{x:1,y:0,z:0};
|
let timer = Instant::now();
|
||||||
let block = app.world.gen_block(pos);
|
app.world.save();
|
||||||
content.write(pos, block);
|
println!("Saved in {}", timer.elapsed().as_secs_f32());
|
||||||
|
|
||||||
let pos = &Point3{x:0,y:0,z:1};
|
// let pos = &Point3{x:1,y:0,z:0};
|
||||||
let block = app.world.gen_block(pos);
|
// let block = app.world.gen_block(pos);
|
||||||
content.write(pos, block);
|
// content.write(pos, block);
|
||||||
|
|
||||||
|
// let pos = &Point3{x:0,y:0,z:1};
|
||||||
|
// let block = app.world.gen_block(pos);
|
||||||
|
// content.write(pos, block);
|
||||||
|
|
||||||
println!("App initialized");
|
println!("App initialized");
|
||||||
app
|
app
|
||||||
@@ -42,16 +55,16 @@ impl App {
|
|||||||
|
|
||||||
pub fn input(&mut self, event: &Event<()>) {
|
pub fn input(&mut self, event: &Event<()>) {
|
||||||
// Update camera controller
|
// Update camera controller
|
||||||
self.camera_controller.handle_input(event);
|
self.player.camera_controller.handle_input(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_state(&mut self, requested_state: GameState) -> GameState {
|
pub fn update_state(&mut self, requested_state: GameState) -> GameState {
|
||||||
match requested_state {
|
match requested_state {
|
||||||
GameState::Paused => {
|
GameState::Paused => {
|
||||||
self.camera_controller.set_enabled(false);
|
self.player.camera_controller.set_enabled(false);
|
||||||
}
|
}
|
||||||
GameState::Running => {
|
GameState::Running => {
|
||||||
self.camera_controller.set_enabled(true);
|
self.player.camera_controller.set_enabled(true);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -60,6 +73,6 @@ impl App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_camera(&mut self, delta: f32, camera_transform: &mut dyn CameraTransform) {
|
pub fn update_camera(&mut self, delta: f32, camera_transform: &mut dyn CameraTransform) {
|
||||||
self.camera_controller.update(delta, camera_transform);
|
self.player.camera_controller.update(delta, camera_transform);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+8
-3
@@ -1,8 +1,13 @@
|
|||||||
|
use super::camera_controller::CameraController;
|
||||||
|
|
||||||
struct Player {
|
pub struct Player {
|
||||||
camera_controller: CameraController
|
pub camera_controller: CameraController
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Player {
|
impl Player {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
camera_controller: CameraController::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
use cgmath::Point3;
|
use cgmath::Point3;
|
||||||
|
use std::iter::FromIterator;
|
||||||
|
use std::io::Write;
|
||||||
|
use flate2::{write::DeflateEncoder, Compression};
|
||||||
|
|
||||||
pub const SIZE: usize = 32;
|
pub const SIZE: usize = 32;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,9 @@
|
|||||||
|
use flate2::Compression;
|
||||||
|
use flate2::write::DeflateEncoder;
|
||||||
|
use byteorder::LittleEndian;
|
||||||
|
use byteorder::ByteOrder;
|
||||||
use cgmath::Point3;
|
use cgmath::Point3;
|
||||||
|
use std::io::Write;
|
||||||
use crate::app::world::WorldBlock;
|
use crate::app::world::WorldBlock;
|
||||||
|
|
||||||
pub const SIZE: u32 = 32;
|
pub const SIZE: u32 = 32;
|
||||||
@@ -30,4 +35,22 @@ impl WorldChunk {
|
|||||||
// Return block reference
|
// Return block reference
|
||||||
&mut self.blocks[(value-1) as usize]
|
&mut self.blocks[(value-1) as usize]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_bytes(&self) -> Vec<u8> {
|
||||||
|
let mut bytes = Vec::with_capacity(self.nodes.len()*4 + self.blocks.len()* 32*32*32);
|
||||||
|
|
||||||
|
// Nodes
|
||||||
|
let mut node_bytes = [0_u8; (SIZE*SIZE*SIZE*4) as usize];
|
||||||
|
LittleEndian::write_u32_into(&self.nodes, &mut node_bytes);
|
||||||
|
bytes.extend(node_bytes.iter());
|
||||||
|
|
||||||
|
// Blocks
|
||||||
|
let mut encoder = DeflateEncoder::new(Vec::new(), Compression::fast());
|
||||||
|
for block in self.blocks.iter() {
|
||||||
|
encoder.write_all(&block.materials).unwrap();
|
||||||
|
}
|
||||||
|
bytes.extend(encoder.finish().unwrap());
|
||||||
|
|
||||||
|
bytes
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,10 @@
|
|||||||
mod generator;
|
mod generator;
|
||||||
pub mod chunk;
|
pub mod chunk;
|
||||||
pub mod block;
|
pub mod block;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::fs;
|
||||||
use cgmath::Point3;
|
use cgmath::Point3;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use generator::WorldGen;
|
use generator::WorldGen;
|
||||||
@@ -21,6 +25,23 @@ impl World {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn save(&self) {
|
||||||
|
// Get directory
|
||||||
|
let mut dir = dirs::data_local_dir().unwrap();
|
||||||
|
dir.push("Voxelgame");
|
||||||
|
fs::create_dir_all(&dir).expect("Failed to create directory");
|
||||||
|
|
||||||
|
// Save all chunks
|
||||||
|
for (pos, chunk) in self.chunks.iter() {
|
||||||
|
let bytes = chunk.to_bytes();
|
||||||
|
let filename = format!("chunk_{}_{}_{}.dat", pos.x, pos.y, pos.z);
|
||||||
|
let path: PathBuf = [dir.to_str().unwrap(), &filename].iter().collect();
|
||||||
|
|
||||||
|
let mut file = fs::File::create(path).expect("Failed to create file");
|
||||||
|
file.write(&bytes).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn gen_block(&mut self, block_pos: &Point3<u32>) -> &WorldBlock {
|
pub fn gen_block(&mut self, block_pos: &Point3<u32>) -> &WorldBlock {
|
||||||
// Split world space to chunk and block space
|
// Split world space to chunk and block space
|
||||||
let ch_pos = block_pos / chunk::SIZE;
|
let ch_pos = block_pos / chunk::SIZE;
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
use crate::renderer::buffers::Buffers;
|
|
||||||
|
|
||||||
pub struct UserInterfacePass {
|
|
||||||
pub pipeline: wgpu::RenderPipeline
|
|
||||||
}
|
|
||||||
|
|
||||||
impl UserInterfacePass {
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user