public void Explode() { _spriteRenderer.enabled = false; _gridController.RemoveObject(VectorTransformer.Vector3ToVector2Int(transform.position)); Vector3 position = transform.position; Vector2Int positionVector2Int = VectorTransformer.Vector3ToVector2Int(position); _gridController.DestroyObjectAt(positionVector2Int + Vector2Int.down); _gridController.DestroyObjectAt(positionVector2Int + Vector2Int.up); _gridController.DestroyObjectAt(positionVector2Int + Vector2Int.left); _gridController.DestroyObjectAt(positionVector2Int + Vector2Int.right); _gridController.DestroyObjectAt(positionVector2Int + Vector2Int.down + Vector2Int.left); _gridController.DestroyObjectAt(positionVector2Int + Vector2Int.up + Vector2Int.left); _gridController.DestroyObjectAt(positionVector2Int + Vector2Int.down + Vector2Int.right); _gridController.DestroyObjectAt(positionVector2Int + Vector2Int.up + Vector2Int.right); CreateStar(position); CreateStar(position + Vector3.down); CreateStar(position + Vector3.up); CreateStar(position + Vector3.left); CreateStar(position + Vector3.right); CreateStar(position + Vector3.down + Vector3.left); CreateStar(position + Vector3.up + Vector3.left); CreateStar(position + Vector3.down + Vector3.right); CreateStar(position + Vector3.up + Vector3.right); }
public void Explode() { _audioSource.Play(); _spriteRenderer.enabled = false; Vector3 position = transform.position; Vector2Int positionVector2Int = VectorTransformer.Vector3ToVector2Int(position); _gridController.DestroyObjectAt(positionVector2Int + Vector2Int.down); _gridController.DestroyObjectAt(positionVector2Int + Vector2Int.up); _gridController.DestroyObjectAt(positionVector2Int + Vector2Int.left); _gridController.DestroyObjectAt(positionVector2Int + Vector2Int.right); _gridController.DestroyObjectAt(positionVector2Int + Vector2Int.down + Vector2Int.left); _gridController.DestroyObjectAt(positionVector2Int + Vector2Int.up + Vector2Int.left); _gridController.DestroyObjectAt(positionVector2Int + Vector2Int.down + Vector2Int.right); _gridController.DestroyObjectAt(positionVector2Int + Vector2Int.up + Vector2Int.right); CreateStar(position); CreateStar(position + Vector3.down); CreateStar(position + Vector3.up); CreateStar(position + Vector3.left); CreateStar(position + Vector3.right); CreateStar(position + Vector3.down + Vector3.left); CreateStar(position + Vector3.up + Vector3.left); CreateStar(position + Vector3.down + Vector3.right); CreateStar(position + Vector3.up + Vector3.right); _characterLose = Instantiate(prefabCharacterLose, position, Quaternion.identity).transform; }
private void Push(Vector2Int position, Vector2Int positionDirection, Direction direction) { Vector2Int vectorDirection = (direction == Direction.Right) ? Vector2Int.right : Vector2Int.left; _gridController.GetObject(positionDirection).transform.position += VectorTransformer.Vector2IntToVector3Int(vectorDirection); _gridController.MoveObject(positionDirection, positionDirection + vectorDirection); }
private bool VerifyCharacterBeforeSlide(Vector2Int position) { if (_gridController.IsCharacterOnCell(position) || _gridController.IsCharacterOnCell(VectorTransformer.Vector2IntDown(position))) { return(false); } return(true); }
// Update is called once per frame void Update() { if (_menuController.DiamondsLeft() <= 0) { _diamondsCollected = true; } if (_diamondsCollected) { Vector3 characterPosition = _character.transform.position; if (Input.GetAxis("Horizontal") > 0.5f) { Vector2Int movePosition = new Vector2Int(Mathf.RoundToInt(characterPosition.x + 0.5f), Mathf.RoundToInt(characterPosition.y)); Vector2Int objectPosition = VectorTransformer.Vector3ToVector2Int(transform.position); if (objectPosition.Equals(movePosition)) { GoToExit(); } } if (Input.GetAxis("Horizontal") < -0.5f) { Vector2Int movePosition = new Vector2Int(Mathf.RoundToInt(characterPosition.x - 0.5f), Mathf.RoundToInt(characterPosition.y)); Vector2Int objectPosition = VectorTransformer.Vector3ToVector2Int(transform.position); if (objectPosition.Equals(movePosition)) { GoToExit(); } } if (Input.GetAxis("Vertical") > 0.5f) { Vector2Int movePosition = new Vector2Int(Mathf.RoundToInt(characterPosition.x), Mathf.RoundToInt(characterPosition.y + 0.5f)); Vector2Int objectPosition = VectorTransformer.Vector3ToVector2Int(transform.position); if (objectPosition.Equals(movePosition)) { GoToExit(); } } if (Input.GetAxis("Vertical") < -0.5f) { Vector2Int movePosition = new Vector2Int(Mathf.RoundToInt(characterPosition.x), Mathf.RoundToInt(characterPosition.y - 0.5f)); Vector2Int objectPosition = VectorTransformer.Vector3ToVector2Int(transform.position); if (objectPosition.Equals(movePosition)) { GoToExit(); } } } }
void Update() { Vector2Int objectPosition = VectorTransformer.Vector3ToVector2Int(transform.position); Vector2Int downPosition = VectorTransformer.Vector2IntDown(objectPosition); if (!_fallControl) { _fallControl = true; StartCoroutine(WaitFall(objectPosition, downPosition)); } if (!_gridController.CellIsEmptyWithoutCharacter(downPosition)) { _startFallFrom = objectPosition; } //Se o objeto abaixo deste é Slider e na posição do lado e lado baixo estão vazias a pedra escorrega if (_gridController.HasGridObjectAt(downPosition)) { Transform downObject = _gridController.GetObject(downPosition); if (downObject.CompareTag("SlideObject") || downObject.CompareTag("Rock"))//verifica se o de baixo é slider { if (!_gridController.HasNextFreeSpace(objectPosition, Direction.Down)) { Vector2Int leftPosition = VectorTransformer.Vector2IntLeft(objectPosition); if (_gridController.CellIsEmpty(leftPosition)) //Verifica lado esquerdo { if (_gridController.CellIsEmpty(VectorTransformer.Vector2IntDown(leftPosition))) //Verifica esquerdo baixo { //Desliza StartCoroutine(WaitSlide(objectPosition, leftPosition)); return; } } Vector2Int rightPosition = VectorTransformer.Vector2IntRight(objectPosition); //Verifica lado direito if (_gridController.CellIsEmpty(rightPosition)) { if (_gridController.CellIsEmpty(VectorTransformer.Vector2IntDown(rightPosition))) //Verifica direita baixo { //Desliza StartCoroutine(WaitSlide(objectPosition, rightPosition)); return; } } } } } if (_gridController.characterPosition.Equals(downPosition)) { if (!_startFallFrom.Equals(VectorTransformer.NullPoint) & _startFallFrom.y > _gridController.characterPosition.y + 1) { _gridController.character.GetComponent <Lose>().StageLose(); } } }
//public AudioClip fallSound; //public AudioClip collideSound; // Start is called before the first frame update void Start() { _gridController = transform.parent.GetComponent <GridController>(); _startFallFrom = VectorTransformer.Vector3ToVector2Int(transform.position); _rigidbody2D = GetComponent <Rigidbody2D>(); //_rigidbody2D.velocity = new Vector2(0,-2); _normalConstraints = _rigidbody2D.constraints; _fallControl = false; _audioSourceCollide = transform.GetComponent <AudioSource>(); _audioSourceFall = transform.GetChild(0).GetComponent <AudioSource>(); }
// Update is called once per frame void Update() { Vector2Int position = VectorTransformer.Vector3ToVector2Int(transform.position); if (Input.GetAxis("Horizontal") >= 1f) { Vector2Int positionRight = position + Vector2Int.right; if (CanBePushed(positionRight, Direction.Right)) //if(_gridController.HasGridObjectAt(positionRight) & _gridController.CellIsEmpty(positionRight + Vector2Int.right)) { if (VectorTransformer.IsAlmostEqualVector3Int(transform.position)) { if (_pushFrom.Item2 != Direction.Right || !_pushFrom.Item1.Equals(position)) { //Save the values of the push _pushFrom = new Tuple <Vector2Int, Direction>(position, Direction.Right); if (!_coroutineIsRunning) { _coroutineIsRunning = true; //chama a corrotina StartCoroutine(WaitPush(position, positionRight, Direction.Right)); } } } } } if (Input.GetAxis("Horizontal") <= -1f) { Vector2Int positionLeft = position + Vector2Int.left; if (CanBePushed(positionLeft, Direction.Left)) //if(_gridController.HasGridObjectAt(positionLeft) & _gridController.CellIsEmpty(positionLeft + Vector2Int.left) ) { if (VectorTransformer.IsAlmostEqualVector3Int(transform.position)) { //Verify if the last push was from the same position and direction to prevent mutiple pushes from the same place if (_pushFrom.Item2 != Direction.Left || !_pushFrom.Item1.Equals(position)) { //Save the values of the push _pushFrom = new Tuple <Vector2Int, Direction>(position, Direction.Left); if (!_coroutineIsRunning) { _coroutineIsRunning = true; //chama a corrotina StartCoroutine(WaitPush(position, positionLeft, Direction.Left)); } } } } } }
public bool HasTileAt(Vector2Int position) { foreach (Tilemap tilemap in _tilemapList) { if (tilemap.HasTile(VectorTransformer.Vector2IntToVector3Int(position))) { return(true); } } return(false); }
private void Fall(Vector2Int fromPosition, Vector2Int toPosition) { if (_gridController.CellIsEmpty(toPosition)) { if (!_startFallFrom.Equals(VectorTransformer.NullPoint)) { _startFallFrom = VectorTransformer.Vector3ToVector2Int(transform.position); } _gridController.MoveObject(fromPosition, toPosition); transform.position = VectorTransformer.Vector2IntToVector3Int(toPosition); } }
IEnumerator DestroyStar(GameObject star, GameObject background, Vector3 position) { yield return(new WaitForSeconds(0.4f)); Destroy(star); Destroy(background); if (_gridController.CellIsEmpty(VectorTransformer.Vector3ToVector2Int(position))) { GameObject diamond = Instantiate(prefabDiamond, position, Quaternion.identity); _gridController.AddObjectToGrid(diamond.transform); } }
public void DestroyObjectAt(Vector2Int position) { if (HasGridObjectAt(position)) { RemoveObject(position); } Vector3Int positionVector3Int = VectorTransformer.Vector2IntToVector3Int(position); foreach (Tilemap tilemap in _tilemapList) { if (tilemap.HasTile(positionVector3Int)) { tilemap.SetTile(positionVector3Int, null); } } }
private void GetDiamond() { if (!_diamondGot) { _diamondGot = true; if (_menuController.PickDiamond()) { StartCoroutine(ExitLabel()); } Vector2Int objectPosition = VectorTransformer.Vector3ToVector2Int(transform.position); _gridController.RemoveObject(objectPosition); _spriteRenderer.enabled = false; _collider2D.enabled = false; _rock.enabled = false; } }
public bool CellIsEmptyWithoutCharacter(Vector2Int position) { //Verify on Objects if (_gridObjects.ContainsKey(position)) { return(false); } //Verify on Tilemaps foreach (Tilemap tilemap in _tilemapList) { if (tilemap.HasTile(VectorTransformer.Vector2IntToVector3Int(position))) { return(false); } } return(true); }
void Update() { Vector2Int objectPosition = VectorTransformer.Vector3ToVector2Int(transform.position); Vector2Int upPosition = VectorTransformer.Vector2IntUp(objectPosition); if (Input.GetKey(KeyCode.L)) { Explode(); } if (_gridController.HasGridObjectAt(Vector2Int.up)) { Transform upObject = _gridController.GetObject(upPosition); if (upObject.CompareTag("Rock")) { Debug.Log("Explodiu"); Explode(); } } }
// Update is called once per frame void Update() { Vector3 characterPosition = _character.transform.position; if (Input.GetAxis("Horizontal") > 0.5f) { Vector2Int movePosition = new Vector2Int(Mathf.RoundToInt(characterPosition.x + 0.5f), Mathf.RoundToInt(characterPosition.y)); Vector2Int objectPosition = VectorTransformer.Vector3ToVector2Int(transform.position); if (objectPosition.Equals(movePosition)) { GetDiamond(); } } if (Input.GetAxis("Horizontal") < -0.5f) { Vector2Int movePosition = new Vector2Int(Mathf.RoundToInt(characterPosition.x - 0.5f), Mathf.RoundToInt(characterPosition.y)); Vector2Int objectPosition = VectorTransformer.Vector3ToVector2Int(transform.position); if (objectPosition.Equals(movePosition)) { GetDiamond(); } } if (Input.GetAxis("Vertical") > 0.5f) { Vector2Int movePosition = new Vector2Int(Mathf.RoundToInt(characterPosition.x), Mathf.RoundToInt(characterPosition.y + 0.5f)); Vector2Int objectPosition = VectorTransformer.Vector3ToVector2Int(transform.position); if (objectPosition.Equals(movePosition)) { GetDiamond(); } } if (Input.GetAxis("Vertical") < -0.5f) { Vector2Int movePosition = new Vector2Int(Mathf.RoundToInt(characterPosition.x), Mathf.RoundToInt(characterPosition.y - 0.5f)); Vector2Int objectPosition = VectorTransformer.Vector3ToVector2Int(transform.position); if (objectPosition.Equals(movePosition)) { GetDiamond(); } } }
IEnumerator Walk() { Vector2Int position = VectorTransformer.Vector3ToVector2Int(transform.position); //Direction newLeft = TurnLeft(_direction); newLeft = TurnLeft(_direction); Vector2Int leftPosition = VectorTransformer.DirectionToVector2Int(position, newLeft); Vector2Int frontPosition = VectorTransformer.DirectionToVector2Int(position, _direction); Debug.Log("Iniciou: " + _direction + '-' + newLeft); //Verificar se pode ir para esquerda. Se sim vira e anda if (_gridController.CellIsEmpty(leftPosition)) { _direction = newLeft; _gridController.MoveObject(position, leftPosition); transform.position = VectorTransformer.Vector2IntToVector3Int(leftPosition); Debug.Log("Go Left: " + _direction + "-" + newLeft); } //Se não anda para frente else if (_gridController.CellIsEmpty(frontPosition)) { _gridController.MoveObject(position, frontPosition); transform.position = VectorTransformer.Vector2IntToVector3Int(frontPosition); Debug.Log("Go Ahead: " + _direction + "-" + newLeft); } //Senão puder andar para esquerda nem para frente apenas vira a direita else { _direction = TurnRight(_direction); Debug.Log("Turn Right: " + _direction + "-" + newLeft); } yield return(new WaitForSeconds(0.5f)); _coroutineControl = true; }
public Animator(Vector _target, Vector _animator, VectorTransformer _transformer) { target = _target; animator = _animator; transformer = _transformer; }
private void Slide(Vector2Int fromPosition, Vector2Int toPosition) { transform.position = VectorTransformer.Vector2IntToVector3Int(toPosition); _gridController.MoveObject(fromPosition, toPosition); }
// Update is called once per frame void Update() { Vector2Int objectPosition = VectorTransformer.Vector3ToVector2Int(transform.position); Debug.Log(objectPosition + "-" + Time.frameCount); Vector2Int downPosition = VectorTransformer.Vector2IntDown(objectPosition); if (_gridController.CellIsEmpty(downPosition)) { if (!_isFalling & _downReallyFree) { if (!AlreadyFallTo(downPosition)) { _isFalling = true; Debug.Log("Fall" + "-" + Time.frameCount); //Debug.Log(transform.name + ": " + "_isFalling: " + _isFalling); StartCoroutine(WaitFall(objectPosition, downPosition)); //Debug.Log(_gridController.HasGridObjectAt(downPosition)); //Debug.Log("Fall" + "-" + Time.frameCount); _startFallFrom = objectPosition; } } } //Se o objeto abaixo deste é Slider e na posição do lado e lado baixo estão vazias a pedra escorrega else if (_gridController.HasGridObjectAt(downPosition)) { //verifica se o de baixo é slider Transform downObject = _gridController.GetObject(downPosition); if (downObject.tag.Equals("SlideObject")) { if (_gridController.CellIsEmpty(VectorTransformer.Vector2IntDown(downPosition))) { _downReallyFree = false; StartCoroutine(WaitHalfSecond()); return; } //Verifica lado esquerdo Vector2Int leftPosition = VectorTransformer.Vector2IntLeft(objectPosition); if (_gridController.CellIsEmpty(leftPosition)) { //Verifica esquerdo baixo if (_gridController.CellIsEmpty(VectorTransformer.Vector2IntDown(leftPosition))) { //Desliza StartCoroutine(WaitSlide(objectPosition, leftPosition)); return; } } //Verifica lado direito Vector2Int rightPosition = VectorTransformer.Vector2IntRight(objectPosition); if (_gridController.CellIsEmpty(rightPosition)) { //Verifica direita baixo if (_gridController.CellIsEmpty(VectorTransformer.Vector2IntDown(rightPosition))) { //Desliza StartCoroutine(WaitSlide(objectPosition, rightPosition)); return; } } } } }
IEnumerator Walk() { Vector2Int position = VectorTransformer.Vector3ToVector2Int(transform.position); IsCharacterNeighbor(position); Direction nextDirection = Direction.None; int nextNeighborPoints = -200; int actualNeighborPoints; List <Vector2Int> isGluedAt = VerifyWhereIsGlued(position); Vector2Int actualNeighborPosition = position + Vector2Int.left; if (_gridController.CellIsEmptyWithoutCharacter(actualNeighborPosition)) { actualNeighborPoints = _gridController.VerifyNeighbor(actualNeighborPosition); if (_directionFrom == Direction.Right) { actualNeighborPoints = actualNeighborPoints - 100; } int isGluedAtPoints = IsInSameWall(actualNeighborPosition, isGluedAt); int wasGluedAtPoints = IsInSameWall(actualNeighborPosition, _wasGluedAt); actualNeighborPoints = actualNeighborPoints + isGluedAtPoints + wasGluedAtPoints; if (actualNeighborPoints > nextNeighborPoints) { nextNeighborPoints = actualNeighborPoints; nextDirection = Direction.Left; } } actualNeighborPosition = position + Vector2Int.up; if (_gridController.CellIsEmptyWithoutCharacter(actualNeighborPosition)) { actualNeighborPoints = _gridController.VerifyNeighbor(actualNeighborPosition); if (_directionFrom == Direction.Down) { actualNeighborPoints = actualNeighborPoints - 100; } int isGluedAtPoints = IsInSameWall(actualNeighborPosition, isGluedAt); int wasGluedAtPoints = IsInSameWall(actualNeighborPosition, _wasGluedAt); actualNeighborPoints = actualNeighborPoints + isGluedAtPoints + wasGluedAtPoints; if (actualNeighborPoints > nextNeighborPoints) { nextNeighborPoints = actualNeighborPoints; nextDirection = Direction.Up; } } actualNeighborPosition = position + Vector2Int.right; if (_gridController.CellIsEmptyWithoutCharacter(actualNeighborPosition)) { actualNeighborPoints = _gridController.VerifyNeighbor(actualNeighborPosition); if (_directionFrom == Direction.Left) { actualNeighborPoints = actualNeighborPoints - 100; } int isGluedAtPoints = IsInSameWall(actualNeighborPosition, isGluedAt); int wasGluedAtPoints = IsInSameWall(actualNeighborPosition, _wasGluedAt); actualNeighborPoints = actualNeighborPoints + isGluedAtPoints + wasGluedAtPoints; if (actualNeighborPoints > nextNeighborPoints) { nextNeighborPoints = actualNeighborPoints; nextDirection = Direction.Right; } } actualNeighborPosition = position + Vector2Int.down; if (_gridController.CellIsEmptyWithoutCharacter(actualNeighborPosition)) { actualNeighborPoints = _gridController.VerifyNeighbor(actualNeighborPosition); if (_directionFrom == Direction.Up) { actualNeighborPoints = actualNeighborPoints - 100; } int isGluedAtPoints = IsInSameWall(actualNeighborPosition, isGluedAt); int wasGluedAtPoints = IsInSameWall(actualNeighborPosition, _wasGluedAt); actualNeighborPoints = actualNeighborPoints + isGluedAtPoints + wasGluedAtPoints; if (actualNeighborPoints > nextNeighborPoints) { nextNeighborPoints = actualNeighborPoints; nextDirection = Direction.Down; } } Vector2Int toPosition = VectorTransformer.DirectionToVector2Int(position, nextDirection); _gridController.MoveObject(position, toPosition); transform.position = VectorTransformer.Vector2IntToVector3Int(toPosition); _directionFrom = nextDirection; _wasGluedAt = isGluedAt; yield return(new WaitForSeconds(0.5f)); _coroutineControl = true; }
private void Slide(Vector2Int fromPosition, Vector2Int toPosition) { _audioSourceFall.Play(); transform.position = VectorTransformer.Vector2IntToVector3Int(toPosition); _gridController.MoveObject(fromPosition, toPosition); }
void Update() { characterPosition = VectorTransformer.Vector3ToVector2Int(character.transform.position); }
public void AddObjectToGrid(Transform newObject) { Vector2Int childPosition = VectorTransformer.Vector3ToVector2Int(newObject.position); _gridObjects.Add(childPosition, newObject); }
void Update() { if (!_menuController.StopMove) { Vector2 characterPosition = VectorTransformer.Vector3ToVector2(transform.position); if (Input.GetAxis("Horizontal") > 0.5f & _movingDirection == Direction.None & ValidatePosition(characterPosition.y)) { _movingDirection = Direction.Right; _velocity = new Vector2(1 * walkSpeed, 0); _isMoving = true; } else if (Input.GetAxis("Horizontal") < -0.5f & _movingDirection == Direction.None & ValidatePosition(characterPosition.y)) { _movingDirection = Direction.Left; _velocity = new Vector2(-1 * walkSpeed, 0); _isMoving = true; } else if (Input.GetAxis("Vertical") > 0.5f & _movingDirection == Direction.None & ValidatePosition(characterPosition.x)) { _movingDirection = Direction.Up; _velocity = new Vector2(0, 1 * walkSpeed); _isMoving = true; } else if (Input.GetAxis("Vertical") < -0.5f & _movingDirection == Direction.None & ValidatePosition(characterPosition.x)) { _movingDirection = Direction.Down; _velocity = new Vector2(0, -1 * walkSpeed); _isMoving = true; } else if (_isMoving) { switch (_movingDirection) { case Direction.Right: _target = new Vector2(Mathf.RoundToInt(characterPosition.x + 1f), Mathf.RoundToInt(characterPosition.y)); break; case Direction.Left: _target = new Vector2(Mathf.RoundToInt(characterPosition.x - 1f), Mathf.RoundToInt(characterPosition.y)); break; case Direction.Up: _target = new Vector2(Mathf.RoundToInt(characterPosition.x), Mathf.RoundToInt(characterPosition.y + 1f)); break; case Direction.Down: _target = new Vector2(Mathf.RoundToInt(characterPosition.x), Mathf.RoundToInt(characterPosition.y - 1f)); break; } _isMoving = false; } else if (!characterPosition.Equals(_target)) { float distance = Mathf.Abs(characterPosition.x - _target.x) + Mathf.Abs(characterPosition.y - _target.y); if (distance < 0.2) { transform.position = _target; _velocity = new Vector2(0, 0); _movingDirection = Direction.None; } } _rigidbody.velocity = _velocity; } }