Exemplo n.º 1
0
 public UndoAction(int layer, TileCell to, TileCell from, Point p)
 {
     LayerID  = layer;
     CellTo   = (TileCell)to.Clone();
     CellFrom = (TileCell)from.Clone();
     Point    = p;
 }
Exemplo n.º 2
0
        private MapEntity[,] ObtainEntitiesFromLayer(string objectGroupName)
        {
            MapEntity[,] entityGrid = new MapEntity[tmxMap.Width, tmxMap.Height];

            //Handle the Entities Layer
            foreach (TmxObject currentObject in tmxMap.ObjectGroups[objectGroupName].Objects)
            {
                for (int row = 0; row < tmxMap.Height; row++)
                {
                    for (int col = 0; col < tmxMap.Width; col++)
                    {
                        //NOTE: For some reason, ObjectLayer objects in Tiled measure Y-axis from the bottom of the tile.c Compensate in the calculation here.
                        if ((col * GameDriver.CellSize) == (int)currentObject.X &&
                            (row * GameDriver.CellSize) == ((int)currentObject.Y - GameDriver.CellSize))
                        {
                            int objectTileId = currentObject.Tile.Gid;
                            if (objectTileId != 0)
                            {
                                Dictionary <string, string> currentProperties =
                                    GetDefaultPropertiesAndOverrides(currentObject);
                                TileCell tileCell = new TileCell(mapSprite, GameDriver.CellSize, objectTileId);

                                entityGrid[col, row] = new MapEntity(currentObject.Name, currentObject.Type, tileCell,
                                                                     new Vector2(col, row), currentProperties);
                            }
                        }
                    }
                }
            }

            return(entityGrid);
        }
Exemplo n.º 3
0
    public void LandChunkGen(int chunkSize, bool firstChunk)
    {
        searchFrontierPhase += 1;
        TileCell firstCell = firstChunk ? FindRandom(.75f) : GetWaterCellWithinChunk();

        firstCell.SearchPhase     = searchFrontierPhase;
        firstCell.Distance        = 0;
        firstCell.SearchHeuristic = 0;
        searchFrontier.Enqueue(firstCell);
        TileCoordinates center = firstCell.coordinates;
        int             size   = 0;

        while (size < chunkSize && searchFrontier.Count > 0)
        {
            TileCell current = searchFrontier.Dequeue();
            if (current.type == TerrainType.Ocean)
            {
                landCells.Add(current);
                current.type = TerrainType.Grassland;
                size        += 1;
                foreach (TileCell neighbor in current.neighbors)
                {
                    if (neighbor.SearchPhase < searchFrontierPhase)
                    {
                        neighbor.SearchPhase     = searchFrontierPhase;
                        neighbor.Distance        = (int)neighbor.coordinates.DistanceTo(center);
                        neighbor.SearchHeuristic =
                            Random.value < jitterProbability ? 1 : 0;
                        searchFrontier.Enqueue(neighbor);
                    }
                }
            }
        }
        searchFrontier.Clear();
    }
Exemplo n.º 4
0
    public void CreateMapObject(int tileX, int tileY, eMapObjectType type, string name)
    {
        TileSystem tileSystem = TileSystem.Instance;
        GameObject prefab     = GetPrefabByType(type);
        GameObject go         = Instantiate(prefab);

        go.InitTransformAsChild(tileSystem.GetTilemap(eTilemapType.GROUND).transform);
        eTileLayer layer     = eTileLayer.NONE;
        MapObject  mapObject = null;

        switch (type)
        {
        case eMapObjectType.ITEM:
            ItemObject itemObject = go.AddComponent <ItemObject>();
            Item       item       = Resources.Load <Item>("Items/" + name);
            itemObject.Init(item);

            mapObject = itemObject;
            layer     = eTileLayer.ITEM;
            break;
        }

        TileCell tileCell = tileSystem.GetTileCell(tileX, tileY);

        if (null != tileCell)
        {
            tileCell.SetObject(mapObject, layer);
        }
    }
Exemplo n.º 5
0
    public override void Start()
    {
        base.Start();

        _targetTileCell = _character.GetTargetTileCell();
        _updateState    = eUpdateState.PATHFINDING;

        _reverseTileCell = null;

        if (_targetTileCell != null)
        {
            GameManager.Instance.GetMap().ResetPathFinding();
            TileCell startTileCell = GameManager.Instance.GetMap().GetTileCell(_character.GetTileX(), _character.GetTileY());

            sPathCommand command;
            command.tileCell  = startTileCell;
            command.heuristic = 0.0f;
            PushCommand(command);
        }

        else
        {
            _nextState = eStateType.IDLE;
        }
    }
