/// Increases capacity of the fill trigger list to accomodate given coordinates private void GenerateVoid(ChunkCoords cc) { //ignore if given coordinates are invalid or they already exist if (!cc.IsValid()) { return; } //add more quadrants until enough exist to make the given coordinates valid while (wasFilled.Count <= (int)cc.quadrant) { wasFilled.Add(new List <List <bool> >()); } //add more quadrants until enough exist to make the given coordinates valid while (Quad(cc).Count <= cc.x) { Quad(cc).Add(new List <bool>()); } //add more quadrants until enough exist to make the given coordinates valid while (Column(cc).Count <= cc.y) { Column(cc).Add(false); } }
/// Returns whether a given chunk is valid and exists in the network public static bool ChunkExists(ChunkCoords cc) { //if coordinates are invalid then chunk definitely doesn't exist if (!cc.IsValid() || !IsReady) { return(false); } //if the quadrant doesn't have x amount of columns or that column doesn't have y amount of cells, //the chunk doesn't exist if ((int)cc.quadrant >= instance.grid.Count || cc.x >= Quad(cc).Count || cc.y >= Column(cc).Count) { return(false); } return(true); }
/// Adds a given entity to the list at given coordinates public static bool AddEntity(Entity e, ChunkCoords cc) { if (!cc.IsValid()) { Debug.LogWarning("Coordinates to add entity to are invalid."); return(false); } instance.EnsureChunkExists(cc); Chunk(cc).Add(e); //update list of occupied coordinates if (Chunk(cc).Count == 1) { instance.occupiedCoords.Add(cc); } //set entity's coordinates to be equal to the given coordinates e.SetCoordinates(cc); return(true); }
/// Removes an entity from its position in the network and replaces it and the given destination /// This will mostly be used by entities themselves as they are responsible for determining their place in the network public static bool Reposition(Entity e, ChunkCoords destChunk) { if (e.GetCoords() == destChunk) { return(true); } if (!destChunk.IsValid()) { Debug.LogWarning("Destination coordinates are invalid."); return(false); } if (RemoveEntity(e)) { return(AddEntity(e, destChunk)); } Debug.LogWarning("Entity failed to reposition on grid."); return(false); }
/// Disables all entities previously in view, gets a new list of entities and enables them. private void UpdateEntitiesInView(ChunkCoords newCoords, ChunkCoords oldCoords) { //keep track of coords previous in view //query the EntityNetwork for a list of coordinates in view based on camera's size int range = ENTITY_VIEW_RANGE + RangeModifier; EntityNetwork.IterateEntitiesInRange( oldCoords, range, e => { ChunkCoords eCC = e.GetCoords(); if (!IsInRange(newCoords, eCC, range) || !newCoords.IsValid() || !eCC.IsValid()) { e.RepositionInNetwork(true); } return(false); }); EntityNetwork.IterateEntitiesInRange( newCoords, range, e => { ChunkCoords eCC = e.GetCoords(); if (IsInRange(oldCoords, eCC, range) || !oldCoords.IsValid() || !eCC.IsValid()) { e.RepositionInNetwork(true); } return(false); }); CheckPhysicsRange(newCoords, oldCoords); }
private void FillSpace(ChunkCoords cc) { if (!cc.IsValid()) { return; } while (items.Count <= (int)cc.quadrant) { items.Add(new List <List <List <CosmicItem> > >(1000)); } while (Quad(cc).Count <= cc.x) { Quad(cc).Add(new List <List <CosmicItem> >(1000)); } while (Column(cc).Count <= cc.y) { Column(cc).Add(new List <CosmicItem>(1000)); } }
public static void FillChunk(ChunkCoords cc, bool excludePriority = false) { //don't bother if the given coordinates are not valid if (!cc.IsValid()) { return; } //if these coordinates have no been generated yet then reserve some space for the new coordinates instance.GenerateVoid(cc); //don't bother if the coordinates have already been filled if (instance.Chunk(cc)) { return; } //flag that this chunk coordinates was filled instance.Column(cc)[cc.y] = true; //look through the space priority entities and check if one may spawn SpawnableEntity se = instance.ChooseEntityToSpawn( ChunkCoords.GetCenterCell(cc, EntityNetwork.CHUNK_SIZE).magnitude); SpawnEntityInChunkNonAlloc(se, null, cc); }
private void CheckPhysicsRange(ChunkCoords newCoords, ChunkCoords oldCoords) { int range = Constants.MAX_PHYSICS_RANGE; EntityNetwork.IterateEntitiesInRange( oldCoords, range, e => { ChunkCoords eCC = e.GetCoords(); if (!IsInRange(newCoords, eCC, range) || !newCoords.IsValid() || !eCC.IsValid()) { e.RepositionInNetwork(true); } return(false); }); EntityNetwork.IterateEntitiesInRange( newCoords, range, e => { ChunkCoords eCC = e.GetCoords(); if (IsInRange(oldCoords, eCC, range) || !oldCoords.IsValid() || !eCC.IsValid()) { e.RepositionInNetwork(true); } return(false); }); }