Skip to content

Instantly share code, notes, and snippets.

@Refsa
Created October 6, 2020 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Refsa/432b4f51e5e4ba039fa8837796aa33f7 to your computer and use it in GitHub Desktop.
Save Refsa/432b4f51e5e4ba039fa8837796aa33f7 to your computer and use it in GitHub Desktop.
Snake in "one line"
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class SnakeOneLine : MonoBehaviour
{
void OnEnable()
{
var quadgo = GameObject.CreatePrimitive(PrimitiveType.Quad);
Mesh quad = quadgo.GetComponent<MeshFilter>().sharedMesh;
GameObject.Destroy(quadgo);
Material appleMat = new Material(Shader.Find("Unlit/Color"));
appleMat.SetColor("_Color", Color.red);
Material snakeMat = new Material(appleMat);
snakeMat.SetColor("_Color", Color.white);
StartCoroutine(Snake(Camera.main.ViewportToWorldPoint(Vector2.one * 0.5f), Vector2.zero, new Queue<Vector2>(), Vector2.one * 2f, quad, appleMat, snakeMat));
}
IEnumerator Snake(Vector2 _pos, Vector2 _vel, Queue<Vector2> _tail, Vector2 _apple, Mesh quad, Material appleMat, Material snakeMat)
{
yield return Draw(_pos, _tail, _apple, quad, appleMat, snakeMat);
(Vector2 pos, Vector2 vel, Queue<Vector2> tail, Vector2 apple, float temp1, System.Func<float> temp2) = ( (pos = _pos + _vel), (vel = Input.GetKey(KeyCode.LeftArrow) && _vel != Vector2.right * 0.1f ? Vector2.left * 0.1f : Input.GetKey(KeyCode.RightArrow) && _vel != Vector2.left * 0.1f ? Vector2.right * 0.1f : Input.GetKey(KeyCode.UpArrow) && _vel != Vector2.down * 0.1f ? Vector2.up * 0.1f : Input.GetKey(KeyCode.DownArrow) && _vel != Vector2.up * 0.1f ? Vector2.down * 0.1f : _vel ), (tail = (_tail.Count == 0 ? new Queue<Vector2>(ArrayList.Repeat(Vector2.one * 50f, 5).Cast<Vector2>()) : _tail)), (apple = ((pos - _apple).magnitude <= 0.01f) ? (Vector2)Camera.main.ViewportToWorldPoint(new Vector2(Random.Range(0.1f, 0.9f), Random.Range(0.1f, 0.9f))) : _apple), (temp2 = () => { if (apple != _apple) {tail.Enqueue(_pos); apple = new Vector2((float)System.Math.Round(apple.x, 1), (float)System.Math.Round(apple.y, 1));} if (pos != _pos) { tail.Enqueue(pos); tail.Dequeue(); } if (OutsideBounds()) { StartCoroutine(Snake(Camera.main.ViewportToWorldPoint(Vector2.one * 0.5f), Vector2.zero, new Queue<Vector2>(), Vector2.one * 2f, quad, appleMat, snakeMat)); return 0f; } StartCoroutine(Snake(pos, vel, tail, apple, quad, appleMat, snakeMat)); return 0f; bool OutsideBounds() { var sp = Camera.main.WorldToViewportPoint(pos); return (sp.x < 0 || sp.y < 0 || sp.x > 1 || sp.y > 1) || tail.Take(tail.Count - 1).Any(val => (val - pos).magnitude == 0f); } } ).Invoke(), temp2 );
}
IEnumerator Draw(Vector2 _pos, Queue<Vector2> _tail, Vector2 _apple, Mesh quad, Material appleMat, Material snakeMat)
{
float time = Time.time; int i = 1;
while (Time.time - time < Mathf.Lerp((1f / 20f), (1f / 90f), _tail.Count / 50f))
{
foreach (var val in _tail.Take(_tail.Count - 1)) Graphics.DrawMesh(quad, Matrix4x4.TRS(val, Quaternion.identity, Vector2.one * 0.1f * 0.75f), snakeMat, 0);
Graphics.DrawMesh(quad, Matrix4x4.TRS(_pos, Quaternion.identity, Vector2.one * 0.1f), snakeMat, 0);
Graphics.DrawMesh(quad, Matrix4x4.TRS(_apple, Quaternion.identity, Vector2.one * 0.1f), appleMat, 0);
const string numbers = "XXXX#XX#XX#XXXX##X##X##X##X##XXXX##XXXXX##XXXXXX##X#XX##XXXXX#XX#XXXX##X##XXXXX##XXX##XXXXXXXX##XXXX#XXXXXXX##X##X##X##XXXXX#XXXXX#XXXXXXXX#XXXX##X##X";
Vector2 pos = Camera.main.ViewportToWorldPoint(new Vector2(0.1f, 0.1f));
foreach (var n in Mathf.Max(0, _tail.Count - 5).ToString()) DrawNumber(numbers.Substring(int.Parse(n.ToString()) * 15, 15));
void DrawNumber(string number)
{
foreach (var c in number)
{
if (c == 'X') Graphics.DrawMesh(quad, Matrix4x4.TRS(pos, Quaternion.identity, Vector2.one * 0.1f), snakeMat, 0);
if (i++ % 3 == 0) { pos.y -= 0.1f; pos.x -= 0.2f; }
else pos.x += 0.1f;
}
pos.x += 0.4f; pos.y += 0.5f;
}
yield return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment