Made socialising NPCs dance

This commit is contained in:
Joshua Barretto 2023-04-03 17:16:29 +01:00
parent 5aaee96cb1
commit 7dfbc2bdab
5 changed files with 32 additions and 14 deletions

View File

@ -216,6 +216,7 @@ pub enum NpcActivity {
Gather(&'static [ChunkResource]),
// TODO: Generalise to other entities? What kinds of animals?
HuntAnimals,
Dance,
}
#[derive(Clone, Copy, Debug)]

View File

@ -66,7 +66,9 @@ impl Controller {
pub fn do_hunt_animals(&mut self) { self.activity = Some(NpcActivity::HuntAnimals); }
pub fn do_greet(&mut self, actor: Actor) { self.actions.push(NpcAction::Greet(actor)); }
pub fn do_dance(&mut self) { self.activity = Some(NpcActivity::Dance); }
pub fn greet(&mut self, actor: Actor) { self.actions.push(NpcAction::Greet(actor)); }
}
pub struct Brain {

View File

@ -454,19 +454,25 @@ fn timeout(time: f64) -> impl FnMut(&mut NpcCtx) -> bool + Clone + Send + Sync {
}
fn socialize() -> impl Action {
just(|ctx| {
now(|ctx| {
let mut rng = thread_rng();
// TODO: Bit odd, should wait for a while after greeting
if thread_rng().gen_bool(0.0003) {
if let Some(other) = ctx
if thread_rng().gen_bool(0.0003) && let Some(other) = ctx
.state
.data()
.npcs
.nearby(ctx.npc.wpos.xy(), 8.0)
.choose(&mut rng)
{
ctx.controller.do_greet(other);
}
just(move |ctx| ctx.controller.greet(other)).boxed()
} else if thread_rng().gen_bool(0.0003) {
just(|ctx| ctx.controller.do_dance())
.repeat()
.stop_if(timeout(6.0))
.map(|_| ())
.boxed()
} else {
idle().boxed()
}
})
}

View File

@ -249,7 +249,8 @@ impl Rule for SimulateNpcs {
Some(
NpcActivity::Goto(_, _)
| NpcActivity::Gather(_)
| NpcActivity::HuntAnimals,
| NpcActivity::HuntAnimals
| NpcActivity::Dance,
) => {},
None => {},
}
@ -276,7 +277,11 @@ impl Rule for SimulateNpcs {
.with_z(0.0);
}
},
Some(NpcActivity::Gather(_) | NpcActivity::HuntAnimals) => {
Some(
NpcActivity::Gather(_)
| NpcActivity::HuntAnimals
| NpcActivity::Dance,
) => {
// TODO: Maybe they should walk around randomly
// when gathering resources?
},

View File

@ -349,6 +349,10 @@ impl<'a> AgentData<'a> {
controller.push_action(ControlAction::Dance);
break 'activity; // Don't fall through to idle wandering
},
Some(NpcActivity::Dance) => {
controller.push_action(ControlAction::Dance);
break 'activity; // Don't fall through to idle wandering
},
Some(NpcActivity::HuntAnimals) => {
if rng.gen::<f32>() < 0.1 {
self.choose_target(