Ejemplo n.º 1
0
 protected override void WriteObject(AssetsWriter writer)
 {
     LocalRotation.Write(writer);
     LocalPosition.Write(writer);
     LocalScale.Write(writer);
     writer.WriteArrayOf(Children, (x, y) => x.Write(y));
     Father.Write(writer);
 }
Ejemplo n.º 2
0
 public override void Write(AssetsWriter writer)
 {
     base.WriteBase(writer);
     LocalRotation.Write(writer);
     LocalPosition.Write(writer);
     LocalScale.Write(writer);
     writer.WriteArrayOf(Children, x => x.WritePtr(writer));
     Father.WritePtr(writer);
 }
Ejemplo n.º 3
0
        protected Rectangle GetDefaultRect(Rectangle parent)
        {
            Rectangle drawRect = new Rectangle();

            if (Size.HasFlag(DrawingSize.XMin))
            {
                drawRect.Width = (int)MinimumSize.X;
            }
            else if (Size.HasFlag(DrawingSize.XMax))
            {
                drawRect.Width = parent.Size.X;
            }

            if (Size.HasFlag(DrawingSize.YMin))
            {
                drawRect.Height = (int)MinimumSize.Y;
            }
            else if (Size.HasFlag(DrawingSize.YMax))
            {
                drawRect.Height = parent.Size.Y;
            }

            if (drawRect.Width > MaximumSize.X && MaximumSize.X != -1)
            {
                drawRect.Width = (int)MaximumSize.X;
            }
            if (drawRect.Height > MaximumSize.Y && MaximumSize.Y != -1)
            {
                drawRect.Height = (int)MaximumSize.Y;
            }

            if (ScalingMethod == ScalingMethod.KeepRatio)
            {
                if (drawRect.Width * ratio > MaximumSize.Y)
                {
                    drawRect.Width = (int)(MaximumSize.Y / ratio);
                }

                drawRect.Height = (int)(drawRect.Width * ratio);
            }

            if (drawRect.Width < MinimumSize.X && MinimumSize.X != -1)
            {
                drawRect.Width = (int)MinimumSize.X;
            }
            if (drawRect.Height < MinimumSize.Y && MinimumSize.Y != -1)
            {
                drawRect.Height = (int)MinimumSize.Y;
            }

            drawRect.Location  = LocalPosition.ToPoint() + parent.Location;
            drawRect.Location += Offset.ToPoint();
            drawRect.Location += (drawRect.Size.ToVector2() * -Origin).ToPoint(); // Origin

            return(drawRect);
        }
Ejemplo n.º 4
0
        public WorldPosition(int x, int y, int z)
        {
            var chunkX = Mathf.FloorToInt((float)x / ChunkSize);
            var chunkY = Mathf.FloorToInt((float)y / ChunkSize);
            var chunkZ = Mathf.FloorToInt((float)z / ChunkSize);

            chunkPosition = new ChunkPosition(chunkX, chunkY, chunkZ);
            localPosition = new LocalPosition((uint)(x - chunkX * ChunkSize),
                                              (uint)(y - chunkY * ChunkSize), (uint)(z - chunkX * ChunkSize));
        }
Ejemplo n.º 5
0
        public override void Read(AssetReader reader)
        {
            base.Read(reader);

            LocalRotation.Read(reader);
            LocalPosition.Read(reader);
            LocalScale.Read(reader);
            m_children = reader.ReadArray <PPtr <Transform> >();
            Father.Read(reader);
        }
Ejemplo n.º 6
0
        public override void Read(AssetStream stream)
        {
            base.Read(stream);

            LocalRotation.Read(stream);
            LocalPosition.Read(stream);
            LocalScale.Read(stream);
            m_children = stream.ReadArray <PPtr <Transform> >();
            Father.Read(stream);
        }
Ejemplo n.º 7
0
        public override void Read(EndianStream stream)
        {
            base.Read(stream);

            LocalRotation.Read(stream);
            LocalPosition.Read(stream);
            LocalScale.Read(stream);
            m_children = stream.ReadArray(() => new PPtr <Transform>(AssetsFile));
            Father.Read(stream);
        }
