Add planks

This commit is contained in:
griffi-gh 2023-02-16 02:44:21 +01:00
parent 287baf15ea
commit 7612ea8d8d
9 changed files with 72 additions and 19 deletions

BIN
assets/blocks/planks.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 247 B

View file

@ -18,6 +18,7 @@ pub enum BlockTexture {
Snow, Snow,
GrassSideSnow, GrassSideSnow,
Cobblestone, Cobblestone,
Planks,
} }
#[derive(Encode, Decode, Clone, Copy, Debug, PartialEq, Eq, EnumIter)] #[derive(Encode, Decode, Clone, Copy, Debug, PartialEq, Eq, EnumIter)]
@ -30,6 +31,7 @@ pub enum Block {
Sand, Sand,
Cobblestone, Cobblestone,
TallGrass, TallGrass,
Planks,
} }
impl Block { impl Block {
@ -81,7 +83,13 @@ impl Block {
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,
} },
Self::Planks => BlockDescriptor {
name: "planks",
render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Planks)),
collision: CollisionType::Solid,
raycast_collision: true,
},
} }
} }
} }

View file

@ -1,14 +1,47 @@
use shipyard::{UniqueViewMut, UniqueView, View, IntoIter, ViewMut, EntitiesViewMut}; use shipyard::{UniqueViewMut, UniqueView, View, IntoIter, ViewMut, EntitiesViewMut, Component, Workload, IntoWorkload};
use glium::glutin::event::VirtualKeyCode;
use kubi_shared::block::Block; use kubi_shared::block::Block;
use crate::{ use crate::{
player::MainPlayer, player::MainPlayer,
world::{raycast::LookingAtBlock, queue::{BlockUpdateQueue, BlockUpdateEvent}}, world::{raycast::{LookingAtBlock, RAYCAST_STEP}, queue::{BlockUpdateQueue, BlockUpdateEvent}},
input::{Inputs, PrevInputs}, input::{Inputs, PrevInputs, RawKbmInputState},
events::{EventComponent, player_actions::PlayerActionEvent}, events::{EventComponent, player_actions::PlayerActionEvent},
}; };
pub fn block_placement_system( #[derive(Component)]
pub struct PlayerHolding(pub Block);
impl Default for PlayerHolding {
fn default() -> Self {
Self(Block::Cobblestone)
}
}
const BLOCK_KEY_MAP: &[(VirtualKeyCode, Block)] = &[
(VirtualKeyCode::Key1, Block::Cobblestone),
(VirtualKeyCode::Key2, Block::Planks),
(VirtualKeyCode::Key3, Block::Dirt),
(VirtualKeyCode::Key4, Block::Grass),
(VirtualKeyCode::Key5, Block::Sand),
(VirtualKeyCode::Key6, Block::Stone),
];
fn pick_block_with_number_keys(
main_player: View<MainPlayer>, main_player: View<MainPlayer>,
mut holding: ViewMut<PlayerHolding>,
input: UniqueView<RawKbmInputState>,
) {
let Some((_, mut holding)) = (&main_player, &mut holding).iter().next() else { return };
for &(key, block) in BLOCK_KEY_MAP {
if input.keyboard_state.contains(&key) {
holding.0 = block;
return
}
}
}
fn block_placement_system(
main_player: View<MainPlayer>,
holding: View<PlayerHolding>,
raycast: View<LookingAtBlock>, raycast: View<LookingAtBlock>,
input: UniqueView<Inputs>, input: UniqueView<Inputs>,
prev_input: UniqueView<PrevInputs>, prev_input: UniqueView<PrevInputs>,
@ -20,12 +53,14 @@ pub fn block_placement_system(
let action_place = input.action_b && !prev_input.0.action_b; let action_place = input.action_b && !prev_input.0.action_b;
let action_break = input.action_a && !prev_input.0.action_a; let action_break = input.action_a && !prev_input.0.action_a;
if action_place ^ action_break { if action_place ^ action_break {
//get raycast info //get components
let Some(ray) = (&main_player, &raycast).iter().next().unwrap().1/**/.0 else { return }; let Some((_, ray, block)) = (&main_player, &raycast, &holding).iter().next() else { return };
let Some(ray) = ray.0 else { return };
//get coord and block type //get coord and block type
let (place_position, place_block) = if action_place { let (place_position, place_block) = if action_place {
let position = (ray.position - ray.direction * 0.251).floor().as_ivec3(); if block.0 == Block::Air { return }
(position, Block::Cobblestone) let position = (ray.position - ray.direction * (RAYCAST_STEP + 0.001)).floor().as_ivec3();
(position, block.0)
} else { } else {
(ray.block_position, Block::Air) (ray.block_position, Block::Air)
}; };
@ -44,3 +79,10 @@ pub fn block_placement_system(
); );
} }
} }
pub fn update_block_placement() -> Workload {
(
pick_block_with_number_keys,
block_placement_system
).into_workload()
}

View file

