void Update() { if (Input.GetKeyDown(KeyCode.Escape)) { TogglePause(); OnButtonClick?.Invoke(); } }
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)); } }
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(); } } }
void PickUpPlant() { StopFiring(); carryingPlant = true; OnPickupPlantSuccess?.Invoke(); spriteRenderer.sprite = withPlantNoAnimationSprite; animator.runtimeAnimatorController = WithPlantController; gameObject.layer = LayerMask.NameToLayer("Plant"); }
private void StopFiring() { if (firingSequence != null) { StopCoroutine(firingSequence); isFiring = false; OnShootEnd?.Invoke(); } }
void KillPlant() { plant_isAlive = false; StopPlantHealthLoop(); OnPlantDied?.Invoke(); if (plantInstance != null) { doPlantDeathPerformance(); } }
private void HandlePlayerPickedUpPlant() { if (plantInstance == null) { return; } Destroy(plantInstance); OnPlantDestroyed?.Invoke(); camera.Follow = player.transform; }
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(); } }
void TogglePause() { GAME_PAUSED = !GAME_PAUSED; if (GAME_PAUSED) { OnGamePaused?.Invoke(); Time.timeScale = 0.0f; } else { OnGameUnpaused?.Invoke(); Time.timeScale = 1.0f; } }
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(); } }
private void AdvanceGameState() { if (!_levelStarted || _levelComplete) { return; } if (IsPlayerVictorious()) { _levelComplete = true; OnPlayerVictory?.Invoke(); return; } if (IsPlayerDefeated()) { _levelComplete = true; OnPlayerDefeat?.Invoke(); return; } }
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; }
private void HandlePlayerFailedShotNoGun() { OnPlayerShotFailedNoGun?.Invoke(); }
private void HandlePlayerLanding() { OnPlayerLanded?.Invoke(); }
private void HandlePlayerStartedShooting() { OnPlayerStartedShooting?.Invoke(); }
public void ReportBadGuyTookDamage() { OnBadGuyTookDamage?.Invoke(); }
private void HandlePlayerStopRunning() { OnPlayerStopRunning?.Invoke(); }
private void HandlePlayerFailedToDropPlant() { OnPlayerDropPlantFailure?.Invoke(); }
public void DoPlayerAttack() { OnPlayerAttack?.Invoke(); }
private void HandlePlayerPickedUpPlant() { OnPlayerPickupPlantSuccess?.Invoke(); }
private void HandlePlayerFailedToPickUpPlant() { OnPlayerPickupPlantFailure?.Invoke(); }
private void HandlePlayerFailedShotNoEnergy() { OnPlayerShotFailedNoEnergy?.Invoke(); }
public void ReportPayAnimation() { OnPayAnimation?.Invoke(); }
public void ReportBadGuyDealtDamage() { OnBadGuyDealtDamage?.Invoke(); }
private void HandleCastleDestroyed() { OnCastleDestroyed?.Invoke(); }
private void HandlePlayerJumpFailed() { OnPlayerJumpFailed?.Invoke(); }
private void HandleTimeExpired() { OnTimerExpire?.Invoke(); }
private void HandlePlayerJumpSuccessful() { OnPlayerJumpSuccessful?.Invoke(); }
void HandlePlantDied() { OnPlantDied?.Invoke(); OnGameOver?.Invoke(); StartCoroutine(PauseAfterDelay(0.4f)); }
private void HandlePlantLeftScene() { OnPlantHasLeftScene?.Invoke(); }