Esempio n. 1
0
    private static void Spawn(MonStat monStat, int x, int y, int level, Transform root)
    {
        if (!CollisionMap.Passable(new Vector2i(x, y) * Iso.SubTileCount, monStat.ext.sizeX))
        {
            return;
        }

        int count = Random.Range(monStat.minGrp, monStat.maxGrp + 1);

        for (int i = 0; i < count; ++i)
        {
            var mon = World.SpawnMonster(monStat, Iso.MapTileToWorld(x, y), root);
            mon.level = level;
        }

        if (monStat.minion1 != null)
        {
            int minionCount = Random.Range(monStat.partyMin, monStat.partyMax);
            for (int i = 0; i < minionCount; ++i)
            {
                var mon = World.SpawnMonster(monStat.minion1, Iso.MapTileToWorld(x, y), root);
                mon.level = level;
            }
        }
    }
Esempio n. 2
0
        private static void ApplyTileCollisions(DT1.Tile tile, int x, int y)
        {
            var pos = Iso.MapTileToWorld(x, y);
            var collisionMapOffset = Iso.Snap(Iso.MapToIso(pos));
            int flagIndex          = 0;

            DT1.BlockFlags mask = DT1.BlockFlags.Walk | DT1.BlockFlags.PlayerWalk;
            for (int dy = 2; dy > -3; --dy)
            {
                for (int dx = -2; dx < 3; ++dx, ++flagIndex)
                {
                    Vector2i        subCellPos    = collisionMapOffset + new Vector2i(dx, dy);
                    bool            passable      = (tile.flags[flagIndex] & mask) == 0;
                    CollisionLayers blockedLayers = passable ? CollisionLayers.None : CollisionLayers.Walk;
                    if (tile.orientation == 0)
                    {
                        CollisionMap.SetBlocked(subCellPos, blockedLayers);
                    }
                    else if (CollisionMap.Passable(subCellPos, CollisionLayers.Walk) && !passable)
                    {
                        CollisionMap.SetBlocked(subCellPos, blockedLayers);
                    }
                }
            }
        }
Esempio n. 3
0
    static private void StepTo(Node node)
    {
        CollisionLayers collisionMask = CollisionLayers.Walk;
        Node            newNode       = null;

        int dirStart;
        int dirEnd;

        if (node.directionIndex == -1)
        {
            dirStart = 0;
            dirEnd   = 8;
        }
        else if (node.directionIndex % 2 == 0)
        {
            dirStart = ((node.directionIndex - 1) + 8) % 8;
            dirEnd   = dirStart + 3;
        }
        else
        {
            dirStart = ((node.directionIndex - 2) + 8) % 8;
            dirEnd   = dirStart + 5;
        }

        for (int i = dirStart; i < dirEnd; ++i)
        {
            int      dir      = i % 8;
            Vector2i pos      = node.pos + directions[dir];
            bool     passable = CollisionMap.Passable(pos, collisionMask, size: size, ignore: self);

            if (passable)
            {
                if (newNode == null)
                {
                    newNode = Node.Get();
                }
                newNode.pos = pos;

                bool closed = closeNodes.Contains(newNode);
                if (!closed)
                {
                    newNode.parent         = node;
                    newNode.gScore         = node.gScore + 1;
                    newNode.hScore         = Vector2i.manhattanDistance(target, newNode.pos);
                    newNode.score          = newNode.gScore + newNode.hScore;
                    newNode.directionIndex = dir;
                    openNodes.Add(newNode);
                    closeNodes.Add(newNode);
                    newNode = null;
                }
            }
        }

        if (newNode != null)
        {
            newNode.Recycle();
        }
    }
Esempio n. 4
0
    private void DrawDebugCellGrid()
    {
        Color    occupiedColor = new Color(1, 0, 0, 0.3f);
        Color    passableColor = new Color(1, 1, 1, 0.03f);
        Vector2i origin        = Iso.Snap(Iso.MapToIso(Camera.main.transform.position));
        int      debugWidth    = 100;
        int      debugHeight   = 100;

        origin.x -= debugWidth / 2;
        origin.y -= debugHeight / 2;
        for (int y = 0; y < debugHeight; ++y)
        {
            for (int x = 0; x < debugWidth; ++x)
            {
                var   pos      = origin + new Vector2i(x, y);
                bool  passable = CollisionMap.Passable(pos, layers);
                Color color    = passable ? passableColor : occupiedColor;
                Iso.DebugDrawTile(pos, color, 0.9f);
            }
        }
    }
