public void LoadTiles() { tilesCount = 0; for (int x = 0; x < level.levelSize.x; x++) { for (int y = 0; y < level.levelSize.y; y++) { if (level.tiles[x, y] != null) { tilesCount++; GameObject go = Instantiate(tilePrefab, levelObjects.transform); go.transform.position = new Vector3(x, 1, y); go.GetComponent <MeshRenderer>().materials[0].SetTexture("_MainTex", Tile.tileTypes[level.tiles[x, y].tileType].texture); if (level.tiles[x, y].moveableTile) { MoveableTile mt = go.AddComponent <MoveableTile>(); mt.endPosition = level.tiles[x, y].tileMoveTo; go.tag = "TileMoveable"; } Vector2[] uvs = go.GetComponent <MeshFilter>().sharedMesh.uv; uvs[6] = new Vector2(0, 0); uvs[7] = new Vector2(1, 0); uvs[10] = new Vector2(0, 1); uvs[11] = new Vector2(1, 1); go.GetComponent <MeshFilter>().sharedMesh.uv = uvs; } } } }
void Update_DoMovement(float deltaTime) { if (currTile == destTile) { pathAStar = null; return; // We're already were we want to be. } if (nextTile == null || nextTile == currTile) { // Get the next tile from the pathfinder. if (pathAStar == null || pathAStar.Length() == 0) { // Generate a path to our destination pathAStar = new PathAStar(World.world, currTile, destTile); // This will calculate a path from curr to dest. if (pathAStar.Length() == 0) { Debug.LogError("Path_AStar returned no path to destination!"); pathAStar = null; return; } } // Grab the next waypoint from the pathing system! nextTile = pathAStar.Dequeue(); if (nextTile == currTile) { Debug.LogError("Update_DoMovement - nextTile is currTile?"); } } float distToTravel = Mathf.Sqrt( Mathf.Pow(currTile.x - nextTile.x, 2) + Mathf.Pow(currTile.y - nextTile.y, 2) ); // How much distance can be travel this Update? float distThisFrame = speed * deltaTime; // How much is that in terms of percentage to our destination? float percThisFrame = distThisFrame / distToTravel; // Add that to overall percentage travelled. movementPercentage += percThisFrame; if (movementPercentage >= 1) { // We have reached our destination // TODO: Get the next tile from the pathfinding system. // If there are no more tiles, then we have TRULY // reached our destination. currTile = nextTile; movementPercentage = 0; // FIXME? Do we actually want to retain any overshot movement? } }
// Use this for initialization void OnEnable() { world = new World(); //BoundsInt bounds = new BoundsInt(0, 0, 0, tilemap.cellBounds.xMax, tilemap.cellBounds.yMax, 0); TileBase[] tileBase = tilemap.GetTilesBlock(tilemap.cellBounds); MoveableTile[,] moveableTiles = new MoveableTile[tilemap.cellBounds.size.x, tilemap.cellBounds.size.y]; for (int x = 0; x < tilemap.cellBounds.size.x; x++) { for (int y = 0; y < tilemap.cellBounds.size.y; y++) { moveableTiles[x, y] = new MoveableTile(tileBase[x + y * tilemap.cellBounds.size.x], x, y); } } world.worldTiles = moveableTiles; World.world = world; world.tilemap = tilemap; world.tileGraph = new PathTileGraph(world, 1); if (world.tileGraph == null) { Debug.Log(" tilegraph is null"); return; } foreach (PathNode <MoveableTile> node in world.tileGraph.nodes.Values) { foreach (PathEdge <MoveableTile> edge in node.edges) { Debug.DrawLine(new Vector3(node.data.x / 4.0f, node.data.y / 4.0f, 0), new Vector3(edge.node.data.x / 4.0f, edge.node.data.y / 4.0f, 0), Color.red, 50000); } Debug.DrawLine(new Vector3(node.data.x / 4.0f, node.data.y / 4.0f, 0), new Vector3((node.data.x / 4.0f) + 0.01f, (node.data.y / 4.0f) + 0.05f, 0), Color.yellow, 50000); } }
bool IsClippingCorner(World world, MoveableTile curr, MoveableTile neigh) { // If the movement from curr to neigh is diagonal (e.g. N-E) // Then check to make sure we aren't clipping (e.g. N and E are both walkable) int dX = curr.x - neigh.x; int dY = curr.y - neigh.y; if (Mathf.Abs(dX) + Mathf.Abs(dY) == 2) { // We are diagonal if (world.getTile(curr.x - dX, curr.y) == null || world.getTile(curr.x - dX, curr.y).tilebase == null) { // East or West is unwalkable, therefore this would be a clipped movement. return(true); } if (world.getTile(curr.x, curr.y - dY) == null || world.getTile(curr.x, curr.y - dY).tilebase == null) { // North or South is unwalkable, therefore this would be a clipped movement. return(true); } // If we reach here, we are diagonal, but not clipping } // If we are here, we are either not clipping, or not diagonal return(false); }
public override void OnTilesMerged(MoveableTile movedTile, MoveableTile mergedInto, Map map) { //moved tile not in tiles mergedInto.KillTile(); map.CreateBlockerTileAt(mergedInto.MapPosition); }
void SetUpButtonColor(MoveableTile t) { upColorDisplay.color = t.GetNextColor(Direction.Up); downColorDisplay.color = t.GetNextColor(Direction.Down); leftColorDisplay.color = t.GetNextColor(Direction.Left); rightColorDisplay.color = t.GetNextColor(Direction.Right); }
public void ToggleValueDisplayForTiles() { foreach (Tile t in GetTilesInLayer(Tile.GetLayerForType(Tile.Type.Movable))) { MoveableTile tM = t as MoveableTile; tM.ToggleValueDisplay(); } }
void BuildMoveableTiles(List <MoveableTileSpawnInfo> layout) { //level moveable tiles foreach (MoveableTileSpawnInfo info in layout) { MoveableTile t = CreateMoveableTileAt(info.GridPosition); t.SetValue(info.Value); } }
void SetUpButtons(MoveableTile t, Bounds bounds) { upButton.gameObject.SetActive(CheckIfButtonInBounds(upButton, bounds) && t.CheckIfMoveable(Direction.Up)); downButton.gameObject.SetActive(CheckIfButtonInBounds(downButton, bounds) && t.CheckIfMoveable(Direction.Down)); leftButton.gameObject.SetActive(CheckIfButtonInBounds(leftButton, bounds) && t.CheckIfMoveable(Direction.Left)); rightButton.gameObject.SetActive(CheckIfButtonInBounds(rightButton, bounds) && t.CheckIfMoveable(Direction.Right)); SetUpButtonColor(t); }
void MoveSelectedTile(Direction dir) { if (SelectedTile is MoveableTile) { MoveableTile t = selectedTile as MoveableTile; t.Move(dir); //deselect after single move SelectedTile = null; } }
public override void EnableInspector(Tile t) { base.EnableInspector(t); if (!(t is MoveableTile)) { throw new System.Exception($"Trying to inspect wrong type of tile, can only inspect {typeof(MoveableTile)} trying to inspect {t.GetType()}"); } moveableTileInspected = t as MoveableTile; selectedImage.color = t.TileColor; }
void TileCreated(Tile t) { t.RegisterToOnDeath(TileDeath); if (t is MoveableTile) { MoveableTile mT = t as MoveableTile; mT.RegisterToOnTileStartMove(TileMove); mT.RegisterToOnMerge(TileMerged); } }
void TileDeath(Tile t) { t.UnregisterFromOnDeath(TileDeath); if (t is MoveableTile) { MoveableTile mT = t as MoveableTile; mT.UnregisterFromOnMerge(TileMerged); mT.UnregisterFromOnTileStartMove(TileMove); } OnTileDeath?.Invoke(GetCurrentGameState()); }
public MoveableTile[] getNeighboursOfTile(MoveableTile tile) { MoveableTile[] tiles = new MoveableTile[8]; tiles[0] = getTile(tile.x + 1, tile.y); tiles[1] = getTile(tile.x, tile.y + 1); tiles[2] = getTile(tile.x - 1, tile.y); tiles[3] = getTile(tile.x, tile.y - 1); tiles[4] = getTile(tile.x - 1, tile.y + 1); tiles[5] = getTile(tile.x + 1, tile.y + 1); tiles[6] = getTile(tile.x - 1, tile.y - 1); tiles[7] = getTile(tile.x + 1, tile.y - 1); return(tiles); }
void Merge(Direction dir) { MoveableTile mergedInto = map.GetTileAt(LayeredGridPosition + dir) as MoveableTile; //OnMerge mergedInto.PlayMergeAnimation(); mergedInto.mergeAudioSrc.Play(); OnMerged?.Invoke(this, mergedInto); //movement ended OnTileEndMove?.Invoke(this); //"merged" with existing tile ActivateMergeEffectSelfMoving(mergedInto); //Destroy(gameObject); //InvokeOnDeath(); KillTile(); }
public MoveableTile CreateMoveableTileAt(Vector2Int pos) { //TODO check if in bounds probably; GameObject obj = Instantiate(moveableTilePrefab); MoveableTile t = obj.GetComponent <MoveableTile>(); t.Initialize(new Vector3Int(pos.x, pos.y, 0), this); t.name = t.GetType().ToString() + ": " + t.LayeredGridPosition.ToString(); t.PlaceInWorld(); t.transform.SetParent(transform); Tiles.Add(t.LayeredGridPosition, t); OnTileCreated?.Invoke(t); return(t); }
public void CheckRotationInput() { if (Input.GetMouseButtonDown(1)) { Debug.Log("Mouse Right"); Vector3 v = Camera.main.ScreenToWorldPoint(Input.mousePosition); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out RaycastHit hit, 20, Kiwi.TileFinderMask)) { MoveableTile tile = hit.collider.gameObject.GetComponentInParent <MoveableTile>(); //MoveableTile tile = hit.collider.gameObject.GetComponent<MoveableTile>(); Rotate(tile); } } }
// Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { Vector3 poss = Camera.main.ScreenToWorldPoint(Input.mousePosition); MoveableTile tile = World.world.getTile((int)(poss.x * 4), (int)(poss.y * 4)); if (tile != null) { destTile = tile; pathAStar = null; } } Update_DoMovement(Time.deltaTime); //Vector3 pos = World.world.tilemap.CellToWorld(new Vector3Int((int)(X / 4), (int)(Y / 4), 0)); go.transform.position = new Vector3((X / 4), (Y / 4), 0); }
private void Update() { if (Input.GetKeyDown(selectionKey) && !EventSystem.IsPointerOverGameObject()) { //get tile Vector3 pos = playerCam.ScreenToWorldPoint(Input.mousePosition); MoveableTile t = map.GetTileFromLayer(map.WorldToGridWithoutLayer(pos), Tile.GetLayerForType(Tile.Type.Movable)) as MoveableTile; //make sure to deselect on end move if (t != null) { t.RegisterToOnTileEndMove(DeselectTile); } SelectedTile = t;// set even if null } }
// Use this for initialization void Start() { currTile = World.world.getTile(5, 5); destTile = nextTile = currTile; }
public PathTileGraph(World world, int unitSize) { Debug.Log("Path_TileGraph"); nodes = new Dictionary <MoveableTile, PathNode <MoveableTile> >(); for (int x = 0; x < world.Width; x++) { for (int y = 0; y < world.Height; y++) { MoveableTile t = world.getTile(x, y); if (t.tilebase != null) { PathNode <MoveableTile> n = new PathNode <MoveableTile>(); n.data = t; nodes.Add(t, n); } } } Debug.Log("Path_TileGraph: Created " + nodes.Count + " nodes."); int edgeCount = 0; foreach (MoveableTile t in nodes.Keys) { PathNode <MoveableTile> n = nodes[t]; List <PathEdge <MoveableTile> > edges = new List <PathEdge <MoveableTile> >(); MoveableTile[] neighbours = world.getNeighboursOfTile(t); for (int i = 0; i < neighbours.Length; i++) { if (neighbours[i] != null && neighbours[i].tilebase != null && !IsClippingCorner(world, t, neighbours[i])) { MoveableTile neighbour = neighbours[i]; bool makeEdge = true; if (unitSize > 1) { for (int x = neighbour.x - (unitSize / 2); x < neighbour.x + (unitSize / 2); x++) { for (int y = neighbour.y - (unitSize / 2); y < neighbour.y + (unitSize / 2); y++) { if (world.getTile(x, y) == null || world.getTile(x, y).tilebase == null) { makeEdge = false; } } } } if (makeEdge) { PathEdge <MoveableTile> e = new PathEdge <MoveableTile>(); e.cost = 1; e.node = nodes[neighbours[i]]; edges.Add(e); edgeCount++; } } } n.edges = edges.ToArray(); } Debug.Log("Path_TileGraph: Created " + edgeCount + " edges."); }
public override void OnTilesMerged(MoveableTile movedTile, MoveableTile mergedInto, Map map) { map.CreateBlockerTileAt(movedTile.MapPosition); }
public void ActivateMergeEffect(MoveableTile moved, MoveableTile mergedInto, Map map) { mergeEffect.OnTilesMerged(moved, mergedInto, map); }
public override void OnTilesMerged(MoveableTile movedTile, MoveableTile mergedInto, Map map) { return; }
public bool CheckSwapInput() { if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out RaycastHit hit, 20, Kiwi.TileFinderMask)) { MoveableTile tile = hit.collider.gameObject.GetComponentInParent <MoveableTile>(); //MoveableTile tile = hit.collider.gameObject.GetComponent<MoveableTile>(); if (tile == null) { return(false); } switch (_state) { case InputState.Nothing: this._tile = tile; _state = InputState.Undefined_Swap; SFXPlayer.PlaySelectSound(transform.position); break; case InputState.Click_Swap: Swap(_tile, tile); _state = InputState.Nothing; _tile = null; return(true); default: _state = InputState.Nothing; break; } } } else if (Input.GetMouseButtonUp(0)) { Vector3 v = Camera.main.ScreenToWorldPoint(Input.mousePosition); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out RaycastHit hit, 20, Kiwi.TileFinderMask)) { MoveableTile tile = hit.collider.gameObject.GetComponentInParent <MoveableTile>(); //MoveableTile tile = hit.collider.gameObject.GetComponent<MoveableTile>(); if (tile == null) { return(false); } switch (_state) { case InputState.Undefined_Swap: if (tile == _tile) { //SFXPlayer.PlaySelectSound(transform.position); _state = InputState.Click_Swap; } else { _state = InputState.Nothing; Swap(_tile, tile); _tile = null; return(true); } break; default: _state = InputState.Nothing; break; } } } return(false); }
public abstract void OnTilesMerged(MoveableTile movedTile, MoveableTile mergedInto, Map map);
public void ActivateMergeEffectSelfMoving(MoveableTile mergedInto) { Map.ActivateMergeEffect(this, mergedInto); }
private void OnLevelWasLoaded(int level) { _state = InputState.Nothing; _tile = null; }
public bool CheckIfTilesCanMerge(MoveableTile tileTryingToMove, MoveableTile tileMovedInto, Direction movingDirection) { return(mergeRule.CanMerge(tileMovedInto.Value, tileTryingToMove.Value, movingDirection)); }
public void ActivateMergeEffect(MoveableTile moved, MoveableTile mergedInto) { Level.ActivateMergeEffect(moved, mergedInto, this); }