using System; using System.Collections; using System.Collections.Generic; using MoreMountains.Feedbacks; using MoreMountains.Tools; using UnityEngine; namespace MoreMountains.Feel { /// /// A class used to handle Feel's Snake demo's snake bodyparts /// public class SnakeBodyPart : MonoBehaviour { /// a position recorder this body part will look at to know where to go to public MMPositionRecorder TargetRecorder; /// a feedback to play when food gets eaten public MMFeedbacks EatFeedback; /// a feedback to play when this part appears public MMFeedbacks NewFeedback; public int Offset = 20; public int Index = 0; protected Snake _snake; protected BoxCollider2D _collider2D; /// /// On awake we store our collider and enable it after a delay /// protected virtual void Awake() { _collider2D = this.gameObject.MMGetComponentNoAlloc(); StartCoroutine(ActivateCollider()); } /// /// Activates this part's collider /// /// protected virtual IEnumerator ActivateCollider() { yield return MMCoroutine.WaitFor(1f); _collider2D.enabled = true; } /// /// On update, we move to the recorded position of our predecessor /// protected void Update() { this.transform.position = TargetRecorder.Positions[Offset]; } /// /// Called when the snake eats a new food /// /// public virtual void Eat(float intensity) { EatFeedback?.PlayFeedbacks(this.transform.position, intensity); } /// /// Called when instantiating a new body part /// public virtual void New() { NewFeedback?.Initialization(); NewFeedback?.PlayFeedbacks(); } /// /// If we connect with the snake's head, we lose a part /// /// protected void OnTriggerEnter2D(Collider2D other) { if (Index == 0) { return; } _snake = other.GetComponent(); if (_snake != null) { _snake.Lose(this); } } } }