Ejemplo n.º 8
0
            public LocalPosition Add(LocalPosition other, out ChunkPosition offset)
            {
                var totalX = x + other.x;
                var totalY = y + other.y;
                var totalZ = z + other.z;

                offset = new ChunkPosition((int)(totalX / ChunkSize),
                                           (int)(totalY / ChunkSize), (int)(totalZ / ChunkSize));

                return(new LocalPosition(totalX % ChunkSize, totalY % ChunkSize, totalZ % ChunkSize));
            }
Ejemplo n.º 9
0
    private Node[] GetCurrentTeamPosition(List <Unit> team)
    {
        Node[] teamPosition = new Node[team.Count];
        for (int i = 0; i < team.Count; ++i)
        {
            LocalPosition localPos = PositionConverter.ToLocalCoordinates(team[i].worldPosition);
            teamPosition[i] = BoardManager.Instance.map.graph[localPos.x, localPos.y];
        }

        return(teamPosition);
    }
Ejemplo n.º 10
0
        protected override YAMLMappingNode ExportYAMLRoot(IExportContainer container)
        {
            YAMLMappingNode node = base.ExportYAMLRoot(container);

            node.Add("m_LocalRotation", LocalRotation.ExportYAML(container));
            node.Add("m_LocalPosition", LocalPosition.ExportYAML(container));
            node.Add("m_LocalScale", LocalScale.ExportYAML(container));
            node.Add("m_Children", Children.ExportYAML(container));
            node.Add("m_Father", Father.ExportYAML(container));
            node.Add("m_RootOrder", GetSiblingIndex());
            node.Add("m_LocalEulerAnglesHint", LocalRotation.ToEuler().ExportYAML(container));
            return(node);
        }
Ejemplo n.º 11
0
    private void OnMouseUp()
    {
        LocalPosition clickedTileLocalPos = PositionConverter.ToLocalCoordinates(new WorldPosition(tileX, 0, tileY));
        Node          clickedTtile        = BoardManager.Instance.map.graph[clickedTileLocalPos.x, clickedTileLocalPos.y];

        if (BoardManager.selectedUnit != null && BoardManager.Instance.isAvailableClickedTile(clickedTtile) && !GameManager.isGameOver)
        {
            BoardManager.Instance.GeneratePathTo(tileX, tileY);
            BoardManager.selectedUnit.MoveToEnterTile();
            Human.isDoUnit       = true;
            Unit.isHumanMakeTurn = true;
        }
    }
Ejemplo n.º 12
0
        protected override YAMLMappingNode ExportYAMLRoot(IExportContainer container)
        {
            YAMLMappingNode node = base.ExportYAMLRoot(container);

            node.Add(LocalRotationName, LocalRotation.ExportYAML(container));
            node.Add(LocalPositionName, LocalPosition.ExportYAML(container));
            node.Add(LocalScaleName, LocalScale.ExportYAML(container));
            node.Add(ChildrenName, Children.ExportYAML(container));
            node.Add(FatherName, Father.ExportYAML(container));
            node.Add(RootOrderName, GetRootOrder(container.Version, container.Flags));
            node.Add(LocalEulerAnglesHintName, GetLocalEulerAnglesHint(container.Version, container.Flags).ExportYAML(container));
            return(node);
        }
