Exemple #1
0
    public static Cell GetCell(Vector3 pos)
    {
        var tilePos = Iso.Snap(pos);
        int index   = instance.MapToIndex(tilePos);

        return(instance.map[index]);
    }
Exemple #2
0
    void Update()
    {
        Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        mousePosition = Iso.MapToIso(mousePos);
        mouseTile     = Iso.Snap(mousePosition);
    }
Exemple #3
0
    void DrawDebugCellGrid()
    {
        Color    color       = new Color(1, 0, 0, 0.3f);
        Color    freeColor   = new Color(1, 1, 1, 0.03f);
        Vector2i pos         = Iso.Snap(Iso.MapToIso(Camera.main.transform.position));
        int      debugWidth  = 100;
        int      debugHeight = 100;

        pos.x -= debugWidth / 2;
        pos.y -= debugHeight / 2;
        int index = instance.MapToIndex(pos);

        for (int y = 0; y < debugHeight; ++y)
        {
            for (int x = 0; x < debugWidth; ++x)
            {
                if (index + x < 0 || index + x >= instance.map.Length)
                {
                    continue;
                }

                if (!instance.map[index + x].passable)
                {
                    Iso.DebugDrawTile(pos + new Vector3(x, y), color, 0.9f);
                }
                else
                {
                    Iso.DebugDrawTile(pos + new Vector3(x, y), freeColor, 0.9f);
                }
            }
            index += width;
        }
    }
Exemple #4
0
    static public RaycastHit Raycast(Vector2 from, Vector2 to, float rayLength = Mathf.Infinity, float maxRayLength = Mathf.Infinity, int size = 1, GameObject ignore = null, bool debug = false)
    {
        var hit     = new RaycastHit();
        var diff    = to - from;
        var stepLen = 0.2f;

        if (rayLength == Mathf.Infinity)
        {
            rayLength = Mathf.Min(diff.magnitude, maxRayLength);
        }
        int stepCount = Mathf.RoundToInt(rayLength / stepLen);
        var step      = diff.normalized * stepLen;
        var pos       = from;

        for (int i = 0; i < stepCount; ++i)
        {
            pos += step;
            if (debug)
            {
                Iso.DebugDrawTile(Iso.Snap(pos), margin: 0.3f, duration: 0.5f);
            }
            Cell cell     = GetCell(pos);
            bool passable = Passable(pos, size, debug, ignore);
            if (!passable)
            {
                hit.hit        = !passable;
                hit.gameObject = cell.gameObject;
                hit.pos        = pos;
                break;
            }
        }
        return(hit);
    }
Exemple #5
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);
                    }
                }
            }
        }
Exemple #6
0
    static public int OverlapBox(Vector2 center, Vector2 size, GameObject[] result)
    {
        int count = 0;

        if (result.Length == 0)
        {
            return(0);
        }
        int rows    = Mathf.RoundToInt(size.y);
        int columns = Mathf.RoundToInt(size.x);
        int index   = instance.MapToIndex(Iso.Snap(center - size / 2));

        for (int row = 0; row < rows; ++row)
        {
            for (int column = 0; column < columns; ++column)
            {
                var gameObject = instance.map[index + column].gameObject;
                if (gameObject != null)
                {
                    result[count] = gameObject;
                    count        += 1;
                    if (count >= result.Length)
                    {
                        return(count);
                    }
                }
            }
            index += instance.width;
        }
        return(count);
    }
