#bevy #action #pool #ability #input #abilities #management

leafwing_abilities

一个方便、经过良好测试的能力管理套件。为 Bevy 游戏引擎构建。

8 个版本 (破坏性更新)

0.8.0 2024年7月7日
0.7.0 2024年2月24日
0.6.0 2023年11月16日
0.5.0 2023年8月28日
0.1.1 2022年10月18日

#428 in 游戏开发

Download history 1/week @ 2024-06-15 1/week @ 2024-06-22 122/week @ 2024-07-06 77/week @ 2024-07-13 23/week @ 2024-07-20 39/week @ 2024-07-27

每月74 次下载

MIT/Apache

91KB
1.5K SLoC

关于

一套用于在 Bevy 中管理能力的完整工具集。这个包旨在与 Leafwing 输入管理器 一起使用,该管理器将输入转换为动作。

其中一些动作将是能力!能力旨在用于游戏玩法,并遵循复杂但相对标准化的逻辑,关于它们可能如何使用。

use bevy::prelude::*;
use bevy::reflect::Reflect;
use leafwing_abilities::prelude::*;
use leafwing_abilities::premade_pools::mana::{ManaPool, Mana};
use leafwing_abilities::premade_pools::life::{LifePool, Life};
use leafwing_input_manager::prelude::*;

// We're modelling https://leagueoflegends.fandom.com/wiki/Zyra/LoL
// to show off this crate's features!
#[derive(Actionlike, Abilitylike, Clone, Copy, Hash, PartialEq, Eq, Reflect)]
pub enum ZyraAbility {
    GardenOfThorns,
    DeadlySpines,
    RampantGrowth,
    GraspingRoots,
    Stranglethorns,
}

impl ZyraAbility {
    /// You could use the `strum` crate to derive this automatically!
    fn variants() -> Vec<ZyraAbility> {
        use ZyraAbility::*;
        vec![GardenOfThorns, DeadlySpines, RampantGrowth, GraspingRoots, Stranglethorns]
    }

    fn input_map() -> InputMap<ZyraAbility> {
        use ZyraAbility::*;

        // We can use this `new` idiom, which accepts an iterator of pairs
        InputMap::new([
            (DeadlySpines, KeyCode::KeyQ),
            (RampantGrowth, KeyCode::KeyW),
            (GraspingRoots, KeyCode::KeyE),
            (Stranglethorns, KeyCode::KeyR),
        ])
    }

    // This match pattern is super useful to be sure you've defined an attribute for every variant
    fn cooldown(&self) -> Cooldown {
        use ZyraAbility::*;

        let seconds: f32 = match *self {
            GardenOfThorns => 13.0,
            DeadlySpines => 7.0,
            RampantGrowth => 18.0,
            GraspingRoots => 12.0,
            Stranglethorns => 110.0,
        };

        Cooldown::from_secs(seconds)
    }

    fn cooldowns() -> CooldownState<ZyraAbility> {
        let mut cooldowns = CooldownState::default();

        // Now, we can loop over all the variants to populate our struct
        for ability in ZyraAbility::variants() {
            cooldowns.set(ability, ability.cooldown());
        }

        cooldowns
    }

    fn charges() -> ChargeState<ZyraAbility> {
        // The builder API can be very convenient when you only need to set a couple of values
        ChargeState::default()
            .set(ZyraAbility::RampantGrowth, Charges::replenish_one(2))
            .build()
    }

    fn mana_costs() -> AbilityCosts<ZyraAbility, ManaPool> {
        use ZyraAbility::*;
        AbilityCosts::new([
            (DeadlySpines, Mana(70.)),
            (GraspingRoots, Mana(70.)),
            (Stranglethorns, Mana(100.)),
        ])
    }
}

/// Marker component for this champion
#[derive(Component)]
struct Zyra;

#[derive(Bundle)]
struct ZyraBundle {
    champion: Zyra,
    life_pool: LifePool,
    input_manager_bundle: InputManagerBundle<ZyraAbility>,
    abilities_bundle: AbilitiesBundle<ZyraAbility>,
    mana_bundle: PoolBundle<ZyraAbility, ManaPool>,
}

impl Default for ZyraBundle {
    fn default() -> Self {
        ZyraBundle {
            champion: Zyra,
            // Max life, then regen
            life_pool: LifePool::new(Life(574.), Life(574.), (Life(5.5))),
            input_manager_bundle: InputManagerBundle::<ZyraAbility> {
                input_map: ZyraAbility::input_map(),
                ..default()
            },
            abilities_bundle: AbilitiesBundle::<ZyraAbility> {
                cooldowns: ZyraAbility::cooldowns(),
                charges: ZyraAbility::charges(),
            },
            mana_bundle: PoolBundle::<ZyraAbility, ManaPool> {
                pool: ManaPool::new(Mana(418.), Mana(418.), Mana(13.0)),
                ability_costs: ZyraAbility::mana_costs(),
            }
        }
    }
}

特性

  • 跟踪和自动计时冷却时间
  • 存储多个能力充能
  • Leafwing Studio 的专有 #[deny(missing_docs)]

计划中

  • 资源管理(健康、法力、能量等)
  • 伤害
  • 施法时间
  • 范围检查

依赖项

~41–78MB
~1.5M SLoC