Ejemplo n.º 13
0
    void CreateTileMap()
    {
        const int mountainLimit     = 6;
        int       countMountainTile = 0;

        System.Random rand = new System.Random();
        tiles = new int[width, height];

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                #region get tile type
                TileType tt;
                int      tileType = 0;
                if (x > width / 3)
                {
                    tileType = rand.Next(2);
                }

                if (tileType == 1 && countMountainTile != mountainLimit)
                {
                    tt = tileTypes[1];
                    ++countMountainTile;
                }
                else
                {
                    tt = tileTypes[0];
                }
                #endregion

                LocalPosition tilePos = new LocalPosition(x, y);

                Transform tile = Instantiate(tt.tileVisualPrefab.transform) as Transform;

                tile.position = PositionConverter.ToWorldCoordinates(tilePos).ToVector3();
                tile.parent   = transform;
                tile.name     = "Hexagon" + tilePos.x + "|" + tilePos.y;

                ClickableAvailableTile ct;
                if (tile.GetComponent <ClickableAvailableTile>() != null)
                {
                    ct       = tile.GetComponent <ClickableAvailableTile>();
                    ct.tileX = tile.position.x;
                    ct.tileY = tile.position.z;
                }

                tiles[x, y] = TileType.GetTileTypeByTileName(tt.name);
            }
        }
    }
        protected override void OnUpdate()
        {
            while (m_Group.Length != 0)
            {
                var spawner      = m_Group.Spawner[0];
                var sourceEntity = m_Group.Entity[0];
                var center       = m_Group.Position[0].Value;

                var entities = new NativeArray <Entity>(spawner.count, Allocator.Temp);
                EntityManager.Instantiate(spawner.prefab, entities);

                var positions = new NativeArray <float3>(spawner.count, Allocator.Temp);

                if (spawner.spawnLocal)
                {
                    GeneratePoints.RandomPointsOnCircle(new float3(), spawner.radius, ref positions);
                    for (int i = 0; i < spawner.count; i++)
                    {
                        var position = new LocalPosition
                        {
                            Value = positions[i]
                        };
                        EntityManager.SetComponentData(entities[i], position);
                        EntityManager.AddComponentData(entities[i], new TransformParent {
                            Value = sourceEntity
                        });
                    }
                }
                else
                {
                    GeneratePoints.RandomPointsOnCircle(center, spawner.radius, ref positions);
                    for (int i = 0; i < spawner.count; i++)
                    {
                        var position = new Position
                        {
                            Value = positions[i]
                        };
                        EntityManager.SetComponentData(entities[i], position);
                    }
                }

                entities.Dispose();
                positions.Dispose();

                EntityManager.RemoveComponent <SpawnRandomCircle>(sourceEntity);

                // Instantiate & AddComponent & RemoveComponent calls invalidate the injected groups,
                // so before we get to the next spawner we have to reinject them
                UpdateInjectedComponentGroups();
            }
        }
Ejemplo n.º 15
0
    public static WorldPosition ToWorldCoordinates(LocalPosition localPosition)
    {
        float offset = 0;

        if (localPosition.y % 2 != 0)
        {
            offset = TileMap.hexWidth / 2;
        }

        float x = TileMap.startPos.x + localPosition.x * TileMap.hexWidth + offset;
        float z = TileMap.startPos.z - localPosition.y * TileMap.hexHeight * 0.75f;

        return(new WorldPosition(x, 0, z));
    }
Ejemplo n.º 16
0
        private static string GetLocalPositionLabel(LocalPosition lp)
        {
            switch (lp)
            {
            case LocalPosition.LpnRepresentative:
                return("LPN Rep.");

            case LocalPosition.ShopSteward:
                return("Shop Steward");

            default:
                return("Missing Label");
            }
        }
Ejemplo n.º 17
0
        protected override YAMLMappingNode ExportYAMLRoot(IExportContainer container)
        {
            YAMLMappingNode node   = base.ExportYAMLRoot(container);
            TransformLayout layout = container.Layout.Transform;

            node.Add(layout.LocalRotationName, LocalRotation.ExportYAML(container));
            node.Add(layout.LocalPositionName, LocalPosition.ExportYAML(container));
            node.Add(layout.LocalScaleName, LocalScale.ExportYAML(container));
            node.Add(layout.ChildrenName, Children.ExportYAML(container));
            node.Add(layout.FatherName, Father.ExportYAML(container));
            node.Add(layout.RootOrderName, RootOrder);
            node.Add(layout.LocalEulerAnglesHintName, LocalEulerAnglesHint.ExportYAML(container));
            return(node);
        }
    protected override void OnUpdate()
    {
        while (m_Group.Length != 0)
        {
            var spawner      = m_Group.Spawner[0];
            var sourceEntity = m_Group.Entity[0];
            //var center = m_Group.Position[0].Value;

            var entities = new NativeArray <Entity>(spawner.count, Allocator.Temp);
            EntityManager.Instantiate(spawner.prefab, entities);

            var positions = new NativeArray <float3>(spawner.count, Allocator.Temp);

            for (int i = 0; i < spawner.count; i++)
            {
                var position = new LocalPosition
                {
                    Value = positions[i] + new float3
                    {
                        x = Random.Range(-10f, 10f),
                        y = Random.Range(-10f, 10f),
                        z = Random.Range(-10f, 10f)
                    }
                };


                EntityManager.SetComponentData(entities[i], position);

                EntityManager.AddComponentData(entities[i], new RotationTestSpeed {
                    Value = Random.Range(0.5f, 3f)
                });
                EntityManager.AddComponentData(entities[i], new TestId {
                    Value = i
                });
                EntityManager.AddComponentData(entities[i], new TransformParent {
                    Value = sourceEntity
                });
            }

            entities.Dispose();
            positions.Dispose();

            EntityManager.RemoveComponent <SpawnTest>(sourceEntity);

            // Instantiate & AddComponent & RemoveComponent calls invalidate the injected groups,
            // so before we get to the next spawner we have to reinject them
            UpdateInjectedComponentGroups();
        }
    }
