コード例 #1
0
    void FixedUpdate()
    {
        if (body != null)
        {
            if (target == null)
            {
                target = FindObjectOfType <Mutator>();
            }
            else if (lastKnownTargetState != target.GetCurrentState())
            {
                ResetTargetState();
            }

            if (target != null)
            {
                var targetDirection = target.transform.localPosition - body.localPosition;

                if (targetDirection.magnitude < seeDistance && !target.isHidden)
                {
                    if (lastKnownTargetState == LoopState.LarvaToRoach || lastKnownTargetState == LoopState.Roach)
                    {
                        intentRenderer.sprite   = scared;
                        life.isAttacking        = false;
                        controller.PressedState = new Controller.Pressed()
                        {
                            hor = attackSpeed * -targetDirection.normalized.x,
                            ver = attackSpeed * -targetDirection.normalized.y,
                        };
                    }
                    else
                    {
                        intentRenderer.sprite   = angry;
                        life.isAttacking        = true;
                        controller.PressedState = new Controller.Pressed()
                        {
                            hor = attackSpeed * targetDirection.normalized.x,
                            ver = attackSpeed * targetDirection.normalized.y,
                        };
                    }
                }
                else
                {
                    intentRenderer.sprite = neutral;
                    life.isAttacking      = false;
                    ApplyRandomMove();
                }
            }
            else
            {
                intentRenderer.sprite = neutral;
                life.isAttacking      = false;
                ApplyRandomMove();
            }

            emote.localPosition = new Vector3(body.localPosition.x, body.localPosition.y + 0.4f, 0);
        }
    }
コード例 #2
0
    void FixedUpdate()
    {
        if (body != null)
        {
            if (target == null)
            {
                target = FindObjectOfType <Mutator>();
            }
            else if (lastKnownTargetState != target.GetCurrentState())
            {
                lastKnownTargetState = target.GetCurrentState();
            }

            if (target != null)
            {
                var targetDirection = target.transform.localPosition - body.localPosition;

                if (isAttacking)
                {
                    life.isAttacking        = true;
                    controller.PressedState = new Controller.Pressed()
                    {
                        hor = speed * targetDirection.normalized.x,
                        ver = speed * targetDirection.normalized.y,
                    };
                }
                else
                {
                    life.isAttacking = false;
                    GoToAssignedSwarmPosition();
                }
            }
            else
            {
                life.isAttacking = false;
                GoToAssignedSwarmPosition();
            }
        }
    }
コード例 #3
0
    void FixedUpdate()
    {
        for (int i = 0; i < fliers.Count; i++)
        {
            if (fliers[i] == null)
            {
                fliers.RemoveAt(i);
                assignedModifiers.RemoveAt(i);
                assignedPositions.RemoveAt(i);
                isClockwise.RemoveAt(i);
                i--;
            }
        }

        var distance = new Vector2(currentPosition.x - target.transform.localPosition.x, currentPosition.y - target.transform.localPosition.y).magnitude;

        if (distance < detectionRadius)
        {
            if (target.GetCurrentState() != LoopState.Roach)
            {
                foreach (var f in fliers)
                {
                    f.isAttacking = true;
                }
            }
        }
        else
        {
            foreach (var f in fliers)
            {
                f.isAttacking = false;
            }
        }

        if (currentPosition.x < startX)
        {
            leftDirection = false;
        }
        if (currentPosition.x > endX)
        {
            leftDirection = true;
        }
        if (currentPosition.y < startY)
        {
            downDirection = false;
        }
        if (currentPosition.y > endY)
        {
            downDirection = true;
        }

        if (timeStart + timeSpan < Time.time)
        {
            SetRandomState();
        }

        currentPosition.x += horSpeed * (leftDirection ? -1f : 1f) * (active ? 1f : 0f);
        currentPosition.y += verSpeed * (downDirection ? -1f : 1f) * (active ? 1f : 0f);

        for (int i = 0; i < fliers.Count; i++)
        {
            assignedModifiers[i] = Mathf.Clamp(assignedModifiers[i] + Random.value * 0.1f - 0.05f, 0.5f, 3.5f);

            var posRatio = (assignedPositions[i] - currentPosition).magnitude / assignedModifiers[i];
            var newPos   = (assignedPositions[i] - currentPosition) / posRatio;

            var randomAngle = (0.03f + Random.value * 0.03f) * (isClockwise[i] ? 1f:-1f);
            newPos = new Vector2(Mathf.Cos(randomAngle) * newPos.x - Mathf.Sin(randomAngle) * newPos.y, Mathf.Sin(randomAngle) * newPos.x + Mathf.Cos(randomAngle) * newPos.y);

            assignedPositions[i] = currentPosition + newPos;

            fliers[i].assignedSwarmPosition = assignedPositions[i];
        }
    }