public Tank SpawnTank(string tankName) { if (Tanks.Count > MaxTanks || Tanks.ContainsKey(tankName)) { return(null); } tankName = $"{Id}_{tankName}"; var newTank = new Tank(this, tankName); Tanks.Add(tankName, newTank); return(newTank); }
/// <summary> /// Computes the health of a tank when hit by a projectile from another tank /// </summary> /// <param name="tankID"> ID of tank that has been hit by enemy projectile </param> /// <param name="ProjID"> ID of the projectile which is damaging the tank </param> public void TankProjectileDamage(int tankID, int ProjID) { // If health of the given tank is not zero, decreases health by 1. if(Tanks[tankID].HitPoints > 1) Tanks[tankID].HitPoints--; // The given tanks health is zero, so the tank dies and the owner // of the projectile given has its score increased by 1. else { if(Tanks.ContainsKey(Projectiles[ProjID].OwnerID)) { Tanks[Projectiles[ProjID].OwnerID].Score++; } else if(DeadTanks.ContainsKey(ProjID)) { DeadTanks[Projectiles[ProjID].OwnerID].Score++; } Tanks[tankID].HitPoints = 0; Tanks[tankID].Died = true; DeadTanks[tankID] = Tanks[tankID]; } }
/// <summary> /// Add Tank has slightly different logic where it is the only object that can be /// "Disconnected". And if so, we remove it from our world. /// </summary> /// <param name="obj"></param> /// <param name="message"></param> public void AddTank(JObject obj, string message) { int id = (int)obj.First; Tank tank = JsonConvert.DeserializeObject <Tank>(message); // Adding new tanks if (!Tanks.ContainsKey(id)) { Tanks.Add(id, tank); TankColors.Add(id, GetTankColor(id)); } // If this tank is disconnected, remove it. else if (tank.disconnected) { Tanks.Remove(id); } // Update the tank else { Tanks[id] = Tanks[id].Update(tank); } }