Pumpkin/crates/pumpkin/src/entity/mob/spider.rs
Alexander Medvedev 8608ac0361 feat: add SynchedEntityData
also some fixes
2026-09-02 22:18:45 +02:00

100 lines
3.1 KiB
Rust

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Weak};
use pumpkin_data::entity::EntityType;
use crate::entity::{
Entity, EntityBase,
ai::goal::{
active_target::ActiveTargetGoal, look_around::RandomLookAroundGoal,
look_at_entity::LookAtEntityGoal, melee_attack::MeleeAttackGoal, revenge::RevengeGoal,
swim::SwimGoal, wander_around::WanderAroundGoal,
},
mob::{Mob, MobEntity},
};
pub struct SpiderEntity {
pub mob_entity: MobEntity,
pub is_climbing: AtomicBool,
}
impl SpiderEntity {
pub fn new(entity: Entity) -> Arc<Self> {
let mob_entity = MobEntity::new(entity);
let spider = Self {
mob_entity,
is_climbing: AtomicBool::new(false),
};
let mob_arc = Arc::new(spider);
let mob_weak: Weak<dyn Mob> = {
let mob_arc: Arc<dyn Mob> = mob_arc.clone();
Arc::downgrade(&mob_arc)
};
{
let mut goal_selector = mob_arc
.mob_entity
.goals_selector
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let mut target_selector = mob_arc
.mob_entity
.target_selector
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
goal_selector.add_goal(1, Box::new(SwimGoal::default()));
goal_selector.add_goal(3, Box::new(MeleeAttackGoal::new(1.0, false)));
goal_selector.add_goal(5, Box::new(WanderAroundGoal::new(0.8)));
goal_selector.add_goal(
6,
LookAtEntityGoal::with_default(mob_weak, &EntityType::PLAYER, 8.0),
);
goal_selector.add_goal(6, Box::new(RandomLookAroundGoal::default()));
target_selector.add_goal(1, Box::new(RevengeGoal::new(true)));
target_selector.add_goal(
2,
ActiveTargetGoal::with_default(&mob_arc.mob_entity, &EntityType::PLAYER, true),
);
target_selector.add_goal(
3,
ActiveTargetGoal::with_default(&mob_arc.mob_entity, &EntityType::IRON_GOLEM, true),
);
};
mob_arc
}
pub fn is_climbing(&self) -> bool {
self.is_climbing.load(Ordering::Relaxed)
}
pub fn set_climbing(&self, climbing: bool) {
if self.is_climbing.swap(climbing, Ordering::Relaxed) != climbing {
let flags = i8::from(climbing);
self.mob_entity
.living_entity
.entity
.set_synced_data(pumpkin_data::tracked_data::spider::DATA_FLAGS_ID, flags);
}
}
}
impl Mob for SpiderEntity {
fn get_mob_entity(&self) -> &MobEntity {
&self.mob_entity
}
fn mob_tick(&self, _caller: &dyn EntityBase) {
let entity = &self.mob_entity.living_entity.entity;
if !entity.is_alive() {
return;
}
let vel = entity.velocity.load();
let is_colliding_horizontally = vel.x.abs() < 1e-4 && vel.z.abs() < 1e-4;
self.set_climbing(is_colliding_horizontally);
}
}