Compare commits

...

2 commits

Author SHA1 Message Date
griffi-gh 0af548320b owo 2023-11-26 01:47:48 +01:00
griffi-gh 16ffb6f786 wip item system 2023-11-26 01:29:02 +01:00
8 changed files with 145 additions and 37 deletions

7
Cargo.lock generated
View file

@ -1026,6 +1026,7 @@ dependencies = [
"hashbrown 0.14.2", "hashbrown 0.14.2",
"nohash-hasher", "nohash-hasher",
"num_enum", "num_enum",
"nz",
"postcard", "postcard",
"rand", "rand",
"rand_xoshiro", "rand_xoshiro",
@ -1325,6 +1326,12 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "nz"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28ba9cab725a7d7ece4bbf38968a1717ed73abf0516f7ff90a4127d016bebd51"
[[package]] [[package]]
name = "objc-sys" name = "objc-sys"
version = "0.3.1" version = "0.3.1"

View file

@ -20,6 +20,7 @@ hashbrown = { version = "0.14", features = ["serde"] }
nohash-hasher = "0.2" nohash-hasher = "0.2"
#bytemuck = { version = "1.14", features = ["derive"] } #bytemuck = { version = "1.14", features = ["derive"] }
static_assertions = "1.1" static_assertions = "1.1"
nz = "0.3"
[features] [features]
default = [] default = []

View file

@ -1,6 +1,7 @@
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use strum::EnumIter; use strum::EnumIter;
use num_enum::TryFromPrimitive; use num_enum::TryFromPrimitive;
use crate::item::Item;
#[derive(Serialize, Deserialize, Clone, Copy, Debug, EnumIter)] #[derive(Serialize, Deserialize, Clone, Copy, Debug, EnumIter)]
#[repr(u8)] #[repr(u8)]
@ -50,24 +51,28 @@ impl Block {
render: RenderType::None, render: RenderType::None,
collision: CollisionType::None, collision: CollisionType::None,
raycast_collision: false, raycast_collision: false,
drops: None,
}, },
Self::Marker => BlockDescriptor { Self::Marker => BlockDescriptor {
name: "marker", name: "marker",
render: RenderType::None, render: RenderType::None,
collision: CollisionType::None, collision: CollisionType::None,
raycast_collision: false, raycast_collision: false,
drops: None,
}, },
Self::Stone => BlockDescriptor { Self::Stone => BlockDescriptor {
name: "stone", name: "stone",
render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Stone)), render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Stone)),
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None,
}, },
Self::Dirt => BlockDescriptor { Self::Dirt => BlockDescriptor {
name: "dirt", name: "dirt",
render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Dirt)), render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Dirt)),
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None,
}, },
Self::Grass => BlockDescriptor { Self::Grass => BlockDescriptor {
name: "grass", name: "grass",
@ -78,54 +83,63 @@ impl Block {
)), )),
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None,
}, },
Self::Sand => BlockDescriptor { Self::Sand => BlockDescriptor {
name: "sand", name: "sand",
render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Sand)), render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Sand)),
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None,
}, },
Self::Cobblestone => BlockDescriptor { Self::Cobblestone => BlockDescriptor {
name: "cobblestone", name: "cobblestone",
render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Cobblestone)), render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Cobblestone)),
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None,
}, },
Self::TallGrass => BlockDescriptor { Self::TallGrass => BlockDescriptor {
name: "tall grass", name: "tall grass",
render: RenderType::CrossShape(CrossTexture::all(BlockTexture::TallGrass)), render: RenderType::CrossShape(CrossTexture::all(BlockTexture::TallGrass)),
collision: CollisionType::None, collision: CollisionType::None,
raycast_collision: true, raycast_collision: true,
drops: None,
}, },
Self::Planks => BlockDescriptor { Self::Planks => BlockDescriptor {
name: "planks", name: "planks",
render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Planks)), render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Planks)),
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None,
}, },
Self::Torch => BlockDescriptor { Self::Torch => BlockDescriptor {
name: "torch", name: "torch",
render: RenderType::CrossShape(CrossTexture::all(BlockTexture::Torch)), render: RenderType::CrossShape(CrossTexture::all(BlockTexture::Torch)),
collision: CollisionType::None, collision: CollisionType::None,
raycast_collision: true, raycast_collision: true,
drops: None,
}, },
Self::Wood => BlockDescriptor { Self::Wood => BlockDescriptor {
name: "leaf", name: "leaf",
render: RenderType::SolidBlock(CubeTexture::horizontal_vertical(BlockTexture::Wood, BlockTexture::WoodTop)), render: RenderType::SolidBlock(CubeTexture::horizontal_vertical(BlockTexture::Wood, BlockTexture::WoodTop)),
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None,
}, },
Self::Leaf => BlockDescriptor { Self::Leaf => BlockDescriptor {
name: "leaf", name: "leaf",
render: RenderType::BinaryTransparency(CubeTexture::all(BlockTexture::Leaf)), render: RenderType::BinaryTransparency(CubeTexture::all(BlockTexture::Leaf)),
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None,
}, },
Self::Water => BlockDescriptor { Self::Water => BlockDescriptor {
name: "water", name: "water",
render: RenderType::BinaryTransparency(CubeTexture::all(BlockTexture::WaterSolid)), render: RenderType::BinaryTransparency(CubeTexture::all(BlockTexture::WaterSolid)),
collision: CollisionType::None, collision: CollisionType::None,
raycast_collision: true, raycast_collision: true,
drops: None,
}, },
} }
} }
@ -137,12 +151,8 @@ pub struct BlockDescriptor {
pub render: RenderType, pub render: RenderType,
pub collision: CollisionType, pub collision: CollisionType,
pub raycast_collision: bool, pub raycast_collision: bool,
pub drops: Option<Item>,
} }
// impl BlockDescriptor {
// pub fn of(block: Block) -> Self {
// block.descriptor()
// }
// }
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct CubeTexture { pub struct CubeTexture {

66
kubi-shared/src/item.rs Normal file
View file

@ -0,0 +1,66 @@
use std::num::NonZeroU8;
use num_enum::TryFromPrimitive;
use serde::{Serialize, Deserialize};
use strum::EnumIter;
use crate::block::Block;
#[derive(Clone, Copy)]
pub enum ItemUsage {
AsBlock(Block)
}
pub struct ItemDescriptor {
pub name: &'static str,
pub usage: Option<ItemUsage>,
pub stack_size: NonZeroU8,
}
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Hash, Debug, EnumIter, TryFromPrimitive)]
#[repr(u8)]
pub enum Item {
TestItem,
}
impl Item {
#[inline]
pub const fn descriptor(self) -> ItemDescriptor {
match self {
Self::TestItem => ItemDescriptor {
name: "Test Item",
usage: None,
stack_size: nz::u8!(32),
},
}
}
}
#[derive(Clone, Copy)]
pub struct ItemCollection(Option<(Item, NonZeroU8)>);
impl ItemCollection {
pub const fn new(item: Item, amount: NonZeroU8) -> Self {
Self(Some((item, amount)))
}
pub const fn new_single(item: Item) -> Self {
Self(Some((item, nz::u8!(1))))
}
pub const fn new_empty() -> Self {
Self(None)
}
pub const fn with_amount(&self, amount: NonZeroU8) -> Self {
Self(match self.0 {
Some((item, _)) => Some((item, amount)),
None => None,
})
}
/// Add items from another slot, copying them\
/// Returns the leftover items
pub fn add(&mut self, from: &Self) -> Self {
let Some((item, count)) = from.0 else { return Self::new_empty() };
todo!() //TODO finish item slot system
}
}

