/// <summary> /// update method use to send info to the client /// </summary> public static void update() { // wait until enough time while (watch.ElapsedMilliseconds < MSperFrame) { } watch.Restart(); // use to append message StringBuilder sb = new StringBuilder(); lock (theworld) { theworld.update(); foreach (star s in theworld.getStar().Values) { sb.Append(s.ToString() + '\n'); } foreach (Ship s in theworld.getShip().Values) { sb.Append(s.ToString() + "\n"); } foreach (projectile s in theworld.getProj().Values) { sb.Append(s.ToString() + "\n"); } // clean up the unconnected ship and died stuffs theworld.cleanup(); } //check all client statuses lock (list) { foreach (SocketState ss in list.ToArray()) { if (ss.theSocket.Connected == true) { NController.Send(ss.theSocket, sb.ToString()); } // deal with the unconnected clients else { lock (theworld) { theworld.addLostID(ss.uid); theworld.getShip()[ss.uid].setLost(); } list.Remove(ss); } } } }
public void TestWorldMethod() { world world1 = new world(); star s = new star(0, new Vector2D(0, 200), 1); projectile p1 = new projectile(1, new Vector2D(0, 376), new Vector2D(0, 0), true, 2); Ship ship = new Ship(1, new Vector2D(0, 0), new Vector2D(0, 0), false, "s", 5, 0); String A = s.ToString(); star s1 = JsonConvert.DeserializeObject <star>(A); world1.addStar(s); world1.setFrame(0); world1.addShip(ship); world1.Fire(1); world1.addproj(p1); world1.setRespawn(300); world1.setSize(750); Assert.AreEqual(world1.getProj().Values.Count, 1); Assert.AreEqual(world1.getShip().Values.Count, 1); Assert.AreEqual(world1.getStar().Values.Count, 1); ship = new Ship(1, new Vector2D(0, 0), new Vector2D(0, 0), true, "s", 5, 0); for (int i = 0; i <= 4; i++) { world1.getShip()[1].hpdecrease(); } world1.respawn(ship); world1.addLostID(1); world1.cleanup(); world1.removeProjectile(p1); world1.removeStar(s); world1.removeShip(ship); Assert.AreEqual(world1.getShip().Values.Count, 0); Assert.AreEqual(world1.getShip().Values.Count, 0); Assert.AreEqual(world1.getStar().Values.Count, 0); world1.update(); s.update(51); s.update(102); s.update(160); s.update(230); }
// This method is invoked when the DrawingPanel needs to be re-drawn protected override void OnPaint(PaintEventArgs e) { // Draw the players lock (theWorld) { foreach (Ship play in theWorld.getShip().Values) { DrawObjectWithTransform(e, play, this.Size.Width, play.getloc().GetX(), play.getloc().GetY(), play.getdir().ToAngle(), drawShip); } foreach (star play in theWorld.getStar().Values) { DrawObjectWithTransform(e, play, this.Size.Width, play.getloc().GetX(), play.getloc().GetY(), 0, drawStar); } foreach (projectile play in theWorld.getProj().Values) { DrawObjectWithTransform(e, play, this.Size.Width, play.getloc().GetX(), play.getloc().GetY(), play.getdir().ToAngle(), drawProjectile); } } // Do anything that Panel (from which we inherit) needs to do base.OnPaint(e); }