change: Implemented the cymbals as a pogo mechanic for projectiles

This commit is contained in:
Nicholas Novak 2023-05-06 22:09:15 -07:00
parent e79f43de22
commit 8daa9cbfd6
4 changed files with 203 additions and 102 deletions

View File

@ -37,6 +37,10 @@ public class PlayerBehavior : MonoBehaviour
[Header("Cymbals:")] [Header("Cymbals:")]
private float cymbalActiveTime = 0f; private float cymbalActiveTime = 0f;
[SerializeField]
private float cymbalBounceForce = 10f;
[SerializeField]
private float cymbalHitboxRange = 1.2f;
[SerializeField] AudioSource cymbalAudio; [SerializeField] AudioSource cymbalAudio;
[Header("Grappling:")] [Header("Grappling:")]
@ -57,7 +61,8 @@ public class PlayerBehavior : MonoBehaviour
AudioSource audioSource; AudioSource audioSource;
void Awake() { void Awake()
{
// initialize // initialize
_rb = GetComponent<Rigidbody2D>(); _rb = GetComponent<Rigidbody2D>();
@ -72,37 +77,46 @@ public class PlayerBehavior : MonoBehaviour
playerIsAlive = true; playerIsAlive = true;
} }
void Start() { void Start()
{
gameUI.UpdateInstrumentUI(); gameUI.UpdateInstrumentUI();
currentDash = dashTime; currentDash = dashTime;
} }
void Update() { void Update()
if (playerIsAlive) { {
if (this.cymbalActiveTime < 0) { if (playerIsAlive)
{
if (this.cymbalActiveTime < 0)
{
this.gameUI.ToggleCymbal(true); this.gameUI.ToggleCymbal(true);
} }
this.cymbalActiveTime -= Time.deltaTime; this.cymbalActiveTime -= Time.deltaTime;
// throw tambourine // throw tambourine
unlockedTambourine = StateController.Instance.HasTambourine(); unlockedTambourine = StateController.Instance.HasTambourine();
if (playerInput.actions["ThrowTambourine"].WasPressedThisFrame()) { if (playerInput.actions["ThrowTambourine"].WasPressedThisFrame())
{
ThrowTambourine(); ThrowTambourine();
} }
// grapple // grapple
tambourine = GameObject.FindGameObjectWithTag("tambourine"); tambourine = GameObject.FindGameObjectWithTag("tambourine");
if (playerInput.actions["Grapple"].WasPressedThisFrame()) { if (playerInput.actions["Grapple"].WasPressedThisFrame())
{
AttemptGrapple(); AttemptGrapple();
} }
if (playerInput.actions["Grapple"].WasReleasedThisFrame() && grapplingRope.isGrappling) { if (playerInput.actions["Grapple"].WasReleasedThisFrame() && grapplingRope.isGrappling)
{
LetGoOfGrapple(); LetGoOfGrapple();
} }
// clarinet // clarinet
unlockedClarinet = StateController.Instance.HasClarinet(); unlockedClarinet = StateController.Instance.HasClarinet();
if (unlockedClarinet) { if (unlockedClarinet)
if (playerInput.actions["ClarinetDive"].WasPressedThisFrame() && !isInWater && !playerController.IsGrounded() && !isDash) { {
if (playerInput.actions["ClarinetDive"].WasPressedThisFrame() && !isInWater && !playerController.IsGrounded() && !isDash)
{
isDash = true; isDash = true;
this.gameUI.ToggleClarinet(false); this.gameUI.ToggleClarinet(false);
playerInput.DeactivateInput(); playerInput.DeactivateInput();
@ -110,8 +124,10 @@ public class PlayerBehavior : MonoBehaviour
clarinetAudio.Play(); clarinetAudio.Play();
} }
if (!playerController.IsGrounded() && isDash && (currentDash < dashTime)) { if (!playerController.IsGrounded() && isDash && (currentDash < dashTime))
if (!forceAdded) { {
if (!forceAdded)
{
dashVec = new Vector2(0.5f * forward, -1f) * dashForce; dashVec = new Vector2(0.5f * forward, -1f) * dashForce;
_rb.AddForce(dashVec, ForceMode2D.Impulse); _rb.AddForce(dashVec, ForceMode2D.Impulse);
forceAdded = true; forceAdded = true;
@ -119,9 +135,11 @@ public class PlayerBehavior : MonoBehaviour
currentDash += Time.deltaTime; currentDash += Time.deltaTime;
} }
else if ((currentDash >= dashTime)) { else if ((currentDash >= dashTime))
{
// dash ends // dash ends
if (!isInWater) { if (!isInWater)
{
isDash = false; isDash = false;
this.gameUI.ToggleClarinet(true); this.gameUI.ToggleClarinet(true);
forceAdded = false; forceAdded = false;
@ -131,7 +149,8 @@ public class PlayerBehavior : MonoBehaviour
currentDash = 0.0f; currentDash = 0.0f;
dashVec = Vector2.zero; dashVec = Vector2.zero;
} }
else { else
{
isDash = false; isDash = false;
this.gameUI.ToggleClarinet(true); this.gameUI.ToggleClarinet(true);
forceAdded = false; forceAdded = false;
@ -141,14 +160,22 @@ public class PlayerBehavior : MonoBehaviour
} }
} }
if (StateController.Instance.HasCymbal()) { if (StateController.Instance.HasCymbal())
if (this.playerInput.actions["CymbalCrash"].WasPressedThisFrame()) { {
// Play the sound if (this.playerInput.actions["CymbalCrash"].WasPressedThisFrame())
{
if (this.HasProjectileInRange())
{
this.gameUI.ToggleCymbal(false); this.gameUI.ToggleCymbal(false);
cymbalAudio.Play(); cymbalAudio.Play();
Vector2 curVel = this.playerController.RB.velocity;
this.playerController.RB.velocity = new Vector2(curVel.x, 0);
this.playerController.RB.AddForce(new Vector2(0, this.cymbalBounceForce), ForceMode2D.Impulse);
// Set the cymbal active for the equivalent of one second // Set the cymbal active for the equivalent of one second
this.cymbalActiveTime = 1; this.cymbalActiveTime = 0.1f;
}
} }
} }
@ -156,28 +183,36 @@ public class PlayerBehavior : MonoBehaviour
} }
} }
void Animate() { void Animate()
{
// start walking // start walking
if (playerInput.actions["Move"].WasPressedThisFrame()) { if (playerInput.actions["Move"].WasPressedThisFrame())
{
animator.SetBool("Walking", true); animator.SetBool("Walking", true);
} }
// return to idle animation // return to idle animation
if (playerInput.actions["Move"].WasReleasedThisFrame()) { if (playerInput.actions["Move"].WasReleasedThisFrame())
{
animator.SetBool("Walking", false); animator.SetBool("Walking", false);
} }
} }
void OnMove(InputValue value) { void OnMove(InputValue value)
if (playerIsAlive) { {
if (playerIsAlive)
{
_hInput = value.Get<Vector2>().x; _hInput = value.Get<Vector2>().x;
if (_hInput < 0) { if (_hInput < 0)
if (forward != -1) { {
if (forward != -1)
{
// if character hasnt already flipped // if character hasnt already flipped
FlipRenderer(); FlipRenderer();
} }
forward = -1; forward = -1;
} }
else if (_hInput > 0) { else if (_hInput > 0)
{
if (forward != 1) if (forward != 1)
{ // if character hasnt already flipped { // if character hasnt already flipped
FlipRenderer(); FlipRenderer();
@ -188,53 +223,68 @@ public class PlayerBehavior : MonoBehaviour
} }
void FlipRenderer() { void FlipRenderer()
{
GetComponent<SpriteRenderer>().flipX = !GetComponent<SpriteRenderer>().flipX; GetComponent<SpriteRenderer>().flipX = !GetComponent<SpriteRenderer>().flipX;
} }
void ThrowTambourine() { void ThrowTambourine()
if (unlockedTambourine && hasTambourine && !grapplingRope.isGrappling) { {
if (unlockedTambourine && hasTambourine && !grapplingRope.isGrappling)
{
launcher.ThrowTambourine(forward); launcher.ThrowTambourine(forward);
SetHasTambourine(false); SetHasTambourine(false);
} }
} }
public void SetHasTambourine(bool state) { public void SetHasTambourine(bool state)
{
hasTambourine = state; hasTambourine = state;
gameUI.ToggleTambourine(state); gameUI.ToggleTambourine(state);
} }
void AttemptGrapple() { void AttemptGrapple()
if (tambourine != null) { // grapple to tambourine {
if (!grapplingRope.isGrappling && tambourine.GetComponent<TambourineBehavior>().pinned) { if (tambourine != null)
{ // grapple to tambourine
if (!grapplingRope.isGrappling && tambourine.GetComponent<TambourineBehavior>().pinned)
{
grapplingGun.GrappleToTambourine(tambourine); grapplingGun.GrappleToTambourine(tambourine);
grapplingRope.isGrappling = true; grapplingRope.isGrappling = true;
} }
} }
else { else
if (grappleSurface != null) { {
if (grappleSurface != null)
{
grapplingGun.GrappleToSurface(grappleSurface.transform.position); grapplingGun.GrappleToSurface(grappleSurface.transform.position);
grapplingRope.isGrappling = true; grapplingRope.isGrappling = true;
} }
} }
} }
void LetGoOfGrapple() { void LetGoOfGrapple()
{
bool currentlyPaused = StateController.Instance.isPaused; bool currentlyPaused = StateController.Instance.isPaused;
if (grapplingRope.isGrappling && !currentlyPaused) { if (grapplingRope.isGrappling && !currentlyPaused)
if (tambourine != null) { {
if (tambourine != null)
{
tambourine.GetComponent<TambourineBehavior>().DestroySelf(); tambourine.GetComponent<TambourineBehavior>().DestroySelf();
} }
grapplingGun.ReleaseGrapple(); grapplingGun.ReleaseGrapple();
} }
} }
void Bounce() { void Bounce()
{
Vector2 reflect; Vector2 reflect;
if (Mathf.Abs(saveVelocity.y) < 1f && hasBounced) { if (Mathf.Abs(saveVelocity.y) < 1f && hasBounced)
{
reflect = Vector2.zero; reflect = Vector2.zero;
} }
else { else
{
reflect = new Vector2(saveVelocity.x * reflectForce, -(saveVelocity.y) * reflectForce); reflect = new Vector2(saveVelocity.x * reflectForce, -(saveVelocity.y) * reflectForce);
hasBounced = true; hasBounced = true;
} }
@ -243,44 +293,73 @@ public class PlayerBehavior : MonoBehaviour
isDash = false; isDash = false;
} }
void Water() { void Water()
if(isDash) { {
if (isDash)
{
saveVelocity = _rb.velocity / velocityCut; saveVelocity = _rb.velocity / velocityCut;
if (isDash) { if (isDash)
{
dashVec = new Vector2(1f * forward, -1f) * (dashForce / velocityCut); dashVec = new Vector2(1f * forward, -1f) * (dashForce / velocityCut);
_rb.AddForce(dashVec, ForceMode2D.Force); _rb.AddForce(dashVec, ForceMode2D.Force);
} }
} }
else { else
{
playerController.FloatGravity(waterGravity); playerController.FloatGravity(waterGravity);
} }
} }
void OnTriggerEnter2D(Collider2D col) { bool HasProjectileInRange()
if (col.tag == "grappleSurface") { {
GameObject[] objs = GameObject.FindGameObjectsWithTag("Projectile");
foreach (GameObject proj in objs)
{
float distance = Vector2.Distance(proj.transform.position, this.transform.position);
Debug.Log($"Distance: {distance}");
if (distance <= this.cymbalHitboxRange)
{
Debug.Log("Yes");
return true;
}
}
Debug.Log("No");
return false;
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "grappleSurface")
{
grappleSurface = col.gameObject; grappleSurface = col.gameObject;
} }
else if (col.tag == "instaDeath") { else if (col.tag == "instaDeath")
{
StartCoroutine(DestroyPlayer()); StartCoroutine(DestroyPlayer());
} }
else if (col.tag == "spawnPoint") { else if (col.tag == "spawnPoint")
{
StateController.Instance.spawnPoint.GetComponent<SpawnPointBehavior>().DeactivateSpawnPoint(); StateController.Instance.spawnPoint.GetComponent<SpawnPointBehavior>().DeactivateSpawnPoint();
col.GetComponent<SpawnPointBehavior>().ActivateSpawnPoint(); col.GetComponent<SpawnPointBehavior>().ActivateSpawnPoint();
} }
else if (col.tag == "Trumpet") { else if (col.tag == "Trumpet")
{
this.playerController.in_range = true; this.playerController.in_range = true;
this.playerController.enemy = col.transform.parent.gameObject; this.playerController.enemy = col.transform.parent.gameObject;
} }
else if (col.tag == "water") { else if (col.tag == "water")
{
isInWater = true; isInWater = true;
Water(); Water();
} }
else if (col.tag == "bouncePad") { else if (col.tag == "bouncePad")
{
// Assign the player's velocity to zero so that the player can // Assign the player's velocity to zero so that the player can
// bounce on the same jump pad // bounce on the same jump pad
this.playerController.RB.velocity = Vector2.zero; this.playerController.RB.velocity = Vector2.zero;
Bouncepad pad = col.GetComponent<Bouncepad>(); Bouncepad pad = col.GetComponent<Bouncepad>();
switch (pad.Direction()) { switch (pad.Direction())
{
case Bouncepad.Facing.Left: case Bouncepad.Facing.Left:
this.playerController.RB.AddForce( this.playerController.RB.AddForce(
new Vector2(-pad.bounceForce, pad.verticalModifier * pad.bounceForce), new Vector2(-pad.bounceForce, pad.verticalModifier * pad.bounceForce),
@ -297,15 +376,19 @@ public class PlayerBehavior : MonoBehaviour
} }
} }
void OnTriggerExit2D(Collider2D col) { void OnTriggerExit2D(Collider2D col)
if (col.tag == "grappleSurface") { {
if (col.tag == "grappleSurface")
{
grappleSurface = null; grappleSurface = null;
} }
else if (col.tag == "Trumpet") { else if (col.tag == "Trumpet")
{
this.playerController.in_range = false; this.playerController.in_range = false;
this.playerController.enemy = null; this.playerController.enemy = null;
} }
else if (col.tag == "water") { else if (col.tag == "water")
{
isInWater = false; isInWater = false;
isDash = false; isDash = false;
hasBounced = false; hasBounced = false;
@ -315,45 +398,45 @@ public class PlayerBehavior : MonoBehaviour
} }
} }
void OnCollisionEnter2D(Collision2D collision) { void OnCollisionEnter2D(Collision2D collision)
if (collision.gameObject.tag == "Enemy" || collision.gameObject.tag == "ProjectileEnemy") { {
if (collision.transform.position.y < transform.position.y) { if (collision.gameObject.tag == "Enemy" || collision.gameObject.tag == "ProjectileEnemy")
{
if (collision.transform.position.y < transform.position.y)
{
_rb.AddForce(Vector2.up * 8, ForceMode2D.Impulse); _rb.AddForce(Vector2.up * 8, ForceMode2D.Impulse);
collision.gameObject.GetComponent<EnemyPatrol>().DefeatEnemy(); collision.gameObject.GetComponent<EnemyPatrol>().DefeatEnemy();
} }
else { else
{
StartCoroutine(DestroyPlayer()); StartCoroutine(DestroyPlayer());
} }
} }
else if (collision.gameObject.tag == "Projectile") { else if (collision.gameObject.tag == "Projectile")
Destroy(collision.gameObject); {
if (this.cymbalActiveTime > 0) {
Vector2 projVel = collision.gameObject.GetComponent<Rigidbody2D>().velocity;
collision.gameObject.GetComponent<Rigidbody2D>().velocity =
new Vector2(
-projVel.x,
-projVel.y
);
}
else {
StartCoroutine(DestroyPlayer()); StartCoroutine(DestroyPlayer());
} }
}
//stupid stuff for claude's house //stupid stuff for claude's house
else if (collision.gameObject.tag == "SirJacques") { else if (collision.gameObject.tag == "SirJacques")
{
Destroy(collision.gameObject); Destroy(collision.gameObject);
} }
else if (collision.gameObject.tag == "Door") { else if (collision.gameObject.tag == "Door")
{
StateController.Instance.RespawnPlayer(); StateController.Instance.RespawnPlayer();
} }
else if (collision.gameObject.tag == "bouncy") { else if (collision.gameObject.tag == "bouncy")
{
Bounce(); Bounce();
} }
} }
IEnumerator DestroyPlayer() { IEnumerator DestroyPlayer()
if (playerIsAlive) { {
if (grapplingRope.isGrappling) { if (playerIsAlive)
{
if (grapplingRope.isGrappling)
{
LetGoOfGrapple(); LetGoOfGrapple();
} }
playerIsAlive = false; playerIsAlive = false;
@ -368,9 +451,12 @@ public class PlayerBehavior : MonoBehaviour
// destroy all tambourines // destroy all tambourines
GameObject[] currentTambourines = GameObject.FindGameObjectsWithTag("tambourine"); GameObject[] currentTambourines = GameObject.FindGameObjectsWithTag("tambourine");
if (currentTambourines != null) { if (currentTambourines != null)
foreach (GameObject tambourine in currentTambourines) { {
if (tambourine != null) { foreach (GameObject tambourine in currentTambourines)
{
if (tambourine != null)
{
tambourine.GetComponent<TambourineBehavior>().DestroySelf(); tambourine.GetComponent<TambourineBehavior>().DestroySelf();
} }
} }

View File

@ -2,21 +2,26 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class ProjectileBehavior : MonoBehaviour { public class ProjectileBehavior : MonoBehaviour
{
public bool pinned = false; public bool pinned = false;
public void Explode() { public void Explode()
{
Destroy(this.gameObject); Destroy(this.gameObject);
} }
public void Pin() { public void Pin()
{
this.gameObject.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeAll; this.gameObject.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeAll;
this.gameObject.GetComponent<BoxCollider2D>().enabled = false; this.gameObject.GetComponent<BoxCollider2D>().enabled = false;
} }
public void OnCollisionEnter2D(Collision2D col) { public void OnCollisionEnter2D(Collision2D col)
if (col.gameObject.tag == "wall") { {
if (col.gameObject.tag == "wall")
{
Explode(); Explode();
} }
} }

View File

@ -2,23 +2,26 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class ProjectileEnemy : MonoBehaviour { public class ProjectileEnemy : MonoBehaviour
{
[SerializeField] GameObject projectile; [SerializeField] GameObject projectile;
[SerializeField] GameObject firePoint; [SerializeField] GameObject firePoint;
[SerializeField] [Range(0.1f, 3f)] float fireSpeed; [SerializeField][Range(0.1f, 3f)] float fireSpeed;
[SerializeField] float projectileSpeed; [SerializeField] float projectileSpeed;
// Start is called before the first frame update // Start is called before the first frame update
void Start() { void Start()
{
StartCoroutine(Fire()); StartCoroutine(Fire());
} }
IEnumerator Fire() { IEnumerator Fire()
yield return new WaitForSeconds(1/fireSpeed); {
yield return new WaitForSeconds(1 / fireSpeed);
GameObject newProjectile = Instantiate(projectile, firePoint.transform.position, firePoint.transform.rotation); GameObject newProjectile = Instantiate(projectile, firePoint.transform.position, firePoint.transform.rotation);
newProjectile.GetComponent<Rigidbody2D>().AddRelativeForce(new Vector2(projectileSpeed, 0)); newProjectile.GetComponent<Rigidbody2D>().AddRelativeForce(new Vector2(projectileSpeed, 0));
newProjectile.transform.Rotate(new Vector3(0,0,-90)); newProjectile.transform.Rotate(new Vector3(0, 0, -90));
StartCoroutine(Fire()); StartCoroutine(Fire());
} }
} }

View File

@ -41,12 +41,16 @@ public class StateController : MonoBehaviour
[Header("Unlocked Items")] [Header("Unlocked Items")]
public UnlockedItems itemProgression = UnlockedItems.None; public UnlockedItems itemProgression = UnlockedItems.None;
public bool projectileInRange = false;
void Awake() void Awake()
{ {
if (Instance == null) if (Instance == null)
{ {
Instance = this; Instance = this;
} else { }
else
{
Destroy(this.gameObject); Destroy(this.gameObject);
return; return;
} }
@ -54,7 +58,8 @@ public class StateController : MonoBehaviour
SceneManager.sceneLoaded += OnSceneLoaded; SceneManager.sceneLoaded += OnSceneLoaded;
} }
void Start() { void Start()
{
if (this.inDebugMode) if (this.inDebugMode)
{ {
debugCanvas = GameObject.Find("DebugCanvas"); debugCanvas = GameObject.Find("DebugCanvas");
@ -180,7 +185,8 @@ public class StateController : MonoBehaviour
void TogglePauseMenu(bool showPauseMenu) void TogglePauseMenu(bool showPauseMenu)
{ {
if (pauseMenuCanvas != null) { if (pauseMenuCanvas != null)
{
pauseMenuCanvas.SetActive(showPauseMenu); pauseMenuCanvas.SetActive(showPauseMenu);
} }
} }
@ -198,7 +204,8 @@ public class StateController : MonoBehaviour
{ {
foreach (GameObject enemy in enemiesInScene) foreach (GameObject enemy in enemiesInScene)
{ {
if (enemy.GetComponent<EnemyPatrol>().isPlayingDefeatAnimation) { if (enemy.GetComponent<EnemyPatrol>().isPlayingDefeatAnimation)
{
enemy.SetActive(false); enemy.SetActive(false);
} }
enemy.SetActive(true); enemy.SetActive(true);