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(); }
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; } } }
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; } }
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; } }
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(); }
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(); }
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(); }
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); }