Example #1
0
    public void Update()
    {
        // Defaults
        spriteRenderer.enabled       = true;
        tongueSpriteRenderer.enabled = true;

        // Flicker when invulnerable
        if (PlayerInfo.IsInvulnerable())
        {
            if (((int)(Time.unscaledTime * InvulnerableFlickerFrequency * 2.0f)) % 2 == 0)
            {
                spriteRenderer.enabled       = false;
                tongueSpriteRenderer.enabled = false;
            }
        }

        // Hide tongue if underwater
        if (_isUnderwater)
        {
            tongueSpriteRenderer.enabled = false;
            animator.SetBool("Underwater", true);
        }
        else
        {
            animator.SetBool("Underwater", false);
        }

        // Sitting or walking
        Vector2?target = targeter.GetTarget();

        if (target != null)
        {
            animator.SetBool("Sitting", false);
        }
        else
        {
            animator.SetBool("Sitting", true);
        }

        // Make the music follow the player (you get a weird panning effect otherwise)
        GameObject musicPlayer = GameObject.Find("Music");

        if (musicPlayer != null)
        {
            musicPlayer.transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y, 0.0f);
        }

        // If currently invulnerable, decrease invulnerable time left
        invulnerableTime = Mathf.Max(invulnerableTime - Time.deltaTime, 0.0f);

        if (_isUnderwater)
        {
            waterLevel = Mathf.Min(waterLevel + Time.deltaTime * WaterRefillRate, 100.0f);
        }
        else
        {
            waterLevel = Mathf.Max(waterLevel - Time.deltaTime * WaterLossRate, 0.0f);

            // You don't lose health now, you just can't shoot any more bubbles

            /*
             * if(waterLevel <= 0) {
             *      DecrementHealth();
             *      Vector3 pondPos;
             *      if(Random.Range(0, 2) == 0)
             *              pondPos = GameObject.Find("Pond_Left").transform.position;
             *      else
             *              pondPos = GameObject.Find("Pond_Right").transform.position;
             *      transform.position = new Vector3(pondPos.x, pondPos.y, transform.position.z);
             *      Camera.main.transform.position = new Vector3(transform.position.x, transform.position.y, Camera.main.transform.position.z);
             * }
             */
        }
    }
Example #2
0
    private void UpdateState()
    {
        // Transition between water and land
        if ((((Vector2)transform.position) - lastWaterTransitionPos).magnitude > WATER_TRANSITION_DISTANCE)
        {
            // Emerge from water
            if ((currentState == State.Sunk) && lastOnLand)
            {
                currentState           = State.Bubbled;
                lastWaterTransitionPos = ((Vector2)transform.position);
                if (!soundPlaying)
                {
                    soundPlaying     = true;
                    soundWasPlaying  = true;
                    SoundSource.clip = SplashSound;
                    SoundSource.Play();
                }
            }

            // Go into water
            if ((currentState != State.Sunk) && !lastOnLand)
            {
                currentState           = State.Sunk;
                lastWaterTransitionPos = ((Vector2)transform.position);
                timeUnderwater         = 0.0f;
                if (!soundPlaying)
                {
                    soundPlaying     = true;
                    soundWasPlaying  = true;
                    SoundSource.clip = SplashSound;
                    SoundSource.Play();
                }
            }
        }

        if (currentState == State.Sunk)
        {
            timeUnderwater += Time.deltaTime;

            if (timeUnderwater > SunkDeadTime)
            {
                PlayerInfo.IncrementSnakesDrowned();
                Destroy(this.gameObject);
            }

            return;
        }

        if (bubbleTimeLeft > 0.0f)
        {
            currentState    = State.Bubbled;
            bubbleTimeLeft -= Time.deltaTime;
            return;
        }

        // Let the parent remain with the egg
        if (child)
        {
            //Debug.Log("with child");
            if ((((Vector2)(transform.position) - (Vector2)(Player.transform.position)).magnitude < GiveUpDistance) || chaseTimeLeft > 0.0f)
            {
                currentState = State.Chasing;
                return;
            }
            else
            {
                currentState = State.Parenting;
                return;
            }
        }

        // Don't attempt to lay eggs near obstacles - we don't want to lay eggs in the lake!
        if (!nearObstacle && (parentingTimer >= ParentAge) && !child)
        {
            parentingTimer = 0f;

            GameObject[] snakes = GameObject.FindGameObjectsWithTag("Predator");
            GameObject[] eggs   = GameObject.FindGameObjectsWithTag("Egg");

            // Extra randomization step to see if an egg is to be created.
            if ((snakes.Length + eggs.Length < MaxSnakes) && (Random.Range(0f, 1f) <= ParentDesire))
            {
                LayEgg();
                currentState = State.Parenting;
                return;
            }
        }

        if ((((Vector2)(transform.position) - (Vector2)(Home.transform.position)).magnitude > LeashLength) &&
            (((Vector2)(transform.position) - (Vector2)(Player.transform.position)).magnitude > GiveUpDistance) &&
            (chaseTimeLeft <= 0.0f))
        {
            currentState = State.HeadingHome;
        }
        else if (timeSinceWentHome > GoHomeTimeout)
        {
            // Try targeting the player directly to see if they're reachable
            homeTargeter.Target = Player;
            aStarTargeter.underlyingTargeter = homeTargeter;
            Vector2?target = aStarTargeter.GetTarget();

            if (target != null)
            {
                // Check if we're gonna chase.
                if (((((Vector2)(Player.transform.position) - (Vector2)(Home.transform.position)).magnitude < LeashLength) ||
                     (((Vector2)(transform.position) - (Vector2)(Player.transform.position)).magnitude < GiveUpDistance) ||
                     (chaseTimeLeft > 0.0f))

                    && !PlayerInfo.IsUnderwater())
                {
                    currentState = State.Chasing;
                }
                else
                {
                    currentState = State.Wandering;
                }
            }
        }
    }