Exemplo n.º 6
0
    private List <VirtualTile> GenerateTileCache(TileCell[] map)
    {
        List <VirtualTile> Cache = new List <VirtualTile>();

        for (int i = map.Length - 1; i >= 0; i--)
        {
            TileCell currentTile = map[i];
            TileCell northTile   = map.FirstOrDefault(tile => tile.x == currentTile.x && tile.y == currentTile.y - 1);
            TileCell southTile   = map.FirstOrDefault(tile => tile.x == currentTile.x && tile.y == currentTile.y + 1);
            TileCell eastTile    = map.FirstOrDefault(tile => tile.x == currentTile.x + 1 && tile.y == currentTile.y);
            TileCell westTile    = map.FirstOrDefault(tile => tile.x == currentTile.x - 1 && tile.y == currentTile.y);

            MAP_CELL_TYPE n = northTile == null || northTile.t == MAP_CELL_TYPE.BLOCKED ? MAP_CELL_TYPE.BLOCKED : MAP_CELL_TYPE.WALKABLE;
            MAP_CELL_TYPE s = southTile == null || southTile.t == MAP_CELL_TYPE.BLOCKED ? MAP_CELL_TYPE.BLOCKED : MAP_CELL_TYPE.WALKABLE;
            MAP_CELL_TYPE e = eastTile == null || eastTile.t == MAP_CELL_TYPE.BLOCKED ? MAP_CELL_TYPE.BLOCKED : MAP_CELL_TYPE.WALKABLE;
            MAP_CELL_TYPE w = westTile == null || westTile.t == MAP_CELL_TYPE.BLOCKED ? MAP_CELL_TYPE.BLOCKED : MAP_CELL_TYPE.WALKABLE;

            VirtualTile vTile = new VirtualTile()
            {
                x     = currentTile.x,
                y     = currentTile.y,
                type  = currentTile.t,
                score = Helpers.GetTileScore(
                    n != MAP_CELL_TYPE.BLOCKED,
                    e != MAP_CELL_TYPE.BLOCKED,
                    s != MAP_CELL_TYPE.BLOCKED,
                    w != MAP_CELL_TYPE.BLOCKED
                    )
            };

            Cache.Add(vTile);
        }

        return(Cache);
    }
Exemplo n.º 7
0
    // Actions
    public void MoveTileCell(TileCell tileCell)
    {
        ResetMoveCooltime();

        TileMap map = GameManager.Instance.GetMap();

        int moveX = tileCell.GetTileX();
        int moveY = tileCell.GetTileY();

        map.ResetObject(_tileX, _tileY, this);
        _tileX = moveX;
        _tileY = moveY;
        map.SetObject(_tileX, _tileY, this, eTileLayer.MIDDLE);
        //pick message 주기
        List <MapObject> mapObejctList = map.GetTileCell(_tileX, _tileY).GetMapObjectList(eTileLayer.MIDDLE);

        for (int i = 0; i < mapObejctList.Count; i++)
        {
            MessageParam msgParam = new MessageParam();
            msgParam.sender   = this;
            msgParam.receiver = mapObejctList[i];
            msgParam.message  = "pick";

            MessageSystem.Instance.Send(msgParam);
        }
    }
Exemplo n.º 8
0
    private void SetCellSprite(FlatTriGrid <int> vertexGrid, PointyHexPoint point, TileCell tileCell)
    {
        var cell = (SpriteCell)tileCell;

        var vertices =
            from vertexPoint in point.GetVertices()
            select vertexGrid [vertexPoint];

        int   imageIndex = vertices.Reverse().Aggregate((x, y) => (x << 1) + y);
        float zRotation  = 30;

        for (int i = 0; i < 6; i++)
        {
            if (frameIndices [imageIndex] != -1)
            {
                cell.FrameIndex = frameIndices [imageIndex];
                cell.transform.SetRotationZ(zRotation);

                break;
            }

            zRotation += 60;
            imageIndex = RotateEdgeNumberClockWise(imageIndex);
        }
    }
Exemplo n.º 9
0
    public override void Update()
    {
        if (_character.EmptyPathFindingTileCell() == false)
        {
            TileCell tileCell = _character.PopPathFindingTileCell();

            sPosition curPosition;
            curPosition.x = _character.GetTileX();
            curPosition.y = _character.GetTileY();

            sPosition nextPosition;
            nextPosition.x = tileCell.GetTileX();
            nextPosition.y = tileCell.GetTileY();

            eMoveDirection direction = GetDirection(curPosition, nextPosition);
            _character.SetNextDirection(direction);

            if ((_character.MoveStart(tileCell.GetTileX(), tileCell.GetTileY()) == false))
            {
                //_nextState = eStateType.ATTACK;
                _nextState = eStateType.DISCOVER;
            }
        }

        else
        {
            _nextState = eStateType.IDLE;
        }
    }
