Fixed block-hopping on edges, added correct inventory slots

This commit is contained in:
Joshua Barretto 2019-07-25 18:41:06 +01:00
parent 5bb7998d5a
commit 123a78552a
7 changed files with 34 additions and 25 deletions

View File

@ -4,8 +4,7 @@ pub mod error;
// Reexports
pub use crate::error::Error;
pub use specs::join::Join;
pub use specs::Entity as EcsEntity;
pub use specs::{join::Join, Entity as EcsEntity, ReadStorage};
use common::{
comp,
@ -188,6 +187,10 @@ impl Client {
self.state.terrain().get_key_arc(chunk_pos).cloned()
}
pub fn inventories(&self) -> ReadStorage<comp::Inventory> {
self.state.read_storage()
}
/// Send a chat message to the server.
#[allow(dead_code)]
pub fn send_chat(&mut self, msg: String) {

View File

@ -13,6 +13,10 @@ pub struct Inventory {
}
impl Inventory {
pub fn slots(&self) -> &[Option<Item>] {
&self.slots
}
// Get info about an item slot
pub fn get(&self, cell: usize) -> Option<Item> {
self.slots.get(cell).cloned().flatten()
@ -33,7 +37,7 @@ impl Inventory {
impl Default for Inventory {
fn default() -> Inventory {
Inventory {
slots: vec![None; 24],
slots: vec![None; 8],
}
}
}

View File

@ -11,7 +11,7 @@ const HUMANOID_ACCEL: f32 = 70.0;
const HUMANOID_SPEED: f32 = 120.0;
const HUMANOID_AIR_ACCEL: f32 = 10.0;
const HUMANOID_AIR_SPEED: f32 = 100.0;
const HUMANOID_JUMP_ACCEL: f32 = 16.5;
const HUMANOID_JUMP_ACCEL: f32 = 18.0;
const ROLL_ACCEL: f32 = 120.0;
const ROLL_SPEED: f32 = 550.0;
const GLIDE_ACCEL: f32 = 15.0;

View File

@ -126,6 +126,7 @@ impl<'a> System<'a> for Sys {
let increments = (pos_delta.map(|e| e.abs()).reduce_partial_max() / 0.3)
.ceil()
.max(1.0);
let old_pos = pos.0;
for _ in 0..increments as usize {
pos.0 += pos_delta / increments;
@ -196,13 +197,13 @@ impl<'a> System<'a> for Sys {
// ...and the vertical resolution direction is sufficiently great...
&& -dir.z > 0.1
// ...and we're falling/standing OR there is a block *directly* beneath our current origin (note: not hitbox)...
&& (vel.0.z <= 0.0 || terrain
&& (vel.0.z <= 0.0 && terrain
.get((pos.0 - Vec3::unit_z()).map(|e| e.floor() as i32))
.map(|vox| !vox.is_empty())
.unwrap_or(false))
// ...and there is a collision with a block beneath our current hitbox...
&& collision_with(
pos.0 + resolve_dir - Vec3::unit_z() * 1.05,
old_pos + resolve_dir - Vec3::unit_z() * 1.05,
near_iter.clone(),
)
{

View File

@ -192,6 +192,7 @@ impl Server {
state.write_component(entity, comp::Ori(Vec3::unit_y()));
state.write_component(entity, comp::ActionState::default());
state.write_component(entity, comp::Inventory::default());
state.write_component(entity, comp::InventoryUpdate);
// Make sure physics are accepted.
state.write_component(entity, comp::ForceUpdate);

View File

@ -1,4 +1,5 @@
use super::{img_ids::Imgs, Fonts, TEXT_COLOR};
use client::Client;
use conrod_core::{
color,
position::Relative,
@ -25,8 +26,7 @@ widget_ids! {
#[derive(WidgetCommon)]
pub struct Bag<'a> {
inventory_space: usize,
client: &'a Client,
imgs: &'a Imgs,
fonts: &'a Fonts,
#[conrod(common_builder)]
@ -34,9 +34,9 @@ pub struct Bag<'a> {
}
impl<'a> Bag<'a> {
pub fn new(inventory_space: usize, imgs: &'a Imgs, fonts: &'a Fonts) -> Self {
pub fn new(client: &'a Client, imgs: &'a Imgs, fonts: &'a Fonts) -> Self {
Self {
inventory_space,
client,
imgs,
fonts,
common: widget::CommonBuilder::default(),
@ -72,16 +72,20 @@ impl<'a> Widget for Bag<'a> {
fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event {
let widget::UpdateArgs { state, ui, .. } = args;
let inventory_slots = self
.client
.inventories()
.get(self.client.entity())
.map(|inv| inv.slots().len())
.unwrap_or(0);
// Bag parts
Image::new(self.imgs.bag_bot)
.w_h(61.0 * BAG_SCALE, 9.0 * BAG_SCALE)
.bottom_right_with_margins_on(ui.window, 60.0, 5.0)
.set(state.ids.bag_bot, ui);
Image::new(self.imgs.bag_mid)
.w_h(
61.0 * BAG_SCALE,
((self.inventory_space + 4) / 5) as f64 * 44.0,
)
.w_h(61.0 * BAG_SCALE, ((inventory_slots + 4) / 5) as f64 * 44.0)
.up_from(state.ids.bag_bot, 0.0)
.set(state.ids.bag_mid, ui);
Image::new(self.imgs.bag_top)
@ -91,10 +95,7 @@ impl<'a> Widget for Bag<'a> {
// Alignment for Grid
Rectangle::fill_with(
[
54.0 * BAG_SCALE,
((self.inventory_space + 4) / 5) as f64 * 44.0,
],
[54.0 * BAG_SCALE, ((inventory_slots + 4) / 5) as f64 * 44.0],
color::TRANSPARENT,
)
.top_left_with_margins_on(state.ids.bag_top, 9.0 * BAG_SCALE, 3.0 * BAG_SCALE)
@ -117,16 +118,17 @@ impl<'a> Widget for Bag<'a> {
.thickness(5.0)
.rgba(0.33, 0.33, 0.33, 1.0)
.set(state.ids.inv_scrollbar, ui);*/
// Create available inventory slot widgets
if state.ids.inv_slot.len() < self.inventory_space {
if state.ids.inv_slot.len() < inventory_slots {
state.update(|s| {
s.ids
.inv_slot
.resize(self.inventory_space, &mut ui.widget_id_generator());
.resize(inventory_slots, &mut ui.widget_id_generator());
});
}
// "Allowed" max. inventory space should be handled serverside and thus isn't limited in the UI
for i in 0..self.inventory_space {
for i in 0..inventory_slots {
let x = i % 5;
let y = i / 5;
Button::image(self.imgs.inv_slot)
@ -140,7 +142,7 @@ impl<'a> Widget for Bag<'a> {
.set(state.ids.inv_slot[i], ui);
}
// Test Item
if self.inventory_space > 0 {
if inventory_slots > 0 {
Button::image(self.imgs.potion_red) // TODO: Insert variable image depending on the item displayed in that slot
.w_h(4.0 * 4.4, 7.0 * 4.4) // TODO: Fix height and scale width correctly to that to avoid a stretched item image
.middle_of(state.ids.inv_slot[0]) // TODO: Items need to be assigned to a certain slot and then placed like in this example

View File

@ -588,9 +588,7 @@ impl Hud {
// Bag contents
if self.show.bag {
match Bag::new(self.inventory_space, &self.imgs, &self.fonts)
.set(self.ids.bag, ui_widgets)
{
match Bag::new(client, &self.imgs, &self.fonts).set(self.ids.bag, ui_widgets) {
Some(bag::Event::Close) => {
self.show.bag(false);
self.force_ungrab = true;