コード例 #1
0
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Escape))
     {
         TogglePause();
         OnButtonClick?.Invoke();
     }
 }
コード例 #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));
        }
    }
コード例 #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();
            }
        }
    }
コード例 #4
0
 void PickUpPlant()
 {
     StopFiring();
     carryingPlant = true;
     OnPickupPlantSuccess?.Invoke();
     spriteRenderer.sprite = withPlantNoAnimationSprite;
     animator.runtimeAnimatorController = WithPlantController;
     gameObject.layer = LayerMask.NameToLayer("Plant");
 }
コード例 #5
0
 private void StopFiring()
 {
     if (firingSequence != null)
     {
         StopCoroutine(firingSequence);
         isFiring = false;
         OnShootEnd?.Invoke();
     }
 }
コード例 #6
0
 void KillPlant()
 {
     plant_isAlive = false;
     StopPlantHealthLoop();
     OnPlantDied?.Invoke();
     if (plantInstance != null)
     {
         doPlantDeathPerformance();
     }
 }
コード例 #7
0
    private void HandlePlayerPickedUpPlant()
    {
        if (plantInstance == null)
        {
            return;
        }

        Destroy(plantInstance);
        OnPlantDestroyed?.Invoke();
        camera.Follow = player.transform;
    }
コード例 #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();
        }
    }
コード例 #9
0
    void TogglePause()
    {
        GAME_PAUSED = !GAME_PAUSED;

        if (GAME_PAUSED)
        {
            OnGamePaused?.Invoke();
            Time.timeScale = 0.0f;
        }
        else
        {
            OnGameUnpaused?.Invoke();
            Time.timeScale = 1.0f;
        }
    }
コード例 #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();
     }
 }
コード例 #11
0
    private void AdvanceGameState()
    {
        if (!_levelStarted || _levelComplete)
        {
            return;
        }

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

        if (IsPlayerDefeated())
        {
            _levelComplete = true;
            OnPlayerDefeat?.Invoke();
            return;
        }
    }
コード例 #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;
    }
コード例 #13
0
 private void HandlePlayerFailedShotNoGun()
 {
     OnPlayerShotFailedNoGun?.Invoke();
 }
コード例 #14
0
 private void HandlePlayerLanding()
 {
     OnPlayerLanded?.Invoke();
 }
コード例 #15
0
 private void HandlePlayerStartedShooting()
 {
     OnPlayerStartedShooting?.Invoke();
 }
コード例 #16
0
 public void ReportBadGuyTookDamage()
 {
     OnBadGuyTookDamage?.Invoke();
 }
コード例 #17
0
 private void HandlePlayerStopRunning()
 {
     OnPlayerStopRunning?.Invoke();
 }
コード例 #18
0
 private void HandlePlayerFailedToDropPlant()
 {
     OnPlayerDropPlantFailure?.Invoke();
 }
コード例 #19
0
 public void DoPlayerAttack()
 {
     OnPlayerAttack?.Invoke();
 }
コード例 #20
0
 private void HandlePlayerPickedUpPlant()
 {
     OnPlayerPickupPlantSuccess?.Invoke();
 }
コード例 #21
0
 private void HandlePlayerFailedToPickUpPlant()
 {
     OnPlayerPickupPlantFailure?.Invoke();
 }
コード例 #22
0
 private void HandlePlayerFailedShotNoEnergy()
 {
     OnPlayerShotFailedNoEnergy?.Invoke();
 }
コード例 #23
0
 public void ReportPayAnimation()
 {
     OnPayAnimation?.Invoke();
 }
コード例 #24
0
 public void ReportBadGuyDealtDamage()
 {
     OnBadGuyDealtDamage?.Invoke();
 }
コード例 #25
0
 private void HandleCastleDestroyed()
 {
     OnCastleDestroyed?.Invoke();
 }
コード例 #26
0
 private void HandlePlayerJumpFailed()
 {
     OnPlayerJumpFailed?.Invoke();
 }
コード例 #27
0
 private void HandleTimeExpired()
 {
     OnTimerExpire?.Invoke();
 }
コード例 #28
0
 private void HandlePlayerJumpSuccessful()
 {
     OnPlayerJumpSuccessful?.Invoke();
 }
コード例 #29
0
 void HandlePlantDied()
 {
     OnPlantDied?.Invoke();
     OnGameOver?.Invoke();
     StartCoroutine(PauseAfterDelay(0.4f));
 }
コード例 #30
0
 private void HandlePlantLeftScene()
 {
     OnPlantHasLeftScene?.Invoke();
 }