Exemplo n.º 10
0
    override public void Update()
    {
        TileCell goalTileCell = _character.GetGoalTileCell();

        if (null != goalTileCell)
        {
            _nextState = eStateType.PATHFINDING;
            return;
        }

        if (Input.GetMouseButtonDown(0))
        {
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 100))
            {
                MapObject mapObject = hit.collider.gameObject.GetComponent <MapObject>();
                if (null != mapObject)
                {
                    if (eMapObjectType.TILE_OBJECT == mapObject.GetObjectType())
                    {
                        //hit.collider.gameObject.GetComponent<SpriteRenderer>().color = Color.blue;
                        _character.ShowMoveCursor(hit.collider.gameObject.transform.position);

                        TileCell selectTileCell = GameManager.Instance.GetMap().GetTileCell(mapObject.GetTileX(), mapObject.GetTileY());
                        if (true == selectTileCell.IsPathfindable())
                        {
                            _character.SetGoalTileCell(selectTileCell);
                        }
                    }
                }
            }
        }
    }
Exemplo n.º 11
0
    protected void SettingMovePossibleTiles()
    {
        TileMap map = GameManager.Instance.GetMap();

        map.ResetVisit(_character);

        TileCell startTileCell = map.GetTileCell(_character.GetTileX(), _character.GetTileY());

        startTileCell.SetPrevTileCell(_character, null);
        sTileHeuristicInfo startCmd;

        startCmd.tileCell  = startTileCell;
        startCmd.heuristic = 0.0f;
        _tileInfoQueue.Add(startCmd);

        while (0 != _tileInfoQueue.Count)
        {
            sTileHeuristicInfo command = _tileInfoQueue[0];
            _tileInfoQueue.RemoveAt(0);
            //가져온 커맨드의 현재 타일셀 방문 표시
            if (false == command.tileCell.IsVisited(_character))
            {
                if (_character.GetMoveRange() == command.tileCell.GetDistanceFromStart(_character))
                {
                    _tileInfoQueue.Clear();
                    return;
                }
                command.tileCell.SetVisit(_character, true);
                _movePossibleTiles.Add(command.tileCell);

                //4방향 next타일들 검사
                for (int direction = (int)eMoveDirection.LEFT; direction < (int)eMoveDirection.DOWN + 1; direction++)
                {
                    sPosition curPosition;
                    curPosition.x = command.tileCell.GetTileX();
                    curPosition.y = command.tileCell.GetTileY();
                    sPosition nextPosition = _character.GetPositionByDirection(curPosition, direction);

                    TileCell nextTileCell = map.GetTileCell(nextPosition.x, nextPosition.y);
                    // nextTileCell 방문 안했고, 움직일수 있는 타일일때
                    if (null != nextTileCell && true == nextTileCell.IsPathfindable() && false == nextTileCell.IsVisited(_character))
                    {
                        float distanceFromStart = command.tileCell.GetDistanceFromStart(_character) + nextTileCell.GetDistanceFromWeight();
                        float heuristic         = distanceFromStart;

                        if (null == nextTileCell.GetPrevTileCell(_character) || distanceFromStart < nextTileCell.GetDistanceFromStart(_character))
                        {
                            nextTileCell.SetDistanceFromStart(_character, distanceFromStart);
                            nextTileCell.SetPrevTileCell(_character, command.tileCell);

                            sTileHeuristicInfo nextCommand;
                            nextCommand.tileCell  = nextTileCell;
                            nextCommand.heuristic = heuristic;
                            PushSortmoveRangeQueue(nextCommand);
                        }
                    }
                }
            }
        }
    }
Exemplo n.º 12
0
    public void Init()
    {
        if (_tilemaps.Count > 0)
        {
            _tilemaps.Clear();
        }

        _tileCellList = new TileCell[_height, _width];
        for (int y = 0; y < _height; y++)
        {
            for (int x = 0; x < _width; x++)
            {
                _tileCellList[y, x] = new TileCell();
            }
        }
        GameObject rootScene  = GameObject.Find("MainGameScene");
        Transform  gridObject = rootScene.transform.Find("Grid");

        _grid = gridObject.GetComponent <Grid>();
        Tilemap[] maps = gridObject.GetComponentsInChildren <Tilemap>();
        for (int i = 0; i < maps.Length; i++)
        {
            eTilemapType type = (eTilemapType)i;
            _tilemaps.Add(type, maps[i]);
        }

        InitTileCell();
    }
