private IntVector2 GetRandomMushroomPosition(IntVector2 gridSize) { IntVector2 pos = new IntVector2(Random.Range(1, gridSize.x - 1), Random.Range(1, gridSize.y - 1)); if (_levelManager.IsCellOccupiedByMushroom(pos) || _levelManager.IsCellOccupiedBySpider(pos)) { pos = GetRandomMushroomPosition(gridSize); } return(pos); }
private void InputPlayerMovement() { Vector3 newPosition = _targetPosition; if ((Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) && _player.transform.position == _targetPosition) { newPosition += Vector3.left; } else if ((Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)) && _player.transform.position == _targetPosition) { newPosition += Vector3.right; } else if ((Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W)) && _player.transform.position == _targetPosition) { newPosition += Vector3.up; } else if ((Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S)) && _player.transform.position == _targetPosition) { newPosition += Vector3.down; } if (_canMove && newPosition != _targetPosition) { // limit the position if (newPosition.x <= 0) { newPosition.x = 0; } if (newPosition.x >= _gridSize.x) { newPosition.x = _gridSize.x - 1; } if (newPosition.y <= 0) { newPosition.y = 0; } if (newPosition.y >= _maxHeightLimit) { newPosition.y = _maxHeightLimit; } // condition for check to collide with mushrooms if (_levelManager.IsCellOccupiedByMushroom(new IntVector2((int)newPosition.x, (int)newPosition.y))) { return; } _canMove = false; _targetPosition = newPosition; MainApp.Instance.StartCoroutine(Move(1f / _speed)); } }
public void Update() { if (_bullet == null || !_bullet.gameObject.activeSelf) { return; } if (_canMove && Vector3.Distance(_bullet.position, _targetPosition) < 0.001f) { _canMove = false; _targetPosition += Vector3.up; IntVector2 position = new IntVector2((int)_targetPosition.x, (int)_targetPosition.y); if (position.y >= _gridMaxHeight) { DestroyBullet(); return; } if (_levelManager.IsCellOccupiedByMushroom(position)) { DestroyBullet(); _mushroomManager.DamageMushroomAtPosition(position, _damage); return; } if (_levelManager.IsCellOccupiedBySpider(position)) { DestroyBullet(); _spiderManager.DestroySpider(position); return; } if (_levelManager.IsCellOccupiedByCentipede(position)) { DestroyBullet(); _centipedeManager.DestroyBodyPart(position); return; } MainApp.Instance.StartCoroutine(Move(1f / _speed)); } }
public void UpdatePosition(Action action) { if (_head == null) { Debug.LogWarning("head is null"); return; } if (_canMove && Vector3.Distance(_head.transform.position, _targetPosition) < 0.001f) { _onHeadMove = action; _canMove = false; Vector3 newPosition = _targetPosition + Vector3.right * (int)HorizontalDirection; IntVector2 position = new IntVector2((int)newPosition.x, (int)newPosition.y); if (position.x > _gridSize.x - 1) { if (position.y >= _gridSize.y - 1 || position.y <= 0) { VerticalDirection = VerticalDirection == VerticalDirection.Up ? VerticalDirection.Down : VerticalDirection.Up; } newPosition.x = _gridSize.x - 1; newPosition += Vector3.up * (int)VerticalDirection; HorizontalDirection = HorizontalDirection == HorizontalDirection.Left ? HorizontalDirection.Right : HorizontalDirection.Left; } if (position.x < 0) { if (position.y >= _gridSize.y - 1) { VerticalDirection = VerticalDirection.Down; } else if (position.y <= 0) { VerticalDirection = VerticalDirection.Up; } newPosition.x = 0; newPosition += Vector3.up * (int)VerticalDirection; HorizontalDirection = HorizontalDirection == HorizontalDirection.Left ? HorizontalDirection.Right : HorizontalDirection.Left; } if (_levelManager.IsCellOccupiedByMushroom(new IntVector2((int)newPosition.x, (int)newPosition.y)) || _levelManager.IsCellOccupiedByCentipede(new IntVector2((int)newPosition.x, (int)newPosition.y))) { newPosition.x = _targetPosition.x; Vector3 cellPos = newPosition + Vector3.up * (int)VerticalDirection; if (cellPos.y >= _gridSize.y - 1 || cellPos.y <= 0) { VerticalDirection = VerticalDirection == VerticalDirection.Up ? VerticalDirection.Down : VerticalDirection.Up; newPosition += Vector3.up * (int)VerticalDirection; } else { newPosition = cellPos; } HorizontalDirection = HorizontalDirection == HorizontalDirection.Left ? HorizontalDirection.Right : HorizontalDirection.Left; } if (_levelManager.IsCellOccupiedByPlayer(new IntVector2((int)newPosition.x, (int)newPosition.y))) { ReducePlayerLife(); } _targetPosition = newPosition; MainApp.Instance.StartCoroutine(MoveHead(1f / _speed)); } }