Exemplo n.º 1
0
        protected override void OnUpdate()
        {
            var rand = _rand;

            var mapEntity = _mapQuery.GetSingletonEntity();
            var mapState  = EntityManager.GetBuffer <MapState>(mapEntity);
            var mapData   = EntityManager.GetComponentData <MapData>(mapEntity);

            Entities
            .WithAll <Monster>()
            .WithAll <TakingATurn>()
            .WithStoreEntityQueryInField(ref _monsterQuery)
            .ForEach((Entity e, ref Position pos, ref Energy energy) =>
            {
                // Come on man
                var r = rand[0];

                var dir = GetRandomDirection(ref r);
                rand[0] = r;

                int2 dest     = pos + dir;
                int destIndex = dest.y * mapData.width + dest.x;

                if (MapUtility.CellIsUnblocked(destIndex, mapState))
                {
                    int oldIndex = MapUtility.PosToIndex(pos, mapData.width);
                    pos          = dest;

                    MapUtility.MoveActor(e, oldIndex, destIndex, mapState);
                }

                energy -= Energy.ActionThreshold;
            }).Run();
        }
Exemplo n.º 2
0
 static void BuildRoom(DynamicBuffer <MapTiles> map, MapData mapData, IntRect room)
 {
     for (int x = room.Min.x; x <= room.Max.x; ++x)
     {
         for (int y = room.Min.y; y <= room.Max.y; ++y)
         {
             int i = MapUtility.PosToIndex(x, y, mapData.width);
             map[i] = TileType.Floor;
         }
     }
 }
Exemplo n.º 3
0
        static void BuildVerticalTunnel(DynamicBuffer <MapTiles> map, MapData mapData,
                                        int y1, int y2, int x)
        {
            int yMin = math.min(y1, y2);
            int yMax = math.max(y1, y2);

            for (int y = yMin; y <= yMax; ++y)
            {
                int i = MapUtility.PosToIndex(x, y, mapData.width);
                map[i] = TileType.Floor;
            }
        }
Exemplo n.º 4
0
        static void BuildHorizontalTunnel(DynamicBuffer <MapTiles> map, MapData mapData,
                                          int x1, int x2, int y)
        {
            int xMin = math.min(x1, x2);
            int xMax = math.max(x1, x2);

            for (int x = xMin; x <= xMax; ++x)
            {
                int i = MapUtility.PosToIndex(x, y, mapData.width);
                map[i] = TileType.Floor;
            }
        }
Exemplo n.º 5
0
        void CleanDestroyedActors(DynamicBuffer <MapState> mapState, MapData mapData, EntityCommandBuffer buffer)
        {
            int mapWidth = mapData.width;

            Entities
            .WithNone <Collidable>()
            .ForEach((int entityInQueryIndex, Entity e, in CollidableStateComponent state) =>
            {
                var p     = state.prevPos;
                int index = MapUtility.PosToIndex(p, mapWidth);

                // Clear the cell
                mapState[index] = default;

                buffer.RemoveComponent <CollidableStateComponent>(e);
            }).Schedule();
        }
Exemplo n.º 6
0
        void HandleMovedActors(DynamicBuffer <MapState> mapState, MapData mapData)
        {
            int mapWidth = mapData.width;

            Entities
            .WithChangeFilter <Position>()
            .WithAll <Collidable>()
            .ForEach((Entity e, ref CollidableStateComponent state, in Position pos) =>
            {
                int2 p    = pos;
                int2 prev = state.prevPos;
                if (p.x != prev.x || p.y != prev.y)
                {
                    int oldIndex = MapUtility.PosToIndex(prev, mapWidth);
                    int newIndex = MapUtility.PosToIndex(p, mapWidth);

                    MapUtility.MoveActor(e, oldIndex, newIndex, mapState);
                }

                state.prevPos = pos;
            }).Schedule();
        }
Exemplo n.º 7
0
        void HandleNewActors(DynamicBuffer <MapState> mapState, MapData mapData, EntityCommandBuffer buffer)
        {
            int mapWidth = mapData.width;

            Entities
            .WithAll <Collidable>()
            .WithNone <CollidableStateComponent>()
            .ForEach((int entityInQueryIndex, Entity e, in Position pos) =>
            {
                int2 p          = pos;
                int index       = MapUtility.PosToIndex(p, mapWidth);
                var cell        = mapState[index];
                cell.content    = e;
                cell.blocked    = true;
                mapState[index] = cell;

                buffer.AddComponent <CollidableStateComponent>(e,
                                                               new CollidableStateComponent
                {
                    prevPos = pos
                });
            }).Schedule();
        }
