-
Notifications
You must be signed in to change notification settings - Fork 10
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
13 changed files
with
152 additions
and
56 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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use bevy::{ | ||
math::{ops::atan2, Quat}, | ||
time::{Fixed, Time}, | ||
}; | ||
use bevy_ecs::system::{Res, ResMut}; | ||
use shared::world::{MobAction, MobTarget, ServerWorldMap}; | ||
|
||
pub fn mob_behavior_system(mut world_map: ResMut<ServerWorldMap>, delta: Res<Time<Fixed>>) { | ||
let mut mobs = world_map.mobs.clone(); | ||
|
||
for (_mob_id, mob) in mobs.iter_mut() { | ||
let target = match mob.target { | ||
MobTarget::Position(pos) => pos, | ||
MobTarget::None => continue, | ||
MobTarget::Player(id) => world_map.players.get(&id).unwrap().position, | ||
MobTarget::Mob(id) => world_map.mobs.get(&id).unwrap().position, | ||
}; | ||
|
||
let dir = (target - mob.position).normalize(); | ||
let velocity = 2.0 * delta.delta_secs(); | ||
|
||
match mob.action { | ||
MobAction::Walk | MobAction::Attack => { | ||
mob.position += dir * velocity; | ||
mob.rotation = Quat::from_rotation_y(atan2(dir.x, dir.z)); | ||
|
||
// If reached destination, start idling | ||
if mob.position.distance(target) < 0.5 { | ||
mob.action = MobAction::Flee; | ||
} | ||
} | ||
MobAction::Flee => { | ||
if mob.position.distance(target) < 15.0 { | ||
mob.position -= dir * velocity; | ||
mob.rotation = Quat::from_rotation_y(atan2(-dir.x, -dir.z)); | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
world_map.mobs = mobs; | ||
} |
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
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,9 +1,10 @@ | ||
use bevy::prelude::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::world::ServerMob; | ||
use crate::world::{MobId, ServerMob}; | ||
|
||
#[derive(Event, Serialize, Deserialize, Debug, Clone)] | ||
pub struct MobUpdateEvent { | ||
pub id: MobId, | ||
pub mob: ServerMob, | ||
} |
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
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,14 +1,36 @@ | ||
use bevy::math::Vec3; | ||
use bevy::math::{Quat, Vec3}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::messages::PlayerId; | ||
|
||
pub type MobId = u128; | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] | ||
pub enum MobKind { | ||
Fox, | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, Serialize, Deserialize)] | ||
pub enum MobTarget { | ||
None, | ||
Position(Vec3), | ||
Player(PlayerId), | ||
Mob(MobId), | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, Serialize, Deserialize)] | ||
pub enum MobAction { | ||
Idle, | ||
Walk, | ||
Attack, | ||
Flee, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug)] | ||
pub struct ServerMob { | ||
pub id: u128, | ||
pub kind: MobKind, | ||
pub target: MobTarget, | ||
pub action: MobAction, | ||
pub position: Vec3, | ||
pub rotation: Quat, | ||
} |