Exemplo n.º 1
0
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Escape))
     {
         TogglePause();
         OnButtonClick?.Invoke();
     }
 }
Exemplo n.º 2
0
    IEnumerator FireIfShooting()
    {
        OnShootStart.Invoke();
        var key = Random.Range(0, 600);

        while (true)
        {
            int shotCost = ComputeShotCost();
            if (gun_energy >= shotCost)
            {
                OnShootSuccess?.Invoke();
                isFiring = true;
                adjustGunEnergy(-shotCost);
                OnShoot?.Invoke();
                GameObject bulletClone = Instantiate(gun.projectilePrefab, bulletOrigin.position, Quaternion.identity);
                bulletClone.GetComponent <Projectile>()?.SetDamage(gun.damage);
                Rigidbody2D rb = bulletClone.GetComponent <Rigidbody2D>();
                rb.velocity = new Vector2((spriteRenderer.flipX ? -1f : 1f) * gun.projectileSpeed, 0f);
                bulletClone.GetComponent <SpriteRenderer>().flipX = spriteRenderer.flipX;
            }
            else
            {
                gameObject.AddComponent <AudioSource>().PlayOneShot((AudioClip)Resources.Load("Firing_OutOfEnergy"));
                OnShootFailedNoEnergy?.Invoke();
            }
            yield return(new WaitForSeconds(1f / gun.projectileRate));
        }
    }
Exemplo n.º 3
0
    void Update()
    {
        base.Update();
        if (isPaused)
        {
            return;
        }

        if (Input.GetButtonDown("UsePlant"))
        {
            if (carryingPlant)
            {
                AttemptToDropPlant();
            }
            else
            {
                AttemptToPickUpPlant();
            }
        }

        if (Input.GetButtonDown("Jump"))
        {
            if (grounded)
            {
                GetComponent <Rigidbody2D>().AddForce(new Vector2(0f, jumpTakeOffSpeed));
                OnJumpSuccessful?.Invoke();
            }
        }

        if (Input.GetButtonUp("Jump"))
        {
            if (!grounded)
            {
                GetComponent <Rigidbody2D>().AddForce(new Vector2(0f, -0.25f * jumpTakeOffSpeed));
            }
        }

        if (Input.GetButtonDown("Fire1"))
        {
            if (carryingPlant)
            {
                OnShootFailedNoGun?.Invoke();
            }
            else
            {
                StopFiring();
                firingSequence = StartCoroutine(FireIfShooting());
                isFiring       = true;
            }
        }

        if (Input.GetButtonUp("Fire1"))
        {
            if (isFiring)
            {
                StopFiring();
            }
        }
    }
Exemplo n.º 4
0
 void PickUpPlant()
 {
     StopFiring();
     carryingPlant = true;
     OnPickupPlantSuccess?.Invoke();
     spriteRenderer.sprite = withPlantNoAnimationSprite;
     animator.runtimeAnimatorController = WithPlantController;
     gameObject.layer = LayerMask.NameToLayer("Plant");
 }
Exemplo n.º 5
0
 private void StopFiring()
 {
     if (firingSequence != null)
     {
         StopCoroutine(firingSequence);
         isFiring = false;
         OnShootEnd?.Invoke();
     }
 }
Exemplo n.º 6
0
 void KillPlant()
 {
     plant_isAlive = false;
     StopPlantHealthLoop();
     OnPlantDied?.Invoke();
     if (plantInstance != null)
     {
         doPlantDeathPerformance();
     }
 }
Exemplo n.º 7
0
    private void HandlePlayerPickedUpPlant()
    {
        if (plantInstance == null)
        {
            return;
        }

        Destroy(plantInstance);
        OnPlantDestroyed?.Invoke();
        camera.Follow = player.transform;
    }
Exemplo n.º 8
0
    void AttemptToPickUpPlant()
    {
        bool closeEnoughX = Mathf.Abs(transform.position.x - droppedX) < minimumDistanceToGetPlant;
        bool closeEnoughY = grounded || transform.position.y < minimumDistanceToGetPlant;

        if (closeEnoughX && closeEnoughY)
        {
            PickUpPlant();
        }
        else
        {
            OnPickupPlantFailure?.Invoke();
        }
    }
Exemplo n.º 9
0
    void TogglePause()
    {
        GAME_PAUSED = !GAME_PAUSED;

        if (GAME_PAUSED)
        {
            OnGamePaused?.Invoke();
            Time.timeScale = 0.0f;
        }
        else
        {
            OnGameUnpaused?.Invoke();
            Time.timeScale = 1.0f;
        }
    }
Exemplo n.º 10
0
 private void AdjustPlantHealth(int adjustment)
 {
     if (!plant_isAlive)
     {
         return;
     }
     if (plant_health < plant_maxHealth && plant_health + adjustment >= plant_maxHealth)
     {
         OnPlantHealthFilled?.Invoke();
     }
     plant_health = Mathf.Clamp(plant_health + adjustment, 0, plant_maxHealth);
     LowHealthVignette.SetActive(plant_health < 255);
     OnPlantHealthChange?.Invoke(plant_health, plant_maxHealth);
     if (plant_health == 0)
     {
         KillPlant();
     }
 }