Exemple #7
0
    void SetMode(string modeName)
    {
        if (objectInfo.draw)
        {
            int newMode = System.Array.IndexOf(COF.ModeNames[2], modeName);
            if (newMode == -1 || !objectInfo.mode[newMode])
            {
                Debug.LogWarning("Failed to set mode '" + modeName + "' of object " + name);
                return;
            }

            if (objectInfo.hasCollision[mode])
            {
                CollisionMap.SetPassable(Iso.Snap(iso.pos), objectInfo.sizeX, objectInfo.sizeY, true, gameObject);
            }

            mode = newMode;

            var cof = COF.Load(@"data\global\objects", objectInfo.token, "HTH", modeName);
            animator.shadow = objectInfo.blocksLight[mode];
            animator.cof    = cof;
            animator.loop   = objectInfo.cycleAnim[mode];
            animator.SetFrameRange(objectInfo.start[mode], objectInfo.frameCount[mode]);
            animator.frameDuration = objectInfo.frameDuration[mode];

            if (objectInfo.hasCollision[mode])
            {
                CollisionMap.SetPassable(Iso.Snap(iso.pos), objectInfo.sizeX, objectInfo.sizeY, false, gameObject);
            }
        }
    }
Exemple #8
0
        public void Kill(Unit killer = null)
        {
            _dying      = true;
            _targetUnit = null;
            _moving     = false;
            _usingSkill = false;
            _skillInfo  = null;

            CollisionMap.SetPassable(Iso.Snap(iso.pos), size, size, true, gameObject);

            Events.InvokeUnitDied(this, killer);
        }
Exemple #9
0
 void OnAnimationFinish()
 {
     targetCharacter = null;
     usingSkill      = false;
     takingDamage    = false;
     ressurecting    = false;
     skillInfo       = null;
     if (dying)
     {
         dying = false;
         dead  = true;
         CollisionMap.SetPassable(Iso.Snap(iso.pos), size, size, true, gameObject);
     }
 }
Exemple #10
0
        public void Hit(int damage, Unit originator = null)
        {
            if (_dying || _dead || _resurrecting)
            {
                return;
            }

            if (!killable)
            {
                return;
            }

            if (originator != null && originator.party == party)
            {
                return;
            }

            health -= damage;
            if (health > 0)
            {
                if (damage > maxHealth * 0.1f)
                {
                    overrideMode = "GH";
                    _targetUnit  = null;
                    _moving      = false;
                    _usingSkill  = false;
                    _skillInfo   = null;
                }

                Events.InvokeUnitTookDamage(this, damage);
            }
            else
            {
                if (originator)
                {
                    LookAtImmediately(originator.iso.pos);
                }
                _dying      = true;
                _targetUnit = null;
                _moving     = false;
                _usingSkill = false;
                _skillInfo  = null;

                CollisionMap.SetPassable(Iso.Snap(iso.pos), size, size, true, gameObject);

                Events.InvokeUnitDied(this, originator);
            }
        }
Exemple #11
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);
            }
        }
    }
Exemple #12
0
        public void SetMode(StaticObjectMode mode)
        {
            if (!objectInfo.mode[mode.index])
            {
                Debug.LogWarning("Failed to set mode '" + mode + "' of object " + name);
                return;
            }

            if (objectInfo.hasCollision[_mode.index])
            {
                CollisionMap.SetPassable(Iso.Snap(_iso.pos), objectInfo.sizeX, objectInfo.sizeY, true, gameObject);
            }
            if (objectInfo.hasCollision[mode.index])
            {
                CollisionMap.SetPassable(Iso.Snap(_iso.pos), objectInfo.sizeX, objectInfo.sizeY, false, gameObject);
            }

            _mode              = mode;
            _animationTime     = 0;
            _animationDuration = objectInfo.frameCount[_mode.index] * objectInfo.frameDuration[_mode.index];
        }
Exemple #13
0
 public static bool Passable(Vector3 pos, CollisionLayers mask, int size = 1, GameObject ignore = null)
 {
     return(Passable(Iso.Snap(pos), mask, size, ignore));
 }
