TO SQUASH: shark movement.

This commit is contained in:
2026-08-11 21:47:03 -04:00
parent d15c7002a8
commit e1c418499f
11 changed files with 327 additions and 80 deletions
+131 -8
View File
@@ -11,6 +11,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 20
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 9123147110355072292}
m_Solo: 0
@@ -36,6 +39,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 60
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: -3560438917037117173}
m_Solo: 0
@@ -87,6 +93,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 250
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: -1682850738014771683}
m_Solo: 0
@@ -138,6 +147,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 290
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 5277595663515725700}
m_Solo: 0
@@ -189,6 +201,9 @@ AnimatorStateTransition:
- m_ConditionMode: 1
m_ConditionEvent: killed
m_EventTreshold: 0
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: -1206562546520099631}
m_Solo: 0
@@ -266,6 +281,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 150
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: -1978790674017899076}
m_Solo: 0
@@ -317,6 +335,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 240
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 7015375175370542177}
m_Solo: 0
@@ -446,6 +467,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 200
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 4558097237328583935}
m_Solo: 0
@@ -471,6 +495,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 270
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: -6743697387058055944}
m_Solo: 0
@@ -522,6 +549,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 50
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: -2911909492595799032}
m_Solo: 0
@@ -572,6 +602,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 40
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 4888496107908782858}
m_Solo: 0
@@ -675,6 +708,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 210
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 7824525655954089900}
m_Solo: 0
@@ -830,6 +866,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 330
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 8645333744770424753}
m_Solo: 0
@@ -881,6 +920,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 320
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: -350104986871661508}
m_Solo: 0
@@ -906,6 +948,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 180
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: -5324687466564205786}
m_Solo: 0
@@ -929,12 +974,6 @@ AnimatorController:
m_Name: Shark
serializedVersion: 6
m_AnimatorParameters:
- m_Name: spawnLeft
m_Type: 4
m_DefaultFloat: 0
m_DefaultInt: 0
m_DefaultBool: 1
m_Controller: {fileID: 9100000}
- m_Name: killed
m_Type: 9
m_DefaultFloat: 0
@@ -947,6 +986,24 @@ AnimatorController:
m_DefaultInt: 0
m_DefaultBool: 0
m_Controller: {fileID: 9100000}
- m_Name: spawnLeft
m_Type: 9
m_DefaultFloat: 0
m_DefaultInt: 0
m_DefaultBool: 0
m_Controller: {fileID: 9100000}
- m_Name: spawnRight
m_Type: 9
m_DefaultFloat: 0
m_DefaultInt: 0
m_DefaultBool: 0
m_Controller: {fileID: 9100000}
- m_Name: canMove
m_Type: 4
m_DefaultFloat: 0
m_DefaultInt: 0
m_DefaultBool: 0
m_Controller: {fileID: 9100000}
m_AnimatorLayers:
- serializedVersion: 5
m_Name: Base Layer
@@ -972,6 +1029,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 280
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: -1967316887960420989}
m_Solo: 0
@@ -997,6 +1057,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 190
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 6514949495483533263}
m_Solo: 0
@@ -1074,6 +1137,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 130
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 5691143146838133271}
m_Solo: 0
@@ -1099,6 +1165,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 0
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: -5938832322387406711}
m_Solo: 0
@@ -1150,6 +1219,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 100
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: -8441714140185346732}
m_Solo: 0
@@ -1175,6 +1247,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 260
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 2776719787719470521}
m_Solo: 0
@@ -1226,6 +1301,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 230
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: -5423501022385390887}
m_Solo: 0
@@ -1251,6 +1329,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 300
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 211193350841979817}
m_Solo: 0
@@ -1276,6 +1357,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 110
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 1755949162279523109}
m_Solo: 0
@@ -1327,6 +1411,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 220
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 4957189558556809040}
m_Solo: 0
@@ -1352,6 +1439,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 80
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 7765654114557479258}
m_Solo: 0
@@ -1377,6 +1467,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 160
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: -721853120501245793}
m_Solo: 0
@@ -1399,8 +1492,8 @@ AnimatorStateTransition:
m_PrefabAsset: {fileID: 0}
m_Name:
m_Conditions:
- m_ConditionMode: 2
m_ConditionEvent: spawnLeft
- m_ConditionMode: 1
m_ConditionEvent: spawnRight
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 331647904783851986}
@@ -1427,6 +1520,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 10
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: -7944689736018436598}
m_Solo: 0
@@ -1812,6 +1908,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 140
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: -4225378035088755024}
m_Solo: 0
@@ -1915,6 +2014,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 30
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: -5472469494141224884}
m_Solo: 0
@@ -1940,6 +2042,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 350
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: -5981924113678901857}
m_Solo: 0
@@ -1965,6 +2070,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 120
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 5177469474983260023}
m_Solo: 0
@@ -1990,6 +2098,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 90
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 1250641032654657572}
m_Solo: 0
@@ -2067,6 +2178,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 170
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 8782352797285012426}
m_Solo: 0
@@ -2092,6 +2206,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 310
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 6225533919839270083}
m_Solo: 0
@@ -2117,6 +2234,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 70
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: -3893262184000258542}
m_Solo: 0
@@ -2220,6 +2340,9 @@ AnimatorStateTransition:
- m_ConditionMode: 6
m_ConditionEvent: angle
m_EventTreshold: 340
- m_ConditionMode: 1
m_ConditionEvent: canMove
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: -1020692297201774755}
m_Solo: 0
+2 -1
View File
@@ -84,7 +84,7 @@ SpriteRenderer:
m_GlobalIlluminationMeshLod: 0
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_SortingOrder: 6
m_MaskInteraction: 0
m_Sprite: {fileID: 9122294262205109509, guid: b634b1f944e581cbeb43efe97e96ec62, type: 3}
m_Color: {r: 1, g: 1, b: 1, a: 1}
@@ -306,3 +306,4 @@ MonoBehaviour:
tiredSound: {fileID: 8300000, guid: bd98ba95bc1ff6961a95e12400cd71ce, type: 3}
deathSound: {fileID: 8300000, guid: e5a1c23ef78e24350938cbaa612817bb, type: 3}
player: {fileID: 0}
gameBoardSpriteRenderer: {fileID: 0}
-62
View File
@@ -2803,67 +2803,6 @@ Transform:
- {fileID: 449049743}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &4230409408318410462
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
serializedVersion: 3
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 1185323843861964289, guid: 4bb45ba16a3bbd351a45d39c3f0b62f9, type: 3}
propertyPath: m_Name
value: Shark
objectReference: {fileID: 0}
- target: {fileID: 1216625218508521996, guid: 4bb45ba16a3bbd351a45d39c3f0b62f9, type: 3}
propertyPath: m_LocalPosition.x
value: -6.2377295
objectReference: {fileID: 0}
- target: {fileID: 1216625218508521996, guid: 4bb45ba16a3bbd351a45d39c3f0b62f9, type: 3}
propertyPath: m_LocalPosition.y
value: -0.99563646
objectReference: {fileID: 0}
- target: {fileID: 1216625218508521996, guid: 4bb45ba16a3bbd351a45d39c3f0b62f9, type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1216625218508521996, guid: 4bb45ba16a3bbd351a45d39c3f0b62f9, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 1216625218508521996, guid: 4bb45ba16a3bbd351a45d39c3f0b62f9, type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1216625218508521996, guid: 4bb45ba16a3bbd351a45d39c3f0b62f9, type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1216625218508521996, guid: 4bb45ba16a3bbd351a45d39c3f0b62f9, type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1216625218508521996, guid: 4bb45ba16a3bbd351a45d39c3f0b62f9, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1216625218508521996, guid: 4bb45ba16a3bbd351a45d39c3f0b62f9, type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1216625218508521996, guid: 4bb45ba16a3bbd351a45d39c3f0b62f9, type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1247360809923930589, guid: 4bb45ba16a3bbd351a45d39c3f0b62f9, type: 3}
propertyPath: player
value:
objectReference: {fileID: 1002905792}
m_RemovedComponents: []
m_RemovedGameObjects: []
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 4bb45ba16a3bbd351a45d39c3f0b62f9, type: 3}
--- !u!1001 &6303327586367475970
PrefabInstance:
m_ObjectHideFlags: 0
@@ -2936,4 +2875,3 @@ SceneRoots:
- {fileID: 292763468}
- {fileID: 40868030}
- {fileID: 1162851830}
- {fileID: 4230409408318410462}
+11 -1
View File
@@ -74,10 +74,20 @@ public class BasicBall : BallBase
{
if (collision.gameObject.CompareTag("Wall") ||
collision.gameObject.CompareTag("BasicBall") ||
collision.gameObject.CompareTag("FruitBall"))
collision.gameObject.CompareTag("FruitBall") ||
collision.gameObject.CompareTag("Shark"))
{
ContactPoint2D contact = collision.GetContact(0);
speed = Vector2.Reflect(speed, contact.normal);
if (collision.gameObject.CompareTag("Shark"))
{
audioSource.PlayOneShot(bounceSound);
}
}
else
{
Physics2D.IgnoreCollision(collision.collider, GetComponent<Collider2D>(), true);
}
}
}
+4
View File
@@ -97,5 +97,9 @@ public class EyeBall : BallBase
ContactPoint2D contact = collision.GetContact(0);
speed = Vector2.Reflect(speed, contact.normal);
}
else
{
Physics2D.IgnoreCollision(collision.collider, GetComponent<Collider2D>(), true);
}
}
}
+13 -2
View File
@@ -86,17 +86,28 @@ public class FruitBall : BallBase
{
if (collision.gameObject.CompareTag("Wall") ||
collision.gameObject.CompareTag("BasicBall") ||
collision.gameObject.CompareTag("FruitBall"))
collision.gameObject.CompareTag("FruitBall") ||
collision.gameObject.CompareTag("Shark"))
{
ContactPoint2D contact = collision.GetContact(0);
speed = Vector2.Reflect(speed, contact.normal);
if (collision.gameObject.CompareTag("BasicBall") ||
collision.gameObject.CompareTag("FruitBall"))
collision.gameObject.CompareTag("FruitBall") ||
collision.gameObject.CompareTag("Shark"))
{
speedIncreaseTimer = 2.0f; // Reset the timer to 2 seconds
speedMultiplier = 20.0f; // Set the speed multiplier to 7x
}
if (collision.gameObject.CompareTag("Shark"))
{
audioSource.PlayOneShot(bounceSound);
}
}
else
{
Physics2D.IgnoreCollision(collision.collider, GetComponent<Collider2D>(), true);
}
}
}
+4
View File
@@ -104,6 +104,10 @@ public class GlassBall : BallBase
Damage();
}
}
else
{
Physics2D.IgnoreCollision(collision.collider, GetComponent<Collider2D>(), true);
}
}
private void Damage()
+4
View File
@@ -143,6 +143,10 @@ public class NukeBall : BallBase
transform.position = currentPosition;
}
}
else
{
Physics2D.IgnoreCollision(collision.collider, GetComponent<Collider2D>(), true);
}
}
private void UpdateFallTargetFromCurrentPosition()
+4
View File
@@ -107,6 +107,10 @@ public class OozeBall : BallBase
ContactPoint2D contact = collision.GetContact(0);
speed = Vector2.Reflect(speed, contact.normal);
}
else
{
Physics2D.IgnoreCollision(collision.collider, GetComponent<Collider2D>(), true);
}
}
public void Split()
+153 -5
View File
@@ -39,26 +39,97 @@ public class Shark : MonoBehaviour
private bool spawnFinished = false;
[SerializeField]
private float normalMaxSpeed = 2.5f;
[SerializeField]
private float turnRateDegreesPerSecond = 180f;
[SerializeField]
private float speedChangeRate = 4f;
[SerializeField]
private float targetReachedDistance = 0.15f;
[SerializeField]
private float alignmentThresholdDegrees = 5f;
private SpriteRenderer sharkSpriteRenderer;
private Bounds? movementBounds = null;
private Vector2 currentTarget;
private bool hasTarget = false;
private float currentSpeed = 0f;
private float currentHeadingAngle = 0f;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
audioSource = GetComponent<AudioSource>();
animator = GetComponent<Animator>();
sharkSpriteRenderer = GetComponent<SpriteRenderer>();
// Ensure movement transitions stay blocked until spawn animation finishes.
animator.SetBool("canMove", false);
bool spawnLeft = Random.Range(0, 2) == 0;
angle = spawnLeft ? 90 : 270;
currentHeadingAngle = angle;
animator.SetBool("spawnLeft", spawnLeft);
// Initialize angle before triggering spawn, so post-spawn movement starts in the correct direction.
animator.SetInteger("angle", angle);
// Clear stale trigger state (useful when object is reused) before setting the selected spawn trigger.
animator.ResetTrigger("spawnLeft");
animator.ResetTrigger("spawnRight");
animator.SetTrigger(spawnLeft ? "spawnLeft" : "spawnRight");
audioSource.PlayOneShot(spawnSound);
}
// Update is called once per frame
void FixedUpdate()
{
// Initialize gameBoardBounds.
if (gameBoardSpriteRenderer != null && gameBoardBounds == null)
{
gameBoardBounds = gameBoardSpriteRenderer.bounds;
}
// Build movement bounds by shrinking the board by 2x the shark's size total (1x on each side).
if (gameBoardBounds != null && sharkSpriteRenderer != null && movementBounds == null)
{
Bounds sharkBounds = sharkSpriteRenderer.bounds;
Bounds boardBounds = gameBoardBounds.Value;
float minX = boardBounds.min.x + sharkBounds.size.x;
float maxX = boardBounds.max.x - sharkBounds.size.x;
float minY = boardBounds.min.y + sharkBounds.size.y;
float maxY = boardBounds.max.y - sharkBounds.size.y;
// Fallback to board center if the shrunken region collapses.
if (minX > maxX)
{
float centerX = boardBounds.center.x;
minX = centerX;
maxX = centerX;
}
if (minY > maxY)
{
float centerY = boardBounds.center.y;
minY = centerY;
maxY = centerY;
}
Vector3 min = new Vector3(minX, minY, boardBounds.min.z);
Vector3 max = new Vector3(maxX, maxY, boardBounds.max.z);
movementBounds = new Bounds((min + max) * 0.5f, max - min);
}
// Check if the spawn animation has finished.
if (
(animator.GetCurrentAnimatorStateInfo(0).IsName("SharkSpawnRight") ||
@@ -67,6 +138,7 @@ public class Shark : MonoBehaviour
)
{
spawnFinished = true;
animator.SetBool("canMove", true);
}
// Do stuff only if the spawn animation has finished.
@@ -79,15 +151,13 @@ public class Shark : MonoBehaviour
{
animator.SetTrigger("killed");
audioSource.PlayOneShot(deathSound);
// TODO: award points to the player.
player.score += sharkKillPoints;
StartCoroutine(DestroyShark());
}
else
{
// TODO: Compute movement position and update the shark's speed and angle.
// TODO: Apply 2x movement speed if the shark is angry.
animator.SetInteger("angle", angle);
UpdateMovement();
animator.SetInteger("angle", angle);
if (!isAngry && !isTired && Random.value < angryProbability)
{
@@ -111,6 +181,84 @@ public class Shark : MonoBehaviour
}
}
private void UpdateMovement()
{
if (movementBounds == null)
{
return;
}
Vector2 currentPosition = transform.position;
if (!hasTarget)
{
currentTarget = ChooseRandomTarget();
hasTarget = true;
}
Vector2 toTarget = currentTarget - currentPosition;
if (toTarget.sqrMagnitude <= targetReachedDistance * targetReachedDistance)
{
currentTarget = ChooseRandomTarget();
toTarget = currentTarget - currentPosition;
}
if (toTarget.sqrMagnitude > 0.0001f)
{
float desiredAngle = AngleFromDirection(toTarget.normalized);
float angleDelta = Mathf.Abs(Mathf.DeltaAngle(currentHeadingAngle, desiredAngle));
currentHeadingAngle = Mathf.MoveTowardsAngle(
currentHeadingAngle,
desiredAngle,
turnRateDegreesPerSecond * Time.fixedDeltaTime
);
float dynamicMaxSpeed = isAngry && !isTired ? normalMaxSpeed * 2f : normalMaxSpeed;
float turningSpeed = normalMaxSpeed * (2f / 3f);
float desiredSpeed = angleDelta <= alignmentThresholdDegrees ? dynamicMaxSpeed : turningSpeed;
currentSpeed = Mathf.MoveTowards(
currentSpeed,
desiredSpeed,
speedChangeRate * Time.fixedDeltaTime
);
Vector2 forward = DirectionFromAngle(currentHeadingAngle);
Vector2 nextPosition = currentPosition + forward * (currentSpeed * Time.fixedDeltaTime);
transform.position = new Vector3(nextPosition.x, nextPosition.y, transform.position.z);
angle = QuantizeAngleToTen(currentHeadingAngle);
}
}
private Vector2 ChooseRandomTarget()
{
Bounds bounds = movementBounds.Value;
float targetX = Random.Range(bounds.min.x, bounds.max.x);
float targetY = Random.Range(bounds.min.y, bounds.max.y);
return new Vector2(targetX, targetY);
}
private static float AngleFromDirection(Vector2 direction)
{
float raw = Mathf.Atan2(-direction.x, -direction.y) * Mathf.Rad2Deg;
return (raw + 360f) % 360f;
}
private static Vector2 DirectionFromAngle(float degrees)
{
float radians = degrees * Mathf.Deg2Rad;
return new Vector2(-Mathf.Sin(radians), -Mathf.Cos(radians));
}
private static int QuantizeAngleToTen(float degrees)
{
int quantized = Mathf.RoundToInt(degrees / 10f) * 10;
quantized = ((quantized % 360) + 360) % 360;
return quantized;
}
private IEnumerator DestroyShark()
{
// Wait two loops of the death animation before destroying the shark object.
+1 -1
View File
@@ -14,7 +14,7 @@ public class SharkSpawner : MonoBehaviour
private bool isActive = true;
private static float spawnProbability = 0.9f;
private static float spawnProbability = 0.5f;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()