//******************** 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 chosen Projectile. If it is dead, also removes it from theProjectiles. /// </summary> /// <param name="proj"></param> /// <param name="ID"></param> public void UpdateProjectile(Projectile proj, int ID) { if (theProjectiles.ContainsKey(ID)) { theProjectiles[ID] = proj; // remove from newProjIDs if this proj is in there. if (newProjIDs.Contains(ID)) { newProjIDs.Remove(ID); } } else { theProjectiles.Add(ID, proj); // add to newProjIDs newProjIDs.Add(ID); } // remove if the projectile sent is dead if (!proj.IsAlive()) { theProjectiles.Remove(ID); } }
/// <summary> /// Computes the new location for the projectile /// </summary> public void MotionForProjectiles(Projectile projectile) { // if the projectile is not alive, don't move it if (!projectile.IsAlive()) { return; } // find the velocity with respect to direction Vector2D velocity = projectile.GetDirection() * projectileSpeed; // reset the location of the projectile projectile.SetLocation(projectile.GetLocation() + velocity); if (!ProjectileOffScreen(projectile)) { foreach (Star star in stars.Values) { CollisionBetweenAStarAndProjectile(star, projectile); } } }
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); }