Exemplo n.º 13
0
    bool CheckPrecondition(eFindMode mode, TileCell nextTileCell, TileCell targetTileCell)
    {
        //if ((null != nextTileCell) && (true == nextTileCell.IsPathfindable() && false == nextTileCell.IsVisit() ||
        //    (nextTileCell.GetTileX() == _targetTileCell.GetTileX() && nextTileCell.GetTileY() == _targetTileCell.GetTileY())))
        //    return true;

        bool condition = false;

        if (false == nextTileCell.IsVisit())
        {
            condition = true;
        }

        if (eFindMode.VIEW_MOVERANGE == mode || eFindMode.FIND_PATH == mode)
        {
            if (condition)
            {
                condition = nextTileCell.CanMove();
            }
        }

        if (eMapType.DUNGEON == GameManager.Instance.GetMapType())
        {
            return(condition);
        }
        else if (eMapType.TOWN == GameManager.Instance.GetMapType())
        {
            //해당 타일이 목표 타일
            if (nextTileCell.GetTileX() == targetTileCell.GetTileX() && nextTileCell.GetTileY() == targetTileCell.GetTileY())
            {
                condition = true;
            }
        }
        return(condition);
    }
Exemplo n.º 14
0
        private MapCursor BuildMapCursor(ITexture2D cursorTexture)
        {
            TileCell cursorCell          = new TileCell(cursorTexture, GameDriver.CellSize, 1);
            Vector2  cursorStartPosition = new Vector2(0);

            return(new MapCursor(cursorCell, cursorStartPosition, MapSize()));
        }
Exemplo n.º 15
0
    public int TravelCost(TileCell a, TileCell b)
    {
        int x         = Mathf.Abs(a.x - b.x);
        int y         = Mathf.Abs(a.y - b.y);
        int remaining = Mathf.Abs(x - y);

        return(14 * Mathf.Min(x, y) + 10 * remaining);
    }
Exemplo n.º 16
0
    //TileCell _prevTileCell;
    public void SetPrevTileCell(Character character, TileCell prevTileCell)
    {
        //_prevTileCell = prevTileCell;
        sTilePathFindInfo tilePathFindInfo = _pathFindInfo[character];

        tilePathFindInfo.prevTileCell = prevTileCell;
        _pathFindInfo[character]      = tilePathFindInfo;
    }
Exemplo n.º 17
0
 public bool IsClickedCharacter(TileCell tileCell)
 {
     if (_tileX == tileCell.GetTileX() && _tileY == tileCell.GetTileY())
     {
         return(true);
     }
     return(false);
 }
Exemplo n.º 18
0
 public void PlaceZone(TileCell cell, Zones zone)
 {
     cell.zone = zone;
     if (!ZoneCells.ContainsKey(zone))
     {
         ZoneCells.Add(zone, new List <TileCell>());
     }
     ZoneCells[zone].Add(cell);
 }
Exemplo n.º 19
0
    TileCell GetWaterCellWithinChunk()
    {
        //get cells with water neighbors
        var      availableCells    = landCells.Where(p => p.neighbors.Any(p2 => p2.type == TerrainType.Ocean) && p.x > -(MapWidth / 2) * border && p.x <(MapWidth / 2) * border && p.y> -(MapHeight / 2) * border && p.y < (MapHeight / 2) * border).ToList();
        TileCell choosenCell       = availableCells[Random.Range(0, availableCells.Count())];
        TileCell neighborWaterCell = choosenCell.neighbors.First(p2 => p2.type == TerrainType.Ocean);

        return(neighborWaterCell);
    }
Exemplo n.º 20
0
 public void BuildPath()
 {
     //경로를 도출
     while (null != _reverseTileCell)
     {
         _character.PushPathTileCell(_reverseTileCell);
         _reverseTileCell = _reverseTileCell.GetPrevTileCell();
     }
 }
Exemplo n.º 21
0
    public void SetTargetTileCell(TileCell targetTileCell)
    {
        _targetTileCell = targetTileCell;

        if (_targetTileCell.GetTileX() != this.GetTileX() && _targetTileCell.GetTileY() != this.GetTileY())
        {
            SetPathFindingType(ePathFindingType.ASTAR);
        }
    }
Exemplo n.º 22
0
    float CalcEuclideanDistance(TileCell tileCell, TileCell targetCell)
    {
        int distanceW = targetCell.GetTileX() - tileCell.GetTileX();
        int distanceH = targetCell.GetTileY() - tileCell.GetTileY();

        distanceW *= distanceW;
        distanceH *= distanceH;

        return(Mathf.Sqrt(distanceW + distanceH));
    }
