using System.Collections; using System.Collections.Generic; using UnityEngine; public static class MyVars { public static float cellSize = 1; public static int gridWidth = 15; public static int gridHeight = 12; public static float slowTime = .1f; public static float defaultTime = 1; public static Vector2Int p1StartPos = new Vector2Int((gridWidth-1)/2, 2); public static Vector2Int p2StartPos = new Vector2Int((gridWidth+1)/2, 2); public static float enemyMaxSpeed = 3; public static float enemySpeedGrowth = 0.2f; public static float minEnemyAmount = 2; public static float maxEnemyAmount = 5; public static float enemyAmountGrowth = 0.25f; public static int enemyKillScore = 1000; public static float spikeAmount = 3; } public static class MyMethods { private static System.Random rng = new System.Random(); public static float Remap(this float value, float from1, float to1, float from2, float to2) { return (value - from1) / (to1 - from1) * (to2 - from2) + from2; } public static void Shuffle(this IList list) { int n = list.Count; while (n > 1) { n--; int k = rng.Next(n + 1); T value = list[k]; list[k] = list[n]; list[n] = value; } } } namespace MyTypes { public enum MyDirection { _UP, _DOWN, _RIGHT, _LEFT, _MAX } public class MyPawn : MonoBehaviour { protected MyCell currentCell=null; public MyCell CurrentCell { get { return currentCell; } set { currentCell = value; } } } public class MyCell { public Vector2Int position; private MyPawn pawn; public MyPawn Pawn { get { return pawn; } set { //pawn = value; if (value != null) //if value does not equal null, the pawn is moving into this cell { if (pawn == null) //I am an empty cell { if(value.CurrentCell!=null) value.CurrentCell.pawn = null; pawn = value; pawn.CurrentCell = this; LeanTween.move(pawn.gameObject, new Vector3(position.x * MyVars.cellSize, 0, position.y * MyVars.cellSize), 0.1f).setEaseOutCirc(); pawn.gameObject.transform.localScale = new Vector3(.75f, .75f, 1.25f); //LeanTween.scaleZ(pawn.gameObject, 1.5f, 0).setEaseOutBack(); //LeanTween.scaleX(pawn.gameObject, .75f, 0).setEaseOutBack(); //LeanTween.scaleY(pawn.gameObject, .75f, 0).setEaseOutBack(); LeanTween.scaleZ(pawn.gameObject, 1, .35f).setEaseOutBack(); LeanTween.scaleX(pawn.gameObject, 1, .35f).setEaseOutBack(); LeanTween.scaleY(pawn.gameObject, 1, .35f).setEaseOutBack(); //pawn.gameObject.transform.position = new Vector3(position.x * MyVars.cellSize, 0, position.y * MyVars.cellSize); } else { Debug.LogError("Assigned pawn to an occupied cell: " + position); EnemyBehavior.enemyAmount--; } } } } public MyCell (Vector2Int position) { this.position = position; pawn = null; } public MyCell (int x, int y) { position = new Vector2Int(x, y); pawn = null; } } [System.Serializable] public class MyGrid { public int width=MyVars.gridWidth; public int height=MyVars.gridHeight; public float cellSize = MyVars.cellSize; public MyCell[,] grid; public List availableCells; public MyGrid(int width, int height) { this.width = width; this.height = height; grid = new MyCell[width, height]; availableCells = new List(); } public MyCell getCell(Vector2Int pos) { return grid[pos.x , pos.y]; } public MyCell getCell(Vector2Int pos, Vector2Int offset) { return grid[pos.x+offset.x, pos.y+offset.y]; } public void AddAvailableCell(MyCell cell) { availableCells.Add(cell); } public MyCell GetAvailableCell() { MyMethods.Shuffle(availableCells); MyCell aux = availableCells[availableCells.Count-1]; availableCells.Remove(aux); return aux; } public void RemoveAvailableCell(MyCell cell) { if(availableCells.Contains(cell)) availableCells.Remove(cell); } public void movePawn(MyPawn pawnToMove, MyCell targetCell) { AddAvailableCell(pawnToMove.CurrentCell); RemoveAvailableCell(targetCell); //pawnToMove.CurrentCell = targetCell; targetCell.Pawn = pawnToMove; //this also updates former cell's pawn reference and pawn's cell reference } } }