public MultiRequest(HashSet <TilePos> tiles, GenerationStage stage, string v, Action <bool, Tile[]> p) { this.indices = tiles; this.stage = stage; this.message = v; this.action = p; }
public bool Request(TilePos index, GenerationStage stage, string reason, Action <bool, Tile> action /* , [CallerMemberName] string memberName = "", * [CallerFilePath] string sourceFilePath = "", * [CallerLineNumber] int sourceLineNumber = 0*/) { using (Profiler.CurrentProfiler.Begin("Requesting Tile Stage")) { //Debug.WriteLine($"Requested Tile {index} stage {stage} because: {reason}"); return(Load(index, (wasImmediate0, tile) => { tile.ScheduleOnUpdate(wasImmediate1 => { if (tile.IsAtPhase(stage)) { action(wasImmediate0 && wasImmediate1, tile); } else { tile.InvokeWhenCompleted(stage, b => action(b && wasImmediate0 && wasImmediate1, tile), $"awaiting stage {stage}");// from {memberName} at {sourceFilePath}:{sourceLineNumber}"); QueueTile(tile, stage); } }, "running task in-thread"); })); } }
/// <summary> /// Create a dungeon using the current starting region /// </summary> public void generate() { #if UNITY_EDITOR if (!UnityEditor.EditorApplication.isPlaying) { return; } #endif stage = GenerationStage.region_gen; startRegion.generationCompleted += endGeneration; startRegion.beginPlacement(!immediateGeneration); }
public bool SetRequiredPhase(GenerationStage required) { var oldRequired = RequiredPhase; RequiredPhase = (GenerationStage)Math.Max(Math.Max((int)RequiredPhase, (int)required), Math.Max((int)GeneratingPhase, (int)CompletedPhase)); if (oldRequired != RequiredPhase && !IsAtPhase(RequiredPhase)) { LogTileMessage($"New RequiredPhase: {RequiredPhase}"); } return(IsAtPhase(RequiredPhase)); }
/// <summary> /// /// </summary> /// <param name="phase"></param> /// <param name="action"></param> /// <returns>True if the action was executed immediately</returns> public bool InvokeWhenCompleted(GenerationStage phase, Action <bool> action, string reason) { action = TaskInProgress.Start(this, action, $"InvokeWhenCompleted[{phase}]:{reason}"); if (CompletedPhase >= phase && Thread.CurrentThread == VoxelGame.GameThread) { action(true); return(true); } else { _pendingActions[(int)phase].Enqueue(() => action(false)); return(false); } }
private void QueueTile(Tile tile, GenerationStage targetStage) { lock (_pendingTilesList) { if (!_pendingTilesSet.TryGetValue(tile, out var currentStage)) { _pendingTilesSet.Add(tile, targetStage); _pendingTilesList.Add(tile); } else if (currentStage < targetStage) { _pendingTilesSet[tile] = targetStage; } } }
private void QueuePendingTiles() { using (Profiler.CurrentProfiler.Begin("Queuing Pending Tiles")) { lock (_pendingTilesList) { for (int j = 0; j < 1 && InProgressTiles < TileInProgressThreshold && _pendingTilesList.Count > 0; j++) { Tile closestTile = null; GenerationStage closestStage = GenerationStage.Unstarted; double closestDistance = -1; int closestIndex = -1; for (int i = 0; i < _pendingTilesList.Count; i++) { var tile = _pendingTilesList[i]; var stage = _pendingTilesSet[tile]; var distance = tile.Centroid.RelativeTo(_lastPlayerPosition).Length(); if (closestTile == null || distance < closestDistance) { closestTile = tile; closestStage = stage; closestDistance = distance; closestIndex = i; } } if (closestTile != null && _pendingTilesList.Count > 0) { _pendingTilesList.RemoveAt(closestIndex); _pendingTilesSet.Remove(closestTile); closestTile.SetRequiredPhase(closestStage); closestTile.ScheduleNextPhase(false); } } } } }
private void endGeneration() { startRegion.generationCompleted -= endGeneration; Debug.Log("[WB] Generation Complete!"); TileSet set = tileSets[Random.Range(0, tileSets.Length - 1)]; if (set == null) { Debug.LogError("[WB] Null tileset in tileset array."); return; } stage = GenerationStage.tile_placement; if (!immediateGeneration) { StartCoroutine(placeTiles(set)); } else { IEnumerator m = placeTiles(set); while (m.MoveNext()) { } } }
public bool IsAtPhase(GenerationStage stage) { return(CompletedPhase >= stage); }
public PendingContext(GenerationStage stage, Tile tile) { this.stage = stage; this.tile = tile; }