void GotHit(Bomb b) { RemoveProjectile(b); MulticastSender.SendGameMsg(4, b.ID.ToString()); me.IsDead = true; new Thread(new ThreadStart(this.WaitUntilRespawn)).Start(); }
private void HandleJoinReq(string msg) { string[] ar = msg.Split(','); if (Guid.Parse(ar[0]) == GameArea.gameID) // don't respond to other game id requests { int n = int.Parse(ar[2]); MulticastSender.SendJoinResp(ar[1], (n == GameArea.nextPlayer && n <= GameArea.MAX_PLAYERS)); } }
private void GameArea_Shown(object sender, EventArgs e) { Thread t = new Thread(new ThreadStart(recv.EnterGame)); t.IsBackground = true; t.Start(); Thread.Sleep(1800); t.Abort(); if (gameID == Guid.Empty) { CreateNewGame(); MulticastSender.SendGameMsg(-9, "game created"); // the line above is for when there aren't any packets flowing in the port // if there are no packets, Thread t will be blocked on Socket.ReceiveFrom() // and calling Abort won't cancel the blocking call } recv.SetMulticastLoopback(false); System.Diagnostics.Debug.WriteLine("started game"); if (playerNum % 2 == 0) { me = new Tank(MulticastSender.ID, rnd.Next(0, this.ClientRectangle.Width - Tank.SIZE.Width), rnd.Next((int)(this.ClientRectangle.Height * 0.55), this.ClientRectangle.Height - Tank.SIZE.Height)); vehicles[playerNum] = me; tanks.Add((Tank)me); } else { me = new Plane(MulticastSender.ID, rnd.Next(0, this.ClientRectangle.Width - Plane.SIZE.Width), rnd.Next(0, (int)(this.ClientRectangle.Height * 0.45) - Plane.SIZE.Height)); vehicles[playerNum] = me; planes.Add((Plane)me); } System.Diagnostics.Debug.WriteLine("my vehicle instatiated"); if (playerNum == 0) { SetNextPlayer(); hostThread = new Thread(new ThreadStart(MulticastSender.SendInvitations)); hostThread.IsBackground = true; hostThread.Start(); System.Diagnostics.Debug.WriteLine("hostThread started"); } recv.IsHost = (playerNum == 0); receiverThread = new Thread(new ThreadStart(recv.run)); receiverThread.IsBackground = true; // thread becomes zombie if this is not explicitly set to true receiverThread.Start(); }
public void MovePlayer(Guid id, int playerNumber, int x, int y, int dir) { if (vehicles[playerNumber] == null) { if (playerNumber % 2 == 0) { Tank t = new Tank(id, x, y); t.SetDirection(dir); vehicles[playerNumber] = t; if (!tanks.Contains(t)) { tanks.Add(t); } } else { Plane p = new Plane(id, x, y); p.SetDirection(dir); vehicles[playerNumber] = p; if (!planes.Contains(p)) { planes.Add(p); } } System.Diagnostics.Debug.WriteLine("NEW PLAYER!!!!!!!!\nNumber: " + playerNumber + " ID: " + id); if (recv.IsHost) { SetNextPlayer(); } PrintGameStateToDebug(); SendMovementMsg(me.X_Coor, me.Y_Coor, me.Direction); // let new player know about me currentNumOfPlayers++; MulticastSender.SendGameMsg(5, 0 + "," + PlaneScore); MulticastSender.SendGameMsg(5, 1 + "," + TankScore); } Vehicle player = vehicles[playerNumber]; player.X_Coor = x; player.Y_Coor = y; player.SetDirection(dir); player.IsDead = false; }
// I probably should check id? public void PlayerIsDead(Guid id, int playerNum) { Vehicle v = vehicles[playerNum]; if (v != null) { vehicles[playerNum].IsDead = true; if (playerNum % 2 == 0) { PlaneScore++; MulticastSender.SendGameMsg(5, 0 + "," + PlaneScore); ChangeScore(0, PlaneScore); } else { TankScore++; MulticastSender.SendGameMsg(5, 1 + "," + TankScore); ChangeScore(1, TankScore); } } }
// returns true for connected, false for failure private bool TryJoin(Guid gameToJoin, int playerNum) { // Send Join Request long until = DateTime.Now.Ticks + TimeSpan.TicksPerMillisecond * 500; byte[] data = new byte[1024]; MulticastSender.SendJoinReq(gameToJoin, MulticastSender.ID, playerNum); while (DateTime.Now.Ticks < until) { int recv = sock.ReceiveFrom(data, ref ep); string stringData = Encoding.ASCII.GetString(data, 0, recv); StringReader reader = new StringReader(stringData); if (!reader.ReadLine().Equals(MulticastSender.HEADER)) { continue; } string secondLine = reader.ReadLine(); if (int.TryParse(secondLine, out int type) && (type == 2 || type == 3)) { string s = reader.ReadLine(); string[] ar = s.Split(','); if (Guid.Parse(ar[0]) == gameToJoin && Guid.Parse(ar[1]) == MulticastSender.ID) { if (type == 2) { Debug.WriteLine("joining game"); GameArea.currentNumOfPlayers = playerNum; GameArea.playerNum = playerNum; GameArea.gameID = gameToJoin; return(true); } else // type == 3 { return(false); } } } } return(false); }
private void Form1_KeyEvent(object sender, KeyEventArgs e) { if (me.IsDead) { return; } switch (e.KeyCode) { case Keys.A: case Keys.Left: dir = 3; me.Move(dir); break; case Keys.W: case Keys.Up: dir = 0; me.Move(dir); break; case Keys.S: case Keys.Down: dir = 2; me.Move(dir); break; case Keys.D: case Keys.Right: dir = 1; me.Move(dir); break; case Keys.Space: case Keys.ShiftKey: if (playerNum % 2 == 0) // tank { if (aTimer_Elapsed) { aTimer_Elapsed = false; int bulletSize = bullets.Count; if (bulletSize > 2) { break; } Bullet b = new Bullet(Guid.NewGuid(), new Point(me.X_Coor + 20, me.Y_Coor)); bullets.Add(b); bullet_ids.Add(b.ID); MulticastSender.SendGameMsg(1, b.X_Coor + "," + b.Y_Coor + "," + b.Direction + "," + b.ID); } } else // plane { if (bTimer_Elapsed) { bTimer_Elapsed = false; int bombSize = bombs.Count; if (bombSize > 2) { break; } Bomb b2 = new Bomb(Guid.NewGuid(), new Point(me.X_Coor + 20, me.Y_Coor)); bombs.Add(b2); bomb_ids.Add(b2.ID); MulticastSender.SendGameMsg(3, b2.X_Coor + "," + b2.Y_Coor + "," + b2.Direction + "," + b2.ID); } } break; default: break; } }
private void GameArea_FormClosing(object sender, FormClosingEventArgs e) { MulticastSender.SendGameMsg(-1, ""); MulticastSender.SendGameMsg(-1, ""); MulticastSender.SendGameMsg(-1, ""); }
private void SendMovementMsg(int x, int y, int dir) { MulticastSender.SendGameMsg(0, x + "," + y + "," + dir); }