Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture the Flag Example Using Entity Layers #426

Merged
merged 22 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ anyhow.workspace = true
clap.workspace = true
criterion.workspace = true
flume.workspace = true
noise.workspace = true # For the terrain example.
noise.workspace = true # For the terrain example.
serde_json.workspace = true
dyc3 marked this conversation as resolved.
Show resolved Hide resolved
tracing.workspace = true

[dev-dependencies.reqwest]
Expand Down
25 changes: 25 additions & 0 deletions crates/valence_core/src/block_pos.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::io::Write;
use std::ops::{Add, Sub};

use anyhow::bail;
use glam::DVec3;
Expand Down Expand Up @@ -108,6 +109,30 @@ impl From<BlockPos> for [i32; 3] {
}
}

impl Add for BlockPos {
type Output = BlockPos;

fn add(self, rhs: Self) -> Self::Output {
Self {
x: self.x + rhs.x,
y: self.y + rhs.y,
z: self.z + rhs.z,
}
}
}

impl Sub for BlockPos {
type Output = BlockPos;

fn sub(self, rhs: Self) -> Self::Output {
Self {
x: self.x - rhs.x,
y: self.y - rhs.y,
z: self.z - rhs.z,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
15 changes: 12 additions & 3 deletions crates/valence_entity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,18 @@ impl PartialEq<Position> for OldPosition {
/// Describes the direction an entity is looking using pitch and yaw angles.
#[derive(Component, Copy, Clone, PartialEq, Default, Debug)]
pub struct Look {
/// The yaw angle in degrees.
/// The yaw angle in degrees, where:
/// - `-90` is looking east (towards positive x).
/// - `0` is looking south (towards positive z).
/// - `90` is looking west (towards negative x).
/// - `180` is looking north (towards negative z).
///
/// Values -180 to 180 are also valid.
pub yaw: f32,
/// The pitch angle in degrees.
/// The pitch angle in degrees, where:
/// - `-90` is looking straight up.
/// - `0` is looking straight ahead.
/// - `90` is looking straight down.
pub pitch: f32,
}

Expand Down Expand Up @@ -367,7 +376,7 @@ impl EntityStatuses {
}
}

#[derive(Component, Default, Debug)]
#[derive(Component, Default, Debug, Copy, Clone)]
pub struct EntityAnimations(pub u8);

impl EntityAnimations {
Expand Down
56 changes: 55 additions & 1 deletion crates/valence_inventory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use valence_client::event_loop::{EventLoopPreUpdate, PacketEvent};
use valence_client::packet::{PlayerAction, PlayerActionC2s};
use valence_client::{Client, FlushPacketsSet, SpawnClientsSet};
use valence_core::game_mode::GameMode;
use valence_core::item::ItemStack;
use valence_core::item::{ItemKind, ItemStack};
use valence_core::protocol::encode::WritePacket;
use valence_core::protocol::var_int::VarInt;
use valence_core::text::{IntoText, Text};
Expand Down Expand Up @@ -316,9 +316,63 @@ impl Inventory {
/// inv.set_slot(3, ItemStack::new(ItemKind::IronIngot, 1, None));
/// assert_eq!(inv.first_empty_slot(), Some(1));
/// ```
#[inline]
pub fn first_empty_slot(&self) -> Option<u16> {
self.first_empty_slot_in(0..self.slot_count())
}

/// Returns the first slot with the given [`ItemKind`] in the inventory
/// where `count() < stack_max`, or `None` if there are no empty slots.
/// ```
/// # use valence_inventory::*;
/// # use valence_core::item::*;
/// let mut inv = Inventory::new(InventoryKind::Generic9x1);
/// inv.set_slot(0, ItemStack::new(ItemKind::Diamond, 1, None));
/// inv.set_slot(2, ItemStack::new(ItemKind::GoldIngot, 64, None));
/// inv.set_slot(3, ItemStack::new(ItemKind::IronIngot, 1, None));
/// inv.set_slot(4, ItemStack::new(ItemKind::GoldIngot, 1, None));
/// assert_eq!(
/// inv.first_slot_with_item_in(ItemKind::GoldIngot, 64, 0..5),
/// Some(4)
/// );
/// ```
pub fn first_slot_with_item_in(
&self,
item: ItemKind,
stack_max: u8,
mut range: Range<u16>,
) -> Option<u16> {
assert!(
(0..=self.slot_count()).contains(&range.start)
&& (0..=self.slot_count()).contains(&range.end),
"slot range out of range"
);
assert!(stack_max > 0, "stack_max must be greater than 0");

range.find(|&idx| {
self.slots[idx as usize]
.as_ref()
.map(|stack| stack.item == item && stack.count() < stack_max)
.unwrap_or(false)
})
}

/// Returns the first slot with the given [`ItemKind`] in the inventory
/// where `count() < stack_max`, or `None` if there are no empty slots.
/// ```
/// # use valence_inventory::*;
/// # use valence_core::item::*;
/// let mut inv = Inventory::new(InventoryKind::Generic9x1);
/// inv.set_slot(0, ItemStack::new(ItemKind::Diamond, 1, None));
/// inv.set_slot(2, ItemStack::new(ItemKind::GoldIngot, 64, None));
/// inv.set_slot(3, ItemStack::new(ItemKind::IronIngot, 1, None));
/// inv.set_slot(4, ItemStack::new(ItemKind::GoldIngot, 1, None));
/// assert_eq!(inv.first_slot_with_item(ItemKind::GoldIngot, 64), Some(4));
/// ```
#[inline]
pub fn first_slot_with_item(&self, item: ItemKind, stack_max: u8) -> Option<u16> {
self.first_slot_with_item_in(item, stack_max, 0..self.slot_count())
}
}

/// Miscellaneous inventory data.
Expand Down
Loading
Loading