-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
281 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,56 @@ | ||
use std::sync::Arc; | ||
|
||
use async_trait::async_trait; | ||
use tokio::sync::Mutex; | ||
|
||
use crate::entity::{mob::MobEntity, player::Player}; | ||
|
||
use super::Goal; | ||
|
||
pub struct LookAtEntityGoal { | ||
// TODO: make this an entity | ||
target: Mutex<Option<Arc<Player>>>, | ||
range: f64, | ||
} | ||
|
||
impl LookAtEntityGoal { | ||
#[must_use] | ||
pub fn new(range: f64) -> Self { | ||
Self { | ||
target: Mutex::new(None), | ||
range, | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Goal for LookAtEntityGoal { | ||
async fn can_start(&self, mob: &crate::entity::mob::MobEntity) -> bool { | ||
// TODO: make this an entity | ||
let mut target = self.target.lock().await; | ||
|
||
*target = mob | ||
.living_entity | ||
.entity | ||
.world | ||
.get_closest_player(mob.living_entity.entity.pos.load(), self.range) | ||
.await; | ||
target.is_some() | ||
} | ||
|
||
async fn should_continue(&self, mob: &MobEntity) -> bool { | ||
if let Some(target) = self.target.lock().await.as_ref() { | ||
let mob_pos = mob.living_entity.entity.pos.load(); | ||
let target_pos = target.living_entity.entity.pos.load(); | ||
return mob_pos.squared_distance_to_vec(target_pos) <= (self.range * self.range); | ||
} | ||
false | ||
} | ||
|
||
async fn tick(&self, mob: &MobEntity) { | ||
if let Some(target) = self.target.lock().await.as_ref() { | ||
let target_pos = target.living_entity.entity.pos.load(); | ||
mob.living_entity.entity.look_at(target_pos).await; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
use async_trait::async_trait; | ||
|
||
use crate::entity::mob::MobEntity; | ||
|
||
pub mod look_at_entity; | ||
|
||
pub trait Goal { | ||
#[async_trait] | ||
pub trait Goal: Send + Sync { | ||
/// How Should the Goal initially start? | ||
fn can_start(&self) -> bool; | ||
async fn can_start(&self, mob: &MobEntity) -> bool; | ||
/// When its started, How it should Continue to run | ||
fn should_continue() -> bool; | ||
async fn should_continue(&self, mob: &MobEntity) -> bool; | ||
/// If the Goal is running, this gets called every tick | ||
fn tick(&self); | ||
async fn tick(&self, mob: &MobEntity); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,54 @@ | ||
use std::sync::Arc; | ||
|
||
use pumpkin_core::math::vector3::Vector3; | ||
use pumpkin_entity::entity_type::EntityType; | ||
use tokio::sync::Mutex; | ||
use uuid::Uuid; | ||
use zombie::Zombie; | ||
|
||
use crate::{server::Server, world::World}; | ||
|
||
use super::{ai::goal::Goal, living::LivingEntity}; | ||
|
||
pub mod zombie; | ||
|
||
pub struct MobEntity { | ||
pub living_entity: Arc<LivingEntity>, | ||
pub goals: Mutex<Vec<(Arc<dyn Goal>, bool)>>, | ||
} | ||
|
||
impl MobEntity { | ||
pub async fn tick(&self) { | ||
let mut goals = self.goals.lock().await; | ||
for (goal, running) in goals.iter_mut() { | ||
if *running { | ||
if goal.should_continue(self).await { | ||
goal.tick(self).await; | ||
} else { | ||
*running = false; | ||
} | ||
} else { | ||
*running = goal.can_start(self).await; | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub async fn from_type( | ||
entity_type: EntityType, | ||
server: &Server, | ||
position: Vector3<f64>, | ||
world: &Arc<World>, | ||
) -> (Arc<MobEntity>, Uuid) { | ||
match entity_type { | ||
EntityType::Zombie => Zombie::make(server, position, world).await, | ||
// TODO | ||
_ => server.add_mob_entity(entity_type, position, world).await, | ||
} | ||
} | ||
|
||
impl MobEntity { | ||
pub async fn goal<T: Goal + 'static>(&self, goal: T) { | ||
self.goals.lock().await.push((Arc::new(goal), false)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,25 @@ | ||
use std::sync::Arc; | ||
|
||
use pumpkin_core::math::vector3::Vector3; | ||
use pumpkin_entity::entity_type::EntityType; | ||
use uuid::Uuid; | ||
|
||
use crate::{entity::ai::goal::look_at_entity::LookAtEntityGoal, server::Server, world::World}; | ||
|
||
use super::MobEntity; | ||
|
||
pub struct Zombie; | ||
|
||
impl Zombie { | ||
pub async fn make( | ||
server: &Server, | ||
position: Vector3<f64>, | ||
world: &Arc<World>, | ||
) -> (Arc<MobEntity>, Uuid) { | ||
let (zombie_entity, uuid) = server | ||
.add_mob_entity(EntityType::Zombie, position, world) | ||
.await; | ||
zombie_entity.goal(LookAtEntityGoal::new(8.0)).await; | ||
(zombie_entity, uuid) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.