Exemplo n.º 1
0
 void GotHit(Bomb b)
 {
     RemoveProjectile(b);
     MulticastSender.SendGameMsg(4, b.ID.ToString());
     me.IsDead = true;
     new Thread(new ThreadStart(this.WaitUntilRespawn)).Start();
 }
Exemplo n.º 2
0
        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();
        }
Exemplo n.º 3
0
        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;
        }
Exemplo n.º 4
0
        // 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);
                }
            }
        }
Exemplo n.º 5
0
        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;
            }
        }
Exemplo n.º 6
0
 private void GameArea_FormClosing(object sender, FormClosingEventArgs e)
 {
     MulticastSender.SendGameMsg(-1, "");
     MulticastSender.SendGameMsg(-1, "");
     MulticastSender.SendGameMsg(-1, "");
 }
Exemplo n.º 7
0
 private void SendMovementMsg(int x, int y, int dir)
 {
     MulticastSender.SendGameMsg(0, x + "," + y + "," + dir);
 }