Exemplo n.º 11
0
    private void AdvanceGameState()
    {
        if (!_levelStarted || _levelComplete)
        {
            return;
        }

        if (IsPlayerVictorious())
        {
            _levelComplete = true;
            OnPlayerVictory?.Invoke();
            return;
        }

        if (IsPlayerDefeated())
        {
            _levelComplete = true;
            OnPlayerDefeat?.Invoke();
            return;
        }
    }
Exemplo n.º 12
0
    protected override void ComputeVelocity()
    {
        if (isPaused)
        {
            return;
        }
        Vector2 move = Vector2.zero;

        move.x = Input.GetAxis("Horizontal");

        if (Mathf.Abs(move.x) > 0.025f)
        {
            bool flipSprite = (spriteRenderer.flipX ? (move.x > 0.01f) : (move.x < 0.01f));
            if (flipSprite)
            {
                OnChangeDirection?.Invoke(!spriteRenderer.flipX);
                spriteRenderer.flipX = !spriteRenderer.flipX;
                bulletOrigin.transform.localPosition = new Vector3(Mathf.Abs(bulletOrigin.transform.localPosition.x) * (spriteRenderer.flipX ? -1 : 1), bulletOrigin.transform.localPosition.y, bulletOrigin.transform.localPosition.z);
            }
        }

        bool isWalking = Mathf.Abs(move.x) > 0f;

        animator.SetBool("IsWalking", isWalking);

        if (!grounded)
        {
            float positionY = transform.position.y;
            // TODO: second part of IF is hacky way to stop ground bounce from being read as !grounded + falling
            // only really fixes if the map stays flat
            if (lastPosition.y != positionY && positionY > -0.80f)
            {
                bool isJumping = positionY > lastPosition.y;
                animator.SetBool("IsFalling", !isJumping);
                animator.SetBool("IsJumping", isJumping);
            }
        }
        else
        {
            animator.SetBool("IsFalling", false);
            animator.SetBool("IsJumping", false);
        }


        if (lastWalkingStatus != isWalking)
        {
            if (isWalking)
            {
                OnStartRunning?.Invoke();
            }
            else
            {
                OnStopRunning?.Invoke();
            }
        }

        if (grounded && !lastGrounded)
        {
            OnLanding?.Invoke();
        }

        float recoil = isFiring ? maxSpeed / 2f : maxSpeed;

        targetVelocity    = move * recoil;
        lastGrounded      = grounded;
        lastPosition      = transform.position;
        lastWalkingStatus = isWalking;
    }
Exemplo n.º 13
0
 private void HandlePlayerFailedShotNoGun()
 {
     OnPlayerShotFailedNoGun?.Invoke();
 }
Exemplo n.º 14
0
 private void HandlePlayerLanding()
 {
     OnPlayerLanded?.Invoke();
 }
Exemplo n.º 15
0
 private void HandlePlayerStartedShooting()
 {
     OnPlayerStartedShooting?.Invoke();
 }
Exemplo n.º 16
0
 public void ReportBadGuyTookDamage()
 {
     OnBadGuyTookDamage?.Invoke();
 }
Exemplo n.º 17
0
 private void HandlePlayerStopRunning()
 {
     OnPlayerStopRunning?.Invoke();
 }
Exemplo n.º 18
0
 private void HandlePlayerFailedToDropPlant()
 {
     OnPlayerDropPlantFailure?.Invoke();
 }
Exemplo n.º 19
0
 public void DoPlayerAttack()
 {
     OnPlayerAttack?.Invoke();
 }
Exemplo n.º 20
0
 private void HandlePlayerPickedUpPlant()
 {
     OnPlayerPickupPlantSuccess?.Invoke();
 }
Exemplo n.º 21
0
 private void HandlePlayerFailedToPickUpPlant()
 {
     OnPlayerPickupPlantFailure?.Invoke();
 }
Exemplo n.º 22
0
 private void HandlePlayerFailedShotNoEnergy()
 {
     OnPlayerShotFailedNoEnergy?.Invoke();
 }
Exemplo n.º 23
0
 public void ReportPayAnimation()
 {
     OnPayAnimation?.Invoke();
 }
Exemplo n.º 24
0
 public void ReportBadGuyDealtDamage()
 {
     OnBadGuyDealtDamage?.Invoke();
 }
Exemplo n.º 25
0
 private void HandleCastleDestroyed()
 {
     OnCastleDestroyed?.Invoke();
 }
Exemplo n.º 26
0
 private void HandlePlayerJumpFailed()
 {
     OnPlayerJumpFailed?.Invoke();
 }
Exemplo n.º 27
0
 private void HandleTimeExpired()
 {
     OnTimerExpire?.Invoke();
 }
Exemplo n.º 28
0
 private void HandlePlayerJumpSuccessful()
 {
     OnPlayerJumpSuccessful?.Invoke();
 }
Exemplo n.º 29
0
 void HandlePlantDied()
 {
     OnPlantDied?.Invoke();
     OnGameOver?.Invoke();
     StartCoroutine(PauseAfterDelay(0.4f));
 }
Exemplo n.º 30
0
 private void HandlePlantLeftScene()
 {
     OnPlantHasLeftScene?.Invoke();
 }