Esempio n. 5
0
    static Renderer CreateTile(DT1.Tile tile, int x, int y, int orderInLayer = 0, Transform parent = null)
    {
        var texture = tile.texture;
        var pos     = Iso.MapTileToWorld(x, y);

        GameObject gameObject = new GameObject();

        gameObject.name = tile.mainIndex + "_" + tile.subIndex + "_" + tile.orientation;
        gameObject.transform.position = pos;
        if (parent)
        {
            gameObject.transform.SetParent(parent);
        }
        var   meshRenderer = gameObject.AddComponent <MeshRenderer>();
        var   meshFilter   = gameObject.AddComponent <MeshFilter>();
        Mesh  mesh         = new Mesh();
        float x0           = tile.textureX;
        float y0           = tile.textureY;
        float w            = tile.width / Iso.pixelsPerUnit;
        float h            = (-tile.height) / Iso.pixelsPerUnit;

        if (tile.orientation == 0 || tile.orientation == 15)
        {
            var topLeft = new Vector3(-1f, 0.5f);
            if (tile.orientation == 15)
            {
                topLeft.y += tile.roofHeight / Iso.pixelsPerUnit;
            }
            mesh.vertices = new Vector3[] {
                topLeft,
                topLeft + new Vector3(0, -h),
                topLeft + new Vector3(w, -h),
                topLeft + new Vector3(w, 0)
            };
            mesh.triangles = new int[] { 2, 1, 0, 3, 2, 0 };
            mesh.uv        = new Vector2[] {
                new Vector2(x0 / texture.width, -y0 / texture.height),
                new Vector2(x0 / texture.width, (-y0 + tile.height) / texture.height),
                new Vector2((x0 + tile.width) / texture.width, (-y0 + tile.height) / texture.height),
                new Vector2((x0 + tile.width) / texture.width, -y0 / texture.height)
            };

            meshRenderer.sortingLayerName = tile.orientation == 0 ? "Floor" : "Roof";
            meshRenderer.sortingOrder     = orderInLayer;
        }
        else
        {
            var topLeft = new Vector3(-1f, h - 0.5f);
            mesh.vertices = new Vector3[] {
                topLeft,
                topLeft + new Vector3(0, -h),
                topLeft + new Vector3(w, -h),
                topLeft + new Vector3(w, 0)
            };
            mesh.triangles = new int[] { 2, 1, 0, 3, 2, 0 };
            mesh.uv        = new Vector2[] {
                new Vector2(x0 / texture.width, (-y0 - tile.height) / texture.height),
                new Vector2(x0 / texture.width, -y0 / texture.height),
                new Vector2((x0 + tile.width) / texture.width, -y0 / texture.height),
                new Vector2((x0 + tile.width) / texture.width, (-y0 - tile.height) / texture.height)
            };
            meshRenderer.sortingOrder = Iso.SortingOrder(pos) - 4;
        }
        meshFilter.mesh = mesh;

        int  flagIndex          = 0;
        var  collisionMapOffset = Iso.Snap(Iso.MapToIso(pos));
        byte mask = DT1.BlockFlags.Walk | DT1.BlockFlags.PlayerWalk;

        for (int dy = 2; dy > -3; --dy)
        {
            for (int dx = -2; dx < 3; ++dx, ++flagIndex)
            {
                var  subCellPos = collisionMapOffset + new Vector2i(dx, dy);
                bool passable   = (tile.flags[flagIndex] & mask) == 0;
                if (tile.orientation == 0)
                {
                    CollisionMap.SetPassable(subCellPos, passable);
                }
                else if (CollisionMap.Passable(subCellPos) && !passable)
                {
                    CollisionMap.SetPassable(subCellPos, false);
                }
            }
        }

        meshRenderer.material = tile.material;
        return(meshRenderer);
    }
