コード例 #1
0
    override protected void HandleState()
    {
        if (currentState == State.Idle)
        {
        }
        else if (currentState == State.Chase)
        {
            FlyToPlayer();
            HandleBounds();
        }
        else if (currentState == State.Reposition)
        {
            if (!IsRepositioned())
            {
                Reposition();
            }
            else
            {
                ResetRotation();
                currentState = State.Idle;
            }
        }
        else if (currentState == State.Relocation)
        {
            Relocation();
        }
        else if (currentState == State.Shooting)
        {
            ResetRotation();
            if (currentLaserTime < laserTime + timeAfterShooting && !playerLife.IsGameOver())
            {
                transform.parent = null;
                float curX    = gameObject.transform.position.x;
                float playerX = 0.0f;

                if (player != null)
                {
                    playerX = player.transform.position.x;
                }
                else
                {
                    player  = GameObject.FindGameObjectWithTag("Player");
                    playerX = 0.0f;
                }
                move = Vector3.zero;
                float randomSpeedFactor = UnityEngine.Random.Range(0.9f, 1.1f);
                if (playerX < curX)                                                                                  // Player is on left side of the enemy (from player's perspective)
                {
                    move += Vector3.left * shootingHorizontalSpeed * greenEnemySpeedMultiplier * randomSpeedFactor;  // Chase the player on LHS
                }
                else if (playerX > curX)                                                                             // Player is on right side of the enemy (from player's perspective)
                {
                    move += Vector3.right * shootingHorizontalSpeed * greenEnemySpeedMultiplier * randomSpeedFactor; // Chase the player on RHS
                }

                if (currentLaserTime > timeBeforeShooting && !isShootingLaser && currentLaserTime < laserTime)
                {
                    // Debug.Log("Gonna SHOOT");
                    isShootingLaser = true;
                    Shoot(1);
                    isLaserRendererEnabled           = true;
                    currentLaserObj                  = Instantiate(laser, transform.position + Vector3.down * laserPosAdjustFactor, Quaternion.identity);
                    currentLaserObj.transform.parent = transform;
                }
                if (currentLaserTime > laserTime && isShootingLaser)
                {
                    // Debug.Log("STOP");
                    isShootingLaser = false;
                    Destroy(currentLaserObj);
                    currentLaserFrameCount = 0;
                }

                if (currentLaserObj != null)
                {
                    // Debug.Log("Shooting");
                    CharacterController laserController = currentLaserObj.GetComponent <CharacterController>();
                    isLaserRendererEnabled = !isLaserRendererEnabled;
                    SpriteRenderer s = currentLaserObj.GetComponentInChildren <SpriteRenderer>();
                    if (currentLaserFrameCount % numberOfFramesToToggle == 0)
                    {
                        Shoot(1);
                        s.enabled = isLaserRendererEnabled;
                    }

                    ++currentLaserFrameCount;
                    laserController.Move(move * Time.deltaTime);
                }

                controller.Move(move * Time.deltaTime);
            }
            else
            {
                transform.parent = originalPos.transform;
                if (transform.parent == null)
                {
                    Debug.Log("THROW ERROR");
                }
                currentLaserTime = 0.0f;
                currentState     = State.Reposition;
            }
            currentLaserTime += Time.deltaTime;
        }
    }