Skip to content

Commit

Permalink
Merge pull request #93 from 2024FALL-SWPP/refactor/final-refactor
Browse files Browse the repository at this point in the history
Refactor/final refactor
  • Loading branch information
silee1103 authored Dec 21, 2024
2 parents 8627834 + 46c455a commit 1f51af4
Show file tree
Hide file tree
Showing 12 changed files with 955 additions and 347 deletions.
795 changes: 708 additions & 87 deletions Assets/01.Scenes/ReleaseStages/Stage8.unity

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion Assets/02.Scripts/Managers/PlayManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class PlayManager : MonoBehaviour
public Dictionary<Transform, List<Color>> activeRipplesColors = new Dictionary<Transform, List<Color>>();
public Transform currentTarget;

private float defaultSize = 6;
private float targetScaleMultiplier = 5.0f;

private void Start()
Expand Down
64 changes: 33 additions & 31 deletions Assets/02.Scripts/Objects/AnimalController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public enum AnimalState

public class AnimalController : Interactable
{
private static readonly int IsMoving = Animator.StringToHash("isMoving");

// SerializeFields
[SerializeField] private AnimalState currentState = AnimalState.FollowPath;
[Range(0f, 5f)]
Expand All @@ -24,23 +26,23 @@ public class AnimalController : Interactable
[Range(0f, 5f)]
[SerializeField] private float pathWaitTime = 0.3f;

private SurfaceContactController animalBody;
private Animator animalAnimator;
private Vector3 currentForward;
private int currentPathIndex = 0;
private bool isRushing = false;
private SurfaceContactController _animalBody;
private Animator _animalAnimator;
private Vector3 _currentForward;
private int _currentPathIndex = 0;
private bool _isRushing = false;

private void Awake()
{
gameObject.AddComponent<InteractionHandler>();
gameObject.AddComponent<PhysicsImmunity>();
Assert.IsNotNull(animalModel);
animalAnimator = animalModel.GetComponent<Animator>();
Assert.IsNotNull(animalAnimator);
animalBody = GetComponent<SurfaceContactController>();
Assert.IsNotNull(animalBody);
_animalAnimator = animalModel.GetComponent<Animator>();
Assert.IsNotNull(_animalAnimator);
_animalBody = GetComponent<SurfaceContactController>();
Assert.IsNotNull(_animalBody);

ResonatableObject resonatable = gameObject.AddComponent<ResonatableObject>();
var resonatable = gameObject.AddComponent<ResonatableObject>();
resonatable.properties = new[] { PitchType.Do };
resonatable.resonate += AnimalResonate;
}
Expand All @@ -49,13 +51,13 @@ private void AnimalResonate(PitchType pitch)
{
switch (pitch)
{
case PitchType.Do: { isRushing = true; break; }
case PitchType.Do: { _isRushing = true; break; }
}
}

void Start()
{
currentForward = animalModel.forward;
_currentForward = animalModel.forward;

// Start the initial behavior based on the current state
if (currentState == AnimalState.Idle)
Expand All @@ -70,16 +72,16 @@ void Start()

void Update()
{
animalModel.forward = currentForward;
animalAnimator.SetBool("isMoving", animalBody.Velocity.magnitude > 0.1f);
animalModel.forward = _currentForward;
_animalAnimator.SetBool(IsMoving, _animalBody.Velocity.magnitude > 0.1f);

if (isRushing)
if (_isRushing)
{
isRushing = false;
_isRushing = false;
if (currentState != AnimalState.Rush) TriggerRush();

// Move forward while rushing
animalBody.Velocity = currentForward * rushSpeed;
_animalBody.Velocity = _currentForward * rushSpeed;
}
else if (currentState == AnimalState.Rush) StopRush(); // Stop rushing when the key is released
}
Expand All @@ -101,13 +103,13 @@ public void TriggerRush()
}

// Set animator parameter
animalAnimator.SetLayerWeight(animalAnimator.GetLayerIndex("Rush"), 1f);
_animalAnimator.SetLayerWeight(_animalAnimator.GetLayerIndex("Rush"), 1f);
}
}

void StopRush()
{
isRushing = false;
_isRushing = false;

// Stop particle effect if playing
if (rushParticleEffect)
Expand All @@ -116,8 +118,8 @@ void StopRush()
}

// Reset animator parameter
animalAnimator.SetLayerWeight(animalAnimator.GetLayerIndex("Rush"), 0f);
animalBody.Velocity = Vector3.zero;
_animalAnimator.SetLayerWeight(_animalAnimator.GetLayerIndex("Rush"), 0f);
_animalBody.Velocity = Vector3.zero;

