mirror of
https://github.com/zyllian/classics.git
synced 2025-05-10 09:56:38 -07:00
make generation presets use string ids
This commit is contained in:
parent
21217a3597
commit
55bbb3a59c
4 changed files with 100 additions and 11 deletions
|
@ -1,5 +1,7 @@
|
|||
use std::{collections::BTreeMap, sync::LazyLock};
|
||||
|
||||
use internment::Intern;
|
||||
|
||||
use crate::player::PlayerType;
|
||||
|
||||
/// information about all blocks implemented
|
||||
|
@ -95,7 +97,7 @@ pub static BLOCK_INFO: LazyLock<BTreeMap<u8, BlockInfo>> = LazyLock::new(|| {
|
|||
});
|
||||
|
||||
/// map of block string ids to their byte ids
|
||||
pub static BLOCK_STRING_ID_MAP: LazyLock<BTreeMap<&'static str, u8>> = LazyLock::new(|| {
|
||||
pub static BLOCK_STRING_ID_MAP: LazyLock<BTreeMap<Intern<String>, u8>> = LazyLock::new(|| {
|
||||
BLOCK_INFO
|
||||
.iter()
|
||||
.map(|(id, info)| (info.str_id, *id))
|
||||
|
@ -106,7 +108,7 @@ pub static BLOCK_STRING_ID_MAP: LazyLock<BTreeMap<&'static str, u8>> = LazyLock:
|
|||
#[derive(Debug)]
|
||||
pub struct BlockInfo {
|
||||
/// the block's string id
|
||||
pub str_id: &'static str,
|
||||
pub str_id: Intern<String>,
|
||||
/// the type of block
|
||||
pub block_type: BlockType,
|
||||
/// permissions needed to place this block
|
||||
|
@ -117,9 +119,9 @@ pub struct BlockInfo {
|
|||
|
||||
impl BlockInfo {
|
||||
/// creates a new block info
|
||||
pub const fn new(str_id: &'static str) -> Self {
|
||||
pub fn new(str_id: &'static str) -> Self {
|
||||
Self {
|
||||
str_id,
|
||||
str_id: Intern::new(str_id.to_owned()),
|
||||
block_type: BlockType::Solid,
|
||||
place_permissions: PlayerType::Normal,
|
||||
break_permissions: PlayerType::Normal,
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
use internment::Intern;
|
||||
use rand::Rng;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::Level;
|
||||
use super::{block::BLOCK_STRING_ID_MAP, Level};
|
||||
|
||||
/// enum for different kinds of level generation
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
|
@ -29,7 +30,7 @@ pub enum FlatPreset {
|
|||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct FlatLayer {
|
||||
/// the block for the layer
|
||||
pub block: u8,
|
||||
pub block: String,
|
||||
/// the depth of the layer
|
||||
pub depth: usize,
|
||||
}
|
||||
|
@ -60,11 +61,17 @@ impl LevelGeneration {
|
|||
FlatPreset::StoneAndGrass => {
|
||||
custom_layers = vec![
|
||||
FlatLayer {
|
||||
block: 1,
|
||||
block: "stone".to_owned(),
|
||||
depth: level.y_size / 2 - 4,
|
||||
},
|
||||
FlatLayer { block: 3, depth: 3 },
|
||||
FlatLayer { block: 2, depth: 1 },
|
||||
FlatLayer {
|
||||
block: "dirt".to_owned(),
|
||||
depth: 3,
|
||||
},
|
||||
FlatLayer {
|
||||
block: "grass".to_owned(),
|
||||
depth: 1,
|
||||
},
|
||||
];
|
||||
layers_ref = &custom_layers;
|
||||
}
|
||||
|
@ -75,10 +82,18 @@ impl LevelGeneration {
|
|||
|
||||
let mut y = 0;
|
||||
for layer in layers_ref {
|
||||
let block = Intern::new(layer.block.to_string());
|
||||
for _ in 0..layer.depth {
|
||||
for x in 0..level.x_size {
|
||||
for z in 0..level.z_size {
|
||||
level.set_block(x, y, z, layer.block);
|
||||
level.set_block(
|
||||
x,
|
||||
y,
|
||||
z,
|
||||
*BLOCK_STRING_ID_MAP
|
||||
.get(&block)
|
||||
.expect("missing block type!"),
|
||||
);
|
||||
}
|
||||
}
|
||||
y += 1;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue