|
|
|
@ -10,6 +10,7 @@ struct State {
|
|
|
|
|
view: Chunk, |
|
|
|
|
loaded_chunks: Box<HashMap<(i32, i32), Chunk>>, |
|
|
|
|
world_pos: (i32, i32), |
|
|
|
|
seed: u64, |
|
|
|
|
player_position: usize, |
|
|
|
|
selected_object: Option<TileType>, |
|
|
|
|
visible: Vec<bool>, |
|
|
|
@ -17,6 +18,10 @@ struct State {
|
|
|
|
|
|
|
|
|
|
impl State { |
|
|
|
|
pub fn new() -> State { |
|
|
|
|
let seed = RandomNumberGenerator::new().rand::<u64>(); |
|
|
|
|
Self::seeded(seed) |
|
|
|
|
} |
|
|
|
|
pub fn seeded(seed: u64) -> State { |
|
|
|
|
let mut chunks: HashMap<(i32, i32), Chunk> = HashMap::new(); |
|
|
|
|
|
|
|
|
|
// Coordinates of the current and surrounding chunks
|
|
|
|
@ -33,8 +38,7 @@ impl State {
|
|
|
|
|
] |
|
|
|
|
.into(); |
|
|
|
|
for key in keys { |
|
|
|
|
let seed = RandomNumberGenerator::new().rand::<u64>(); |
|
|
|
|
let chunk = map::new_chunk(seed); |
|
|
|
|
let chunk = map::new_chunk(seed, Point::from(key)); |
|
|
|
|
chunks.insert(key, chunk); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -42,6 +46,7 @@ impl State {
|
|
|
|
|
world_pos: (0, 0), |
|
|
|
|
view: chunks.get(&(0, 0)).unwrap().to_vec(), |
|
|
|
|
loaded_chunks: Box::new(chunks), |
|
|
|
|
seed, |
|
|
|
|
player_position: (25 * 80) + 40, // Equivalent to point2d_to_index(40, 25) but we haven't initialized it yet
|
|
|
|
|
visible: vec![false; 80 * 50], |
|
|
|
|
selected_object: None, |
|
|
|
@ -58,7 +63,7 @@ impl State {
|
|
|
|
|
let chunk = match self.loaded_chunks.get(&self.world_pos) { |
|
|
|
|
Some(chunk) => chunk, |
|
|
|
|
None => { |
|
|
|
|
let new_chunk = map::new_chunk(crypto::hash_to_number(&self.world_pos)); |
|
|
|
|
let new_chunk = map::new_chunk(self.seed, Point::from(self.world_pos)); |
|
|
|
|
self.loaded_chunks.insert(self.world_pos, new_chunk); |
|
|
|
|
self.loaded_chunks |
|
|
|
|
.get(&self.world_pos) |
|
|
|
|