Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Snake : MonoBehaviour
- {
- [Header("Movement Keys")]
- [SerializeField] private KeyCode upKey = KeyCode.W;
- [SerializeField] private KeyCode downKey = KeyCode.S;
- [SerializeField] private KeyCode leftKey = KeyCode.A;
- [SerializeField] private KeyCode rightKey = KeyCode.D;
- [Header("Snake Head")]
- [SerializeField] private Segment.Direction headDirection;
- [Header("Snake Segments")]
- [SerializeField] private Transform snakeSegmentPrefab;
- [SerializeField] private int initialSize = 4;
- private Vector3Int spawnPoint;
- private Vector2 direction = Vector2.right;
- private readonly List<Transform> segments = new List<Transform>();
- private bool moved = true;
- private bool alive = true;
- #region GETTERS
- public Vector2 GetDirection() => direction;
- public List<Transform> GetSegments() => segments;
- public bool Moved() => moved;
- public bool isAlive() => alive;
- #endregion
- private void Awake()
- {
- spawnPoint = new Vector3Int((int)transform.position.x, (int)transform.position.y, 0);
- ResetState();
- }
- private void Update()
- {
- HandleInputs();
- }
- private void FixedUpdate()
- {
- if (!alive) return;
- HandleMovement();
- }
- private void HandleInputs()
- {
- if (!moved) return;
- if (Input.GetKeyDown(upKey) && direction != Vector2.down)
- {
- direction = Vector2.up;
- moved = false;
- }
- else if (Input.GetKeyDown(downKey) && direction != Vector2.up)
- {
- direction = Vector2.down;
- moved = false;
- }
- else if (Input.GetKeyDown(leftKey) && direction != Vector2.right)
- {
- direction = Vector2.left;
- moved = false;
- }
- else if (Input.GetKeyDown(rightKey) && direction != Vector2.left)
- {
- direction = Vector2.right;
- moved = false;
- }
- }
- private void HandleMovement()
- {
- MoveSegments();
- transform.position += (Vector3)direction;
- moved = true;
- }
- public void Grow()
- {
- Transform segment = Instantiate(snakeSegmentPrefab);
- if (segments.Count >= initialSize)
- {
- segment.position = segments[segments.Count - 1].position;
- }
- else
- {
- segment.position = segments[segments.Count - 1].position -
- (Vector3)direction;
- }
- segments.Add(segment);
- }
- private void OnTriggerEnter2D(Collider2D other)
- {
- if (other.CompareTag("Obstacle") || other.CompareTag("Snake"))
- {
- alive = false;
- }
- }
- public void ResetState()
- {
- for (int i = 1; i < segments.Count; i++)
- {
- Destroy(segments[i].gameObject);
- }
- direction = Segment.DirectionToVector[headDirection];
- segments.Clear();
- segments.Add(transform);
- transform.position = spawnPoint;
- for (int i = 1; i < initialSize; i++)
- {
- Grow();
- }
- alive = true;
- moved = true;
- }
- private void MoveSegments()
- {
- for (int i = segments.Count - 1; i > 0; i--)
- {
- segments[i].position = segments[i - 1].position;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement