protected virtual void OnClick(Vector2 location) { // See if there is a targeted actor to select // at the clicked location. targetedActor = null; Vector2Int gridPosition = grid.WorldToGrid(location); if (grid.DoesTileExist(gridPosition)) { // In theory there should only be one unit on a tile // at any given time during the command phase. TODO // may want to just check the first actor, but not doing // this for now in case non-unit actors are to be implemented. foreach (TileActor actor in grid.GetActorsOnTile(gridPosition)) { if (actor.TeamID == teamID) { currentTile = gridPosition; targetedActor = actor; // Tell this actor that it has been clicked. targetedActor.OnClick(); break; } } } }
private TileActor createTileActor(TileModel tileModel) { Assert.IsTrue(tileModel != null && !tileModel.isOffBorad()); float halfRows = (currentData.numRows - 1) * currentData.tileSpacing / 2.0f; float halfCols = (currentData.numCols - 1) * currentData.tileSpacing / 2.0f; TileActor currentTile = Instantiate(currentData.tileInstance, Vector3.zero, Quaternion.identity, myTransform); //updating visual currentTile.tileModel = tileModel; currentTile.name = tileModel.row + "," + tileModel.col; currentTile.transform.localPosition = new Vector3(tileModel.col * currentData.tileSpacing - halfCols, tileModel.row * currentData.tileSpacing - halfRows); currentTile.setupFromConfig(currentData.tileConfigs[tileModel.type]); //linking visual and model Assert.IsNull(tileModel.AttachedTileActor); tileModel.AttachedTileActor = currentTile; return(currentTile); }
public virtual TileActor getActorUnderPointer(float rayDistance) { Ray r = mainCamera.ScreenPointToRay(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(r.origin, r.direction, rayDistance); if (hit) { TileActor hitted = hit.transform.GetComponent <TileActor>(); return(hitted); } return(null); }
public void onInputBegin() { Assert.IsNull(selectedTile); TileActor t = userInput.getActorUnderPointer(20.0f); if (t == null) { return; } selectedTile = t; Color32 tC = selectedTile.getColor(); selectedTile.setHighlight(new Color(tC.r, tC.g, tC.b, 0.5f)); }
public Tile(TileEngine tileEngine) : base() { this.tileEngine = tileEngine; // Add all supported actors. actors = new Dictionary <PaintType, TileActor>() { { PaintType.Texture, new TexturedTileActor(tileEngine, this) } }; // Set textured tile actor as default. lastPaintType = PaintType.Texture; currentActor = actors[lastPaintType]; }
public void Paint(PaintArgs args) { // Check if the actor is valid for this paint operation. if (lastPaintType != args.PaintType) { // Invalid actor. Change it. currentActor.Clear(); currentActor = actors[args.PaintType]; } lastPaintType = args.PaintType; // Let the actor paint. currentActor.Paint(args); }
public void onInputBegin() { Assert.IsNull(selectedTile); TileActor t = userInput.getActorUnderPointer(20.0f); if (t == null) { return; } // Debug.Log("selected actor model data "+t.tileModel.ToString()); selectedTile = t; Color32 tC = selectedTile.getColor(); selectedTile.setHighlight(new Color(tC.r, tC.g, tC.b, 0.5f)); }
IEnumerator _addNewTileActors(List <TileModel> addedTiles, float aliveTime) { if (addedTiles.Count == 0) { yield break; } TileActor[] actors = new TileActor[addedTiles.Count]; for (int i = 0; i < addedTiles.Count; ++i) { actors[i] = createTileActor(addedTiles[i]); actors[i].transform.localScale = Vector3.zero; } float timer = 0; if (aliveTime <= 0) { aliveTime = Mathf.Epsilon; } while (timer <= aliveTime) { for (int i = 0; i < addedTiles.Count; ++i) { float factor = timer / aliveTime; actors[i].transform.localScale = new Vector3(factor, factor, factor); } timer += Time.deltaTime; yield return(null); } yield return(null); //snapping for (int i = 0; i < addedTiles.Count; ++i) { actors[i].transform.localScale = Vector3.one; } }
IEnumerator _deleteTileActors(TileModel [] modelsDeleted, float deathTime) { if (modelsDeleted.Length == 0) { yield break; } float timer = 0; if (deathTime <= 0) { deathTime = Mathf.Epsilon; } while (timer <= deathTime) { for (int i = 0; i < modelsDeleted.Length; ++i) { TileActor act = modelsDeleted[i].AttachedTileActor; Color c = act.getColor(); act.setColor(new Color(c.r, c.g, c.b, 1 - timer / deathTime)); } timer += Time.deltaTime; yield return(null); } yield return(null); //actual deletion for (int i = 0; i < modelsDeleted.Length; ++i) { Assert.IsTrue(modelsDeleted[i].isOffBorad()); TileActor act = modelsDeleted[i].AttachedTileActor; Destroy(act.gameObject); modelsDeleted[i].AttachedTileActor = null; } }
public void onInputEnd() { TileActor selected = selectedTile; //clear state at top selectedTile = null; if (selected != null) { selected.restoreHighlight(); Vector2 diff = getpointerPosInBoardSpace() - (Vector2)selected.transform.localPosition; float angle = Mathf.Repeat(Vector2.SignedAngle(myTransform.right, diff) + 360.0f, 360.0f); Dir userDir = Match3.Utils.getSwipeDirection(diff, currentData.swipeDelta, angle); int otherRow = selected.tileModel.row, otherCol = selected.tileModel.col; switch (userDir) { case Dir.Up: ++otherRow; break; case Dir.Down: --otherRow; break; case Dir.Left: --otherCol; break; case Dir.Right: ++otherCol; break; case Dir.NoDir: return; } if (otherCol < 0 || otherCol >= currentData.numCols || otherRow < 0 || otherRow >= currentData.numRows) { return; } TileModel other = boardModel.getTileModelAt(otherRow, otherCol); if (other == null || !boardModel.canSwap(selected.tileModel, other)) { return; } Assert.IsNotNull(other.AttachedTileActor); TileActor otherActor = other.AttachedTileActor; //controller work,update model,disable view //model work boardModel.swapTiles(selected.tileModel, other); //view work userInput.enabled = false; StartCoroutine(_tileMovmentAndStabilization(selected, otherActor, currentData.tileMovementTime, onStabilizatonOver)); } }
private IEnumerator _tileMovmentAndStabilization(TileActor first, TileActor second, float moveTime, OncompleteBoardStabilization callback) { float timer = 0; if (moveTime <= 0) { moveTime = Mathf.Epsilon; } Vector3 firstPos = first.transform.localPosition; Vector3 secondPos = second.transform.localPosition; //move sprites while (timer <= moveTime) { float factor = Match3.Utils.smoothStop2(timer / moveTime); first.transform.localPosition = Vector3.Lerp(firstPos, secondPos, factor); second.transform.localPosition = Vector3.Lerp(secondPos, firstPos, factor); timer += Time.deltaTime; yield return(null); } //snap positions first.transform.localPosition = secondPos; second.transform.localPosition = firstPos; yield return(null); Chains c = boardModel.deleteChains(); TileModel[] tilesDeleted = c.toArray(); //update gui variables --movesRemaining; while (tilesDeleted.Length > 0) { //delete chained actors yield return(StartCoroutine(_deleteTileActors(tilesDeleted, currentData.tileDeathTime))); BoardModel.TileShiftData [] tilesShift = boardModel.settleTiles(); //updating gui scoreGui += c.getScore(); //make cookies fall yield return(StartCoroutine(_fallTiles(tilesShift))); //add new cookies // boardModel.print("before adding"); List <TileModel> addedTiles = boardModel.addTilesTillfull(); // Debug.Log("tiles added "+addedTiles.Count); // boardModel.print("after adding"); yield return(StartCoroutine(_addNewTileActors(addedTiles, currentData.tileScaleTime))); //next iteration setup here c = boardModel.deleteChains(); tilesDeleted = c.toArray(); } checkGameOver(); callback(); }
private IEnumerator _tileMovmentAndStabilization(TileActor first, TileActor second, float moveTime, OncompleteBoardStabilization callback) { float timer = 0; if (moveTime <= 0) { moveTime = Mathf.Epsilon; } Vector3 firstPos = first.transform.localPosition; Vector3 secondPos = second.transform.localPosition; //move sprites while (timer <= moveTime) { float factor = Match3.Utils.smoothStop2(timer / moveTime); first.transform.localPosition = Vector3.Lerp(firstPos, secondPos, factor); second.transform.localPosition = Vector3.Lerp(secondPos, firstPos, factor); timer += Time.deltaTime; yield return(null); } //snap positions first.transform.localPosition = secondPos; second.transform.localPosition = firstPos; yield return(null); // yield return StartCoroutine(_testing()); Chains c = boardModel.deleteChains(); TileModel[] tilesDeleted = c.toArray(); //note update gui variables --movesRemaining; while (tilesDeleted.Length > 0) { //delete chained actors yield return(StartCoroutine(_deleteTileActors(tilesDeleted, currentData.tileDeathTime))); List <BoardModel.TileShift> [] shift = boardModel.settleTiles(); //todo updating gui scoreGui += c.getScore(); yield return(StartCoroutine(_fallTiles(shift))); //add new cookies // boardModel.print("before adding"); List <TileModel> addedTiles = boardModel.addTilesTillfull(); // Debug.Log("tiles added "+addedTiles.Count); // boardModel.print("after adding"); yield return(StartCoroutine(_addNewTileActors(addedTiles, currentData.tileScaleTime))); //next iteration setup here c = boardModel.deleteChains(); tilesDeleted = c.toArray(); } callback(); //this is the last thing since this might reset game if (checkGameOver()) { var gameEnd = new LevelManager.GameEndData(scoreGui >= currentData.targetScore && movesRemaining > 0); GetComponent <LevelManager>().onGameEnd(gameEnd); } }