// Return to the previous behavior
if (pathPoints != null && pathPoints.Length > 0)
Expand All @@ -138,9 +140,9 @@ IEnumerator IdleBehavior()
while (currentState == AnimalState.Idle)
{
yield return new WaitForSeconds(3f);
animalAnimator.Play("stand_to_sit");
_animalAnimator.Play("stand_to_sit");
yield return new WaitForSeconds(3f);
animalAnimator.Play("sit_to_stand");
_animalAnimator.Play("sit_to_stand");
}
}

Expand All @@ -152,15 +154,15 @@ IEnumerator FollowPathBehavior()
yield break;

// Get the next waypoint
Vector3 targetPosition = pathPoints[currentPathIndex].position;
Vector3 targetPosition = pathPoints[_currentPathIndex].position;

// Rotate towards the target
Vector3 targetDirection = Vector3.ProjectOnPlane(targetPosition - transform.position, Vector3.up).normalized;
Vector3 currentDirection = Vector3.ProjectOnPlane(currentForward, Vector3.up).normalized;
Vector3 currentDirection = Vector3.ProjectOnPlane(_currentForward, Vector3.up).normalized;
while (Vector3.Angle(targetDirection, currentDirection) > 0.5f)
{
currentDirection = Vector3.ProjectOnPlane(currentForward, Vector3.up).normalized;
currentForward = Vector3.RotateTowards(currentDirection, targetDirection, idleSpeed * 3 * Time.deltaTime, 0f);
currentDirection = Vector3.ProjectOnPlane(_currentForward, Vector3.up).normalized;
_currentForward = Vector3.RotateTowards(currentDirection, targetDirection, idleSpeed * 3 * Time.deltaTime, 0f);
yield return null;

if (currentState != AnimalState.FollowPath) yield break;
Expand All @@ -175,18 +177,18 @@ IEnumerator FollowPathBehavior()
flattenDistance = flattenDisplacement.magnitude;

targetDirection = Vector3.ProjectOnPlane(targetPosition - transform.position, Vector3.up).normalized;
currentForward = targetDirection;
animalBody.Velocity = currentForward * idleSpeed;
_currentForward = targetDirection;
_animalBody.Velocity = _currentForward * idleSpeed;
yield return null;

if (currentState != AnimalState.FollowPath) yield break;
}

// Stop walking animation
animalBody.Velocity = Vector3.zero;
_animalBody.Velocity = Vector3.zero;

// Move to the next waypoint
currentPathIndex = (currentPathIndex + 1) % pathPoints.Length;
_currentPathIndex = (_currentPathIndex + 1) % pathPoints.Length;

// Wait before moving to the next waypoint
yield return new WaitForSeconds(pathWaitTime);
Expand Down
10 changes: 5 additions & 5 deletions Assets/02.Scripts/Objects/DoorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private void DisableInput()
{
PlayerInput playerInput = playerMovement.GetPlayerInput();
if (playerInput != null)
playerInput.active = false; // 플레이어 움직임 활성화
playerInput.Active = false; // 플레이어 움직임 활성화
}
}

Expand All @@ -77,7 +77,7 @@ private void EnableInput()
{
PlayerInput playerInput = playerMovement.GetPlayerInput();
if (playerInput != null)
playerInput.active = false; // 플레이어 움직임 활성화
playerInput.Active = false; // 플레이어 움직임 활성화
}
}

Expand Down Expand Up @@ -218,18 +218,18 @@ private IEnumerator StageClearDirection()
private IEnumerator OpenDoorAnimation(GameObject door)
{
float duration = 1f; // animation duration
float startRotation = door.transform.eulerAngles.y;
float startRotation = door.transform.localEulerAngles.y;
float endRotation = startRotation + 90;
float time = 0;

while (time < duration)
{
float yRotation = Mathf.Lerp(startRotation, endRotation, time / duration);
door.transform.eulerAngles = new Vector3(door.transform.eulerAngles.x, yRotation, door.transform.eulerAngles.z);
door.transform.localEulerAngles = new Vector3(door.transform.localEulerAngles.x, yRotation, door.transform.localEulerAngles.z);
time += Time.deltaTime;
yield return null;
}

door.transform.eulerAngles = new Vector3(door.transform.eulerAngles.x, endRotation, door.transform.eulerAngles.z);
door.transform.localEulerAngles = new Vector3(door.transform.localEulerAngles.x, endRotation, door.transform.localEulerAngles.z);
}
}
2 changes: 1 addition & 1 deletion Assets/02.Scripts/Objects/ResonatableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ResonatableObject : MonoBehaviour
private int colliderNum = 0;
public Vector3 ripplesPositionOffset = Vector3.zero;