View file

@ -1,4 +1,5 @@
pub mod block; pub mod block;
pub mod item;
pub mod networking; pub mod networking;
pub mod worldgen; pub mod worldgen;
pub mod chunk; pub mod chunk;

View file

@ -1,6 +1,8 @@
use std::borrow::Cow;
use glam::{Vec2, Vec4, vec2}; use glam::{Vec2, Vec4, vec2};
use crate::text::TextRenderer;
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum UiDrawCommand { pub enum UiDrawCommand {
///Filled, colored rectangle ///Filled, colored rectangle
Rectangle { Rectangle {
@ -10,7 +12,17 @@ pub enum UiDrawCommand {
size: Vec2, size: Vec2,
///Color (RGBA) ///Color (RGBA)
color: Vec4, color: Vec4,
} },
Text {
///Position in pixels
position: Vec2,
///Font size
size: u8,
///Color (RGBA)
color: Vec4,
///Text to draw
text: Cow<'static, str>,
},
} }
#[derive(Default)] #[derive(Default)]
@ -25,6 +37,11 @@ pub struct UiDrawCommands {
// } // }
// } // }
pub enum BindTexture {
FontTexture,
//UserDefined(usize),
}
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]
pub struct UiVertex { pub struct UiVertex {
pub position: Vec2, pub position: Vec2,
@ -45,7 +62,7 @@ pub struct UiDrawPlan {
} }
impl UiDrawPlan { impl UiDrawPlan {
pub fn build(calls: &UiDrawCommands) -> Self { pub fn build(calls: &UiDrawCommands, tr: &mut TextRenderer) -> Self {
let mut call = UiDrawCall::default(); let mut call = UiDrawCall::default();
for command in &calls.commands { for command in &calls.commands {
match command { match command {
@ -74,6 +91,9 @@ impl UiDrawPlan {
uv: vec2(0.0, 1.0), uv: vec2(0.0, 1.0),
}, },
]); ]);
},
UiDrawCommand::Text { position, size, color, text } => {
todo!()
} }
} }
} }

View file

@ -15,11 +15,11 @@ use event::UiEvent;
use draw::{UiDrawCommands, UiDrawPlan}; use draw::{UiDrawCommands, UiDrawPlan};
use text::TextRenderer; use text::TextRenderer;
pub struct ElementContext<'a> { // pub struct ElementContext<'a> {
pub state: &'a mut StateRepo, // pub state: &'a mut StateRepo,
pub draw: &'a mut UiDrawCommands, // pub draw: &'a mut UiDrawCommands,
pub text: &'a mut TextRenderer, // pub text: &'a mut TextRenderer,
} // }
pub struct KubiUi { pub struct KubiUi {
mouse_position: Vec2, mouse_position: Vec2,
@ -67,7 +67,7 @@ impl KubiUi {
if self.draw_commands.commands == self.prev_draw_commands.commands { if self.draw_commands.commands == self.prev_draw_commands.commands {
return return
} }
self.draw_plan = UiDrawPlan::build(&self.draw_commands); self.draw_plan = UiDrawPlan::build(&self.draw_commands, &mut self.font_renderer);
self.draw_plan_modified = true; self.draw_plan_modified = true;
} }

View file

@ -10,6 +10,9 @@ const BIN_FONT: &[u8] = include_bytes!("../assets/font/ProggyTiny.ttf");
#[derive(Clone, Copy, PartialEq, Eq, Hash)] #[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct FontHandle(pub(crate) usize); pub struct FontHandle(pub(crate) usize);
#[cfg(feature = "builtin_font")]
pub const BUILTIN_FONT: FontHandle = FontHandle(0);
#[derive(PartialEq, Eq, Hash)] #[derive(PartialEq, Eq, Hash)]
struct GlyphCacheKey { struct GlyphCacheKey {
font_index: usize, font_index: usize,