Ejemplo n.º 19
0
            public LocalPosition Subtract(LocalPosition other, out ChunkPosition offset)
            {
                uint totalX;
                uint totalY;
                uint totalZ;

                int chunkX;
                int chunkY;
                int chunkZ;

                if (x > other.x)
                {
                    totalX = x - other.x;
                    chunkX = (int)(totalX / ChunkSize);
                }
                else
                {
                    totalX = other.x - x;
                    chunkX = -(int)(totalX / ChunkSize);
                }

                if (y > other.y)
                {
                    totalY = y - other.y;
                    chunkY = (int)(totalY / ChunkSize);
                }
                else
                {
                    totalY = other.y - y;
                    chunkY = -(int)(totalY / ChunkSize);
                }

                if (z > other.z)
                {
                    totalZ = z - other.z;
                    chunkZ = (int)(totalZ / ChunkSize);
                }
                else
                {
                    totalZ = other.z - z;
                    chunkZ = -(int)(totalZ / ChunkSize);
                }

                offset = new ChunkPosition(chunkX, chunkY, chunkZ);

                return(new LocalPosition(totalX % ChunkSize, totalY % ChunkSize, totalZ & ChunkSize));
            }
Ejemplo n.º 20
0
    private List <Node> GetAllAvailableUnitMoves(Unit unit)
    {
        BoardManager.selectedUnit = unit;
        List <Node>   moves    = new List <Node>();
        LocalPosition localPos = PositionConverter.ToLocalCoordinates(unit.worldPosition);

        BoardManager.Instance.GetAvailableMovementTiles(moves, BoardManager.Instance.map.graph[localPos.x, localPos.y]);

        List <Unit> availableHumanUnitsForAttack = BoardManager.Instance.GetAvailableHumanUnitsForAttack();

        foreach (var item in availableHumanUnitsForAttack)
        {
            moves.Add(BoardManager.Instance.map.graph[item.localPosition.x, item.localPosition.y]);
        }

        return(moves);
    }
Ejemplo n.º 21
0
 public void UpdateGlobalsFromParent()
 {
     if (parent == null)
     {
         Position    = LocalPosition;
         Orientation = LocalOrientation;
     }
     else
     {
         Position    = parent.Position + LocalPosition.Transform(Parent.Orientation);
         Orientation = parent.Orientation * LocalOrientation;
     }
     foreach (var child in Children)
     {
         child.UpdateGlobalsFromParent();
     }
 }
Ejemplo n.º 22
0
        public override void Read(AssetReader reader)
        {
            base.Read(reader);

            LocalRotation.Read(reader);
            LocalPosition.Read(reader);
            LocalScale.Read(reader);
            Children = reader.ReadAssetArray <PPtr <Transform> >();
            Father.Read(reader);
#if UNIVERSAL
            if (HasRootOrder(reader.Version, reader.Flags))
            {
                RootOrder = reader.ReadInt32();
            }
            if (HasLocalEulerAnglesHint(reader.Version, reader.Flags))
            {
                LocalEulerAnglesHint.Read(reader);
            }
#endif
        }
