void Start() { m_animCtrl = GetComponents <DirectionalAnimation>()[0]; m_phyChar = GetComponent <PhysicCharBehaviour>(); TargetTile = RpgMapHelper.GetAutoTileByPosition(transform.position, 0); ableToMove = true; }
//float now; float GetDistToTarget(AutoTile targetTile) { Vector3 target = RpgMapHelper.GetTileCenterPosition(targetTile.TileX, targetTile.TileY); target.z = transform.position.z; return((transform.position - target).magnitude); }
void Update() { m_CurTile = RpgMapHelper.GetAutoTileByPosition(transform.position, 0); if (TargetTile == null) TargetTile = m_CurTile; _DoStateLogic(); }
public void ActivateTrigger(eTrigger trigger) { if (m_CurTile == null) return; Vector2 targetMapPos = new Vector2(m_CurTile.TileX, m_CurTile.TileY); switch (trigger) { case eTrigger.MoveDown: targetMapPos.y += 1; break; case eTrigger.MoveLeft: targetMapPos.x -= 1; break; case eTrigger.MoveRight: targetMapPos.x += 1; break; case eTrigger.MoveUp: targetMapPos.y -= 1; break; } if (PlayerState == eState.Idle && (targetMapPos.x != m_CurTile.TileX || targetMapPos.y != m_CurTile.TileY)) { if (AutoTileMap.Instance.GetCellAutotileCollision((int)targetMapPos.x, (int)targetMapPos.y) == eTileCollisionType.PASSABLE) { Vector3 vTargetPos = RpgMapHelper.GetTileCenterPosition((int)targetMapPos.x, (int)targetMapPos.y); TargetTile = RpgMapHelper.GetAutoTileByPosition(vTargetPos, 0); m_fAxisX = TargetTile.TileX - m_CurTile.TileX; m_fAxisX = m_fAxisX != 0 ? Mathf.Sign(m_fAxisX) : 0; m_fAxisY = TargetTile.TileY - m_CurTile.TileY; m_fAxisY = m_fAxisY != 0 ? Mathf.Sign(m_fAxisY) : 0; SetState(eState.Moving); } } }
public override bool IsPassable() { AutoTileMap autoTileMap = AutoTileMap.Instance; if (autoTileMap.IsValidAutoTilePos(TileX, TileY)) { for (int iLayer = autoTileMap.GetLayerCount() - 1; iLayer >= 0; --iLayer) { if (autoTileMap.MapLayers[iLayer].LayerType == eLayerType.Ground) { AutoTile autoTile = autoTileMap.GetAutoTile(TileX, TileY, iLayer); eTileCollisionType collType = autoTile.Id >= 0 ? autoTileMap.Tileset.AutotileCollType[autoTile.Id] : eTileCollisionType.EMPTY; if (IsEmptyTilePassable && collType == eTileCollisionType.EMPTY || collType == eTileCollisionType.PASSABLE || collType == eTileCollisionType.WALL) { return(true); } else if (collType == eTileCollisionType.BLOCK || collType == eTileCollisionType.FENCE) { return(false); } } } } return(false); }
float GetDistToTarget(AutoTile targetTile) { Vector3 target = m_curNode.Next != null?RpgMapHelper.GetTileCenterPosition(targetTile.TileX, targetTile.TileY) : (Vector3)TargetPos; target.z = transform.position.z; float dist = (transform.position - target).magnitude; return(dist); }
void Update() { // if (!ableToMove) { // TargetTile = null; // } m_CurTile = RpgMapHelper.GetAutoTileByPosition(transform.position, 0); if (TargetTile == null) { TargetTile = m_CurTile; } _DoStateLogic(); }
// special case for walls bool _IsWallPassable(MapTileNode neighNode) { AutoTileMap autoTileMap = AutoTileMap.Instance; eTileCollisionType collType = eTileCollisionType.EMPTY; eTileCollisionType collTypeNeigh = eTileCollisionType.EMPTY; for (int iLayer = autoTileMap.GetLayerCount() - 1; iLayer >= 0; --iLayer) { if (autoTileMap.MapLayers[iLayer].LayerType == eLayerType.Ground) { AutoTile autoTile = autoTileMap.GetAutoTile(TileX, TileY, iLayer); AutoTile autoTileNeigh = autoTileMap.GetAutoTile(neighNode.TileX, neighNode.TileY, iLayer); if (autoTile.Id == autoTileNeigh.Id) // you can walk over two wall tiles if they have the same type { if (autoTile.Id >= 0) { return(true); } else { continue; } } else { // collType will keep the first collision type found of type wall or passable if (collType != eTileCollisionType.PASSABLE && collType != eTileCollisionType.WALL) { collType = autoTile.Id >= 0 ? autoTileMap.Tileset.AutotileCollType[autoTile.Id] : eTileCollisionType.EMPTY; } if (collTypeNeigh != eTileCollisionType.PASSABLE && collTypeNeigh != eTileCollisionType.WALL) { collTypeNeigh = autoTileNeigh.Id >= 0 ? autoTileMap.Tileset.AutotileCollType[autoTileNeigh.Id] : eTileCollisionType.EMPTY; } if (collType == eTileCollisionType.PASSABLE && collTypeNeigh == eTileCollisionType.PASSABLE) { return(true); } else if (collType == eTileCollisionType.WALL || collTypeNeigh == eTileCollisionType.WALL) { return(false); } } } } return(true); }
private void _DoStateLogic() { switch (PlayerState) { case eState.Idle: UpdateMovement(0, 0); transform.position = RpgMapHelper.GetTileCenterPosition(m_CurTile.TileX, m_CurTile.TileY) + new Vector3(0, 0, transform.position.z); break; case eState.Moving: UpdateMovement(m_fAxisX, -m_fAxisY); Vector3 vCheckPos = transform.position - new Vector3((AutoTileMap.Instance.CellSize.x / 2) * m_fAxisX, -(AutoTileMap.Instance.CellSize.y / 2) * m_fAxisY); AutoTile checkTile = RpgMapHelper.GetAutoTileByPosition(vCheckPos, 0); if (checkTile == TargetTile) { transform.position = RpgMapHelper.GetTileCenterPosition(TargetTile.TileX, TargetTile.TileY) + new Vector3(0, 0, transform.position.z); SetState(eState.Idle); } break; } }
/// <summary> /// Refresh a tile according to neighbors /// </summary> /// <param name="autoTile">Tile to be refreshed</param> public void RefreshTile( AutoTile autoTile ) { if (autoTile == null) return; m_tileChunkPoolNode.MarkUpdatedTile( autoTile.TileX, autoTile.TileY, autoTile.Layer); if( MapLayers[autoTile.Layer].LayerType == eLayerType.FogOfWar ) { return; } SubTilesetConf tilesetConf = Tileset.SubTilesets[autoTile.TilesetIdx]; if( autoTile.Id >= 0 ) { int relativeTileIdx = autoTile.Id % AutoTileset.k_TilesPerSubTileset; // Check if the tile is a normal tile, non autotile if (relativeTileIdx >= 128 || !tilesetConf.HasAutotiles) // 128 start with NORMAL tileset, treated differently ) { if( tilesetConf.HasAutotiles ) { relativeTileIdx -= 128; // relative idx to its normal tileset } int tx = relativeTileIdx % Tileset.AutoTilesPerRow; int ty = relativeTileIdx / Tileset.AutoTilesPerRow; //fix tileset OBJECTS, the other part of the tileset is in the right side if( ty >= 16 ) { ty -= 16; tx += 8; } //--- int tileBaseIdx = tilesetConf.TilePartOffset[autoTile.Type == eTileType.OBJECTS? 0 : 4]; // set base tile idx of autoTile tileset ( 4 is the index of Normal tileset in autotilesets ) int tileIdx = (autoTile.Type == eTileType.OBJECTS) ? ty * 2 * Tileset.AutoTilesPerRow + tx : ty * Tileset.AutoTilesPerRow + tx; tileIdx += tileBaseIdx; autoTile.TilePartsIdx[ 0 ] = tileIdx; // set the kind of tile, for collision use autoTile.TilePartsType[ 0 ] = eTilePartType.EXT_CORNER; // Set Length of tileparts autoTile.TilePartsLength = 1; } else { int gridX = autoTile.TileX; int gridY = autoTile.TileY; int iLayer = autoTile.Layer; int tilePartIdx = 0; for( int j = 0; j < 2; ++j ) { for( int i = 0; i < 2; ++i, ++tilePartIdx ) { int tile_x = gridX*2 + i; int tile_y = gridY*2 + j; int tilePartX = 0; int tilePartY = 0; eTilePartType tilePartType; if (tile_x % 2 == 0 && tile_y % 2 == 0) //A { tilePartType = _getTileByNeighbours( autoTile.Id, GetAutoTile( gridX, gridY-1, iLayer ).Id, //V GetAutoTile( gridX-1, gridY, iLayer ).Id, //H GetAutoTile( gridX-1, gridY-1, iLayer ).Id //D ); tilePartX = aTileAff[ (int)tilePartType, 0 ]; tilePartY = aTileAff[ (int)tilePartType, 1 ]; } else if (tile_x % 2 != 0 && tile_y % 2 == 0) //B { tilePartType = _getTileByNeighbours( autoTile.Id, GetAutoTile( gridX, gridY-1, iLayer ).Id, //V GetAutoTile( gridX+1, gridY, iLayer ).Id, //H GetAutoTile( gridX+1, gridY-1, iLayer ).Id //D ); tilePartX = aTileBff[ (int)tilePartType, 0 ]; tilePartY = aTileBff[ (int)tilePartType, 1 ]; } else if (tile_x % 2 == 0 && tile_y % 2 != 0) //C { tilePartType = _getTileByNeighbours( autoTile.Id, GetAutoTile( gridX, gridY+1, iLayer ).Id, //V GetAutoTile( gridX-1, gridY, iLayer ).Id, //H GetAutoTile( gridX-1, gridY+1, iLayer ).Id //D ); tilePartX = aTileCff[ (int)tilePartType, 0 ]; tilePartY = aTileCff[ (int)tilePartType, 1 ]; } else //if (tile_x % 2 != 0 && tile_y % 2 != 0) //D { tilePartType = _getTileByNeighbours( autoTile.Id, GetAutoTile( gridX, gridY+1, iLayer ).Id, //V GetAutoTile( gridX+1, gridY, iLayer ).Id, //H GetAutoTile( gridX+1, gridY+1, iLayer ).Id //D ); tilePartX = aTileDff[ (int)tilePartType, 0 ]; tilePartY = aTileDff[ (int)tilePartType, 1 ]; } // set the kind of tile, for collision use autoTile.TilePartsType[ tilePartIdx ] = tilePartType; int tileBaseIdx = tilesetConf.TilePartOffset[ (int)autoTile.Type ]; // set base tile idx of autoTile tileset //NOTE: All tileset have 32 autotiles except the Wall tileset with 48 tiles ( so far it's working because wall tileset is the last one ) relativeTileIdx = autoTile.MappedIdx - ((int)autoTile.Type * 32); // relative to owner tileset ( All tileset have 32 autotiles ) int tx = relativeTileIdx % Tileset.AutoTilesPerRow; int ty = relativeTileIdx / Tileset.AutoTilesPerRow; int tilePartSpriteIdx; if( autoTile.Type == eTileType.BUILDINGS ) { tilePartY = Mathf.Max( 0, tilePartY - 2); tilePartSpriteIdx = tileBaseIdx + ty * (Tileset.AutoTilesPerRow * 4) * 4 + tx * 4 + tilePartY * (Tileset.AutoTilesPerRow * 4) + tilePartX; } //NOTE: It's not working with stairs shapes // XXXXXX // IIIXXX // IIIXXX // IIIIII else if( autoTile.Type == eTileType.WALLS ) { if( ty % 2 == 0 ) { tilePartSpriteIdx = tileBaseIdx + (ty/2) * (Tileset.AutoTilesPerRow * 4) * 10 + tx * 4 + tilePartY * (Tileset.AutoTilesPerRow * 4) + tilePartX; } else { //tilePartY = Mathf.Max( 0, tilePartY - 2); tilePartY -= 2; if( tilePartY < 0 ) { if( tilePartX == 2 && tilePartY == -2 ) {tilePartX = 2; tilePartY = 0;} else if( tilePartX == 3 && tilePartY == -2 ) {tilePartX = 1; tilePartY = 0;} else if( tilePartX == 2 && tilePartY == -1 ) {tilePartX = 2; tilePartY = 3;} else if( tilePartX == 3 && tilePartY == -1 ) {tilePartX = 1; tilePartY = 3;} } tilePartSpriteIdx = tileBaseIdx + (Tileset.AutoTilesPerRow * 4) * ((ty/2) * 10 + 6) + tx * 4 + tilePartY * (Tileset.AutoTilesPerRow * 4) + tilePartX; } } else { tilePartSpriteIdx = tileBaseIdx + ty * (Tileset.AutoTilesPerRow * 4) * 6 + tx * 4 + tilePartY * (Tileset.AutoTilesPerRow * 4) + tilePartX; } autoTile.TilePartsIdx[ tilePartIdx ] = tilePartSpriteIdx; // Set Length of tileparts autoTile.TilePartsLength = 4; } } } } }
// calculate tileset idx of autotile base in the number of tiles of each tileset private eTileType _GetAutoTileType( AutoTile autoTile ) { SubTilesetConf tilesetConf = Tileset.SubTilesets[autoTile.TilesetIdx]; if( tilesetConf.HasAutotiles ) { int relTileIdx = autoTile.Id % AutoTileset.k_TilesPerSubTileset; if( relTileIdx >= 0 && relTileIdx < 16 ) return eTileType.ANIMATED; else if( relTileIdx >= 16 && relTileIdx < 48 ) return eTileType.GROUND; else if (relTileIdx >= 48 && relTileIdx < 80 ) return eTileType.BUILDINGS; else if (relTileIdx >= 80 && relTileIdx < 128) return eTileType.WALLS; else return eTileType.NORMAL; } else return eTileType.OBJECTS; }
/// <summary> /// Set a tile in the grid coordinates specified and layer ( 0: ground, 1: overground, 2: overlay ) /// </summary> /// <param name="gridX">Tile x position of the map</param> /// <param name="gridY">Tile y position of the map</param> /// <param name="tileId">This is the id of the tile. You can see it in the editor while editing the map in the top left corner. Use -1 for an empty tile</param> /// <param name="iLayer"> Layer where to set the tile ( 0: ground, 1: overground, 2: overlay )</param> /// <param name="refreshTile">If tile and neighbors should be refreshed by this method or do it layer</param> public void SetAutoTile(int gridX, int gridY, int tileId, int iLayer, bool refreshTile = true) { if( !IsValidAutoTilePos( gridX, gridY ) || iLayer >= MapLayers.Count ) { return; } bool tileHasChange = false; //+++ Special Case for Fog of War if (MapLayers[iLayer].LayerType == eLayerType.FogOfWar) { int idx = gridX + gridY * MapTileWidth; AutoTile autoTile = TileLayers[MapLayers[iLayer].TileLayerIdx][idx]; if (autoTile == null) { autoTile = new AutoTile(); TileLayers[MapLayers[iLayer].TileLayerIdx][idx] = autoTile; } tileHasChange = autoTile.Id != tileId; autoTile.Id = tileId; autoTile.TileX = gridX; autoTile.TileY = gridY; autoTile.Layer = iLayer; if (refreshTile && tileHasChange) { RefreshTile(autoTile); } } else //---- { tileId = Mathf.Clamp(tileId, -1, Tileset.ThumbnailRects.Count - 1); int idx = gridX + gridY * MapTileWidth; AutoTile autoTile = TileLayers[MapLayers[iLayer].TileLayerIdx][idx]; if (autoTile == null) { autoTile = new AutoTile(); TileLayers[MapLayers[iLayer].TileLayerIdx][idx] = autoTile; autoTile.TilePartsType = new eTilePartType[4]; autoTile.TilePartsIdx = new int[4]; } int tilesetIdx = tileId / AutoTileset.k_TilesPerSubTileset; tileHasChange = autoTile.Id != tileId; autoTile.Id = tileId; autoTile.TilesetIdx = tilesetIdx; autoTile.MappedIdx = tileId < 0 ? -1 : Tileset.AutotileIdxMap[tileId % AutoTileset.k_TilesPerSubTileset]; autoTile.TileX = gridX; autoTile.TileY = gridY; autoTile.Layer = iLayer; autoTile.Type = _GetAutoTileType(autoTile); // refresh tile and neighbours if (refreshTile && tileHasChange) { for (int xf = -1; xf < 2; ++xf) { for (int yf = -1; yf < 2; ++yf) { RefreshTile(gridX + xf, gridY + yf, iLayer); } } } } }
/// <summary> /// Copy a section of the map and use it as drawing template /// </summary> /// <param name="tile_start_x"></param> /// <param name="tile_start_y"></param> /// <param name="tile_end_x"></param> /// <param name="tile_end_y"></param> /// <param name="_dragEndTileX"></param> /// <param name="_dragEndTileY"></param> /// <param name="isCtrlKeyHold"></param> public void RefreshBrushGizmo(int tile_start_x, int tile_start_y, int tile_end_x, int tile_end_y, int _dragEndTileX, int _dragEndTileY, bool isCtrlKeyHold) { Vector2 pivot = new Vector2(0f, 1f); SpriteRenderer[] aSprites = GetComponentsInChildren <SpriteRenderer>(); int sprIdx = 0; for (int tile_x = tile_start_x; tile_x <= tile_end_x; ++tile_x) { for (int tile_y = tile_start_y; tile_y <= tile_end_y; ++tile_y) { for (int tile_layer = 0; tile_layer < MyAutoTileMap.GetLayerCount(); ++tile_layer) { if ( (isCtrlKeyHold && tile_layer == SelectedLayer) || //copy all layers over the SelectedLayer !MyAutoTileMap.MapLayers[tile_layer].Visible // skip invisible layers ) { continue; } AutoTile autoTile = MyAutoTileMap.GetAutoTile(tile_x, tile_y, tile_layer); if (autoTile != null && autoTile.TilePartsIdx != null && autoTile.Id >= 0) { for (int partIdx = 0; partIdx < autoTile.TilePartsLength; ++partIdx, ++sprIdx) { SpriteRenderer spriteRender = sprIdx < aSprites.Length? aSprites[sprIdx] : null; if (spriteRender == null) { GameObject spriteObj = new GameObject(); spriteObj.transform.parent = transform; spriteRender = spriteObj.AddComponent <SpriteRenderer>(); } spriteRender.transform.gameObject.name = "BrushGizmoPart" + sprIdx; spriteRender.sprite = Sprite.Create(MyAutoTileMap.Tileset.AtlasTexture, MyAutoTileMap.Tileset.AutoTileRects[autoTile.TilePartsIdx[partIdx]], pivot, AutoTileset.PixelToUnits); spriteRender.sortingOrder = 50; //TODO: +50 temporal? see for a const number later spriteRender.color = new Color32(192, 192, 192, 192); // get last tile as relative position int tilePart_x = (tile_x - _dragEndTileX) * 2 + partIdx % 2; int tilePart_y = (tile_y - _dragEndTileY) * 2 + partIdx / 2; float xFactor = MyAutoTileMap.CellSize.x / 2f; float yFactor = MyAutoTileMap.CellSize.y / 2f; spriteRender.transform.localPosition = new Vector3(tilePart_x * xFactor, -tilePart_y * yFactor, spriteRender.transform.position.z); spriteRender.transform.localScale = new Vector3(MyAutoTileMap.CellSize.x * AutoTileset.PixelToUnits / MyAutoTileMap.Tileset.TileWidth, MyAutoTileMap.CellSize.y * AutoTileset.PixelToUnits / MyAutoTileMap.Tileset.TileHeight, 1f); } } } } } // clean unused sprite objects while (sprIdx < aSprites.Length) { if (Application.isEditor) { DestroyImmediate(aSprites[sprIdx].transform.gameObject); } else { Destroy(aSprites[sprIdx].transform.gameObject); } ++sprIdx; } }
/// <summary> /// Set a tile in the grid coordinates specified and layer ( 0: ground, 1: overground, 2: overlay ) /// </summary> /// <param name="autoTile_x">Tile x position of the map</param> /// <param name="autoTile_y">Tile y position of the map</param> /// <param name="tileIdx">This is the index of the tile. You can see it in the editor while editing the map in the top left corner. Use -1 for an empty tile</param> /// <param name="iLayer"> Layer where to set the tile ( 0: ground, 1: overground, 2: overlay )</param> /// <param name="refreshTile">If tile and neighbors should be refreshed by this method or do it layer</param> public void SetAutoTile(int autoTile_x, int autoTile_y, int tileIdx, int iLayer, bool refreshTile = true) { if( !IsValidAutoTilePos( autoTile_x, autoTile_y ) || iLayer >= m_AutoTileLayers.Count ) { return; } tileIdx = Mathf.Clamp( tileIdx, -1, Tileset.ThumbnailRects.Count-1 ); AutoTile autoTile = m_AutoTileLayers[iLayer][autoTile_x, autoTile_y]; if( autoTile == null) { autoTile = new AutoTile(); m_AutoTileLayers[iLayer][autoTile_x, autoTile_y] = autoTile; autoTile.TilePartsType = new eTilePartType[4]; autoTile.TilePartsIdx = new int[4]; } int tilesetIdx = tileIdx / AutoTileset.k_TilesPerSubTileset; autoTile.Idx = tileIdx; autoTile.TilesetIdx = tilesetIdx; autoTile.MappedIdx = tileIdx < 0 ? -1 : Tileset.AutotileIdxMap[tileIdx % AutoTileset.k_TilesPerSubTileset]; autoTile.TileX = autoTile_x; autoTile.TileY = autoTile_y; autoTile.Layer = iLayer; autoTile.Type = _GetAutoTileType( autoTile ); // refresh tile and neighbours if (refreshTile) { for (int xf = -1; xf < 2; ++xf) { for (int yf = -1; yf < 2; ++yf) { RefreshTile(autoTile_x + xf, autoTile_y + yf, iLayer); } } } }
/// <summary> /// Reveal an area using as center, the tile over worldPosition and a sight length in tiles (sightLength) /// Uses a coroutine to make a fade transition /// </summary> /// <param name="worldPosition">The center tile</param> /// <param name="sightLength">The sight length in tiles</param> public static void RemoveFogOfWarWithFade(Vector3 worldPosition, int sightLength) { float sqrMaxDist = sightLength * sightLength * 4; int iFogLayer = AutoTileMap.Instance.MapLayers.FindIndex(x => x.LayerType == eLayerType.FogOfWar); if (iFogLayer >= 0) { AutoTile centerTile = RpgMapHelper.GetAutoTileByPosition(worldPosition, iFogLayer); for (int yf = -sightLength; yf <= sightLength; ++yf) { for (int xf = -sightLength; xf <= sightLength; ++xf) { int tx = centerTile.TileX + xf; int ty = centerTile.TileY + yf; if (AutoTileMap.Instance.IsValidAutoTilePos(tx, ty)) // This extra check is needed with fog of war layer { byte[] aFogAlpha = new byte[4]; for (int i = 0; i < aFogAlpha.Length; ++i) { //NOTE: for the fog level, each tile is divided in 4 tileparts. // The index i of each tile part is: // 0|2 // 1|3 int xf2 = 0; if (xf < 0) { xf2 = (i == 0 || i == 1) ? 2 * -xf : 2 * -xf - 1; //NOTE: 0, 1 are the left tile parts } else if (xf > 0) { xf2 = (i == 2 || i == 3) ? 2 * xf : 2 * xf - 1; } else //if( xf == 0 ) { xf2 = 0; } int yf2 = 0; if (yf < 0) { yf2 = (i == 0 || i == 2) ? 2 * -yf : 2 * -yf - 1; } else if (yf > 0) { yf2 = (i == 1 || i == 3) ? 2 * yf : 2 * yf - 1; } else //if( xf == 0 ) { yf2 = 0; } float sqrDist = xf2 * xf2 + yf2 * yf2; //NOTE: sqrDist = [0..(sightAreaSize*2)^2] float fAlphaFactor = Mathf.Clamp01(sqrDist / sqrMaxDist); fAlphaFactor *= fAlphaFactor; byte fogAlpha = (byte)(0xff * fAlphaFactor); aFogAlpha[i] = fogAlpha; } AutoTileMap.Instance.AddFogOfWarSetToQueue(tx + ty * AutoTileMap.Instance.MapTileWidth, aFogAlpha); } } } AutoTileMap.Instance.RefreshMinimapTexture(centerTile.TileX - sightLength, centerTile.TileY - sightLength, 2 * sightLength, 2 * sightLength); } }
protected void FixedUpdate() { if (m_curNode != null) { MapTileNode curTileNode = m_curNode.Value as MapTileNode; AutoTile autoTile = RpgMapHelper.GetAutoTileByPosition(transform.position, 0); if ( (autoTile.TileX != curTileNode.TileX || autoTile.TileY != curTileNode.TileY) || GetDistToTarget(autoTile) > MinDistToMoveNextTarget // wait until min dist is reached ) { Vector3 vSeek = curTileNode.Position; vSeek.z = transform.position.z; // put at this object level if (m_movingBehavior) { if (m_curNode.Next != null) { m_movingBehavior.Seek(vSeek); } else { m_movingBehavior.Arrive(vSeek); } } } else { m_curNode = m_curNode.Next; } } //if (TargetPos != null) //TODO: TargetPos can't be null { int prevTileidx = m_startTileIdx; m_startTileIdx = RpgMapHelper.GetTileIdxByPosition(TargetPos); m_isUpdatePath |= prevTileidx != m_startTileIdx; if (m_isUpdatePath)//|| !m_isComputing) //Removed to keep actor moving until reach the center of the node { m_isUpdatePath = false; m_endTileIdx = RpgMapHelper.GetTileIdxByPosition(transform.position); //now = Time.realtimeSinceStartup; StopCoroutine("ComputePath"); StartCoroutine("ComputePath"); } } //+++ Debug for (LinkedListNode <IPathNode> it = Path.First; it != null; it = it.Next) { MapTileNode mapTileNode0 = it.Value as MapTileNode; if (it.Next != null) { MapTileNode mapTileNode1 = it.Next.Value as MapTileNode; Vector3 v0 = mapTileNode0.Position; Vector3 v1 = mapTileNode1.Position; v0.z = transform.position.z; v1.z = transform.position.z; Debug.DrawLine(v0, v1, Color.red); } } //--- }
/// <summary> /// Save the map configuration /// </summary> /// <param name="_autoTileMap"></param> /// <returns></returns> public bool SaveData(AutoTileMap _autoTileMap, int width = -1, int height = -1) { if (width < 0) { width = TileMapWidth; } if (height < 0) { height = TileMapHeight; } // avoid clear map data when auto tile map is not initialized if (!_autoTileMap.IsInitialized) { //Debug.LogError(" Error saving data. Autotilemap is not initialized! Map will not be saved. "); return(false); } Metadata.version = k_version; TileData.Clear(); for (int iLayer = 0; iLayer < _autoTileMap.GetLayerCount(); ++iLayer) { AutoTileMap.MapLayer mapLayer = _autoTileMap.MapLayers[iLayer]; List <int> tileData = new List <int>(width * height); int iTileRepetition = 0; int savedTileId = 0; int mapWidth = _autoTileMap.MapTileWidth; int mapHeight = _autoTileMap.MapTileHeight; for (int tile_y = 0; tile_y < height; ++tile_y) { for (int tile_x = 0; tile_x < width; ++tile_x) { int iType = -1; if (tile_x < mapWidth && tile_y < mapHeight) { AutoTile autoTile = _autoTileMap.TileLayers[_autoTileMap.MapLayers[iLayer].TileLayerIdx][tile_x + tile_y * mapWidth]; iType = autoTile != null? autoTile.Id : -1; } //+++fix: FogOfWar tiles could be < -1, and this is not good for compress system, excepting ids >= -1 if (mapLayer.LayerType == eLayerType.FogOfWar) { iType = ((iType >> 1) & 0x7FFFFFFF); // remove the last bit of the last byte. Will be << 1 later when loading } //--- if (iTileRepetition == 0) { savedTileId = iType; iTileRepetition = 1; } else { // compression data. All tiles of the same type are store with number of repetitions ( negative number ) and type // ex: 5|5|5|5 --> |-4|5| (4 times 5) ex: -1|-1|-1 --> |-3|-1| ( 3 times -1 ) if (iType == savedTileId) { ++iTileRepetition; } else { if (iTileRepetition > 1) { tileData.Add(-iTileRepetition); // save number of repetition with negative sign } if (savedTileId < -1) { Debug.LogError(" Wrong tile id found when compressing the tile layer " + mapLayer.Name); savedTileId = -1; } tileData.Add(savedTileId); savedTileId = iType; iTileRepetition = 1; } } } } // save last tile type found if (iTileRepetition > 1) { tileData.Add(-iTileRepetition); } tileData.Add(savedTileId); // TileData.Add(new TileLayer() { Tiles = tileData, Depth = mapLayer.Depth, LayerType = mapLayer.LayerType, SortingLayer = mapLayer.SortingLayer, SortingOrder = mapLayer.SortingOrder, Name = mapLayer.Name, Visible = mapLayer.Visible }); } TileMapWidth = width; TileMapHeight = height; return(true); }
//float now; float GetDistToTarget( AutoTile targetTile ) { Vector3 target = RpgMapHelper.GetTileCenterPosition(targetTile.TileX, targetTile.TileY); target.z = transform.position.z; return (transform.position - target).magnitude; }