[HideInInspector] public delegate void Resonate(PitchType pitch);
public delegate void Resonate(PitchType pitch);
[HideInInspector] public Resonate resonate;

public void OnEnterRadius()
Expand Down
50 changes: 24 additions & 26 deletions Assets/02.Scripts/Objects/RockController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,21 @@ public class RockController : Interactable

[Tooltip("The distance at which the object stops approaching the player")]
[Range(0f, 0.1f)]
[SerializeField]
private float approachDistance = 3f;
[SerializeField] private float approachDistance = 3f;

[Range(0f, 10f)]
public float moveSpeed = 3f;
[Range(0f, 10f)] public float moveSpeed = 3f;

[SerializeField]
private Transform rockModel;
[SerializeField] private Transform rockModel;

private const string PlayerTag = "Player"; // Tag of the player to follow
private GameObject _player; // Reference to the player object

private const float SpeedThreshold = 1.0f; // Speed threshold for playing sound
private float _radius; // Radius of round rock (only used when isRolling=true)
private bool _isPlayingSound = false;
private Vector3 _currentVelocity = Vector3.zero;
private SurfaceContactController _rockBody;
private Vector3 _targetPosition;
private const float SpeedThreshold = 1.0f; // Speed threshold for playing sound
private bool isPlayingSound = false;
private SurfaceContactController _rockBody;
private GameObject _movingSound;

private void Awake()
Expand Down Expand Up @@ -72,18 +69,7 @@ void Update()
{
ApplyVelocity();
if (isRolling) RollRock(_rockBody.Velocity);

// Check speed and play/stop sound
if (_rockBody.Velocity.magnitude > SpeedThreshold && !isPlayingSound)
{
PlayMovingSound();
isPlayingSound = true;
}
else if (_rockBody.Velocity.magnitude <= SpeedThreshold && isPlayingSound)
{
StopMovingSound();
isPlayingSound = false;
}
HandleSound();
}

private void ApplyVelocity()
Expand All @@ -102,7 +88,7 @@ private void RollRock(Vector3 velocity)

private void MoveToPlayer()
{
if (Vector3.Distance(_player.transform.position, transform.position) < approachDistance) return;
// if (Vector3.Distance(_player.transform.position, transform.position) < approachDistance) return;
var directionToPlayer = (_player.transform.position - transform.position).normalized;
_currentVelocity = directionToPlayer * moveSpeed;
}
Expand All @@ -113,22 +99,34 @@ private void MoveAwayFromPlayer()
_currentVelocity = directionAwayFromPlayer * moveSpeed;
}

private void HandleSound()
{
// Check speed and play/stop sound
if (_rockBody.Velocity.magnitude > SpeedThreshold && !_isPlayingSound)
{
PlayMovingSound();
_isPlayingSound = true;
}
else if (_rockBody.Velocity.magnitude <= SpeedThreshold && _isPlayingSound)
{
StopMovingSound();
_isPlayingSound = false;
}
}

private void PlayMovingSound()
{
// Implement sound playing logic here
_movingSound = GameManager.sm.PlayLoopSound("stone-moving");
AudioSource source = _movingSound.GetComponent<AudioSource>();
if (source != null)
if (_movingSound.TryGetComponent<AudioSource>(out var source))
{
source.volume *= 0.3f; // Reduce volume by half
}
Debug.Log("Playing rolling sound");
}

private void StopMovingSound()
{
// Implement sound stopping logic here
Destroy(_movingSound);
Debug.Log("Stopping rolling sound");
}
}
4 changes: 2 additions & 2 deletions Assets/02.Scripts/Objects/SignController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void Inspect(GameObject floatingText)
currentIndex = 0;

if (playerInput != null)
playerInput.active = true; // 플레이어 움직임 활성화
playerInput.Active = true; // 플레이어 움직임 활성화
}
else
{
Expand Down Expand Up @@ -98,7 +98,7 @@ public void Inspect(GameObject floatingText)
isActive = true;

if (playerInput != null)
playerInput.active = false; // 플레이어 움직임 비활성화
playerInput.Active = false; // 플레이어 움직임 비활성화
}
}

Expand Down
Loading

0 comments on commit 1f51af4

Please sign in to comment.