Ejemplo n.º 1
0
        public void TheHexesInRangeReturnsTheCorrectAmmountOfValues()
        {
            int range   = 15;
            var results = Hex.AllHexesInRange(range);

            int expected = 0;

            for (int i = 0; i <= range; i++)
            {
                expected += i * 6;
            }


            Assert.AreEqual(expected, results.Count);
        }
Ejemplo n.º 2
0
        public void TheHexesInRangeReturnsTheCorrectAmmountOfValuesWithHex()
        {
            Hex hex     = new Hex(7, 7);
            int range   = 6;
            var results = Hex.AllHexesInRange(hex, range);

            int expected = 0;

            for (int i = 0; i <= range; i++)
            {
                expected += i * 6;
            }


            Assert.AreEqual(expected, results.Count);
        }
Ejemplo n.º 3
0
    public static List <SortPoint> GetAllPointsAtRange(Hex hex, int range, Dictionary <Hex, List <SortPoint> > pointForeachHex)
    {
        var pointsAtRange = new List <SortPoint>();

        var cellsToCheck = Hex.AllHexesInRange(hex, range, true);

        foreach (Hex neightbor in cellsToCheck)
        {
            if (pointForeachHex.TryGetValue(neightbor, out List <SortPoint> neightborPoints))
            {
                pointsAtRange.AddRange(neightborPoints);
            }
        }

        return(pointsAtRange);
    }
Ejemplo n.º 4
0
    public static List <EntityOnVision> GetAllPointsAtRange(Hex hex, int range, Dictionary <Hex, List <EntityOnVision> > entitiesForeachHex)
    {
        var pointsAtRange = new List <EntityOnVision>();

        var cellsToCheck = Hex.AllHexesInRange(hex, range, true);

        foreach (Hex neightbor in cellsToCheck)
        {
            if (entitiesForeachHex.TryGetValue(neightbor, out List <EntityOnVision> neightborEntities))
            {
                pointsAtRange.AddRange(neightborEntities);
            }
        }

        return(pointsAtRange);
    }
Ejemplo n.º 5
0
        public void TheHexesInRangeDebugValues()
        {
            int range   = 2;
            var results = Hex.AllHexesInRange(range);

            int expected = 0;

            for (int i = 0; i <= range; i++)
            {
                expected += i * 6;
            }

            foreach (var value in results)
            {
                Debug.Log(value);
            }

            Assert.AreEqual(expected, results.Count);
        }
