public void move(Manager.Direction direction) { saveOldPositions(); bool didCompress = compress(direction); if (!merge(direction, didCompress) && !didCompress) { afterMove(false); return; } else { playImpact(); } afterMove(true); }
public bool compress(Manager.Direction direction) { bool change = true; bool looped = false; while (true) { change = false; foreach (GameObject gameObject in tiles) { Tile tile = gameObject.GetComponent <Tile>(); if (tile.piece != null) { GameObject neighbor = tile.neighbors[direction]; if (neighbor != null) { Tile neighborTile = neighbor.GetComponent <Tile>(); if (neighborTile.piece == null && !neighborTile.pieceExploded) { change = true; GameObject piece = tile.piece; tile.unsetPiece(); neighborTile.setPiece(piece); Vector2 vector = Manager.instance.directionVector(direction) * 0.1f; Piece pieceComponent = piece.GetComponent <Piece>(); pieceComponent.animationOffset = vector; } } } } if (!change) { return(looped); } looped = true; } }
public void keyboardControls() { if (Input.GetKeyDown(KeyCode.R)) { direction = Manager.Direction.UP_RIGHT; shouldMove = true; } if (Input.GetKeyDown(KeyCode.F)) { direction = Manager.Direction.RIGHT; shouldMove = true; } if (Input.GetKeyDown(KeyCode.C)) { direction = Manager.Direction.DOWN_RIGHT; shouldMove = true; } if (Input.GetKeyDown(KeyCode.X)) { direction = Manager.Direction.DOWN_LEFT; shouldMove = true; } if (Input.GetKeyDown(KeyCode.S)) { direction = Manager.Direction.LEFT; shouldMove = true; } if (Input.GetKeyDown(KeyCode.E)) { direction = Manager.Direction.UP_LEFT; shouldMove = true; } }
public void touchControlsDelta(Vector2 delta) { if (delta.magnitude < 16f) { return; } float angle = Mathf.Rad2Deg * Mathf.Atan2(delta.y, -delta.x); angle = Mathf.Floor((angle - 30f) / 60f); Manager.Direction direction = Manager.Direction.RIGHT; switch ((int)angle) { case 1: direction = Manager.Direction.UP_RIGHT; break; case 0: direction = Manager.Direction.UP_LEFT; break; case -1: direction = Manager.Direction.LEFT; break; case -2: direction = Manager.Direction.DOWN_LEFT; break; case -3: direction = Manager.Direction.DOWN_RIGHT; break; } this.direction = direction; shouldMove = true; }
public bool merge(Manager.Direction direction, bool didCompress) { foreach (GameObject gameObject in tiles) { Tile tile = gameObject.GetComponent <Tile>(); if (tile.piece != null) { GameObject piece = tile.piece; Piece pieceComponent = piece.GetComponent <Piece>(); pieceComponent.merge = 0; } } bool change = true; bool looped = false; while (change) { change = false; foreach (GameObject gameObject in tiles) { Tile tile = gameObject.GetComponent <Tile>(); if (tile.piece != null) { Piece piece = tile.piece.GetComponent <Piece>(); Manager.PieceType pieceType = piece.pieceType; GameObject neighborAhead = tile.neighbors[direction]; Tile neighborAheadTile = neighborAhead != null?neighborAhead.GetComponent <Tile>() : null; Piece neighborAheadPiece = neighborAheadTile != null && neighborAheadTile.piece != null?neighborAheadTile.piece.GetComponent <Piece>() : null; Manager.PieceType neighborAheadPieceType = neighborAheadPiece != null ? neighborAheadPiece.pieceType : null; GameObject neighborBehind = tile.neighbors[Manager.instance.opposite(direction)]; Tile neighborBehindTile = neighborBehind != null?neighborBehind.GetComponent <Tile>() : null; Piece neighborBehindPiece = neighborBehindTile != null && neighborBehindTile.piece != null?neighborBehindTile.piece.GetComponent <Piece>() : null; Manager.PieceType neighborBehindPieceType = neighborBehindPiece != null ? neighborBehindPiece.pieceType : null; if (neighborAheadTile != null && neighborAheadPiece != null && canMerge(pieceType, neighborAheadPieceType) && (neighborBehindTile == null || neighborBehindPiece == null || !canMerge(pieceType, neighborBehindPieceType))) { change = true; neighborAheadPiece.merge = piece.merge + 1; neighborAheadPiece.combo = pieceType.contribuesToCombo() ? 1 : 0; neighborAheadPiece.scoreAccumulator = piece.scoreAccumulator + piece.pieceType.getPointsForMerge(); List <Manager.PieceType> mergingPieceTypes = new List <Manager.PieceType>(); mergingPieceTypes.AddRange(piece.mergingPieceTypes); mergingPieceTypes.Add(neighborAheadPiece.pieceType); neighborAheadPiece.mergingPieceTypes = mergingPieceTypes; GameObject pieceObject = tile.piece; Piece pieceToAnimate = pieceObject.GetComponent <Piece>(); tile.unsetPiece(); Vector3 position = piece.oldPosition; Vector3 tilePosition = tile.transform.localPosition; float scaleBy = transform.localScale.x; Vector2 vector = Manager.instance.directionVector(direction) * 0.4f * scaleBy; pieceToAnimate.clearAnimations(); pieceToAnimate.moveWhenMergeAnimation = TweenFactory.Tween(null, new Vector3(0, 0, -3) + position * scaleBy, new Vector3(vector.x, vector.y, -3) + position * scaleBy, 0.25f, TweenScaleFunctions.CubicEaseOut, t => { if (pieceObject == null) { return; } pieceObject.transform.localPosition = t.CurrentValue; }, t => { Destroy(pieceObject); }); pieceToAnimate.scaleOutWhenMergeAnimation = TweenFactory.Tween(null, new Vector3(Manager.instance.tileScale, Manager.instance.tileScale, Manager.instance.tileScale), new Vector3(0, 0, 0), 0.25f, TweenScaleFunctions.CubicEaseOut, t => { if (pieceObject == null) { return; } pieceObject.transform.localScale = t.CurrentValue * scaleBy; }); } } } if (!change && !looped) { animateOffset(); return(false); } looped = true; } float toAddScore = 0; int maxCombo = 0; Dictionary <Tile, GameObject> newPieces = new Dictionary <Tile, GameObject>(); List <MergeBehaviorPair> mergeBehaviors = new List <MergeBehaviorPair>(); foreach (GameObject gameObject in tiles) { Tile tile = gameObject.GetComponent <Tile>(); if (tile.piece != null) { GameObject piece = tile.piece; Piece pieceComponent = piece.GetComponent <Piece>(); int combo = pieceComponent.combo; int merge = pieceComponent.merge; if (combo > maxCombo) { maxCombo = combo; } toAddScore += pieceComponent.scoreAccumulator; if (merge > 0) { Manager.PieceType pieceType = pieceComponent.mergingPieceTypes[0]; for (int i = 1; i < pieceComponent.mergingPieceTypes.Count; i++) { pieceType = pieceComponent.mergingPieceTypes[i].mergeResult(pieceType); } GameObject newPiece = createPiece(pieceType); Piece newPieceComponent = newPiece.GetComponent <Piece>(); newPieceComponent.scaleIn.Stop(TweenStopBehavior.Complete); tile.unsetPiece(); newPieces[tile] = newPiece; newPieceComponent.tile = tile; for (int i = 0; i < pieceComponent.mergingPieceTypes.Count; i++) { mergeBehaviors.Add(new MergeBehaviorPair(pieceComponent.mergingPieceTypes[i], newPieceComponent)); } Destroy(piece); newPieceComponent.clearAnimations(); newPieceComponent.mergeAnimation = TweenFactory.Tween(null, new Vector3(1.15f, 1.15f, 1.15f), new Vector3(1f, 1f, 1f), 0.2f, TweenScaleFunctions.QuadraticEaseIn, t => { if (newPiece == null) { return; } Vector3 scale = t.CurrentValue; if (newPieceComponent.tile == null) { scale *= Manager.instance.tileScale; } newPiece.transform.localScale = scale; }); } } } foreach (Tile tile in newPieces.Keys) { tile.setPiece(newPieces[tile]); } foreach (MergeBehaviorPair behavior in mergeBehaviors) { behavior.execute(); } comboManager.addCombo(maxCombo); toAddScore *= comboManager.getComboMultiplier(); score += (int)Mathf.Floor(toAddScore); compress(direction); animateOffset(); return(true); }