Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using UnityEngine.Animations;
- public class Move : MonoBehaviour
- {
- private SnakeDirection _currentDirection = SnakeDirection.Up;
- public float Speed = 4f;
- public float CellSize = 5;
- public Transform SegmentPrefab;
- public GameObject FoodPrefab;
- public GameObject HeadPrefab;
- public List<Transform> StartBody = new List<Transform>();
- private List<Transform> _body = new List<Transform>();
- private Vector3 _startPos;
- void Start()
- {
- _startPos = transform.position;
- SpawnBody();
- StartCoroutine(MoveLogic());
- Food.SpawnFood(FoodPrefab);
- }
- void Update()
- {
- ReadDirection();
- }
- public IEnumerator MoveLogic()
- {
- while (true)
- {
- Vector3 previousPos = transform.position;
- switch (_currentDirection)
- {
- case SnakeDirection.Up:
- transform.position += new Vector3(0, 0, -CellSize);
- break;
- case SnakeDirection.Down:
- transform.position += new Vector3(0, 0, CellSize);
- break;
- case SnakeDirection.Left:
- transform.position += new Vector3(CellSize, 0, 0);
- break;
- case SnakeDirection.Right:
- transform.position += new Vector3(-CellSize, 0, 0);
- break;
- }
- foreach (var segment in _body) {
- Vector3 temp = segment.transform.position;
- segment.transform.position = previousPos;
- previousPos = temp;
- }
- yield return new WaitForSeconds(1 / Speed);
- }
- }
- public void ReadDirection()
- {
- if (Input.GetKeyDown(KeyCode.W) && _currentDirection != SnakeDirection.Down)
- _currentDirection = SnakeDirection.Up;
- else if (Input.GetKeyDown(KeyCode.S) && _currentDirection != SnakeDirection.Up)
- _currentDirection = SnakeDirection.Down;
- else if (Input.GetKeyDown(KeyCode.A) && _currentDirection != SnakeDirection.Right)
- _currentDirection = SnakeDirection.Left;
- else if (Input.GetKeyDown(KeyCode.D) && _currentDirection != SnakeDirection.Left)
- _currentDirection = SnakeDirection.Right;
- }
- public IEnumerator moveObject(GameObject obj, Vector3 destination)
- {
- float totalMovementTime = 5f; //the amount of time you want the movement to take
- float currentMovementTime = 2f;//The amount of time that has passed
- while (Vector3.Distance(obj.transform.localPosition, destination) > 0)
- {
- obj.transform.localPosition = Vector3.Lerp(obj.transform.position, destination, currentMovementTime / totalMovementTime);
- yield return null;
- }
- }
- private void OnCollisionEnter(Collision collision)
- {
- if (collision.gameObject.tag == "Body")
- print("Game Over!");
- else if (collision.gameObject.tag == "Food")
- {
- _body.Add(Instantiate(SegmentPrefab, _body[^1].position, Quaternion.identity));
- Destroy(collision.gameObject);
- Food.SpawnFood(FoodPrefab);
- }
- else if (collision.gameObject.tag == "Wall")
- {
- StopAllCoroutines();
- Death();
- }
- }
- private void SpawnBody()
- {
- Vector3 nextCellPos = new Vector3(_startPos.x + transform.localScale.x, _startPos.y, _startPos.z);
- StartBody.ForEach(body => {
- body.tag = "Body";
- _body.Add(Instantiate(body, nextCellPos, Quaternion.identity));
- });
- }
- private void Death()
- {
- _body.ForEach(x => Destroy(x.gameObject));
- _body.Clear();
- Destroy(gameObject);
- Instantiate(HeadPrefab, _startPos, Quaternion.identity);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment