public void OnStep( World world ) { currentDirection = nextDirection; Vector2 newHeadPos = headPos; Vector2 previousPosition = headPos; switch( currentDirection ) { case Direction.UP: newHeadPos.y--; break; case Direction.DOWN: newHeadPos.y++; break; case Direction.LEFT: newHeadPos.x--; break; case Direction.RIGHT: newHeadPos.x++; break; default: throw new ArgumentOutOfRangeException(); } Cell cellAtHead = checkNewHeadPosition( newHeadPos ); bool isGameOver = !handleNewCell(cellAtHead); if( isGameOver && world.GameManager.shouldSkip ) { world.GameManager.shouldSkip = false; return;} if( isGameOver ) { world.GameManager.GameOver(world, previousPosition); return; } headPos = newHeadPos; world.GameManager.shouldSkip = true; Tile oldHead = world[previousPosition]; Tile newHead = world[headPos]; snakeSegments.Enqueue( newHead ); oldHead.changeTypeTo(Cell.Body, false); newHead.changeTypeTo(Cell.Head, false); bool shouldGrow = snakeSegments.Count < supposedSize; if( shouldGrow ) return; Tile tail = snakeSegments.Dequeue(); tail.changeTypeTo(Cell.Empty); }
static Direction() { Left = new Direction(-1, 0, "Left"); Right = new Direction(1, 0, "Right"); Up = new Direction(0, -1, "Up"); Down = new Direction(0, 1, "Down"); }
public bool TurnTo( Direction dir ) { bool targetDirIsVertical = isDirectionVertical( dir ); bool currentDirIsVertical = isDirectionVertical( currentDirection ); if( !(currentDirIsVertical ^ targetDirIsVertical) ) return false; // If they're not different nextDirection = dir; return true; }
public Snake( Vector2 startPos, Direction startDir, int startSize, World world ) { snakeSegments = new Queue<Tile>(); headPos = startPos; snakeSegments.Enqueue(world[headPos]); world[headPos].changeTypeTo(Cell.Head); supposedSize = startSize; nextDirection = currentDirection = startDir; this.world = world; }
bool UpdateInput() { var vertical = Input.GetAxis("Vertical"); var horizontal = Input.GetAxis("Horizontal"); var velocity = _rigidbody.velocity; velocity += new Vector3(horizontal, 0f, vertical); velocity = Vector3.ClampMagnitude(new Vector3(velocity.x, 0f, velocity.z), _maxSpeed); velocity.y = _rigidbody.velocity.y; _rigidbody.velocity = velocity; if (_rigidbody.velocity.magnitude < 0.01f) { _animator.speed = 0f; } else { if (_animator.speed < 1f) { _animator.speed = 1f; } } transform.rotation = new Quaternion(); if (vertical > 0) { _direction = Direction.North; } else if (vertical < 0) { _direction = Direction.South; } else if (horizontal > 0) { _direction = Direction.East; } else if (horizontal < 0) { _direction = Direction.West; } else { return false; } return true; }
/// <summary> /// Sets the player vector based off of the flipped status and the current room direction /// </summary> /// <param name="forward">Direction of room creation</param> public static void updateDirection(Direction forward) { switch(forward) { case (Direction.UP): if(flipped) { Player.UpdateDirection(Constants.VECTOR_LEFT, Constants.VECTOR_UP); } else { Player.UpdateDirection(Constants.VECTOR_RIGHT, Constants.VECTOR_UP); } break; case (Direction.RIGHT): if(flipped) { Player.UpdateDirection(Constants.VECTOR_RIGHT, Constants.VECTOR_UP); } else { Player.UpdateDirection(Constants.VECTOR_RIGHT, Constants.VECTOR_DOWN); } break; case (Direction.DOWN): if (flipped) { Player.UpdateDirection(Constants.VECTOR_LEFT, Constants.VECTOR_DOWN); } else { Player.UpdateDirection(Constants.VECTOR_RIGHT, Constants.VECTOR_DOWN); } break; case (Direction.LEFT): if (flipped) { Player.UpdateDirection(Constants.VECTOR_LEFT, Constants.VECTOR_UP); } else { Player.UpdateDirection(Constants.VECTOR_LEFT, Constants.VECTOR_DOWN); } break; } }
private bool isDirectionVertical( Direction dir ) { return dir == Direction.UP || dir == Direction.DOWN; }
private Texture GetTexture(Direction direction) { if (direction == Direction.Up) return Up; else if (direction == Direction.Down) return Down; else if (direction == Direction.Left) return Left; else if (direction == Direction.Right) return Right; throw new NotSupportedException(); }
private void ChangeFacing() { if (Input.GetKey(KeyCode.UpArrow)) { anim.SetInteger("Facing", 0); Facing = Direction.Up; movementLockout = true; } if (Input.GetKey(KeyCode.RightArrow)) { anim.SetInteger("Facing", 1); Facing = Direction.Right; movementLockout = true; } if (Input.GetKey(KeyCode.DownArrow)) { anim.SetInteger("Facing", 2); Facing = Direction.Down; movementLockout = true; } if (Input.GetKey(KeyCode.LeftArrow)) { anim.SetInteger("Facing", 3); Facing = Direction.Left; movementLockout = true; } }