@@ -9,30 +9,119 @@ public class PlayerController : MonoBehaviour {
99 [ SerializeField ]
1010 private float speed ;
1111
12+ [ SerializeField ]
13+ private float turnSpeed ;
14+
15+ [ SerializeField ]
16+ private float smoothTime ;
17+
18+
19+ private Rigidbody playerRigidbody ;
20+
21+ private Vector2 currentSmoothDampVelocity ;
22+
23+
24+ private Vector3 initialPosition ;
25+ private Quaternion initialRotation ;
26+
27+
28+ private void Start ( ) {
29+ playerRigidbody = GetComponent < Rigidbody > ( ) ;
30+
31+ initialPosition = transform . position ;
32+ initialRotation = transform . rotation ;
33+
34+ GameManager . Instance . onGameStateChanged += GameStateChanged ;
35+ }
36+
37+ private void OnDestroy ( ) {
38+ if ( GameManager . Instance != null ) {
39+ GameManager . Instance . onGameStateChanged -= GameStateChanged ;
40+ }
41+ }
1242
1343 private void Update ( ) {
14- var newPosition = transform . position + ( Vector3 ) ( speed * Time . deltaTime * movementAction . action . ReadValue < Vector2 > ( ) ) ;
44+ if ( GameManager . Instance . GameState == GameState . Running ) {
45+ CalculateNewVelocity ( ) ;
46+
47+ CheckBoundaries ( ) ;
1548
16- var lowerBoundary = GameManager . Instance . GameLowerBoundaries ;
17- var upperBoundary = GameManager . Instance . GameUpperBoundaries ;
49+ RotateByVelocity ( ) ;
50+ }
51+ }
1852
19- if ( newPosition . x < lowerBoundary . x ) {
20- newPosition . x = lowerBoundary . x ;
2153
22- } else if ( newPosition . x > upperBoundary . x ) {
23- newPosition . x = upperBoundary . x ;
54+ private void CalculateNewVelocity ( ) {
55+ var newVelocity = speed * movementAction . action . ReadValue < Vector2 > ( ) ;
56+
57+ newVelocity = Vector2 . SmoothDamp (
58+ playerRigidbody . velocity ,
59+ newVelocity ,
60+ ref currentSmoothDampVelocity ,
61+ smoothTime
62+ ) ;
63+
64+ playerRigidbody . velocity = newVelocity ;
65+ }
66+
67+ private void CheckBoundaries ( ) {
68+ var lowerBoundaries = GameManager . Instance . GameLowerBoundaries ;
69+ var upperBoundaries = GameManager . Instance . GameUpperBoundaries ;
70+
71+ var position = transform . position ;
72+ var velocity = playerRigidbody . velocity ;
73+
74+ if ( position . x < lowerBoundaries . x ) {
75+ position . x = lowerBoundaries . x ;
76+ velocity . x = 0 ;
77+
78+ } else if ( position . x > upperBoundaries . x ) {
79+ position . x = upperBoundaries . x ;
80+ velocity . x = 0 ;
2481 }
2582
26- if ( newPosition . y < lowerBoundary . y ) {
27- newPosition . y = lowerBoundary . y ;
83+ if ( position . y < lowerBoundaries . y ) {
84+ position . y = lowerBoundaries . y ;
85+ velocity . y = 0 ;
2886
29- } else if ( newPosition . y > upperBoundary . y ) {
30- newPosition . y = upperBoundary . y ;
87+ } else if ( position . y > upperBoundaries . y ) {
88+ position . y = upperBoundaries . y ;
89+ velocity . y = 0 ;
3190 }
3291
33- transform . position = newPosition ;
92+ transform . position = position ;
93+ playerRigidbody . velocity = velocity ;
94+ }
95+
96+ private void RotateByVelocity ( ) {
97+ var direction = Quaternion . LookRotation (
98+ playerRigidbody . velocity + ( Vector3 . forward * speed )
99+ ) ;
100+
101+ var rotation = Quaternion . Slerp (
102+ transform . rotation ,
103+ direction ,
104+ turnSpeed * Time . deltaTime
105+ ) ;
106+
107+ playerRigidbody . MoveRotation (
108+ rotation
109+ ) ;
34110 }
35111
112+
113+ private void GameStateChanged ( GameState newState ) {
114+ if ( newState == GameState . Stopped ) {
115+ playerRigidbody . velocity = Vector3 . zero ;
116+
117+ transform . SetPositionAndRotation (
118+ initialPosition ,
119+ initialRotation
120+ ) ;
121+ }
122+ }
123+
124+
36125 private void OnTriggerEnter ( Collider other ) {
37126 if ( other . gameObject . TryGetComponent < PointController > ( out var pointController ) ) {
38127 GameManager . Instance . CurrentScore += pointController . Value ;
0 commit comments