/// <summary> /// Change state. Used force mode. /// </summary> /// <param name="newState"></param> /// <param name="actorID">0 is "general actor"; 1,2,3,4 - players</param> public void ChangeState(FieldSquareState newState, int actorID) { SquareState = newState; ActorID = actorID; switch (newState) { case FieldSquareState.Empty: spriteRenderer.color = Color.white; break; case FieldSquareState.Fruit: spriteRenderer.color = Color.red; break; case FieldSquareState.BodyOfSnake: case FieldSquareState.HeadOfSnake: spriteRenderer.color = ColorByActorID.Get(actorID); break; default: spriteRenderer.color = Color.black; break; } }
/// <summary> /// Rewrite added position. Resolve collisions. /// </summary> /// <param name="fieldPosition"></param> /// <param name="newState"></param> /// <param name="actorID"></param> public bool ChangeSquareOfField(Vector2Int fieldPosition, FieldSquareState newState, int actorID) { //Check borders if (fieldPosition.x < 0 || fieldPosition.x > field.GetUpperBound(0) || fieldPosition.y < 0 || fieldPosition.y > field.GetUpperBound(0)) { return(false); } if (newState == FieldSquareState.HeadOfSnake) { //Resolve collision //It's a very simple method. It isn't perfect, but enough good. //Possible non-critical inaccuracies (неточности) are extremely rare. switch (field[fieldPosition.x, fieldPosition.y].SquareState) { case FieldSquareState.Fruit: GameplayManager.FruitCollected(actorID, RandomFreePosition()); break; case FieldSquareState.BodyOfSnake: return(false); case FieldSquareState.HeadOfSnake: var otherActorID = field[fieldPosition.x, fieldPosition.y].ActorID; GameplayManager.Snakes[otherActorID - 1].CreateRespawnEvent(); return(false); } } field[fieldPosition.x, fieldPosition.y].ChangeState(newState, actorID); return(true); }