/// <summary> /// Checks to see if any pending entities can be added to the world and /// adds them if applicable. /// </summary> private void UpdatePendingEntities(Tick serverTick) { foreach (RailEntityClient entity in pendingEntities.Values) { if (!entity.HasReadyState(serverTick)) { continue; } // Note: We're using ToRemove here to remove from the *pending* list ToRemove.Add(entity); // If the entity was removed while pending, forget about it Tick removeTick = entity.RemovedTick; // Can't use ShouldRemove if (removeTick.IsValid && removeTick <= serverTick) { knownEntities.Remove(entity.Id); } else { RegisterEntity(entity); } } foreach (RailEntityClient entity in ToRemove) { pendingEntities.Remove(entity.Id); } ToRemove.Clear(); }
/// <summary>Commit changes made to the collection.</summary> async Task ICollectionRef <T> .Commit() { try { using LiteRepository _liteRepo = new LiteRepository(RefConfig.Location); if (ToSave.Any() || ToModify.Any()) { IList <T> _combinedList = ToSave.Concat(ToModify).ToList(); _liteRepo.Upsert <T>(_combinedList, RefConfig.Collection); } if (ToRemove.Any()) { BsonValue[] _bsonValues = ToRemove.Select(_id => new BsonValue(_id)).ToArray(); _liteRepo.DeleteMany <T>(Query.In("_id", _bsonValues), RefConfig.Collection); } await Task.Run(() => { ToSave.Clear(); ToModify.Clear(); ToRemove.Clear(); }); } catch (Exception ex) { throw ex; } }
public static void Update() { foreach (Entity e in Entities) { e.Update(); //if (e.ToRemove()) // ToRemove.Add(e); } foreach (Entity e in ToRemove) { Entities.Remove(e); } ToRemove.Clear(); foreach (Entity e in ToAdd) { Entities.Add(e); } ToAdd.Clear(); }
/// <summary> /// Updates the room a number of ticks. If we have entities waiting to be /// added, this function will check them and add them if applicable. /// </summary> public void ClientUpdate(Tick localTick, Tick estimatedServerTick) { Tick = estimatedServerTick; UpdatePendingEntities(estimatedServerTick); OnPreRoomUpdate(Tick); // Collect the entities in the priority order and // separate them out for either update or removal foreach (RailEntityBase railEntityBase in Entities) { RailEntityClient entity = (RailEntityClient)railEntityBase; if (entity.ShouldRemove) { ToRemove.Add(entity); } else { ToUpdate.Add(entity); } } // Wave 0: Remove all sunsetted entities ToRemove.ForEach(RemoveEntity); // Wave 1: Start/initialize all entities ToUpdate.ForEach(e => e.PreUpdate()); // Wave 2: Update all entities ToUpdate.ForEach(e => e.ClientUpdate(localTick)); // Wave 3: Post-update all entities ToUpdate.ForEach(e => e.PostUpdate()); ToRemove.Clear(); ToUpdate.Clear(); OnPostRoomUpdate(Tick); }
public void ServerUpdate() { Tick = Tick.GetNext(); OnPreRoomUpdate(Tick); // Collect the entities in the priority order and // separate them out for either update or removal foreach (RailEntityBase railEntityBase in Entities) { RailEntityServer entity = (RailEntityServer)railEntityBase; if (entity.ShouldRemove) { ToRemove.Add(entity); } else { ToUpdate.Add(entity); } } // Wave 0: Remove all sunsetted entities ToRemove.ForEach(RemoveEntity); // Wave 1: Start/initialize all entities ToUpdate.ForEach(e => e.PreUpdate()); // Wave 2: Update all entities ToUpdate.ForEach(e => e.ServerUpdate()); // Wave 3: Post-update all entities ToUpdate.ForEach(e => e.PostUpdate()); ToRemove.Clear(); ToUpdate.Clear(); OnPostRoomUpdate(Tick); }
public void Update(GameTime gameTime) { foreach (Bullet b in Bullets.ToArray()) { b.Update(gameTime); if (b.Bounds.Intersects(Parent.Player.Bounds)) { Parent.Health.Value--; AssetManager.PlaySound("Hit", 1f); Parent.GFM.SpawnMiniExplosion(b); Bullets.Remove(b); } else if (b.ShouldRemove) { Parent.GFM.SpawnMiniExplosion(b); Bullets.Remove(b); } } CurrentTime += gameTime.ElapsedGameTime.TotalSeconds; if (CurrentTime >= SpawnInterval) { CurrentTime = 0; for (int i = 0; i < Rand.Next(5); i++) { //Spawn at top of screen, choose random enemy int R = Rand.Next(4); if (R == 1) { Enemies.Add(new ShooterEnemy(new Rectangle(Rand.Next(64, 64 + 192 - 9), Rand.Next(-32, 0), 7, 14), this)); } else if (R == 2) { Enemies.Add(new FighterEnemy(new Rectangle(Rand.Next(64, 64 + 192 - 9), Rand.Next(-32, 0), 9, 13), this)); } else if (R == 3) { Enemies.Add(new FollowEnemy(new Rectangle(Rand.Next(64, 64 + 192 - 9), Rand.Next(-32, 0), 9, 9), this)); } else { Enemies.Add(new SpikeEnemy(new Rectangle(Rand.Next(64, 64 + 192 - 9), Rand.Next(-32, 0), 13, 13), this)); } } } ToRemove.Clear(); foreach (IEnemy E in Enemies) { E.Update(gameTime); if (E.Bounds.Intersects(Parent.Player.Bounds)) { ToRemove.Add(E); Parent.Health.Value--; } foreach (Bullet B in Parent.Player.Bullets.ToArray()) { if (B.Bounds.Intersects(E.Bounds)) { E.Health--; Parent.GFM.SpawnMiniExplosion(E); if (E.Health <= 0) { Parent.Score.Value++; ToRemove.Add(E); } else { AssetManager.PlaySound("Hit"); } Parent.Player.Bullets.Remove(B); } } } foreach (IEnemy E in ToRemove) { if (Enemies.Contains(E)) { Parent.GFM.SpawnExplosion(E); Parent.Camera.ScreenShake(20, 0.075f); Enemies.Remove(E); } } }