예제 #1
0
    public void DamageUnit(
        Collider hitCollider,
        int damage,
        float knockbackForce,
        float impactForce,
        Vector3 direction,
        Vector3 point
        )
    {
        Unit unit = collToUnit.FindOrNull(hitCollider);

        if (unit == null)
        {
            return;
        }

        BodyParts bodyParts = unit.parts.bodyParts;

        BodyParts.PartData partData = bodyParts.GetPartDataForCollider(hitCollider);

        if (!partData.isAttached)
        {
            return;
        }

        BodyParts.PartDef partDef = bodyParts.GetPartDefForKind(partData.kind);

        int newHp = Mathf.Max(0, partData.hitPoints - damage);

        partData.hitPoints = newHp;

        Dbg.Log("{0} in {1} for {2} damage!", GarbageCache.GetName(unit), partData.kind, damage);

        if (unit.parts.navMeshAgent != null)
        {
            unit.parts.navMeshAgent.Move(direction * knockbackForce);
        }

        if (newHp == 0)
        {
            if (partDef.lifeCritical)
            {
                Vector3 killPoint = point;
                float?  killForce = impactForce;
                KillUnit(unit, killPoint, killForce);
            }
            else
            {
                Vector3?worldForce = null;

                if (direction != Vector3.zero)
                {
                    worldForce = direction * impactForce;
                }

                bodyParts.Detach(partData, worldForce);
            }
        }
    }