veloren/common/src/comp/pet.rs
Ben Wallis 01ca6911a9 * Pets are now saved on logout and spawned with the player on login
* Pets now teleport to their owner when they are too far away from them
* Limited the animals that can be tamed to `QuadrupedLow` and `QuadrupedSmall` to prevent players taming overly powerful creatures before the pet feature is further developed
* Added `Pet` component used to store pet information about an entity - currently only used to store the pet's database ID
* Added `pet` database table which stores a pet's `body_id` and `name`, alongside the `character_id` that it belongs to
* Replaced `HomeChunk` component with more flexible `Anchor` component which supports anchoring entities to other entities as well as chunks.
2021-07-28 22:36:41 +00:00

52 lines
1.4 KiB
Rust

use crate::comp::body::Body;
use crossbeam_utils::atomic::AtomicCell;
use serde::{Deserialize, Serialize};
use specs::Component;
use specs_idvs::IdvStorage;
use std::{num::NonZeroU64, sync::Arc};
pub type PetId = AtomicCell<Option<NonZeroU64>>;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Pet {
#[serde(skip)]
database_id: Arc<PetId>,
}
impl Pet {
/// Not to be used outside of persistence - provides mutable access to the
/// pet component's database ID which is required to save the pet's data
/// from the persistence thread.
#[doc(hidden)]
pub fn get_database_id(&self) -> Arc<PetId> { Arc::clone(&self.database_id) }
pub fn new_from_database(database_id: NonZeroU64) -> Self {
Self {
database_id: Arc::new(AtomicCell::new(Some(database_id))),
}
}
}
impl Default for Pet {
fn default() -> Self {
Self {
database_id: Arc::new(AtomicCell::new(None)),
}
}
}
/// Determines whether an entity of a particular body variant is tameable.
pub fn is_tameable(body: &Body) -> bool {
// Currently only Quadruped animals can be tamed pending further work
// on the pets feature (allowing larger animals to be tamed will
// require balance issues to be addressed).
matches!(
body,
Body::QuadrupedLow(_) | Body::QuadrupedMedium(_) | Body::QuadrupedSmall(_)
)
}
impl Component for Pet {
type Storage = IdvStorage<Self>;
}