Ejemplo n.º 6
0
    protected override void OnUpdate()
    {
        int visionPointCount = m_VisionPointEntities.CalculateEntityCount();

        VisionPointsForEachTeam = new Dictionary <int, List <VisionPoint> >(visionPointCount);


        int observableUnitCount = m_ObservableUnits.CalculateEntityCount();

        UnitsOnVisionRangeOfTeams = new Dictionary <int, List <EntityOnVision> >(observableUnitCount);

        var positionsOfObservableUnits = new FractionalHex[observableUnitCount];
        var teamsOfObservableUnits     = new int[observableUnitCount];
        var collidersOfObservableUnits = new Collider[observableUnitCount];
        var entitiesOfObservableUnits  = new Entity[observableUnitCount];

        int observableBuildingCount = m_ObservableBuildings.CalculateEntityCount();

        BuildingsOnVisionRangeOfTeams = new Dictionary <int, List <BuildingOnVision> >(observableBuildingCount);

        var observableBuildings   = new Dictionary <Hex, BuildingOnVision>(observableBuildingCount);
        var obserbableSubstitutes = new Dictionary <Hex, Entity>();
        var obserbableResources   = new Dictionary <Hex, Entity>();//unico uso es para la visibilidad de monobehaviour.

        EntitiesOnVisionRangeOfTeamsHashset = new Dictionary <int, HashSet <Entity> >(observableUnitCount + observableBuildingCount);



        //INIT THE LISTS OF UNIT COMPONENTS
        int iteration = 0;

        Entities.WithNone <Group, HexTile, ExcludeFromSimulation>().ForEach((Entity entity, ref HexPosition position, ref Collider collider, ref Team team) =>
        {
            positionsOfObservableUnits[iteration] = position.HexCoordinates;
            teamsOfObservableUnits[iteration]     = team.Number;
            collidersOfObservableUnits[iteration] = collider;
            entitiesOfObservableUnits[iteration]  = entity;
            iteration++;
        });
        Entities.WithNone <Group, HexTile, ExcludeFromSimulation>().ForEach((Entity entity, ref Building building, ref Team team) =>
        {
            Hex hex = building.position;
            if (observableBuildings.ContainsKey(hex))
            {
                Debug.LogError($"In {hex} are more than 1 building.");
            }
            else
            {
                observableBuildings.Add(hex, new BuildingOnVision(entity, building, team));
            }
        });
        Entities.ForEach((Entity entity, ref Substitute substitute) =>
        {
            if (obserbableSubstitutes.ContainsKey(substitute.position))
            {
                Debug.LogError($"In {substitute.position} are more than 1 substitute. And the current sight system only supports 1 substitute per hex.");
            }
            else
            {
                obserbableSubstitutes.Add(substitute.position, entity);
            }
        });
        Entities.ForEach((Entity entity, ref ResourceSource resource) =>
        {
            if (obserbableResources.ContainsKey(resource.position))
            {
                Debug.LogError($"In {resource.position} are more than 1 resource. And the current sight system only supports 1 resource per hex.");
            }
            else
            {
                obserbableResources.Add(resource.position, entity);
            }
        });

        //WE GET THE OBSERVABLE UNITS SORTED BY THE HEX THAT THEY ARE STANDING ON
        var observablePointsSortedByHex = SpartialSortUtils.GetHexToPointsDictionary(positionsOfObservableUnits);



        //1- AGREGA LA ENTIDAD A LOS PUNTOS DE VISION DE SU EQUIPO
        Entities.WithNone <ExcludeFromSimulation>().ForEach((ref Building building, ref SightRange sightRange, ref Team team) =>
        {
            var position = (FractionalHex)building.position;

            //ADD THE UNIT TO THE POINT OF VISION OF HIS TEAM.
            if (VisionPointsForEachTeam.TryGetValue(team.Number, out List <VisionPoint> listOfVisionPointsOfTeam))
            {
                listOfVisionPointsOfTeam.Add(new VisionPoint(position, sightRange.Value));
                VisionPointsForEachTeam[team.Number] = listOfVisionPointsOfTeam;
            }
            else
            {
                var newListOfVisionPointsOfTeam = new List <VisionPoint>()
                {
                    new VisionPoint(position, sightRange.Value)
                };
                VisionPointsForEachTeam.Add(team.Number, newListOfVisionPointsOfTeam);
            }
        });
        Entities.WithNone <Group, ExcludeFromSimulation>().ForEach((ref HexPosition hexPosition, ref SightRange sightRange, ref Team team) =>
        {
            var position = hexPosition.HexCoordinates;

            //ADD THE UNIT TO THE POINT OF VISION OF HIS TEAM.
            if (VisionPointsForEachTeam.TryGetValue(team.Number, out List <VisionPoint> listOfVisionPointsOfTeam))
            {
                listOfVisionPointsOfTeam.Add(new VisionPoint(position, sightRange.Value));
                VisionPointsForEachTeam[team.Number] = listOfVisionPointsOfTeam;
            }
            else
            {
                var newListOfVisionPointsOfTeam = new List <VisionPoint>()
                {
                    new VisionPoint(position, sightRange.Value)
                };
                VisionPointsForEachTeam.Add(team.Number, newListOfVisionPointsOfTeam);
            }
        });

        // loopea por todas las ENTIDADES(unidades y estructuras) con sight range y luego en cada una revisa las ENTIDADES obserbables (dentro de los hexagonos de su vision)
        // y si es que estan en su rango de vision los agrega a unidades/buildings en vista y a la hashset de entidades.
        // hay que agragar que esto revisa por las estructuras a la vista.
        //  -> llena la coleccion que es usada para la vision en la presentacion y las coleccione para la simulacion.
        Entities.WithNone <Group, HexTile, ExcludeFromSimulation>().WithAny <HexPosition, Building>().ForEach((Entity entity, ref SightRange sightRange, ref Team team) =>
        {
            FractionalHex position;
            if (EntityManager.HasComponent <HexPosition>(entity))
            {
                position = EntityManager.GetComponentData <HexPosition>(entity).HexCoordinates;
            }
            else
            {
                position = (FractionalHex)EntityManager.GetComponentData <Building>(entity).position;
            }


            //ACÁ SE REVISA 1 A 1 SI LA ENTIDAD ACTUAL VE ALGUNA DE LAS UNIDADES QUE ESTAN DENTRO DE LOS HEXAGONOS QUE VE.

            //BUILDINGS.
            int sightDistanceInHex = (int)Fix64.Ceiling(sightRange.Value);

            var allVisibleHexes = Hex.AllHexesInRange(position.Round(), sightDistanceInHex, true);
            foreach (var visHex in allVisibleHexes)
            {
                if (observableBuildings.ContainsKey(visHex))
                {
                    BuildingOnVision buildingComps = observableBuildings[visHex];

                    List <BuildingOnVision> buildingViewedByTeam;
                    if (BuildingsOnVisionRangeOfTeams.TryGetValue(team.Number, out buildingViewedByTeam))
                    {
                        if (!buildingViewedByTeam.Contains(buildingComps))
                        {
                            buildingViewedByTeam.Add(buildingComps);
                            BuildingsOnVisionRangeOfTeams[team.Number] = buildingViewedByTeam;
                        }
                    }
                    else
                    {
                        var newBuildingViewedByTeam = new List <BuildingOnVision>()
                        {
                            buildingComps
                        };
                        BuildingsOnVisionRangeOfTeams.Add(team.Number, newBuildingViewedByTeam);
                    }
                    //add to hash set.
                    HashSet <Entity> entitiesOnSightOfTeamHashSet;
                    if (EntitiesOnVisionRangeOfTeamsHashset.TryGetValue(team.Number, out entitiesOnSightOfTeamHashSet))
                    {
                        if (!entitiesOnSightOfTeamHashSet.Contains(buildingComps.entity))
                        {
                            entitiesOnSightOfTeamHashSet.Add(buildingComps.entity);
                            EntitiesOnVisionRangeOfTeamsHashset[team.Number] = entitiesOnSightOfTeamHashSet;
                        }
                    }
                    else
                    {
                        entitiesOnSightOfTeamHashSet = new HashSet <Entity>()
                        {
                            buildingComps.entity
                        };
                        EntitiesOnVisionRangeOfTeamsHashset.Add(team.Number, entitiesOnSightOfTeamHashSet);
                    }
                }

                //substitutes to hashset.
                if (obserbableSubstitutes.ContainsKey(visHex))
                {
                    var subsEntity = obserbableSubstitutes[visHex];

                    HashSet <Entity> entitiesOnSightOfTeamHashSet;
                    if (EntitiesOnVisionRangeOfTeamsHashset.TryGetValue(team.Number, out entitiesOnSightOfTeamHashSet))
                    {
                        if (!entitiesOnSightOfTeamHashSet.Contains(subsEntity))
                        {
                            entitiesOnSightOfTeamHashSet.Add(subsEntity);
                            EntitiesOnVisionRangeOfTeamsHashset[team.Number] = entitiesOnSightOfTeamHashSet;
                        }
                    }
                    else
                    {
                        entitiesOnSightOfTeamHashSet = new HashSet <Entity>()
                        {
                            subsEntity
                        };
                        EntitiesOnVisionRangeOfTeamsHashset.Add(team.Number, entitiesOnSightOfTeamHashSet);
                    }
                }

                //resources to hashset
                if (obserbableResources.ContainsKey(visHex))
                {
                    var resEntity = obserbableResources[visHex];

                    HashSet <Entity> entitiesOnSightOfTeamHashSet;
                    if (EntitiesOnVisionRangeOfTeamsHashset.TryGetValue(team.Number, out entitiesOnSightOfTeamHashSet))
                    {
                        if (!entitiesOnSightOfTeamHashSet.Contains(resEntity))
                        {
                            entitiesOnSightOfTeamHashSet.Add(resEntity);
                            EntitiesOnVisionRangeOfTeamsHashset[team.Number] = entitiesOnSightOfTeamHashSet;
                        }
                    }
                    else
                    {
                        entitiesOnSightOfTeamHashSet = new HashSet <Entity>()
                        {
                            resEntity
                        };
                        EntitiesOnVisionRangeOfTeamsHashset.Add(team.Number, entitiesOnSightOfTeamHashSet);
                    }
                }
            }

            //UNIDADES.
            var observablePointsOnRange = SpartialSortUtils.GetAllPointsAtRange(position.Round(), sightDistanceInHex, observablePointsSortedByHex);
            foreach (var point in observablePointsOnRange)
            {
                var positionOfPoint      = point.position;
                int teamOfPoint          = teamsOfObservableUnits[point.index];
                Collider colliderOfPoint = collidersOfObservableUnits[point.index];
                Fix64 radiusOfPoint      = colliderOfPoint.Radius;
                Entity entityOfPoint     = entitiesOfObservableUnits[point.index];


                var distance = position.Distance(positionOfPoint);
                bool onSight = distance <= sightRange.Value + radiusOfPoint;
                if (onSight)
                {
                    var pointToAdd = new EntityOnVision(entityOfPoint, colliderOfPoint, teamOfPoint, positionOfPoint);

                    //add the entity on the list dictionary if it is not there already.
                    List <EntityOnVision> entitiesOnSightOfTeam;
                    if (UnitsOnVisionRangeOfTeams.TryGetValue(team.Number, out entitiesOnSightOfTeam))
                    {
                        if (!entitiesOnSightOfTeam.Contains(pointToAdd))
                        {
                            entitiesOnSightOfTeam.Add(pointToAdd);
                            UnitsOnVisionRangeOfTeams[team.Number] = entitiesOnSightOfTeam;
                        }
                    }
                    else
                    {
                        entitiesOnSightOfTeam = new List <EntityOnVision>()
                        {
                            pointToAdd
                        };
                        UnitsOnVisionRangeOfTeams.Add(team.Number, entitiesOnSightOfTeam);
                    }


                    //Add the entity to the hash set dicionary if it is not there already.
                    HashSet <Entity> entitiesOnSightOfTeamHashSet;
                    if (EntitiesOnVisionRangeOfTeamsHashset.TryGetValue(team.Number, out entitiesOnSightOfTeamHashSet))
                    {
                        if (!entitiesOnSightOfTeamHashSet.Contains(entityOfPoint))
                        {
                            entitiesOnSightOfTeamHashSet.Add(entityOfPoint);
                            EntitiesOnVisionRangeOfTeamsHashset[team.Number] = entitiesOnSightOfTeamHashSet;
                        }
                    }
                    else
                    {
                        entitiesOnSightOfTeamHashSet = new HashSet <Entity>()
                        {
                            entityOfPoint
                        };
                        EntitiesOnVisionRangeOfTeamsHashset.Add(team.Number, entitiesOnSightOfTeamHashSet);
                    }
                }
            }
        });
    }