Esempio n. 6
0
        static Renderer CreateTile(DT1.Tile tile, int x, int y, int orderInLayer = 0, Transform parent = null)
        {
            var texture = tile.texture;
            var pos     = Iso.MapTileToWorld(x, y);

            GameObject gameObject = new GameObject();

            gameObject.name = tile.mainIndex + "_" + tile.subIndex + "_" + tile.orientation;
            gameObject.transform.position = pos;
            if (parent)
            {
                gameObject.transform.SetParent(parent);
            }
            var   meshRenderer = gameObject.AddComponent <MeshRenderer>();
            var   meshFilter   = gameObject.AddComponent <MeshFilter>();
            Mesh  mesh         = new Mesh();
            float x0           = tile.textureX;
            float y0           = tile.textureY;
            float w            = tile.width / Iso.pixelsPerUnit;
            float h            = (-tile.height) / Iso.pixelsPerUnit;

            if (tile.orientation == 0 || tile.orientation == 15)
            {
                var topLeft = new Vector3(-1f, 0.5f);
                if (tile.orientation == 15)
                {
                    topLeft.y += tile.roofHeight / Iso.pixelsPerUnit;
                }
                mesh.vertices = new Vector3[]
                {
                    topLeft,
                    topLeft + new Vector3(0, -h),
                    topLeft + new Vector3(w, -h),
                    topLeft + new Vector3(w, 0)
                };
                mesh.triangles = new int[] { 2, 1, 0, 3, 2, 0 };
                mesh.uv        = new Vector2[]
                {
                    new Vector2(x0 / texture.width, -y0 / texture.height),
                    new Vector2(x0 / texture.width, (-y0 + tile.height) / texture.height),
                    new Vector2((x0 + tile.width) / texture.width, (-y0 + tile.height) / texture.height),
                    new Vector2((x0 + tile.width) / texture.width, -y0 / texture.height)
                };

                meshRenderer.sortingLayerID = tile.orientation == 0 ? SortingLayers.Floor : SortingLayers.Roof;
                meshRenderer.sortingOrder   = orderInLayer;

                gameObject.name += tile.orientation == 0 ? " (floor)" : " (roof)";
            }
            else if (tile.orientation > 15)
            {
                int upperPart = Math.Min(96, -tile.height);
                y0 -= upperPart;
                var topLeft = new Vector3(-1f, upperPart / Iso.pixelsPerUnit - 0.5f);
                mesh.vertices = new Vector3[] {
                    topLeft,
                    topLeft + new Vector3(0, -h),
                    topLeft + new Vector3(w, -h),
                    topLeft + new Vector3(w, 0)
                };
                mesh.triangles = new int[] { 2, 1, 0, 3, 2, 0 };
                mesh.uv        = new Vector2[] {
                    new Vector2(x0 / texture.width, -y0 / texture.height),
                    new Vector2(x0 / texture.width, (-y0 + tile.height) / texture.height),
                    new Vector2((x0 + tile.width) / texture.width, (-y0 + tile.height) / texture.height),
                    new Vector2((x0 + tile.width) / texture.width, -y0 / texture.height)
                };
                meshRenderer.sortingLayerID = SortingLayers.LowerWall;
                meshRenderer.sortingOrder   = orderInLayer;

                gameObject.name += " (lower wall)";
            }
            else
            {
                var topLeft = new Vector3(-1f, h - 0.5f);
                mesh.vertices = new Vector3[] {
                    topLeft,
                    topLeft + new Vector3(0, -h),
                    topLeft + new Vector3(w, -h),
                    topLeft + new Vector3(w, 0)
                };
                mesh.triangles = new int[] { 2, 1, 0, 3, 2, 0 };
                mesh.uv        = new Vector2[] {
                    new Vector2(x0 / texture.width, (-y0 - tile.height) / texture.height),
                    new Vector2(x0 / texture.width, -y0 / texture.height),
                    new Vector2((x0 + tile.width) / texture.width, -y0 / texture.height),
                    new Vector2((x0 + tile.width) / texture.width, (-y0 - tile.height) / texture.height)
                };
                if (tile.orientation == 13) // shadows
                {
                    meshRenderer.sortingLayerID = SortingLayers.Shadow;
                }
                meshRenderer.sortingOrder = Iso.SortingOrder(pos) - 4;
            }
            meshFilter.mesh = mesh;

            int flagIndex          = 0;
            var collisionMapOffset = Iso.Snap(Iso.MapToIso(pos));

            DT1.BlockFlags mask = DT1.BlockFlags.Walk | DT1.BlockFlags.PlayerWalk;
            for (int dy = 2; dy > -3; --dy)
            {
                for (int dx = -2; dx < 3; ++dx, ++flagIndex)
                {
                    Vector2i        subCellPos    = collisionMapOffset + new Vector2i(dx, dy);
                    bool            passable      = (tile.flags[flagIndex] & mask) == 0;
                    CollisionLayers blockedLayers = passable ? CollisionLayers.None : CollisionLayers.Walk;
                    if (tile.orientation == 0)
                    {
                        CollisionMap.SetBlocked(subCellPos, blockedLayers);
                    }
                    else if (CollisionMap.Passable(subCellPos, CollisionLayers.Walk) && !passable)
                    {
                        CollisionMap.SetBlocked(subCellPos, blockedLayers);
                    }
                }
            }

            meshRenderer.material = tile.material;
            return(meshRenderer);
        }