private void Awake() { snake = this; snake.gridPos = new Vector2Int(1, 1); snake.gridDirection = Direction.Right; snake.moveTimerMax = .15f; snake.moveTimer = snake.moveTimerMax; snake.bodyPositions = new List <SnakeMovePos>(); snake.bodyObjects = new List <SnakeBodySegment>(); snake.bodySize = 3; snake.gameOverFlag = false; for (int i = 0; i < bodySize; i++) { SnakeMovePos lastMovePos = null; if (bodyPositions.Count > 0) { lastMovePos = bodyPositions[0]; } snake.bodyPositions.Add(new SnakeMovePos(lastMovePos, snake.gridPos, snake.gridDirection)); snake.addBody(); } Field.field.spawnFood(); }
private int getAngle(SnakeMovePos movePos) { switch (movePos.GetDirection()) { case Direction.Right: switch (movePos.getPrevDirection()) { default: return(90); case Direction.Down: return(45); case Direction.Up: return(-45); } case Direction.Left: switch (movePos.getPrevDirection()) { default: return(-90); case Direction.Down: return(-45); case Direction.Up: return(45); } case Direction.Up: switch (movePos.getPrevDirection()) { default: return(0); case Direction.Left: return(45); case Direction.Right: return(-45); } case Direction.Down: switch (movePos.getPrevDirection()) { default: return(180); case Direction.Left: return(180 - 45); case Direction.Right: return(180 + 45); } default: return(0); } }
// This method rotates the body parts correctly, but since we are using sprites // that occupy the whole "grid" and are square, we don't need this method. // If the sprites change to something non-rectangular, uncomment the switch - case portion public void SetSnakeMovPos(SnakeMovePos snakeMovePos) { this.snakeMovePos = snakeMovePos; transform.position = new Vector3(snakeMovePos.GetGridPos().x, snakeMovePos.GetGridPos().y); /* ----- UNCOMMENT IF THE SPRITE YOU USE IS NON-RECTANGULAR ----- * float angle; * switch (snakeMovePos.GetDir()) * { * default: * case Dir.Up: * switch (snakeMovePos.GetPrevDir()) * { * default: * angle = 0; break; * case Dir.Left: * angle = 45; break; * case Dir.Right: * angle = -45; break; * } * break; * case Dir.Down: * switch (snakeMovePos.GetPrevDir()) * { * default: * angle = 180; break; * case Dir.Down: * angle = 180 + 45; break; * case Dir.Up: * angle = 180 - 45; break; * } * break; * case Dir.Left: * switch (snakeMovePos.GetPrevDir()) * { * default: * angle = -90; break; * case Dir.Down: * angle = -45; break; * case Dir.Up: * angle = 45; break; * } * break; * case Dir.Right: * switch (snakeMovePos.GetPrevDir()) * { * default: * angle = 90; break; * case Dir.Down: * angle = 45; break; * case Dir.Up: * angle = -45; break; * } * break; * } * transform.eulerAngles = new Vector3(0, 0, angle); */ }
private void movementHandler() { snake.moveTimer += Time.deltaTime; if (snake.moveTimer >= snake.moveTimerMax) { snake.moveTimer -= snake.moveTimerMax; SnakeMovePos lastMovePos = null; if (bodyPositions.Count > 0) { lastMovePos = bodyPositions[0]; } snake.bodyPositions.Insert(0, new SnakeMovePos(lastMovePos, snake.gridPos, snake.gridDirection)); snake.gridPos += directionVector(); snake.gridPos = Field.field.checkBoundries(snake.gridPos); if (Field.field.checkFood(snake.gridPos)) { SoundManager.playEatSound(); snake.bodySize++; snake.addBody(); } if (snake.bodyPositions.Count >= snake.bodySize + 1) { snake.bodyPositions.RemoveAt(snake.bodyPositions.Count - 1); } foreach (SnakeMovePos snakeMovePos in snake.bodyPositions) { if (snake.gridPos == snakeMovePos.getGridPos()) { SoundManager.playDeathSound(); Game_script.gameOver(); snake.gameOverFlag = true; } } transform.position = new Vector3(gridPos.x, gridPos.y); transform.eulerAngles = new Vector3(0, 0, angleFromVector(directionVector()) - 90); snake.updateBody(); snake.lastMoveDir = snake.gridDirection; } }
public SnakeMovePos(SnakeMovePos previousSnakeMovePos, Vector2Int gridPos, Dir dir) { this.previousSnakeMovePos = previousSnakeMovePos; this.gridPos = gridPos; this.dir = dir; }
//------------------------------------------------------MOVE HANDLER------------------------------------------------------// // How much time must pass before the next movement action is // executed. Also tells the LevelGrid where the snake is. private void MoveHandler() { gridMoveTimer += Time.deltaTime; if (gridMoveTimer >= gridMoveTimerMax) { gridMoveTimer -= gridMoveTimerMax; SoundManager.PlaySound(SoundManager.Sound.SnakeMove); // Insert the first Index of snakePosHistory to the previous position SnakeMovePos previousSnakeMovePos = null; if (snakePosHistoryList.Count > 0) { previousSnakeMovePos = snakePosHistoryList[0]; } // Creating a new position from the snakeMovPos class and inserting // the position into the snakePosHistory list SnakeMovePos snakeMovePos = new SnakeMovePos(previousSnakeMovePos, gridPos, moveDir); snakePosHistoryList.Insert(0, snakeMovePos); // Movement Logic Vector2Int gridMoveDirVec; switch (moveDir) { default: case Dir.Right: gridMoveDirVec = new Vector2Int(1, 0); break; case Dir.Left: gridMoveDirVec = new Vector2Int(-1, 0); break; case Dir.Up: gridMoveDirVec = new Vector2Int(0, 1); break; case Dir.Down: gridMoveDirVec = new Vector2Int(0, -1); break; } gridPos += gridMoveDirVec; // Denying snake access to the outer worlds gridPos = levelGrid.ValidateGridPos(gridPos); // If the snake succeeds to eat, it grows and creates a new body part // The new body part is created from the SnakeBodyPart class bool snakeAteFood = levelGrid.TryEatFood(gridPos); if (snakeAteFood) { snakeBodySize++; CreateSnakeBody(); SoundManager.PlaySound(SoundManager.Sound.SnakeEat); Instantiate(eatEffect, transform.position, Quaternion.identity); } // Removing the last snake body part from the list (not to bloat the list) if (snakePosHistoryList.Count >= snakeBodySize + 1) { snakePosHistoryList.RemoveAt(snakePosHistoryList.Count - 1); } // Updating the body parts to angle them correctly // (The current sprite doesnt need a new angle so it's commented out) UpdateSnakeBodyParts(); // Checking if the head position is the same as a body part position foreach (SnakeBodyPart snakeBodyPart in snakeBodyPartList) { Vector2Int snakeBodyPartGridPos = snakeBodyPart.GetGridPosInSnakeMove(); if (gridPos == snakeBodyPartGridPos) { Debug.Log("GAME OVER!!!"); state = State.Dead; GameHandler.SnakeDied(); SoundManager.PlaySound(SoundManager.Sound.GameOver); } } // Moving the snake to new position and rotating the head transform.position = new Vector3(gridPos.x, gridPos.y); transform.eulerAngles = new Vector3(0, 0, GetAngle(gridMoveDirVec) - 90); } }
public SnakeMovePos(SnakeMovePos lastMovePos, Vector2Int gridPos, Direction direction) { this.lastMovePos = lastMovePos; this.gridPos = gridPos; this.direction = direction; }
public void setMovePos(SnakeMovePos movePos) { this.currPos = movePos; transform.position = new Vector3(this.currPos.getGridPos().x, this.currPos.getGridPos().y); transform.eulerAngles = new Vector3(0, 0, getAngle(this.currPos)); }