Ejemplo n.º 23
0
        void InstantiateFromArray(NativeArray <Entity> prefabs, float xCoord, float yCoord, Entity parent)
        {
            // Create a random index for the array.
            int randomIndex = Random.Range(0, prefabs.Length);

            var entity   = EntityManager.Instantiate(prefabs[randomIndex]);
            var position = new LocalPosition
            {
                Value = new float3(xCoord, 0.0f, yCoord)
            };

            EntityManager.SetComponentData(entity, position);

            var transformParent = new TransformParent
            {
                Value = parent
            };

            EntityManager.SetComponentData(entity, transformParent);
        }
Ejemplo n.º 24
0
        public override void Write(AssetWriter writer)
        {
            base.Write(writer);

            LocalRotation.Write(writer);
            LocalPosition.Write(writer);
            LocalScale.Write(writer);
            Children.Write(writer);
            Father.Write(writer);
#if UNIVERSAL
            TransformLayout layout = writer.Layout.Transform;
            if (layout.HasRootOrder)
            {
                writer.Write(RootOrder);
            }
            if (layout.HasLocalEulerAnglesHint)
            {
                LocalEulerAnglesHint.Write(writer);
            }
#endif
        }
Ejemplo n.º 25
0
        public override void Read(AssetReader reader)
        {
            base.Read(reader);

            LocalRotation.Read(reader);
            LocalPosition.Read(reader);
            LocalScale.Read(reader);
            Children = reader.ReadAssetArray <PPtr <Transform> >();
            Father.Read(reader);
#if UNIVERSAL
            TransformLayout layout = reader.Layout.Transform;
            if (layout.HasRootOrder)
            {
                RootOrder = reader.ReadInt32();
            }
            if (layout.HasLocalEulerAnglesHint)
            {
                LocalEulerAnglesHint.Read(reader);
            }
#endif
        }
Ejemplo n.º 26
0
        public void Update()
        {
            if (toInitialize?.Count > 0)
            {
                for (int index = 0; index < toInitialize.Count; index++)
                {
                    var cmpt = toInitialize[index];
                    cmpt.InitializeComponent();
                }
                toInitialize.Clear();
            }

            if (isUpdateEnabled)
            {
                for (int index = 0; index < components.Count; index++)
                {
                    var component = components[index];
                    if (component.Enabled)
                    {
                        component.UpdateComponent();
                    }
                }
            }
            if (isUpdateEnabled && !IsStatic)
            {
                UpdateSpeculativePosition();
            }
            else
            {
                SpeculativePosition = LocalPosition.ToVector2();
            }

            if (children != null)
            {
                updateChildren();
            }
        }
Ejemplo n.º 27
0
    public void SelectAvailableTile()
    {
        LocalPosition unitPosition = PositionConverter.ToLocalCoordinates(selectedUnit.worldPosition);

        selectedUnit.availableMovementTiles = new List <Node>();
        Node currentTile = map.graph[unitPosition.x, unitPosition.y];

        GetAvailableMovementTiles(selectedUnit.availableMovementTiles, currentTile);

        foreach (var item in selectedUnit.availableMovementTiles)
        {
            Collider[]    colliders;
            WorldPosition tileWorldPos = PositionConverter.ToWorldCoordinates(new LocalPosition(item.x, item.y));
            colliders = Physics.OverlapSphere(tileWorldPos.ToVector3(), 0.125f /*Radius*/);

            if (colliders.Length >= 1)
            {
                foreach (var collider in colliders)
                {
                    var go = collider.gameObject;
                    if (go.name.StartsWith("Hexagon"))
                    {
                        if (map.tiles[item.x, item.y] != (int)TileTypes.SWAMP)
                        {
                            selectedUnit.startColor         = go.GetComponent <Renderer>().material.GetColor("_Color");
                            selectedUnit.availableTileColor = selectedUnit.startColor;
                            //TODO: think about  other variant of colorized tile
                            selectedUnit.availableTileColor.g = 0.6f;
                            selectedUnit.availableTileColor.b = 0.4f;
                            GameObjectHighlighter.Select(selectedUnit.startColor, selectedUnit.availableTileColor, go.GetComponent <Renderer>());
                        }
                    }
                }
            }
        }
    }
