Pumpkin/crates/pumpkin/src/entity/mob/piglin.rs
2026-08-24 16:24:23 +02:00

79 lines
2.5 KiB
Rust

use std::sync::{Arc, Weak};
use pumpkin_data::entity::EntityType;
use crate::entity::{
Entity,
ai::goal::{
active_target::ActiveTargetGoal, look_around::RandomLookAroundGoal,
look_at_entity::LookAtEntityGoal, melee_attack::MeleeAttackGoal, open_door::OpenDoorGoal,
swim::SwimGoal, wander_around::WanderAroundGoal,
},
mob::{Mob, MobEntity},
};
pub struct PiglinEntity {
pub mob_entity: MobEntity,
}
impl PiglinEntity {
pub fn new(entity: Entity) -> Arc<Self> {
let mob_entity = MobEntity::new(entity);
let piglin = Self { mob_entity };
let mob_arc = Arc::new(piglin);
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);
goal_selector.add_goal(0, Box::new(SwimGoal::default()));
goal_selector.add_goal(1, Box::new(OpenDoorGoal::new(true)));
// Piglins use crossbows or swords, but for now we give them melee
goal_selector.add_goal(2, Box::new(MeleeAttackGoal::new(1.0, true)));
goal_selector.add_goal(5, Box::new(WanderAroundGoal::new(1.0)));
goal_selector.add_goal(
6,
LookAtEntityGoal::with_default(mob_weak.clone(), &EntityType::PLAYER, 8.0),
);
goal_selector.add_goal(7, Box::new(RandomLookAroundGoal::default()));
let mut target_selector = mob_arc
.mob_entity
.target_selector
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
target_selector.add_goal(
1,
ActiveTargetGoal::with_default(&mob_arc.mob_entity, &EntityType::PLAYER, true),
);
target_selector.add_goal(
2,
ActiveTargetGoal::with_default(
&mob_arc.mob_entity,
&EntityType::WITHER_SKELETON,
true,
),
);
target_selector.add_goal(
2,
ActiveTargetGoal::with_default(&mob_arc.mob_entity, &EntityType::WITHER, true),
);
};
mob_arc
}
}
impl Mob for PiglinEntity {
fn get_mob_entity(&self) -> &MobEntity {
&self.mob_entity
}
}