Guest User

Untitled

a guest
Jan 18th, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.02 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Animations;
  6.  
  7. public class Move : MonoBehaviour
  8. {
  9.     private SnakeDirection _currentDirection = SnakeDirection.Up;
  10.     public float Speed = 4f;
  11.     public float CellSize = 5;
  12.  
  13.     public Transform SegmentPrefab;
  14.     public GameObject FoodPrefab;
  15.     public GameObject HeadPrefab;
  16.  
  17.     public List<Transform> StartBody = new List<Transform>();
  18.    
  19.     private List<Transform> _body = new List<Transform>();
  20.     private Vector3 _startPos;
  21.    
  22.  
  23.     void Start()
  24.     {
  25.         _startPos = transform.position;
  26.         SpawnBody();
  27.         StartCoroutine(MoveLogic());
  28.         Food.SpawnFood(FoodPrefab);
  29.     }
  30.  
  31.     void Update()
  32.     {
  33.         ReadDirection();
  34.     }
  35.  
  36.     public IEnumerator MoveLogic()
  37.     {
  38.         while (true)
  39.         {
  40.            
  41.             Vector3 previousPos = transform.position;
  42.             switch (_currentDirection)
  43.             {
  44.                 case SnakeDirection.Up:
  45.                     transform.position += new Vector3(0, 0, -CellSize);
  46.                     break;
  47.                 case SnakeDirection.Down:
  48.                     transform.position += new Vector3(0, 0, CellSize);
  49.                     break;
  50.                 case SnakeDirection.Left:
  51.                     transform.position += new Vector3(CellSize, 0, 0);
  52.                     break;
  53.                 case SnakeDirection.Right:
  54.                     transform.position += new Vector3(-CellSize, 0, 0);
  55.                     break;
  56.             }
  57.             foreach (var segment in _body) {
  58.                 Vector3 temp = segment.transform.position;
  59.                 segment.transform.position = previousPos;
  60.                 previousPos = temp;
  61.             }
  62.             yield return new WaitForSeconds(1 / Speed);
  63.         }
  64.     }
  65.  
  66.     public void ReadDirection()
  67.     {
  68.         if (Input.GetKeyDown(KeyCode.W) && _currentDirection != SnakeDirection.Down)
  69.             _currentDirection = SnakeDirection.Up;
  70.         else if (Input.GetKeyDown(KeyCode.S) && _currentDirection != SnakeDirection.Up)
  71.             _currentDirection = SnakeDirection.Down;
  72.         else if (Input.GetKeyDown(KeyCode.A) && _currentDirection != SnakeDirection.Right)
  73.             _currentDirection = SnakeDirection.Left;
  74.         else if (Input.GetKeyDown(KeyCode.D) && _currentDirection != SnakeDirection.Left)
  75.             _currentDirection = SnakeDirection.Right;
  76.     }
  77.  
  78.     public IEnumerator moveObject(GameObject obj, Vector3 destination)
  79.     {
  80.         float totalMovementTime = 5f; //the amount of time you want the movement to take
  81.         float currentMovementTime = 2f;//The amount of time that has passed
  82.         while (Vector3.Distance(obj.transform.localPosition, destination) > 0)
  83.         {
  84.             obj.transform.localPosition = Vector3.Lerp(obj.transform.position, destination, currentMovementTime / totalMovementTime);
  85.             yield return null;
  86.         }
  87.     }
  88.  
  89.     private void OnCollisionEnter(Collision collision)
  90.     {
  91.         if (collision.gameObject.tag == "Body")
  92.             print("Game Over!");
  93.         else if (collision.gameObject.tag == "Food")
  94.         {
  95.             _body.Add(Instantiate(SegmentPrefab, _body[^1].position, Quaternion.identity));
  96.             Destroy(collision.gameObject);
  97.             Food.SpawnFood(FoodPrefab);
  98.         }
  99.         else if (collision.gameObject.tag == "Wall")
  100.         {
  101.             StopAllCoroutines();
  102.             Death();
  103.         }
  104.     }
  105.    
  106.     private void SpawnBody()
  107.     {
  108.         Vector3 nextCellPos = new Vector3(_startPos.x + transform.localScale.x, _startPos.y, _startPos.z);
  109.         StartBody.ForEach(body => {
  110.             body.tag = "Body";
  111.             _body.Add(Instantiate(body, nextCellPos, Quaternion.identity));
  112.         });
  113.     }
  114.  
  115.     private void Death()
  116.     {
  117.         _body.ForEach(x => Destroy(x.gameObject));
  118.         _body.Clear();
  119.         Destroy(gameObject);
  120.         Instantiate(HeadPrefab, _startPos, Quaternion.identity);
  121.     }
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment