@@ -6,18 +6,23 @@ public class EnemyController : MonoBehaviour
66{
77 public float speed = 1f ;
88 public int health = 10 ;
9-
9+ bool carrying = false ;
10+ Transform pathContainer ;
11+ BowlController candyBowl ;
1012
1113 // Start is called before the first frame update
1214 void Start ( )
1315 {
1416 // Create waypoint list from path object
15- Transform pathContainer = GameObject . FindGameObjectWithTag ( "Path" ) . transform ;
17+ pathContainer = GameObject . FindGameObjectWithTag ( "Path" ) . transform ;
1618 Vector3 [ ] waypoints = new Vector3 [ pathContainer . childCount ] ;
1719 for ( int i = 0 ; i < waypoints . Length ; i ++ ) {
1820 waypoints [ i ] = pathContainer . GetChild ( i ) . position ;
1921 }
2022
23+ // Get bowl object
24+ candyBowl = GameObject . FindGameObjectWithTag ( "Bowl" ) . GetComponent < BowlController > ( ) ;
25+
2126 // Start following the path
2227 StartCoroutine ( FollowPath ( waypoints ) ) ;
2328 }
@@ -36,11 +41,43 @@ IEnumerator FollowPath(Vector3[] waypoints) {
3641 // Move towards target waypoint
3742 transform . position = Vector3 . MoveTowards ( transform . position , targetWaypoint , speed * Time . deltaTime ) ;
3843
39- // Determine next target once target is reached
44+ // Set new target once target is reached
4045 if ( transform . position == targetWaypoint ) {
46+ // Refresh waypoint list
47+ waypoints = new Vector3 [ pathContainer . childCount ] ;
48+ for ( int i = 0 ; i < waypoints . Length ; i ++ ) {
49+ waypoints [ i ] = pathContainer . GetChild ( i ) . position ;
50+ }
51+ if ( targetWaypointIndex > waypoints . Length - 1 ) {
52+ targetWaypointIndex = waypoints . Length - 1 ;
53+ }
54+
55+ // Destroy if at the first waypoint
56+ if ( targetWaypointIndex == 0 ) {
57+ Destroy ( gameObject ) ;
58+ }
59+ // Remove waypoint if carrying bowl
60+ if ( carrying ) {
61+ if ( targetWaypointIndex != 0 ) {
62+ Destroy ( pathContainer . GetChild ( targetWaypointIndex ) . gameObject ) ;
63+ }
64+ else {
65+ // Loose condition
66+ print ( "Game over, all your candy is belong to us." ) ;
67+ candyBowl . Drop ( ) ;
68+ // idk restart menu???
69+ }
70+ }
71+ // Final target logic
4172 if ( targetWaypointIndex == waypoints . Length - 1 ) {
73+ // Turn around
4274 movementDirection = - 1 ;
75+
76+ // Attempt to carry bowl
77+ carrying = candyBowl . Carry ( gameObject ) ;
4378 }
79+
80+ // Set next target
4481 targetWaypointIndex += movementDirection ;
4582 targetWaypoint = waypoints [ targetWaypointIndex ] ;
4683 LookAt2D ( targetWaypoint ) ;
0 commit comments