Ejemplo n.º 28
0
    //todo: think about signature
    public void GetAvailableMovementTiles(List <Node> availableMovementTiles, Node startPos)
    {
        int maxDepth                    = selectedUnit.distance;
        int currentDepth                = 0;
        int elementsToDepthIncrease     = 1;
        int nextElementsToDepthIncrease = 0;

        Queue <Node> burningTiles = new Queue <Node>();

        burningTiles.Enqueue(startPos);

        List <Node> visitedTiles = new List <Node>();

        visitedTiles.Add(startPos);

        // check other player units pos
        foreach (var item in Instance.playerUnits)
        {
            LocalPosition unitPosition        = PositionConverter.ToLocalCoordinates(item.worldPosition);
            Node          currentTileWithUnit = map.graph[unitPosition.x, unitPosition.y];
            visitedTiles.Add(currentTileWithUnit);
        }

        // check other AI units pos
        foreach (var item in Instance.enemyUnits)
        {
            LocalPosition unitPosition        = PositionConverter.ToLocalCoordinates(item.worldPosition);
            Node          currentTileWithUnit = map.graph[unitPosition.x, unitPosition.y];
            visitedTiles.Add(currentTileWithUnit);
        }

        while (burningTiles.Count != 0)
        {
            Node currentVertex = burningTiles.Dequeue();
            foreach (var item in currentVertex.neighbours)
            {
                if (!visitedTiles.Contains(item) && map.tiles[item.x, item.y] != (int)TileTypes.SWAMP)
                {
                    ++nextElementsToDepthIncrease;
                }
            }

            if (--elementsToDepthIncrease == 0)
            {
                elementsToDepthIncrease     = nextElementsToDepthIncrease;
                nextElementsToDepthIncrease = 0;
                ++currentDepth;
            }

            for (int i = 0; i < currentVertex.neighbours.Count; ++i)
            {
                Node tile = currentVertex.neighbours[i];
                if ((!visitedTiles.Contains(tile) && map.tiles[tile.x, tile.y] != 1 /*mountain*/))
                {
                    visitedTiles.Add(tile);
                    burningTiles.Enqueue(tile);
                    availableMovementTiles.Add(tile);
                }
            }

            if (currentDepth == maxDepth)
            {
                return;
            }
        }
    }
Ejemplo n.º 29
0
    public void GeneratePathToEnemy(Unit enemy)
    {
        selectedUnit.currentPathToEnemy = null;
        // check the neighborhood of units
        if (selectedUnit.DistanceTo(enemy) == 1)
        {
            Node        enemyTile           = map.graph[enemy.localPosition.x, enemy.localPosition.y];
            List <Node> enemyTileNeighbours = enemyTile.neighbours;
            Node        selectedUnitTile    = map.graph[selectedUnit.localPosition.x, selectedUnit.localPosition.y];
            foreach (var item in enemyTileNeighbours)
            {
                if (selectedUnitTile.DistanceBetweenNode(item) == 1 &&
                    map.tiles[item.x, item.y] != (int)TileTypes.SWAMP &&
                    !isUnitOccupiedNode(item))
                {
                    currentPathToEnemy = new List <Node>();
                    currentPathToEnemy.Add(item);
                    selectedUnit.currentPathToEnemy = currentPathToEnemy;
                    return;
                }
            }
        }
        Dictionary <Node, float> dist = new Dictionary <Node, float>();
        Dictionary <Node, Node>  prev = new Dictionary <Node, Node>();
        List <Node> unvisited         = new List <Node>();

        LocalPosition sourceLocalPos = PositionConverter.ToLocalCoordinates(selectedUnit.worldPosition);
        LocalPosition targetLocalPos = PositionConverter.ToLocalCoordinates(enemy.worldPosition);

        Node source         = map.graph[sourceLocalPos.x, sourceLocalPos.y];
        Node target         = map.graph[targetLocalPos.x, targetLocalPos.y];
        Node currentUnitPos = map.graph[selectedUnit.localPosition.x, selectedUnit.localPosition.y];

        if (selectedUnit.availableMovementTiles == null)
        {
            selectedUnit.availableMovementTiles = new List <Node>();
        }

        GetAvailableMovementTiles(selectedUnit.availableMovementTiles, currentUnitPos);

        if (selectedUnit.CanAttack(enemy))
        {
            dist[source] = 0;
            prev[source] = null;

            foreach (var vertex in map.graph)
            {
                if (vertex != source)
                {
                    dist[vertex] = Mathf.Infinity;
                    prev[vertex] = null;
                }
                unvisited.Add(vertex);
            }

            while (unvisited.Count > 0)
            {
                Node u = null;

                foreach (var possibleU in unvisited)
                {
                    if (u == null || dist[u] > dist[possibleU])
                    {
                        u = possibleU;
                    }
                }

                if (u == target)
                {
                    break;
                }

                unvisited.Remove(u);

                foreach (var v in u.neighbours)
                {
                    if (selectedUnit.availableMovementTiles.Contains(v) || (v.x == enemy.localPosition.x && v.y == enemy.localPosition.y))
                    {
                        float alt = dist[u] + u.DistanceTo(v); //without calc tile cost
                        if (alt < dist[v])
                        {
                            dist[v] = alt;
                            prev[v] = u;
                        }
                    }
                }
            }

            if (prev[target] == null)
            {
                //No route between source and target
                return;
            }
            else
            {
                currentPathToEnemy = new List <Node>();
                Node currentTile = target;

                while (currentTile != null)
                {
                    currentPathToEnemy.Add(currentTile);
                    currentTile = prev[currentTile];
                }

                currentPathToEnemy.Reverse();
                currentPathToEnemy.RemoveAt(currentPathToEnemy.Count - 1);
                selectedUnit.currentPathToEnemy = currentPathToEnemy;
            }
        }
    }