Exemplo n.º 23
0
    float CalcComplexHeuristic(TileCell nextTileCell, TileCell targetTileCell)
    {
        int distanceW = nextTileCell.GetTileX() - targetTileCell.GetTileX();
        int distanceH = nextTileCell.GetTileY() - targetTileCell.GetTileY();

        distanceW = distanceW * distanceW;
        distanceH = distanceH * distanceH;

        return((float)(distanceW + distanceH));
    }
Exemplo n.º 24
0
 void InitMove()
 {
     _pathStack = _character.GetPathStack();
     if (0 != _pathStack.Count)
     {
         _pathTargetCell = _pathStack.Pop();
         _direction      = TileHelper.GetDirectionVector(_character.GetCurrentTileCell(), _pathTargetCell);
         //if (_direction.Equals(Vector2Int.zero))
         //	Debug.Log("ZERO DIRECTION");
     }
 }
Exemplo n.º 25
0
 public bool CheckRange(TileCell tileCell)
 {
     for (int i = 0; i < _rangeTile.Count; i++)
     {
         if (_rangeTile[i] == tileCell)
         {
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 26
0
    public override void UpdateCharacter()
    {
        base.UpdateCharacter();

        if (Input.GetMouseButtonDown(0))
        {
            Vector2      worldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Ray2D        ray           = new Ray2D(worldPosition, Vector2.zero);
            RaycastHit2D hit           = Physics2D.Raycast(ray.origin, ray.direction);

            if (null != hit.collider)
            {
                if (EventSystem.current.IsPointerOverGameObject())
                {
                    Debug.Log("Clicked on the UI");
                    return;
                }

                TileObject hitTile = hit.transform.GetComponent <TileObject>();
                int        tileX   = hitTile.GetTileX();
                int        tileY   = hitTile.GetTileY();

                TileCell hitCell = GameManager.Instance.GetMap().GetTileCell(tileX, tileY);
                if (null == hitCell)
                {
                    return;
                }

                if (eMapType.TOWN == GameManager.Instance.GetMapType())
                {
                    SetTargetTileCell(tileX, tileY);
                    ChangeState(eStateType.PATHFINDING);
                }
                else if (eMapType.DUNGEON == GameManager.Instance.GetMapType())
                {
                    if (false == IsMenuOn() && IsClickedCharacter(hitCell))
                    {
                        OpenBattleMenu();
                    }
                    else
                    {
                        SetTargetTileCell(tileX, tileY);
                    }
                }
            }
        }

        if (Input.GetKeyDown(KeyCode.F1))
        {
            DataManager.Instance.SaveCharacter(GetCharacterInfo());
            SceneManager.LoadScene("Map01");
        }
    }
Exemplo n.º 27
0
 virtual protected void UpdateState()
 {
     if (_character.IsActionPossible())
     {
         SettingMovePossibleTiles();  //_movePossibleTiles 구하는 용
         int      targetIndex    = Random.Range(0, _movePossibleTiles.Count);
         TileCell targetTileCell = _movePossibleTiles[targetIndex];
         _character.SetTargetTileCell(targetTileCell);
         SettingTilePath();  //target Astar 탐색용
         _nextState = eStateType.MOVE;
     }
 }
Exemplo n.º 28
0
    public TileCell GetTileUnderPoint(Vector3 position)
    {
        TileCell cellUnderPoint = null;
        // Do a raycast from the position and get the tile that hit. Expensive? Maybe. Optimize later.
        RaycastHit hitInfo;

        if (Physics.Raycast(position, Vector3.down, out hitInfo, 10, 1 << m_tileLayer))
        {
            cellUnderPoint = hitInfo.collider.GetComponent <TileCell>();
        }
        return(cellUnderPoint);
    }
Exemplo n.º 29
0
    float CalcComplexHeuristic(TileCell nextTileCell, TileCell targetTileCell)
    {
        int deltaX = nextTileCell.GetTileX() - targetTileCell.GetTileX();

        deltaX = deltaX * deltaX;

        int deltaY = nextTileCell.GetTileY() - targetTileCell.GetTileY();

        deltaY = deltaY * deltaY;

        return((float)(deltaX + deltaY));
    }
Exemplo n.º 30
0
    public void DiscoverArea(TileCell cell, int radius, bool lookForCity = true)
    {
        var cellsInRadius = CellsInRadius(cell, radius);

        foreach (var discoverCell in cellsInRadius)
        {
            Destroy(discoverCell.Fog);
            discoverCell.fogged = false;
        }
        Destroy(cell.Fog);
        cell.fogged = false;
    }