/// <summary> /// Once name has been recieved, creates a tank for them and sends startup info, then asks for commands. /// </summary> /// <param name="state"></param> private void sendStartUpInfo(SocketState state) { StringBuilder temp = new StringBuilder(); temp.Append(state.ID.ToString() + "\n"); temp.Append(world.GetSize().ToString() + "\n"); foreach (Wall w in world.GetWalls()) { temp.Append(JsonConvert.SerializeObject(w) + "\n"); } if (state.ErrorOccured) { return; } ProcessMessage(state); state.OnNetworkAction = update; Networking.Send(state.TheSocket, temp.ToString()); lock (users) { users.Add(state, (int)state.ID); } lock (state) { Networking.GetData(state); } }
private void sendStartUpInfo(SocketState state) { if (state.ErrorOccured) { return; } ProcessMessage(state); if (users.ContainsKey(state)) { Networking.Send(state.TheSocket, users[state].ToString()); Networking.Send(state.TheSocket, world.GetSize().ToString()); state.OnNetworkAction = update; lock (state) { Networking.GetData(state); } } else { } }
/// <summary> /// This method is invoked when the DrawingPanel needs to be re-drawn /// </summary> protected override void OnPaint(PaintEventArgs e) { //Makes sure world is null, since it is null while loading game if (theWorld != null) { //Locks world since it accesses info from it lock (theWorld) { //Get player's tank to center view Tank player; if (theWorld.GetTank(PlayerID, out player)) { double playerX = player.location.GetX(); double playerY = player.location.GetY(); double ratio = (double)ViewSize / (double)theWorld.GetSize(); int halfSizeScaled = (int)(theWorld.GetSize() / 2.0 * ratio); double inverseTranslateX = -WorldSpaceToImageSpace(theWorld.GetSize(), playerX) + halfSizeScaled; double inverseTranslateY = -WorldSpaceToImageSpace(theWorld.GetSize(), playerY) + halfSizeScaled; e.Graphics.TranslateTransform((float)inverseTranslateX, (float)inverseTranslateY); } //Draw background e.Graphics.DrawImage(Background, new Rectangle(0, 0, theWorld.GetSize(), theWorld.GetSize())); // Draw the tanks foreach (Tank tank in theWorld.GetTanks()) { //Draw tank, turret, and info if tank is alive and ingame if (!tank.died && !tankTimer.ContainsKey(tank) && tank.hitPoints > 0) { DrawObjectWithTransform(e, tank, theWorld.GetSize(), tank.location.GetX(), tank.location.GetY(), tank.orientation.ToAngle(), TankDrawer); DrawObjectWithTransform(e, tank, theWorld.GetSize(), tank.location.GetX(), tank.location.GetY(), tank.aiming.ToAngle(), TurretDrawer); DrawObjectWithTransform(e, tank, theWorld.GetSize(), tank.location.GetX(), tank.location.GetY(), 0, InfoDrawer); } //Otherwise, add tank to tank timer, increment tank timer, or remove tank from tank timer if it is alive else { //If tank is a live again, remove from tank timer if (tank.hitPoints > 0) { tankTimer.Remove(tank); break; } //If tank is already in tank timer, decrement tank timer or remove tank from timer if (tankTimer.ContainsKey(tank)) { if (tankTimer[tank] < 200) { tankTimer[tank]++; } else { tankTimer.Remove(tank); } } //Add tank to timer if not in timer yet else { tankTimer.Add(tank, 0); } } } // Draw the walls foreach (Wall w in theWorld.GetWalls()) { Vector2D location = GetWallLocation(w); DrawObjectWithTransform(e, w, theWorld.GetSize(), location.GetX(), location.GetY(), 0, WallDrawer); } //Draw beams if existed for less than 60 frames List <Beam> beams = new List <Beam>(beamsTimer.Keys); foreach (Beam b in beams) { if (beamsTimer[b] > 60) { beamsTimer.Remove(b); } //Increments beamsTimer and draws beams else { beamsTimer[b]++; DrawObjectWithTransform(e, b, theWorld.GetSize(), b.origin.GetX(), b.origin.GetY(), b.direction.ToAngle(), BeamDrawer);; } } // Draw the powerups if not dead foreach (Powerup pow in theWorld.GetPowerups()) { if (!pow.died) { DrawObjectWithTransform(e, pow, theWorld.GetSize(), pow.location.GetX(), pow.location.GetY(), 0, PowerupDrawer); } } //Draw projectiles if not dead foreach (Projectile p in theWorld.GetProjectiles()) { if (!p.died) { DrawObjectWithTransform(e, p, theWorld.GetSize(), p.location.GetX(), p.location.GetY(), p.orientation.ToAngle(), ProjectileDrawer); } } //Draw tank explosions with tanks in tankTimer List <Tank> tanks = new List <Tank>(tankTimer.Keys); foreach (Tank t in tanks) { DrawObjectWithTransform(e, t, theWorld.GetSize(), t.location.GetX(), t.location.GetY(), 0, ExplosionDrawer); } } } // Do anything that Panel (from which we inherit) needs to do base.OnPaint(e); }