示例#1
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);
        }
    }
示例#2
0
    public Character CreateCharacter(int tileX, int tileY, string scriptName, string resourceName)
    {
        TileSystem tileSystem = TileSystem.Instance;

        string filePath = "Prefabs/Character/" + resourceName;

        GameObject charPrefabs     = Resources.Load <GameObject>(filePath);
        GameObject characterObject = Instantiate(charPrefabs);

        characterObject.InitTransformAsChild(tileSystem.GetTilemap(eTilemapType.GROUND).transform);

        Vector3 position = characterObject.transform.position;

        characterObject.transform.position   = position;
        characterObject.transform.localScale = new Vector3(1.3f, 1.3f, 1.3f);

        Character character = null;

        switch (scriptName)
        {
        case "Player":
            character = characterObject.AddComponent <Player>();
            character._playerController = characterObject.AddComponent <PlayerController>();
            character.name = "Player";
            break;

        case "Enemy":
            character      = characterObject.AddComponent <Enemy>();
            character.name = "Enemy";
            break;

        default:
            break;
        }
        var spriteRenderers = character.GetComponentsInChildren <SpriteRenderer>(true);

        foreach (var spriteRenderer in spriteRenderers)
        {
            spriteRenderer.sortingOrder = tileSystem.GetTileCell(tileX, tileY).GetGroundLayerOrder();
        }
        tileSystem.GetTileCell(tileX, tileY).SetObject(character, eTileLayer.GROUND);

        Debug.Log("SPAWN::Create " + character.name);
        return(character);
    }
示例#3
0
    public void UpdatePosition(Vector2 position)
    {
        float   speed       = _status.speed + GetCurrentTileCell().GetProperties(eTileLayer.GROUND).speed;
        Vector2 destination = (Vector2)(_transform.position) + (position.normalized * speed * Time.deltaTime);

        TileSystem     tileSystem        = TileSystem.Instance;
        TileCell       curTileCell       = GetCurrentTileCell();
        eTileDirection boundaryDirection = curTileCell.CheckTileBoundary(destination);
        sTilePosition  nextTilePos       = new sTilePosition(_tileX, _tileY);

        TileHelper.GetNextTilePosByTileDirection(boundaryDirection, ref nextTilePos);

        if (tileSystem.CanMoveTileCell(nextTilePos.tileX, nextTilePos.tileY))
        {
            //타일 오프셋에 따른 캐릭터 y값 보정(des = des + (next.offset - cur.offset))
            if (eTileDirection.IN_TILE != boundaryDirection)
            {
                int layerOrder = tileSystem.GetTileCell(nextTilePos.tileX, nextTilePos.tileY).GetGroundLayerOrder();
                var sprites    = GetComponentsInChildren <SpriteRenderer>(true);
                foreach (var spriteRenderer in sprites)
                {
                    spriteRenderer.sortingOrder = layerOrder;
                }

                float curOffset  = curTileCell.GetOffset();
                float nextOffset = tileSystem.GetTileCell(nextTilePos.tileX, nextTilePos.tileY).GetOffset();
                destination.y = destination.y + (nextOffset - curOffset);

                var layer = GetCurrentLayer();
                tileSystem.GetTileCell(_tileX, _tileY).RemoveObject(this, layer);
                tileSystem.GetTileCell(nextTilePos).AddObject(this, layer);
            }

            _tileX = nextTilePos.tileX;
            _tileY = nextTilePos.tileY;

            //z값 -1 : 동일한 레이어 + 동일한 order 에서 캐릭터들이 타일보다 뒤에 있으면 안됨
            transform.position = new Vector3(destination.x, destination.y, -1.0f);
        }
    }
示例#4
0
    void FindPath()
    {
        TileSystem tileSystem = TileSystem.Instance;

        while (0 != _pathfindingQueue.Count)
        {
            //제일 앞 타일을 하나 꺼낸다.
            sPathCommand command = _pathfindingQueue[0];
            _pathfindingQueue.RemoveAt(0);

            //방문한 타일인가?
            if (false == command.tileCell.IsVisit())
            {
                command.tileCell.Visit();

                if (_targetTileCell.GetTilePosition().Equals(command.tileCell.GetTilePosition()))
                {
                    _reverseTileCell = _targetTileCell;
                    return;
                }

                for (int direction = 0; direction < (int)eDirection.MAX; direction++)
                {
                    sTilePosition nextTilePos = new sTilePosition(command.tileCell.GetTileX(), command.tileCell.GetTileY());
                    TileHelper.GetNextTilePosByDirection((eDirection)direction, ref nextTilePos);
                    TileCell nextTileCell = tileSystem.GetTileCell(nextTilePos);

                    if (((true == tileSystem.CanMoveTileCell(nextTilePos.tileX, nextTilePos.tileY)) && false == nextTileCell.IsVisit()) ||
                        (nextTilePos.tileX == _targetTileCell.GetTileX() && nextTilePos.tileY == _targetTileCell.GetTileY()))
                    {
                        float newDistanceFromStart = command.tileCell.GetDistanceFromStart() + command.tileCell.GetDistanceWeight();
                        float newHeuristic         = CalcAstarHeuristic(newDistanceFromStart, nextTileCell, _targetTileCell);

                        if (null == nextTileCell.GetPrevCell())
                        {
                            nextTileCell.SetDistanceFromStart(newDistanceFromStart);
                            nextTileCell.SetPrevCell(command.tileCell);                                 //이전 타일 기억

                            sPathCommand newCommand = new sPathCommand();
                            newCommand.heuristic = newHeuristic;
                            newCommand.tileCell  = nextTileCell;
                            PushCommand(newCommand);
                        }
                    }
                }
            }
        }
    }