Ejemplo n.º 30
0
    public void GeneratePathTo(float x, float z)
    {
        selectedUnit.currentPath = null;
        Dictionary <Node, float> dist = new Dictionary <Node, float>();
        Dictionary <Node, Node>  prev = new Dictionary <Node, Node>();
        List <Node> unvisited         = new List <Node>();

        LocalPosition sourceLocalPos = PositionConverter.ToLocalCoordinates(selectedUnit.worldPosition);
        LocalPosition targetLocalPos = PositionConverter.ToLocalCoordinates(new WorldPosition(x, 0, z));

        Node source = map.graph[sourceLocalPos.x, sourceLocalPos.y];
        Node target = map.graph[targetLocalPos.x, targetLocalPos.y];

        if (selectedUnit.availableMovementTiles == null)
        {
            selectedUnit.availableMovementTiles = new List <Node>();
            Node currentTile = map.graph[selectedUnit.localPosition.x, selectedUnit.localPosition.y];
            GetAvailableMovementTiles(selectedUnit.availableMovementTiles, currentTile);
        }

        if (selectedUnit.availableMovementTiles.Contains(target))
        {
            dist[source] = 0;
            prev[source] = null;

            foreach (var vertex in map.graph)
            {
                if (vertex != source)
                {
                    dist[vertex] = Mathf.Infinity;
                    prev[vertex] = null;
                }
                unvisited.Add(vertex);
            }

            while (unvisited.Count > 0)
            {
                Node u = null;

                foreach (var possibleU in unvisited)
                {
                    if (u == null || dist[u] > dist[possibleU])
                    {
                        u = possibleU;
                    }
                }

                if (u == target)
                {
                    break;
                }

                unvisited.Remove(u);

                foreach (var v in u.neighbours)
                {
                    if (selectedUnit.availableMovementTiles.Contains(v))
                    {
                        //float alt = dist[u] + u.DistanceTo(v); //without calc tile cost
                        float alt = dist[u] + map.CostToEnterTile(v.x, v.y);
                        if (alt < dist[v])
                        {
                            dist[v] = alt;
                            prev[v] = u;
                        }
                    }
                }
            }

            if (prev[target] == null)
            {
                //No route between source and target
                return;
            }
            else
            {
                currentPath = new List <Node>();
                Node currentTile = target;

                while (currentTile != null)
                {
                    currentPath.Add(currentTile);
                    currentTile = prev[currentTile];
                }

                currentPath.Reverse();
                selectedUnit.currentPath = currentPath;
            }
        }
    }