Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public struct Pos {
- public int X;
- public int Y;
- public Pos(int x, int y) {
- X = x;
- Y = y;
- }
- public static Pos operator -(Pos pos1, Pos pos2) {
- return new Pos(pos1.X - pos2.X, pos1.Y - pos2.Y);
- }
- public static Pos operator +(Pos pos1, Pos pos2) {
- return new Pos(pos1.X + pos2.X, pos1.Y + pos2.Y);
- }
- public static Pos operator *(Pos pos1, int koef) {
- return new Pos(pos1.X * koef, pos1.Y * koef);
- }
- public static Pos operator *(int koef, Pos pos1) {
- return new Pos(pos1.X * koef, pos1.Y * koef);
- }
- }
- public class Snake : MonoBehaviour {
- public Field field;
- public BlocksManager blocksManager;
- public List<Pos> Positions;
- public int Height = 20;
- public int Width = 10;
- public GameObject SnakePrefab;
- public List<SpriteRenderer> Segments;
- public int MaxSnakeSegments = 8;
- public int CurSnakeSegments;
- public int CurHeadDownIndex;
- void Start() {
- CreateSnakeSegments();
- }
- public void CreateSnakeSegments() {
- Segments = new List<SpriteRenderer>();
- for (int i = 0; i < MaxSnakeSegments; i++) {
- Segments.Add(Instantiate(SnakePrefab, transform).GetComponent<SpriteRenderer>());
- }
- }
- private int GetBodyIndex(int ind) {
- Pos last = Positions[ind - 1];
- Pos cur = Positions[ind];
- Pos next = Positions[ind + 1];
- Pos curLast = last - cur;
- Pos curNext = next - cur;
- if (curLast.Y == -1) {
- if (curNext.X == -1) {
- return (int)SnakeBlocks.PartOfBody.DownLeft;
- }
- else if (curNext.Y == 1) {
- return (int)SnakeBlocks.PartOfBody.DownUp;
- }
- else {//X==1
- return (int)SnakeBlocks.PartOfBody.DownRight;
- }
- }
- else if (curLast.X == -1) {
- if (curNext.Y == 1) {
- return (int)SnakeBlocks.PartOfBody.LeftUp;
- }
- else if (curNext.X == 1) {
- return (int)SnakeBlocks.PartOfBody.LeftRight;
- }
- else {//Y==-1
- return (int)SnakeBlocks.PartOfBody.DownLeft;
- }
- }
- else if (curLast.Y == 1) {
- if (curNext.X == 1) {
- return (int)SnakeBlocks.PartOfBody.UpRight;
- }
- else if (curNext.Y == -1) {
- return (int)SnakeBlocks.PartOfBody.DownUp;
- }
- else {//X==-1
- return (int)SnakeBlocks.PartOfBody.LeftUp;
- }
- }
- else {//X==1
- if (curNext.Y == -1) {
- return (int)SnakeBlocks.PartOfBody.DownRight;
- }
- else if (curNext.X == -1) {
- return (int)SnakeBlocks.PartOfBody.LeftRight;
- }
- else {//Y==1
- return (int)SnakeBlocks.PartOfBody.UpRight;
- }
- }
- }
- private int GetHeadIndex() {
- Pos cur = Positions[0];
- Pos next = Positions[1];
- Pos curNext = next - cur;
- if (curNext.Y == -1) {
- return (int)SnakeBlocks.PartOfBody.HeadUp;
- }
- else if (curNext.X == -1) {
- return (int)SnakeBlocks.PartOfBody.HeadRight;
- }
- else if (curNext.Y == 1) {
- return (int)SnakeBlocks.PartOfBody.HeadDown;
- }
- else {//X==1
- return (int)SnakeBlocks.PartOfBody.HeadLeft;
- }
- }
- private int GetTailIndex() {
- Pos cur = Positions[CurSnakeSegments - 1];
- Pos last = Positions[CurSnakeSegments - 2];
- Pos curLast = last - cur;
- if (curLast.Y == -1) {
- return (int)SnakeBlocks.PartOfBody.TailUp;
- }
- else if (curLast.X == -1) {
- return (int)SnakeBlocks.PartOfBody.TailRight;
- }
- else if (curLast.Y == 1) {
- return (int)SnakeBlocks.PartOfBody.TailDown;
- }
- else {//X==1
- return (int)SnakeBlocks.PartOfBody.TailLeft;
- }
- }
- private
- private bool CooldownFinished() {
- Time.
- }
- public IEnumerator SnakeMoving() {
- yield return null;
- int visible = 1;
- while (true) {
- Debug.Log(Time.deltaTime);
- yield return new WaitForSeconds(1f);
- Pos head = Positions[0];
- Positions[0] += Positions[0] - Positions[1];
- Segments[0].transform.position = new Vector2(Positions[0].X, Positions[0].Y);
- for (int i = CurSnakeSegments - 1; i > 1; i--) {
- Positions[i] = Positions[i - 1];
- Segments[i].transform.position = new Vector2(Positions[i].X, Positions[i].Y);
- }
- Positions[1] = head;
- Segments[1].transform.position = new Vector2(Positions[1].X, Positions[1].Y);
- Segments[0].sprite = blocksManager.Blocks[CurHeadDownIndex + GetHeadIndex()];
- if (visible < CurSnakeSegments) {
- for (int i = 1; i < visible; i++) {
- Segments[i].sprite = blocksManager.Blocks[CurHeadDownIndex + GetBodyIndex(i)];
- }
- }
- else {
- for (int i = 1; i < CurSnakeSegments - 1; i++) {
- Segments[i].sprite = blocksManager.Blocks[CurHeadDownIndex + GetBodyIndex(i)];
- }
- Segments[CurSnakeSegments - 1].sprite = blocksManager.Blocks[CurHeadDownIndex + GetTailIndex()];
- }
- visible++;
- }
- }
- public void NewSnake(int segments/*, int headDownIndex*/) {
- for (int i = segments; i < CurSnakeSegments; i++) {
- Segments[i].sprite = null;
- }
- CurSnakeSegments = segments;
- Positions = new List<Pos>(segments);
- Positions.Add(new Pos(Random.Range(0, Width), Height));
- for (int i = 1; i < segments; i++) {
- Positions.Add(new Pos(Positions[i - 1].X, Positions[i - 1].Y+1));
- }
- CurHeadDownIndex = 1;
- StartCoroutine(SnakeMoving());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement