Пример #1
0
            public static void Wander(Entity e, NativeReference <Random> RNG, ComponentDataFromEntity <Position> PosFromEntity, PathingBitGrid pathMap,
                                      GridData2D <bool> obstacles,
                                      GridData2D <Entity> mapEntities)
            {
                int  dirIndex = RNG.NextInt(0, 8);
                int2 dir      = Grid2D.Directions8Way[dirIndex];
                int2 curr     = PosFromEntity[e];
                int2 next     = curr + dir;

                if (!pathMap[next])
                {
                    MapUtility.MoveActor(e, PosFromEntity,
                                         curr, next, obstacles, mapEntities);
                }
            }
Пример #2
0
            public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
            {
                var viewAccessor = chunk.GetBufferAccessor(ROViewHandle);

                var obstacles   = MapStateData.Obstacles;
                var mapEntities = MapStateData.Entities;
                int playerIndex = 0;

                if (PlayerEntity != Entity.Null)
                {
                    int2 playerPos = PosFromEntity[PlayerEntity];
                    playerIndex = obstacles.PosToIndex(playerPos);
                }


                var entities = chunk.GetNativeArray(EntityHandle);

                var energyArr = chunk.GetNativeArray(EnergyHandle);
                var attackArr = chunk.GetNativeArray(AttackPowerHandle);

                //Debug.Log("Monster AI running");

                for (int i = 0; i < viewAccessor.Length; ++i)
                {
                    Entity e          = entities[i];
                    var    viewBuffer = viewAccessor[i].Reinterpret <bool>();
                    var    pathMap    = new PathingBitGrid(obstacles, Allocator.Temp);

                    //Debug.Log($"Monster is taking a turn");

                    energyArr[i] -= Energy.ActionThreshold;

                    // For testing purposes - comment out
                    // "RequireSingletonForUpdate<Player>()" in OnCreate to
                    // let monsters wander in "real time"
                    if (PlayerEntity == Entity.Null)
                    {
                        Wander(e, RNG, PosFromEntity, pathMap, obstacles, mapEntities);
                        return;
                    }

                    if (viewBuffer.Length == 0)
                    {
                        return;
                    }

                    bool playerIsVisible = viewBuffer[playerIndex];
                    if (playerIsVisible)
                    {
                        int2 curr       = PosFromEntity[e];
                        int  startIndex = obstacles.PosToIndex(curr);
                        if (startIndex == playerIndex)
                        {
                            continue;
                        }

                        // Open the player's space for pathfinding
                        pathMap[playerIndex] = false;
                        var astar = new AStar(10, Allocator.Temp);
                        var path  = new NativeList <int>(10, Allocator.Temp);
                        astar.FindPath(pathMap, startIndex, playerIndex, path);
                        if (path.Length > 2)
                        {
                            //Debug.Log("MONSTER MOVED");
                            int2 next = obstacles.IndexToPos(path[1]);
                            MapUtility.MoveActor(e, PosFromEntity,
                                                 curr, next, obstacles, mapEntities);
                        }
                        if (path.Length == 2)
                        {
                            //Debug.Log("Monster is next to the player");
                            var defend = DefendBFE[PlayerEntity];
                            var attack = attackArr[i];
                            int dmg    = RNG.RollDice(attack);
                            defend.Add(new CombatDefendBuffer
                            {
                                Attacker = e,
                                Damage   = dmg
                            });
                        }
                        pathMap[playerIndex] = true;
                    }
                }
            }