Exemple #14
0
        public void TakeDamage(int damage, Character originator = null)
        {
            if (_dying || _dead || _resurrecting)
            {
                return;
            }

            if (!killable)
            {
                return;
            }

            if (originator != null && originator.party == party)
            {
                return;
            }

            health -= damage;
            if (health > 0)
            {
                if (OnTakeDamage != null)
                {
                    OnTakeDamage(originator, damage);
                }
                if (damage > maxHealth * 0.1f)
                {
                    overrideMode     = "GH";
                    _targetCharacter = null;
                    _moving          = false;
                    _usingSkill      = false;
                    _skillInfo       = null;
                }

                if (monStat != null)
                {
                    AudioManager.instance.Play(monStat.sound.hit, transform, monStat.sound.hitDelay);
                }
            }
            else
            {
                if (originator)
                {
                    LookAtImmediately(originator.iso.pos);
                }
                _dying           = true;
                _targetCharacter = null;
                _moving          = false;
                _usingSkill      = false;
                _skillInfo       = null;

                CollisionMap.SetPassable(Iso.Snap(iso.pos), size, size, true, gameObject);

                if (OnDeath != null)
                {
                    OnDeath(this, originator);
                }

                if (monStat != null)
                {
                    AudioManager.instance.Play(monStat.sound.death, transform, monStat.sound.deathDelay);
                }
            }
        }
Exemple #15
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);
    }
Exemple #16
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);
        }
Exemple #17
0
    static public List <Step> BuildPath(Vector2 from_, Vector2 target_, float minRange = 0.1f, int size = 2, GameObject self = null, int depth = 100)
    {
        UnityEngine.Profiling.Profiler.BeginSample("BuildPath");
        Vector2i from = Iso.Snap(from_);

        target = Iso.Snap(target_);
        path.Clear();
        if (from == target)
        {
            UnityEngine.Profiling.Profiler.EndSample();
            return(path);
        }
        openNodes.Clear();
        Node.Recycle(closeNodes);

        Pathing.size = size;
        Pathing.self = self;
        Node startNode = Node.Get();

        startNode.parent         = null;
        startNode.pos            = from;
        startNode.gScore         = 0;
        startNode.hScore         = Vector2i.manhattanDistance(from, target);
        startNode.score          = int.MaxValue;
        startNode.directionIndex = -1;
        openNodes.Add(startNode);
        closeNodes.Add(startNode);
        int  iterCount = 0;
        Node bestNode  = startNode;

        while (openNodes.Count > 0)
        {
            Node node = openNodes.Take();

            if (node.hScore < bestNode.hScore)
            {
                bestNode = node;
            }
            if (node.hScore <= minRange)
            {
                TraverseBack(node);
                break;
            }
            StepTo(node);
            iterCount += 1;
            if (iterCount > depth)
            {
                TraverseBack(bestNode);
                break;
            }
        }
        //foreach (Node node in closeNodes)
        //{
        //    Iso.DebugDrawTile(node.pos, Color.magenta, 0.3f);
        //}
        //foreach (Node node in openNodes)
        //{
        //    Iso.DebugDrawTile(node.pos, Color.green, 0.3f);
        //}
        UnityEngine.Profiling.Profiler.EndSample();
        return(path);
    }
Exemple #18
0
 public static void SetPassable(Vector3 pos, bool passable)
 {
     SetPassable(Iso.Snap(pos), passable);
 }
Exemple #19
0
 public static void SetBlocked(Vector3 pos, CollisionLayers value)
 {
     SetBlocked(Iso.Snap(pos), value);
 }
Exemple #20
0
 public static bool Passable(Vector3 pos, int size = 1, bool debug = false, GameObject ignore = null)
 {
     return(Passable(Iso.Snap(pos), size, debug, ignore));
 }
Exemple #21
0
 private int MapToIndex(Vector3 pos)
 {
     return(MapToIndex(Iso.Snap(pos)));
 }
Exemple #22
0
 public static void SetPassable(Vector3 pos, int sizeX, int sizeY, bool passable, GameObject gameObject)
 {
     SetPassable(Iso.Snap(pos), sizeX, sizeY, passable, gameObject);
 }