public static void GenerateOnThread(IntVector2 coord, Action <RegionState> onComplete) { object lockObj = new object(); RegionState regionState = null; Thread genThread = new Thread(new ThreadStart(() => { RegionState rs = Generate(coord); lock (lockObj) { regionState = rs; } })); TofuAnimation waitForGeneration = new TofuAnimation() .Execute(() => { genThread.Start(); }) .WaitUntil(() => { lock (lockObj) { return(regionState != null); } }) .Then() .Execute(() => { onComplete(regionState); }) .Play(); }
public void SetCenterRegion(IntVector2 coord, Action onComplete) { TofuAnimation currentLoadAnim = null; int numRegionsLoaded = 0; for (int x = -SimulateRegionDistance; x <= SimulateRegionDistance; x++) { for (int y = -SimulateRegionDistance; y <= SimulateRegionDistance; y++) { IntVector2 regionCoord = coord + new IntVector2(x, y); new TofuAnimation() .WaitUntil(() => { // by waiting for the current loadAnim to be null, we ensure that we only generate/load one region at a time (since it will be threaded) return(currentLoadAnim == null); }) .Then() .Execute(() => { RegionState regionState = null; currentLoadAnim = new TofuAnimation() .Execute(() => { LoadRegionState(regionCoord, (RegionState payload) => { regionState = payload; }); }) .WaitUntil(() => { return(regionState != null); }) .Then() .Execute(() => { _state.AddRegionState(regionCoord, regionState); numRegionsLoaded++; currentLoadAnim = null; }) .Play(); }) .Play(); } } new TofuAnimation() .WaitUntil(() => { return(numRegionsLoaded == ((SimulateRegionDistance * 2) + 1) * ((SimulateRegionDistance * 2 + 1))); }) .Then() .Execute(() => { onComplete(); }) .Play(); }
public void AddRegionState(IntVector2 coord, RegionState regionState) { if (!regions.ContainsKey(coord.x)) { regions.Add(coord.x, new Dictionary <int, RegionState>()); } regions[coord.x][coord.y] = regionState; }
public bool TryGetRegionState(IntVector2 coord, out RegionState regionState) { if (regions.TryGetValue(coord.x, out Dictionary <int, RegionState> yDict)) { if (yDict.TryGetValue(coord.y, out RegionState rs)) { regionState = rs; return(true); } } regionState = null; return(false); }
public Region(World world, RegionState state) : base($"Region {state.coord.ToString()}") { this.world = world; _state = state; for (int x = 0; x < _state.tiles.GetLength(0); x++) { for (int y = 0; y < _state.tiles.GetLength(1); y++) { IntVector2 tileCoord = new IntVector2(x, y); Tile tile = new Tile(this, _state.tiles[x, y], new IntVector2(x, y)); tile.LocalPosition = tileCoord.ToUnityVector3_XY(); AddChild(tile); } } }
public static RegionState Generate(IntVector2 coord) { RegionState toReturn = new RegionState(); toReturn.coord = coord; for (int x = 0; x < toReturn.tiles.GetLength(0); x++) { for (int y = 0; y < toReturn.tiles.GetLength(1); y++) { TileState tileState = new TileState(); // TODO: perlin noise to use different variations of the tile toReturn.tiles[x, y] = tileState; } } return(toReturn); }