@ -1,12 +1,12 @@
use shipyard::{UniqueView, UniqueViewMut, Unique, AllStoragesView}; use shipyard::{UniqueView, UniqueViewMut, Unique, AllStoragesView};
use glium::glutin::{event::VirtualKeyCode, event_loop::ControlFlow}; use glium::glutin::{event::VirtualKeyCode, event_loop::ControlFlow};
use crate::input::RawInputState; use crate::input::RawKbmInputState;
#[derive(Unique)] #[derive(Unique)]
pub struct SetControlFlow(pub Option<ControlFlow>); pub struct SetControlFlow(pub Option<ControlFlow>);
pub fn exit_on_esc( pub fn exit_on_esc(
raw_inputs: UniqueView<RawInputState>, raw_inputs: UniqueView<RawKbmInputState>,
mut control_flow: UniqueViewMut<SetControlFlow> mut control_flow: UniqueViewMut<SetControlFlow>
) { ) {
if raw_inputs.keyboard_state.contains(&VirtualKeyCode::Escape) { if raw_inputs.keyboard_state.contains(&VirtualKeyCode::Escape) {

View file

@ -18,7 +18,7 @@ pub struct Inputs {
pub struct PrevInputs(pub Inputs); pub struct PrevInputs(pub Inputs);
#[derive(Unique, Clone, Default, Debug)] #[derive(Unique, Clone, Default, Debug)]
pub struct RawInputState { pub struct RawKbmInputState {
pub keyboard_state: HashSet<VirtualKeyCode, BuildNoHashHasher<u32>>, pub keyboard_state: HashSet<VirtualKeyCode, BuildNoHashHasher<u32>>,
pub button_state: [bool; 32], pub button_state: [bool; 32],
pub mouse_delta: DVec2 pub mouse_delta: DVec2
@ -35,7 +35,7 @@ pub struct ActiveGamepad(Option<GamepadId>);
fn process_events( fn process_events(
device_events: View<InputDeviceEvent>, device_events: View<InputDeviceEvent>,
mut input_state: UniqueViewMut<RawInputState>, mut input_state: UniqueViewMut<RawKbmInputState>,
) { ) {
input_state.mouse_delta = DVec2::ZERO; input_state.mouse_delta = DVec2::ZERO;
for event in device_events.iter() { for event in device_events.iter() {
@ -79,7 +79,7 @@ fn input_start(
} }
fn update_input_state ( fn update_input_state (
raw_inputs: UniqueView<RawInputState>, raw_inputs: UniqueView<RawKbmInputState>,
mut inputs: UniqueViewMut<Inputs>, mut inputs: UniqueViewMut<Inputs>,
) { ) {
inputs.movement += Vec2::new( inputs.movement += Vec2::new(
@ -123,7 +123,7 @@ pub fn init_input (
storages.add_unique(ActiveGamepad::default()); storages.add_unique(ActiveGamepad::default());
storages.add_unique(Inputs::default()); storages.add_unique(Inputs::default());
storages.add_unique(PrevInputs::default()); storages.add_unique(PrevInputs::default());
storages.add_unique(RawInputState::default()); storages.add_unique(RawKbmInputState::default());
} }
pub fn process_inputs() -> Workload { pub fn process_inputs() -> Workload {

View file

@ -72,7 +72,7 @@ use rendering::{
world::draw_world, world::draw_world,
world::draw_current_chunk_border, world::draw_current_chunk_border,
}; };
use block_placement::block_placement_system; use block_placement::update_block_placement;
use delta_time::{DeltaTime, init_delta_time}; use delta_time::{DeltaTime, init_delta_time};
use cursor_lock::{insert_lock_state, update_cursor_lock_state, lock_cursor_now}; use cursor_lock::{insert_lock_state, update_cursor_lock_state, lock_cursor_now};
use control_flow::{exit_on_esc, insert_control_flow_unique, SetControlFlow}; use control_flow::{exit_on_esc, insert_control_flow_unique, SetControlFlow};
@ -124,7 +124,7 @@ fn update() -> Workload {
update_controllers, update_controllers,
generate_move_events, generate_move_events,
update_raycasts, update_raycasts,
block_placement_system, update_block_placement,
apply_queued_blocks, apply_queued_blocks,
).into_workload().run_if(is_ingame), ).into_workload().run_if(is_ingame),
compute_cameras, compute_cameras,

View file

@ -3,7 +3,8 @@ use crate::{
transform::Transform, transform::Transform,
camera::Camera, camera::Camera,
fly_controller::FlyController, fly_controller::FlyController,
world::raycast::LookingAtBlock, world::raycast::LookingAtBlock,
block_placement::PlayerHolding,
}; };
#[derive(Component)] #[derive(Component)]
@ -23,5 +24,6 @@ pub fn spawn_player (
Camera::default(), Camera::default(),
FlyController, FlyController,
LookingAtBlock::default(), LookingAtBlock::default(),
PlayerHolding::default(),
)); ));
} }

View file

@ -30,6 +30,7 @@ impl AssetPaths for BlockTexture {
Self::Snow => "snow.png", Self::Snow => "snow.png",
Self::GrassSideSnow => "grass_side_snow.png", Self::GrassSideSnow => "grass_side_snow.png",
Self::Cobblestone => "cobblestone.png", Self::Cobblestone => "cobblestone.png",
Self::Planks => "planks.png",
} }
} }
} }

View file

@ -4,7 +4,7 @@ use kubi_shared::block::Block;
use crate::transform::Transform; use crate::transform::Transform;
use super::ChunkStorage; use super::ChunkStorage;
const RAYCAST_STEP: f32 = 0.25; pub const RAYCAST_STEP: f32 = 0.25;
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct RaycastReport { pub struct RaycastReport {