Skip to content

Commit

Permalink
actor: Add creature codex
Browse files Browse the repository at this point in the history
Ref #200
  • Loading branch information
keis committed Jan 13, 2024
1 parent 8d82734 commit 06d871c
Show file tree
Hide file tree
Showing 32 changed files with 380 additions and 194 deletions.
21 changes: 21 additions & 0 deletions assets/codex/default.creature.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[warrior]
attack = { low = 0, high = 8 }
health = 10
movement = 2
view_radius = 2
color = { Rgba = { red = 0.165, green = 0.631, blue = 0.596, alpha = 1.0 } }
outline_color = { Rgba = { red = 0.155, green = 0.621, blue = 0.586, alpha = 1.0 } }
mesh = "models/heather-shield.obj"
offset = 0.5
scale = 0.1

[slime]
attack = { low = 1, high = 10 }
health = 10
movement = 1
view_radius = 2
color = { Rgba = { red = 0.749, green = 0.584, blue = 0.901, alpha = 0.666 } }
outline_color = { Rgba = { red = 0.739, green = 0.574, blue = 0.891, alpha = 1.0 } }
mesh = "models/blob.obj"
offset = 0.05
scale = 0.5
37 changes: 21 additions & 16 deletions src/action/system.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::queue::{GameAction, GameActionQueue, GameActionSystems};
use crate::{
actor::{
Group, GroupCommandsExt, Movement, Party, PartyBundle, PartyParams, Slide, SlideEvent,
CreatureCodex, CreatureParams, Group, GroupCommandsExt, Movement, Party, PartyBundle,
Slide, SlideEvent,
},
combat::CombatEvent,
inventory::Inventory,
Expand Down Expand Up @@ -41,7 +42,8 @@ pub fn handle_move(
&mut Slide,
&mut Transform,
&MapPresence,
Option<(&mut Movement, &Group)>,
&mut Movement,
Option<&Group>,
),
Without<MapPosition>,
>,
Expand All @@ -54,7 +56,7 @@ pub fn handle_move(
let Some(ref action) = queue.current else {
return Ok(());
};
let (mut slide, mut transform, presence, maybe_movement) =
let (mut slide, mut transform, presence, mut movement, maybe_group) =
party_query.get_mut(action.source)?;
let (map_entity, zone_layer) = zone_layer_query.get_single()?;
let (next_position, next_transform) = map_position_query.get(action.target()?)?;
Expand All @@ -63,16 +65,15 @@ pub fn handle_move(
.ok_or(ExplError::OutOfBounds)
.and_then(|&e| fog_query.get(e).map_err(ExplError::from))?;

// Movement is not tracked for enemies
if let Some((mut movement, group)) = maybe_movement {
if movement.points == 0 {
queue.clear();
return Err(ExplError::MoveWithoutMovementPoints);
}
movement.points -= 1;
if let Err(e) = movement.consume() {
queue.clear();
return Err(e);
}

if let Some(group) = maybe_group {
let mut iter = member_movement_query.iter_many_mut(&group.members);
while let Some(mut movement) = iter.fetch_next() {
movement.points -= 1;
movement.consume().unwrap();
}
}

Expand Down Expand Up @@ -139,7 +140,7 @@ pub fn follow_path(
}

// Keep moving if a path is set
if party_movement.points > 0 {
if party_movement.current > 0 {
if let Some(next) = pathguided.next() {
let action = GameAction::new_move(action.source, *next);
queue.add(action);
Expand Down Expand Up @@ -255,14 +256,16 @@ pub fn handle_enter_camp(
pub fn handle_create_party_from_camp(
mut commands: Commands,
queue: ResMut<GameActionQueue>,
mut party_params: PartyParams,
mut party_params: CreatureParams,
mut camp_query: Query<(&mut Inventory, &MapPresence), With<Camp>>,
map_query: Query<Entity, With<PresenceLayer>>,
creature_codex: CreatureCodex,
) -> Result<(), ExplError> {
let Some(ref action) = queue.current else {
return Ok(());
};

let creature_codex = creature_codex.get()?;
info!(
"Creating party at camp {:?} {:?}",
action.source, action.targets
Expand All @@ -280,7 +283,7 @@ pub fn handle_create_party_from_camp(
Name::new("Party"),
save::Save,
PartyBundle::new(presence.position, "New Party".to_string(), new_supplies)
.with_fluff(&mut party_params),
.with_fluff(&mut party_params, &creature_codex),
))
.add_members(&action.targets);
});
Expand All @@ -291,14 +294,16 @@ pub fn handle_create_party_from_camp(
pub fn handle_split_party(
mut commands: Commands,
queue: ResMut<GameActionQueue>,
mut party_params: PartyParams,
mut party_params: CreatureParams,
mut party_query: Query<(&mut Inventory, &Group, &MapPresence), With<Party>>,
map_query: Query<Entity, With<PresenceLayer>>,
creature_codex: CreatureCodex,
) -> Result<(), ExplError> {
let Some(ref action) = queue.current else {
return Ok(());
};

let creature_codex = creature_codex.get()?;
let (mut party_inventory, group, presence) = party_query.get_mut(action.source)?;
let map_entity = map_query.get_single()?;
if group.members.len() == action.targets.len() {
Expand All @@ -315,7 +320,7 @@ pub fn handle_split_party(
Name::new("Party"),
save::Save,
PartyBundle::new(presence.position, "New Party".to_string(), new_supplies)
.with_fluff(&mut party_params),
.with_fluff(&mut party_params, creature_codex),
))
.add_members(&action.targets);
});
Expand Down
50 changes: 50 additions & 0 deletions src/actor/asset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::combat::Attack;
use bevy::{asset::LoadContext, prelude::*};
use expl_codex::{CodexSource, FromWithLoadContext};
use serde::Deserialize;

#[derive(Clone, Debug, Default, Deserialize)]
pub(super) struct RawCreature {
attack: Attack,
health: u16,
movement: u16,
view_radius: u16,
color: Color,
outline_color: Color,
mesh: String,
offset: f32,
scale: f32,
}

#[derive(Clone, Debug, Default, TypePath)]
pub struct Creature {
pub attack: Attack,
pub health: u16,
pub movement: u16,
pub view_radius: u16,
pub color: Color,
pub outline_color: Color,
pub mesh: Handle<Mesh>,
pub offset: f32,
pub scale: f32,
}

impl CodexSource for Creature {
const EXTENSION: &'static str = "creature.toml";
}

impl FromWithLoadContext<RawCreature> for Creature {
fn from_with_load_context(raw: RawCreature, load_context: &mut LoadContext) -> Self {
Self {
attack: raw.attack,
health: raw.health,
movement: raw.movement,
view_radius: raw.view_radius,
color: raw.color,
outline_color: raw.outline_color,
mesh: load_context.load(raw.mesh),
offset: raw.offset,
scale: raw.scale,
}
}
}
Loading

0 comments on commit 06d871c

Please sign in to comment.