Exemplo n.º 8
0
        protected override void OnUpdate()
        {
            if (_quitAction.triggered)
            {
                Application.Quit();
                return;
            }

            int2 dir = (int2)(_moveAction.triggered ? (float2)_moveAction.ReadValue <Vector2>() : float2.zero);

            if (dir.x == 0 && dir.y == 0)
            {
                return;
            }

            var mapEntity = _mapQuery.GetSingletonEntity();
            var mapData   = EntityManager.GetComponentData <MapData>(mapEntity);
            var mapState  = EntityManager.GetBuffer <MapState>(mapEntity);

            var monsterFromEntity = GetComponentDataFromEntity <Monster>(true);

            var buffer = _barrier.CreateCommandBuffer();

            Entities
            .WithReadOnly(monsterFromEntity)
            .WithAll <Player>()
            .WithAll <TakingATurn>()
            .ForEach((Entity e, ref Energy energy, ref Position pos) =>
            {
                int2 dest     = pos + dir;
                int destIndex = dest.y * mapData.width + dest.x;

                // Don't use up an action if we don't do anything meaningful
                int cost = 0;

                if (MapUtility.CellIsUnblocked(destIndex, mapState))
                {
                    int oldIndex = pos.value.y * mapData.width + pos.value.x;
                    pos          = dest;

                    // Immediately update map state so the next actors have up to date state.
                    MapUtility.MoveActor(e, oldIndex, destIndex, mapState);

                    cost = Energy.ActionThreshold;
                }
                else
                {
                    var cell = mapState[destIndex];

                    if (monsterFromEntity.Exists(cell.content))
                    {
                        buffer.AddComponent(e, new Attacking
                        {
                            target = cell.content
                        });

                        cost = Energy.ActionThreshold;
                    }
                }

                energy -= cost;
            }).Run();
        }
Exemplo n.º 9
0
        void SetMonsterCount(int current, int target)
        {
            if (current > target)
            {
                var monsters = _monstersQuery.ToEntityArray(Allocator.TempJob);

                int removeCount = current - target;
                var slice       = new NativeSlice <Entity>(monsters, current - removeCount, removeCount);

                EntityManager.DestroyEntity(slice);

                monsters.Dispose();
                return;
            }

            uint   seed = (uint)UnityEngine.Random.Range(1, int.MaxValue);
            Random rand = new Random(seed);

            var buffer = _barrier.CreateCommandBuffer();

            var roomsEntity = GetSingletonEntity <MapRooms>();
            var rooms       = EntityManager.GetBuffer <MapRooms>(roomsEntity);

            if (rooms.Length <= 1)
            {
                return;
            }

            var prefabs = _monsterPrefabsQuery.ToEntityArray(Allocator.TempJob);

            var mapEntity = _mapQuery.GetSingletonEntity();
            var mapState  = EntityManager.GetBuffer <MapState>(mapEntity);
            int mapWidth  = EntityManager.GetComponentData <MapData>(mapEntity).width;

            Job
            .WithReadOnly(prefabs)
            .WithReadOnly(rooms)
            .WithCode(() =>
            {
                for (int i = 0; i < target - current; ++i)
                {
                    int prefabIndex = rand.NextInt(0, prefabs.Length);

                    // Try to find a viable spot up to 5 times
                    for (int j = 0; j < 5; ++j)
                    {
                        var room = rooms[rand.NextInt(1, rooms.Length)];
                        var p    = RandomPointInRoom(ref rand, room);

                        int index = MapUtility.PosToIndex(p, mapWidth);

                        if (mapState[index].blocked)
                        {
                            continue;
                        }

                        var monster = buffer.Instantiate(prefabs[prefabIndex]);

                        mapState[index] = new MapState
                        {
                            blocked = true,
                            content = monster
                        };

                        buffer.SetComponent <Position>(monster, p);
                        buffer.SetComponent <Speed>(monster, rand.NextInt(20, 30));
                        break;
                    }
                }
            }).Schedule();

            prefabs.Dispose(Dependency);
        }