예제 #1
0
    private static void FindAndAddPosibleTargetsToBuffer(FractionalHex position, Fix64 sightRange, ActTargetFilters filters, Team team, Dictionary <Hex, List <EntityOnVision> > unitsForeachHex, List <BuildingOnVision> buildingsOnSight, DynamicBuffer <BEPosibleTarget> buffer)
    {
        Hex roundedPosition = position.Round();

        //buildings
        foreach (var building in buildingsOnSight)
        {
            var buildingHex       = building.building.position;
            var reversedDirection = (position - (FractionalHex)buildingHex).NormalizedManhathan();
            var fullHexTargetPos  = FractionalHex.GetBorderPointOfTheHex(buildingHex, reversedDirection);

            var buildingPos = (FractionalHex)buildingHex;

            if (ValidPossibleTarget(position, sightRange, team.Number, filters, buildingPos, (Fix64)0.5, building.team))
            {
                buffer.Add(new BEPosibleTarget()
                {
                    Position        = buildingPos,
                    Radius          = Fix64.Zero,
                    Entity          = building.entity,
                    IsUnit          = false,
                    OccupiesFullHex = true,
                    OccupyingHex    = buildingHex,
                    GatherTarget    = false,
                    ActTypeTarget   = filters.actType
                });
            }
        }


        //units
        var pointsOfHex = SpartialSortUtils.GetAllPointsAtRange(roundedPosition, (int)Fix64.Ceiling(sightRange), unitsForeachHex);

        foreach (var point in pointsOfHex)
        {
            int    pointTeam   = point.team;
            Entity pointEntity = point.entity;
            var    pointRadius = point.collider.Radius;


            if (ValidPossibleTarget(position, sightRange, team.Number, filters, point.position, pointRadius, pointTeam))
            {
                buffer.Add(new BEPosibleTarget()
                {
                    Position        = point.position,
                    Radius          = pointRadius,
                    Entity          = pointEntity,
                    IsUnit          = true,
                    OccupiesFullHex = false,
                    OccupyingHex    = point.position.Round(),
                    GatherTarget    = false,
                    ActTypeTarget   = filters.actType
                });
            }
        }
    }
예제 #2
0
    /// <summary>
    /// Gets the target if true.
    /// debo ver una buena manera de conseguir el parent entity.
    /// </summary>
    private static bool GetPriorityTargetAndAddPosibleTargets(out PriorityGroupTarget target, FractionalHex position, Fix64 sightRange, Team team, ActTargetFilters filters, Dictionary <Hex, List <EntityOnVision> > unitsForeachHex, List <BuildingOnVision> buildingsOnSight, DynamicBuffer <BEPosibleTarget> buffer)
    {
        //no es lo más eficiente. Pero es más facil de mantener.
        FindAndAddPosibleTargetsToBuffer(position, sightRange, filters, team, unitsForeachHex, buildingsOnSight, buffer);


        Hex roundedPosition = position.Round();
        var pointsOfHex     = SpartialSortUtils.GetAllPointsAtRange(roundedPosition, (int)Fix64.Ceiling(sightRange), unitsForeachHex);

        target = new PriorityGroupTarget();
        var  bestTargetDistance    = Fix64.MaxValue;
        bool arePrioriryTarget     = false;
        bool areUnitPriorityTarget = false;

        //units firs because we search if there is a unit in sight and if so there cannot be a building as a priority target.
        foreach (var point in pointsOfHex)
        {
            int    pointTeam   = point.team;
            Entity pointEntity = point.entity;
            var    pointLayer  = point.collider.Layer;
            var    pointRadius = point.collider.Radius;


            if (ValidPossibleTarget(position, sightRange, team.Number, filters, point.position, pointRadius, pointTeam))
            {
                //buffer.Add(new BEPosibleTarget()
                //{
                //    Position = point.position,
                //    Radius = pointRadius,
                //    Entity = pointEntity,
                //    IsUnit = true,
                //    GatherTarget = false,
                //    OccupiesFullHex = false,
                //    ActTypeTarget = filters.actType
                //});
                //hay que ver si hay algo que bloquea el paso
                if (MapUtilities.PathToPointIsClear(position, point.position))
                {
                    areUnitPriorityTarget = true;
                    arePrioriryTarget     = true;
                    var distance = point.position.Distance(position);
                    if (distance < bestTargetDistance)
                    {
                        bestTargetDistance = distance;
                        target             = new PriorityGroupTarget()
                        {
                            //TargetPosition = point.position,
                            //TargetEntity = pointEntity,
                            TargetHex = point.position.Round(),
                            //IsUnit = true
                        };
                    }
                }
            }
        }

        //buildings
        foreach (var building in buildingsOnSight)
        {
            var buildingPos = (FractionalHex)building.building.position;

            //if (ValidPossibleTarget(position, sightRange, team.Number, filters, buildingPos, (Fix64)0.5, building.team))
            //{
            //    buffer.Add(new BEPosibleTarget()
            //    {
            //        Position = buildingPos,
            //        Radius = Fix64.Zero,
            //        Entity = building.entity,
            //        IsUnit = false,
            //        GatherTarget = false,
            //        OccupiesFullHex = true,
            //        ActTypeTarget = filters.actType
            //    });
            //}
            if (ValidPossibleTarget(position, sightRange, team.Number, filters, buildingPos, (Fix64)0.5, building.team))
            {
                if (!areUnitPriorityTarget)
                {
                    if (MapUtilities.PathToPointIsClear(position, buildingPos))
                    {
                        arePrioriryTarget = true;
                        var distance = buildingPos.Distance(position);
                        if (distance < bestTargetDistance)
                        {
                            bestTargetDistance = distance;
                            target             = new PriorityGroupTarget()
                            {
                                //TargetPosition = buildingPos,
                                //TargetEntity = building.entity,
                                TargetHex = buildingPos.Round(),
                                //IsUnit = false
                            };
                        }
                    }
                }
            }
        }

        return(arePrioriryTarget);
    }
예제 #3
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);
                    }
                }
            }
        });
    }