Advertisement
drakon-firestone

Untitled

Jun 5th, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class Snake : MonoBehaviour
  7. {
  8. [Header("Movement Keys")]
  9. [SerializeField] private KeyCode upKey = KeyCode.W;
  10. [SerializeField] private KeyCode downKey = KeyCode.S;
  11. [SerializeField] private KeyCode leftKey = KeyCode.A;
  12. [SerializeField] private KeyCode rightKey = KeyCode.D;
  13. [Header("Snake Head")]
  14. [SerializeField] private Segment.Direction headDirection;
  15. [Header("Snake Segments")]
  16. [SerializeField] private Transform snakeSegmentPrefab;
  17. [SerializeField] private int initialSize = 4;
  18.  
  19. private Vector3Int spawnPoint;
  20. private Vector2 direction = Vector2.right;
  21. private readonly List<Transform> segments = new List<Transform>();
  22. private bool moved = true;
  23. private bool alive = true;
  24.  
  25. #region GETTERS
  26. public Vector2 GetDirection() => direction;
  27. public List<Transform> GetSegments() => segments;
  28. public bool Moved() => moved;
  29. public bool isAlive() => alive;
  30. #endregion
  31.  
  32. private void Awake()
  33. {
  34. spawnPoint = new Vector3Int((int)transform.position.x, (int)transform.position.y, 0);
  35. ResetState();
  36. }
  37.  
  38. private void Update()
  39. {
  40. HandleInputs();
  41. }
  42.  
  43. private void FixedUpdate()
  44. {
  45. if (!alive) return;
  46.  
  47. HandleMovement();
  48. }
  49.  
  50. private void HandleInputs()
  51. {
  52. if (!moved) return;
  53.  
  54. if (Input.GetKeyDown(upKey) && direction != Vector2.down)
  55. {
  56. direction = Vector2.up;
  57. moved = false;
  58. }
  59. else if (Input.GetKeyDown(downKey) && direction != Vector2.up)
  60. {
  61. direction = Vector2.down;
  62. moved = false;
  63. }
  64. else if (Input.GetKeyDown(leftKey) && direction != Vector2.right)
  65. {
  66. direction = Vector2.left;
  67. moved = false;
  68. }
  69. else if (Input.GetKeyDown(rightKey) && direction != Vector2.left)
  70. {
  71. direction = Vector2.right;
  72. moved = false;
  73. }
  74. }
  75.  
  76. private void HandleMovement()
  77. {
  78. MoveSegments();
  79. transform.position += (Vector3)direction;
  80. moved = true;
  81. }
  82.  
  83.  
  84.  
  85. public void Grow()
  86. {
  87. Transform segment = Instantiate(snakeSegmentPrefab);
  88. if (segments.Count >= initialSize)
  89. {
  90. segment.position = segments[segments.Count - 1].position;
  91. }
  92. else
  93. {
  94. segment.position = segments[segments.Count - 1].position -
  95.  
  96. (Vector3)direction;
  97. }
  98. segments.Add(segment);
  99. }
  100.  
  101. private void OnTriggerEnter2D(Collider2D other)
  102. {
  103. if (other.CompareTag("Obstacle") || other.CompareTag("Snake"))
  104. {
  105. alive = false;
  106. }
  107. }
  108.  
  109. public void ResetState()
  110. {
  111. for (int i = 1; i < segments.Count; i++)
  112. {
  113. Destroy(segments[i].gameObject);
  114. }
  115.  
  116. direction = Segment.DirectionToVector[headDirection];
  117.  
  118. segments.Clear();
  119. segments.Add(transform);
  120. transform.position = spawnPoint;
  121. for (int i = 1; i < initialSize; i++)
  122. {
  123. Grow();
  124. }
  125.  
  126. alive = true;
  127. moved = true;
  128. }
  129.  
  130. private void MoveSegments()
  131. {
  132. for (int i = segments.Count - 1; i > 0; i--)
  133. {
  134. segments[i].position = segments[i - 1].position;
  135. }
  136. }
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement