public static void SetBlock(Position position, Block newBlock) { lock (_lock) { var coords = new ChunkCoords(position); _chunksN[coords].Blocks[position] = newBlock; } }
public static void InstantFillChunks(List <ChunkCoords> coords) { for (int i = 0; i < coords.Count; i++) { ChunkCoords c = coords[i]; FillChunk(c); } }
public virtual void Initialise() { coords = new ChunkCoords(transform.position, EntityNetwork.CHUNK_SIZE); EntityNetwork.AddEntity(this, coords); RepositionInNetwork(true); healthComponent?.SetToUpperLimit(); enabled = true; }
public TileChunk RequiredChunk(ChunkCoords coords) { if (!loadedChunks.ContainsKey(coords)) { return(GenerateChunk(coords)); } return(loadedChunks[coords]); }
public bool Equals(ChunkCoords other) { if (other == null) { return(false); } return(other.X == X && other.Z == Z); }
public void Update() { _playerChunkCoords = GetChunkCoordsFromVector3(player.position); if (!_playerChunkCoords.Equals(_playerLastChunkCoords)) { CheckViewDistance(); } }
/// Only called if the camera's coordinates change private void CoordsChanged(ChunkCoords newCoords, ChunkCoords oldCoords) { coords = newCoords; if (!EntityNetwork.IsReady) { return; } UpdateEntitiesInView(newCoords, oldCoords); }
public static void AddChunk(ChunkCoords coords, Chunk chunk) { lock (_lock) { _chunksN.Add(coords, chunk); _chunksS.Add(new ChunkCoords(Global.MaxChunkLimit - coords.X, Global.MaxChunkLimit - coords.Z), chunk); _chunksE.Add(new ChunkCoords(Global.MaxChunkLimit - coords.X, coords.Z), chunk); _chunksW.Add(new ChunkCoords(coords.X, Global.MaxChunkLimit - coords.Z), chunk); } }
public static List <Entity> SpawnEntityInChunkNorthOfCamera(SpawnableEntity se, EntityData?data = null) { Vector3 cameraPos = Camera.main.transform.position; ChunkCoords cc = new ChunkCoords(cameraPos, EntityNetwork.CHUNK_SIZE); cc.y++; cc = cc.Validate(); return(SpawnEntityInChunk(se, data, cc)); }
public void UnloadChunk(ChunkCoords coords) { if (!loadedChunks.ContainsKey(coords)) { return; } loadedChunks.Remove(coords); Events.TriggerTileChunkRemoved(coords); }
//This will determine where to set up more particle systems and create them in those positions private void Expansion() { int size = Random.Range(minSystemSize, maxSystemSize + 1); cluster = new List <NebulaSetup>(size); List <ChunkCoords> filled = new List <ChunkCoords>(size); ChunkCoords c = coords; filled.Add(c); int count = 1; int failCount = 0; while (count < size && failCount < FAIL_LIMIT) { c = filled[Random.Range(0, filled.Count)]; //pick a random adjacent coordinate float randomVal = Random.value; if (randomVal >= 0.5f) { c.x += randomVal >= 0.75f ? 1 : -1; } else { c.y += randomVal >= 0.25f ? 1 : -1; } c = c.Validate(); bool alreadyExists = false; //this will check for nebulas already created in this group for (int i = 0; i < filled.Count; i++) { ChunkCoords check = filled[i]; if (c == check) { alreadyExists = true; break; } } //if new coordinates have already been filled with nebula then pick a new coordinate if (alreadyExists) { failCount++; continue; } count++; filled.Add(c); NebulaSetup newNebula = Instantiate(this, transform.parent); cluster.Add(newNebula); newNebula.cluster = cluster; newNebula.SetColors(col1, col2); newNebula.SetThrusterReference(thrusterRef); newNebula.transform.position = ChunkCoords.GetCenterCell(c, EntityNetwork.CHUNK_SIZE); newNebula.shouldExpand = false; EntityGenerator.FillChunk(c, true); } }
private void CreateRiver() { for (var i = 0; i < Settings.RiverCount; i++) { var river = new River(worldInstance); var chunkCoords = new ChunkCoords(river.Source); Log.WriteInfo($"Creating river from chunk {chunkCoords}"); Rivers.Add(river); } }
/// Only called if position of the camera changes private void Moved(Vector2 newPos) { ChunkCoords newCc = new ChunkCoords(newPos, EntityNetwork.CHUNK_SIZE); if (newCc == coords) { return; } CoordsChanged(newCc, coords); }
/// Returns a list of all entities located in cells within range of the given coordinates public static bool IterateEntitiesInRange(ChunkCoords center, int range, Func <Entity, bool> action) { return(IterateCoordsInRange( center, range, cc => { return IterateEntitiesAtCoord(cc, action); }, false)); }
public static List <Entity> SpawnEntityInChunkNorthOfCamera(string entityName) { Vector3 cameraPos = Camera.main.transform.position; ChunkCoords cc = new ChunkCoords(cameraPos, EntityNetwork.CHUNK_SIZE); cc.y++; cc = cc.Validate(); SpawnableEntity se = GetSpawnableEntity(entityName); return(SpawnEntityInChunk(se, null, cc)); }
public TileChunk GenerateChunk(ChunkCoords coords) { if (loadedChunks.ContainsKey(coords)) { return(loadedChunks[coords]); } var chunk = generator.GenerateChunk(coords); Events.TriggerTileChunkGenerated(coords, chunk); return(loadedChunks[coords] = chunk); }
public void AssignUnoccupiedCoords(GatherBot b) { if (b == null) { return; } CheckEmptyMarkedCoords(); botOccupiedCoords.Clear(); botOccupiedCoords.AddRange(emptyCoords); for (int i = 0; i < childBots.Count; i++) { EntityNetwork.IterateCoordsInRange( childBots[i].GetIntendedCoords(), 1, cc => { botOccupiedCoords.Add(cc); return(false); }, false); } //find a random nearby coordinate that is not already occupied int searchRange = 1; ChunkCoords location = ChunkCoords.Invalid; while (location == ChunkCoords.Invalid) { EntityNetwork.IterateCoordsOnRangeBorder( coords, searchRange, cc => { if (botOccupiedCoords.Contains(cc)) { return(false); } if (Random.value < 0.1f) { location = cc; return(true); } return(false); }, false); searchRange++; } Vector2 pos = ChunkCoords.GetCenterCell(location, EntityNetwork.CHUNK_SIZE); b.HiveOrders(pos); }
/* * internal bool IsChunkLoaded(int x, int z) * { * lock (mapChunks) * { * if (x > MaxChunkLimit || x < -MaxChunkLimit || z > MaxChunkLimit || z < -MaxChunkLimit) throw new ArgumentException("Chunk index exceeded"); * int idx = x * MaxChunkLimit + z; * return mapChunks.ContainsKey(idx); * } * } */ public bool IsChunkLoaded(ChunkCoords chunkCoords) { lock (mapChunks) { if (chunkCoords.X > MaxChunkLimit || chunkCoords.X < -MaxChunkLimit || chunkCoords.Z > MaxChunkLimit || chunkCoords.Z < -MaxChunkLimit) { throw new ArgumentException("Chunk index exceeded"); } int idx = chunkCoords.X * MaxChunkLimit + chunkCoords.Z; return(mapChunks.ContainsKey(idx)); } }
private static List <string> sent = new List <string>(); // TODO - do better public void GetMap(int x, int z) { string hash = $"{x},{z}"; if (!sent.Contains(hash)) { sent.Add(hash); var coords = new ChunkCoords(x, z); var chunk = WorldInstance.GetChunk(coords); MapManager.AddChunk(coords, chunk); } }
public TileChunk GenerateChunk(ChunkCoords coords) { short[,] tiles = new short[TileChunk.Size, TileChunk.Size]; for (int y = 0; y < TileChunk.Size; y++) { for (int x = 0; x < TileChunk.Size; x++) { tiles[x, y] = 0; } } return(new TileChunk(coords, tiles)); }
private bool ChunkExists(ChunkCoords cc) { if (cc.quadrant < 0 || (int)cc.quadrant >= items.Count) { return(false); } if (cc.x < 0 || cc.x >= Quad(cc).Count) { return(false); } return(cc.y > 0 && cc.y < Column(cc).Count); }
private void Start() { //get ref to ChunkFiller component chunkFiller = chunkFiller ?? GetComponent <ChunkFiller>(); //get camera's coordinates on the grid coords = new ChunkCoords(transform.position, EntityNetwork.CHUNK_SIZE); //start camera size at minimum size CamSize = minCamSize; //default follow target to shuttle if no target is set followTarget = followTarget ?? FindObjectOfType <Shuttle>(); LoadingController.AddListener(Initialise); }
public static void ChunkIgnore(ChunkCoords chunkCoords, Guid clientId) { var reg = registrations [chunkCoords]; if (reg != null) { reg.Remove(clientId); if (reg.Count == 0) { registrations.Remove(chunkCoords); } } }
private void FillChunks(ChunkCoords center, bool batchOrder = false) { chunksToFill.Clear(); GetChunkFillList(center, chunksToFill); if (!batchOrder) { EntityGenerator.InstantFillChunks(chunksToFill); } else { EntityGenerator.EnqueueBatchOrder(chunksToFill); } }
private static void OnGeneratorWorldEvents(WorldEventArgs e) { var chunkCoords = new ChunkCoords(e.blockLocation); if (!registrations.ContainsKey(chunkCoords)) { return; } foreach (var client in registrations[chunkCoords]) { MessageProcessor.SendMapUpdate(client, e.blockLocation, e.action, e.block); } }
private void ProcessMessage(Guid clientId, Message msg) { try { //Console.WriteLine ($"Processing response... {msg.ToString ()}"); if (msg.Map != null) { var position = msg.Map.MinPosition; var coords = new ChunkCoords(position); var chunk = Sean.Shared.Chunk.Deserialize(coords, msg.Data); MapManager.AddChunk(coords, chunk); } if (msg.WorldMapResponse != null) { var size = new ArraySize() { scale = msg.WorldMapResponse.Scale, minX = msg.WorldMapResponse.MinPosition.X, minY = msg.WorldMapResponse.MinPosition.Y, minZ = msg.WorldMapResponse.MinPosition.Z, maxX = msg.WorldMapResponse.MaxPosition.X, maxY = msg.WorldMapResponse.MaxPosition.Y, maxZ = msg.WorldMapResponse.MaxPosition.Z, }; var map = new Array <byte>(size); map.DeSerialize(msg.Data); if (msg.WorldMapResponse.MapRequestType == Sean.Shared.Comms.MapRequestType.HeightMap) { MapManager.SetWorldMapHeight(map); } else if (msg.WorldMapResponse.MapRequestType == Sean.Shared.Comms.MapRequestType.Terrain) { MapManager.SetWorldMapTerrain(map); } } if (msg.MapCharacterUpdate != null) { CharacterManager.UpdateLocation(msg.MapCharacterUpdate.CharacterId, msg.MapCharacterUpdate.Position); } if (msg.MapUpdate != null) { MapManager.SetBlock(msg.MapUpdate.Position, msg.MapUpdate.NewBlock); } } catch (Exception e) { Console.WriteLine("Exception caught in ProcessMessage - {0}", e.ToString()); } }
private List <ChunkCoords> GetChunkFillList(ChunkCoords center, List <ChunkCoords> addToList = null) { int r = FillRange + RangeIncrease; int listSize = ((r + 1) * 2) * ((r + 1) * 2); List <ChunkCoords> coords = addToList ?? new List <ChunkCoords>(listSize); for (int i = -r; i <= r; i++) { for (int j = -r; j <= r; j++) { coords.Add(new ChunkCoords(center.quadrant, center.x + i, center.y + j, true)); } } return(coords); }
/// Returns a list of coordinates around a given center coordinate in a specified range public static bool IterateCoordsInRange(ChunkCoords center, int range, Func <ChunkCoords, bool> action, bool ignoreLackOfExistenceInGrid) { int r = range + 2; //loop through surrounding chunks for (int i = 0; i <= range; i++) { if (IterateCoordsOnRangeBorder(center, i, action, ignoreLackOfExistenceInGrid)) { return(true); } } return(false); }
private bool ChunkExists(ChunkCoords cc) { if (cc.quadrant < 0 || (int)cc.quadrant >= wasFilled.Count) { return(false); } if (cc.x < 0 || cc.x >= Quad(cc).Count) { return(false); } if (cc.y < 0 || cc.y >= Column(cc).Count) { return(false); } return(true); }
/// Returns whether a chunk contains any entities of a certain type public static bool ContainsType(EntityType type, ChunkCoords c, Entity entToExclude = null) { if (!ChunkExists(c)) { return(false); } for (int i = 0; i < Chunk(c).Count; i++) { Entity e = Chunk(c)[i]; if (e.GetEntityType() == type && e != entToExclude) { return(true); } } return(false); }