For the complete documentation index, see llms.txt. This page is also available as Markdown.

投射物

CSL 没有内置的投射物组件。使用带有移动脚本的预制体,以及触发器碰撞体或范围查询来检测命中。

基于触发器的投射物

创建一个包含以下内容的预制体:

  • 一个精灵或 Spine_Animator

  • 一个 圆形碰撞体 包含 是触发器 已启用

  • 下面的组件

Damageable :: class : Component {
    health: float = 100 @ao_serialize;

    take_damage :: method(amount: float) {
        health -= amount;
    }
}

Projectile :: class : Component {
    direction: v2;
    owner: Entity;
    speed: float = 12 @ao_serialize;
    damage: float = 25 @ao_serialize;
    lifetime: float = 3 @ao_serialize;
    spent: bool;

    ao_start :: method() {
        collider := entity.get_component(Circle_Collider);
        collider.is_trigger = true;
        collider.on_trigger_start = proc(self: Collider, other: Collider) {
            projectile := self.entity.get_component(Projectile);
            projectile.hit(other.entity);
        };

        entity.queue_for_destruction(lifetime);
    }

    ao_update :: method(dt: float) {
        entity.add_local_position(direction * speed * dt);
    }

    hit :: method(other: Entity) {
        if spent return;
        if other == owner return;

        target := other.get_component(Damageable);
        if target == null return;

        spent = true;
        target.take_damage(damage);
        entity.destroy();
    }
}

生成该预制体时使用 Scene.instantiate,然后初始化其组件:

在正常的共享游戏流程中运行投射物移动和伤害。预测会在客户端和服务器上运行相同的代码,所以不要用 Game.is_server().

快速投射物

触发器重叠检查可能会在投射物在模拟帧之间穿过一个较小目标时漏检。对于快速投射物,沿着运动线段上的多个点使用 Scene.get_all_components_in_range,或者使用更大的碰撞半径。参见 Navmesh 和碰撞 有关子步进的示例。

调用 SFX.play 来自相同的预测命中路径。音效系统会将预测的声音与服务器结果进行同步。

最后更新于