Advertisement
Graf_Rav

Untitled

Jul 18th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.06 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public struct Pos {
  6.     public int X;
  7.     public int Y;
  8.  
  9.     public Pos(int x, int y) {
  10.         X = x;
  11.         Y = y;
  12.     }
  13.  
  14.     public static Pos operator -(Pos pos1, Pos pos2) {
  15.         return new Pos(pos1.X - pos2.X, pos1.Y - pos2.Y);
  16.     }
  17.  
  18.     public static Pos operator +(Pos pos1, Pos pos2) {
  19.         return new Pos(pos1.X + pos2.X, pos1.Y + pos2.Y);
  20.     }
  21.  
  22.     public static Pos operator *(Pos pos1, int koef) {
  23.         return new Pos(pos1.X * koef, pos1.Y * koef);
  24.     }
  25.  
  26.     public static Pos operator *(int koef, Pos pos1) {
  27.         return new Pos(pos1.X * koef, pos1.Y * koef);
  28.     }
  29. }
  30.  
  31. public class Snake : MonoBehaviour {
  32.  
  33.     public Field field;
  34.     public BlocksManager blocksManager;
  35.  
  36.     public List<Pos> Positions;
  37.     public int Height = 20;
  38.     public int Width = 10;
  39.     public GameObject SnakePrefab;
  40.     public List<SpriteRenderer> Segments;
  41.     public int MaxSnakeSegments = 8;
  42.     public int CurSnakeSegments;
  43.     public int CurHeadDownIndex;
  44.  
  45.     void Start() {
  46.         CreateSnakeSegments();
  47.     }
  48.  
  49.     public void CreateSnakeSegments() {
  50.         Segments = new List<SpriteRenderer>();
  51.         for (int i = 0; i < MaxSnakeSegments; i++) {
  52.             Segments.Add(Instantiate(SnakePrefab, transform).GetComponent<SpriteRenderer>());
  53.         }
  54.     }
  55.  
  56.     private int GetBodyIndex(int ind) {
  57.         Pos last = Positions[ind - 1];
  58.         Pos cur = Positions[ind];
  59.         Pos next = Positions[ind + 1];
  60.  
  61.         Pos curLast = last - cur;
  62.         Pos curNext = next - cur;
  63.  
  64.         if (curLast.Y == -1) {
  65.             if (curNext.X == -1) {
  66.                 return (int)SnakeBlocks.PartOfBody.DownLeft;
  67.             }
  68.             else if (curNext.Y == 1) {
  69.                 return (int)SnakeBlocks.PartOfBody.DownUp;
  70.             }
  71.             else {//X==1
  72.                 return (int)SnakeBlocks.PartOfBody.DownRight;
  73.             }
  74.         }
  75.         else if (curLast.X == -1) {
  76.             if (curNext.Y == 1) {
  77.                 return (int)SnakeBlocks.PartOfBody.LeftUp;
  78.             }
  79.             else if (curNext.X == 1) {
  80.                 return (int)SnakeBlocks.PartOfBody.LeftRight;
  81.             }
  82.             else {//Y==-1
  83.                 return (int)SnakeBlocks.PartOfBody.DownLeft;
  84.             }
  85.         }
  86.         else if (curLast.Y == 1) {
  87.             if (curNext.X == 1) {
  88.                 return (int)SnakeBlocks.PartOfBody.UpRight;
  89.             }
  90.             else if (curNext.Y == -1) {
  91.                 return (int)SnakeBlocks.PartOfBody.DownUp;
  92.             }
  93.             else {//X==-1
  94.                 return (int)SnakeBlocks.PartOfBody.LeftUp;
  95.             }
  96.         }
  97.         else {//X==1
  98.             if (curNext.Y == -1) {
  99.                 return (int)SnakeBlocks.PartOfBody.DownRight;
  100.             }
  101.             else if (curNext.X == -1) {
  102.                 return (int)SnakeBlocks.PartOfBody.LeftRight;
  103.             }
  104.             else {//Y==1
  105.                 return (int)SnakeBlocks.PartOfBody.UpRight;
  106.             }
  107.         }
  108.     }
  109.  
  110.     private int GetHeadIndex() {
  111.         Pos cur = Positions[0];
  112.         Pos next = Positions[1];
  113.  
  114.         Pos curNext = next - cur;
  115.  
  116.         if (curNext.Y == -1) {
  117.             return (int)SnakeBlocks.PartOfBody.HeadUp;
  118.         }
  119.         else if (curNext.X == -1) {
  120.             return (int)SnakeBlocks.PartOfBody.HeadRight;
  121.         }
  122.         else if (curNext.Y == 1) {
  123.             return (int)SnakeBlocks.PartOfBody.HeadDown;
  124.         }
  125.         else {//X==1
  126.             return (int)SnakeBlocks.PartOfBody.HeadLeft;
  127.         }
  128.     }
  129.  
  130.     private int GetTailIndex() {
  131.         Pos cur = Positions[CurSnakeSegments - 1];
  132.         Pos last = Positions[CurSnakeSegments - 2];
  133.  
  134.         Pos curLast = last - cur;
  135.  
  136.         if (curLast.Y == -1) {
  137.             return (int)SnakeBlocks.PartOfBody.TailUp;
  138.         }
  139.         else if (curLast.X == -1) {
  140.             return (int)SnakeBlocks.PartOfBody.TailRight;
  141.         }
  142.         else if (curLast.Y == 1) {
  143.             return (int)SnakeBlocks.PartOfBody.TailDown;
  144.         }
  145.         else {//X==1
  146.             return (int)SnakeBlocks.PartOfBody.TailLeft;
  147.         }
  148.     }
  149.  
  150.     private
  151.     private bool CooldownFinished() {
  152.         Time.
  153.     }
  154.  
  155.     public IEnumerator SnakeMoving() {
  156.         yield return null;
  157.         int visible = 1;
  158.         while (true) {
  159.             Debug.Log(Time.deltaTime);
  160.  
  161.             yield return new WaitForSeconds(1f);
  162.  
  163.             Pos head = Positions[0];
  164.             Positions[0] += Positions[0] - Positions[1];
  165.             Segments[0].transform.position = new Vector2(Positions[0].X, Positions[0].Y);
  166.             for (int i = CurSnakeSegments - 1; i > 1; i--) {
  167.                 Positions[i] = Positions[i - 1];
  168.                 Segments[i].transform.position = new Vector2(Positions[i].X, Positions[i].Y);
  169.             }
  170.             Positions[1] = head;
  171.             Segments[1].transform.position = new Vector2(Positions[1].X, Positions[1].Y);
  172.  
  173.             Segments[0].sprite = blocksManager.Blocks[CurHeadDownIndex + GetHeadIndex()];
  174.             if (visible < CurSnakeSegments) {
  175.                 for (int i = 1; i < visible; i++) {
  176.                     Segments[i].sprite = blocksManager.Blocks[CurHeadDownIndex + GetBodyIndex(i)];
  177.                 }
  178.             }
  179.             else {
  180.                 for (int i = 1; i < CurSnakeSegments - 1; i++) {
  181.                     Segments[i].sprite = blocksManager.Blocks[CurHeadDownIndex + GetBodyIndex(i)];
  182.                 }
  183.                 Segments[CurSnakeSegments - 1].sprite = blocksManager.Blocks[CurHeadDownIndex + GetTailIndex()];
  184.             }
  185.             visible++;
  186.         }
  187.     }
  188.  
  189.     public void NewSnake(int segments/*, int headDownIndex*/) {
  190.         for (int i = segments; i < CurSnakeSegments; i++) {
  191.             Segments[i].sprite = null;
  192.         }
  193.         CurSnakeSegments = segments;
  194.         Positions = new List<Pos>(segments);
  195.         Positions.Add(new Pos(Random.Range(0, Width), Height));
  196.         for (int i = 1; i < segments; i++) {
  197.             Positions.Add(new Pos(Positions[i - 1].X, Positions[i - 1].Y+1));
  198.         }
  199.         CurHeadDownIndex = 1;
  200.         StartCoroutine(SnakeMoving());
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement