//******************** World methods for the projectiles ********************// /// <summary> /// If Projectile p does not exist in projectiles adds it. If the id already exists /// in projectiles, updates reference in projectiles to p. /// </summary> public void AddProjectile(Projectile projectile) { // if the projectile is null then do nothing if (projectile == null) { return; } // if the projectile is dead then remove it fromt he world if (!projectile.IsAlive()) { projectiles.Remove(projectile.GetID()); } // if the projectile is in the world then replace the old projectile with the // passed in projectile else if (projectiles.ContainsKey(projectile.GetID())) { projectiles[projectile.GetID()] = projectile; } // if the projectile is not in the world then add it to the world else { projectiles.Add(projectile.GetID(), projectile); } }
/// <summary> /// Updates the the projectiles dictionary to contain the given projectile /// </summary> /// <param name="proj"></param> public void Update(Projectile proj) { if (!proj.GetActive()) { Projectiles.Remove(proj.GetID()); } else { Projectiles[proj.GetID()] = proj; } }
/// <summary> /// This method removes a projectile from the world. /// </summary> /// <param name="proj"></param> public void removeProjectile(Projectile proj) { int projId = proj.GetID(); lock (projectileDictionary) { projectileDictionary.Remove(projId); } }
public void ReceiveData(SocketState state) { StringBuilder sb = state.sb; JsonSerializerSettings settings = new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error }; try { char[] separator = new char[] { '\n' }; string[] strArray = sb.ToString().Split(separator, StringSplitOptions.RemoveEmptyEntries); int length = strArray.Length; if (sb.ToString()[sb.Length - 1] != '\n') { length--; } List <Ship> list = new List <Ship>(); List <Ship> list2 = new List <Ship>(); for (int i = 0; i < length; i++) { string json = strArray[i]; if ((json[0] == '{') && (json[json.Length - 1] == '}')) { Ship item = null; Projectile projectile = null; Star star = null; JObject obj1 = JObject.Parse(json); JToken token = obj1["ship"]; JToken token2 = obj1["proj"]; if (token != null) { item = JsonConvert.DeserializeObject <Ship>(json, settings); } if (token2 != null) { projectile = JsonConvert.DeserializeObject <Projectile>(json, settings); } if (obj1["star"] != null) { star = JsonConvert.DeserializeObject <Star>(json, settings); } World world = this.world; lock (world) { if (item != null) { if (this.world.GetShips().ContainsKey(item.GetID())) { if (this.world.GetShips()[item.GetID()].Alive && !item.Alive) { list2.Add(item); } } else { list.Add(item); } this.world.GetShips()[item.GetID()] = item; } if (projectile != null) { if (projectile.IsAlive()) { this.world.GetProjectiles()[projectile.GetID()] = projectile; } else if (this.world.GetProjectiles().ContainsKey(projectile.GetID())) { this.world.GetProjectiles().Remove(projectile.GetID()); } } if (star != null) { this.world.GetStars()[star.GetID()] = star; } } sb.Remove(0, json.Length + 1); } } foreach (Ship ship2 in list2) { this.ShipDied(ship2); } foreach (Ship ship3 in list) { this.NewShip(ship3); } this.FrameTick(); } catch (JsonReaderException) { } catch (Exception) { } state.call_me = new Action <SocketState>(this.ReceiveData); Networking.RequestMoreData(state); }