Beispiel #1
0
 public void PlayerReady(Player.PlayerId id)
 {
     if (id == Player.PlayerId.Player1)
     {
         mPlayer1.isReady = true;
     }
     else if (id == Player.PlayerId.Player2)
     {
         mPlayer2.isReady = true;
     }
     else
     {
         Debug.Log("Error: Unknown player is trying to ready");
     }
 }
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            KeyboardState keyboard = Keyboard.GetState();
            if (keyboard.IsKeyDown(Keys.Enter) &&
                prevKeyboardState.IsKeyUp(Keys.Enter) &&
                this.game.GetGameStatus() == Shooter.GameStatus.Simulation &&
                this.game.GetGameStatus() != Shooter.GameStatus.Sending &&
                this.game.GetGameStatus() != Shooter.GameStatus.Receive)
            {
                this.game.SetGameStatus(Shooter.GameStatus.Sending);
            }
            this.prevKeyboardState = keyboard;

            if (this.game.GetGameStatus() == Shooter.GameStatus.Sending)
            {
                if (this.game.bMapIsReady)
                {
                    //
                    // If there's input; send it to server
                    //
                    NetOutgoingMessage om = client.CreateMessage();
                    om.Write((byte)OutgoingMessageType.DataPlayerInfo);
                    om.Write(this.game.path.GetWayPoints().Count);
                    foreach (WayPoint point in this.game.path.GetWayPoints())
                    {
                        Vector3 tempPos = point.CenterPos;//this.game.gamePlayer.GetPlayerPosition();
                        float tempOri = this.game.gamePlayer.GetPlayerOrientation();
                        om.Write(tempPos.X);
                        om.Write(tempPos.Y);
                        om.Write(tempPos.Z);
                        om.Write(tempOri);
                    }
                    client.SendMessage(om, NetDeliveryMethod.ReliableOrdered);

                }
                this.game.SetGameStatus(Shooter.GameStatus.Receive);

            }

            if (this.game.GetGameStatus() == Shooter.GameStatus.Receive ||
                this.game.GetGameStatus() == Shooter.GameStatus.MainMenu)
            {
                //read message
                NetIncomingMessage msg;
                while ((msg = client.ReadMessage()) != null)
                {
                    switch (msg.MessageType)
                    {
                        case NetIncomingMessageType.DiscoveryResponse:
                            //just connect to first server discovered
                            client.Connect(msg.SenderEndpoint);
                            playerId = (Project_Origin.Player.PlayerId)(msg.ReadInt32());
                            int mapSeed = msg.ReadInt32();
                            this.game.bMapIsReady = true;
                            this.game.BuildGameComponents(mapSeed);
                            this.game.gamePlayer.SetPlayId();
                            break;
                        case NetIncomingMessageType.Data:
                            //server sent a position update
                            IncomingMessageType imt = (IncomingMessageType)msg.ReadByte();
                            if (imt == IncomingMessageType.DataOtherPlayerInfo)
                            {
                                opponentWaypoint.Clear();
                                int wayPointCounter = msg.ReadInt32();
                                for (int i = 1; i <= wayPointCounter; ++i)
                                {
                                    otherPlayerInfo.position.X = msg.ReadFloat();
                                    otherPlayerInfo.position.Y = msg.ReadFloat();
                                    otherPlayerInfo.position.Z = msg.ReadFloat();
                                    otherPlayerInfo.orientation = msg.ReadFloat();

                                    Console.WriteLine("{0} {1} {2}",otherPlayerInfo.position.X,otherPlayerInfo.position.Y,otherPlayerInfo.position.Z);
                                    opponentWaypoint.Add(new WayPoint(this.game.GraphicsDevice, new Vector3(otherPlayerInfo.position.X,
                                                                                                        otherPlayerInfo.position.Y,
                                                                                                        otherPlayerInfo.position.Z)));
                                }

                            }
                            this.game.gamePlayer.Path.OpponentWayPoints = opponentWaypoint;
                            this.game.SetGameStatus(Shooter.GameStatus.Start);
                            this.game.gamePlayer.StartToMove();
                            break;
                    }
                }
            }

            base.Update(gameTime);
        }
Beispiel #3
0
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            KeyboardState keyboard = Keyboard.GetState();

            if (keyboard.IsKeyDown(Keys.Enter) &&
                prevKeyboardState.IsKeyUp(Keys.Enter) &&
                this.game.GetGameStatus() == Shooter.GameStatus.Simulation &&
                this.game.GetGameStatus() != Shooter.GameStatus.Sending &&
                this.game.GetGameStatus() != Shooter.GameStatus.Receive)
            {
                this.game.SetGameStatus(Shooter.GameStatus.Sending);
            }
            this.prevKeyboardState = keyboard;


            if (this.game.GetGameStatus() == Shooter.GameStatus.Sending)
            {
                if (this.game.bMapIsReady)
                {
                    //
                    // If there's input; send it to server
                    //
                    NetOutgoingMessage om = client.CreateMessage();
                    om.Write((byte)OutgoingMessageType.DataPlayerInfo);
                    om.Write(this.game.path.GetWayPoints().Count);
                    foreach (WayPoint point in this.game.path.GetWayPoints())
                    {
                        Vector3 tempPos = point.CenterPos;//this.game.gamePlayer.GetPlayerPosition();
                        float   tempOri = this.game.gamePlayer.GetPlayerOrientation();
                        om.Write(tempPos.X);
                        om.Write(tempPos.Y);
                        om.Write(tempPos.Z);
                        om.Write(tempOri);
                    }
                    client.SendMessage(om, NetDeliveryMethod.ReliableOrdered);
                }
                this.game.SetGameStatus(Shooter.GameStatus.Receive);
            }

            if (this.game.GetGameStatus() == Shooter.GameStatus.Receive ||
                this.game.GetGameStatus() == Shooter.GameStatus.MainMenu)
            {
                //read message
                NetIncomingMessage msg;
                while ((msg = client.ReadMessage()) != null)
                {
                    switch (msg.MessageType)
                    {
                    case NetIncomingMessageType.DiscoveryResponse:
                        //just connect to first server discovered
                        client.Connect(msg.SenderEndpoint);
                        playerId = (Project_Origin.Player.PlayerId)(msg.ReadInt32());
                        int mapSeed = msg.ReadInt32();
                        this.game.bMapIsReady = true;
                        this.game.BuildGameComponents(mapSeed);
                        this.game.gamePlayer.SetPlayId();
                        break;

                    case NetIncomingMessageType.Data:
                        //server sent a position update
                        IncomingMessageType imt = (IncomingMessageType)msg.ReadByte();
                        if (imt == IncomingMessageType.DataOtherPlayerInfo)
                        {
                            opponentWaypoint.Clear();
                            int wayPointCounter = msg.ReadInt32();
                            for (int i = 1; i <= wayPointCounter; ++i)
                            {
                                otherPlayerInfo.position.X  = msg.ReadFloat();
                                otherPlayerInfo.position.Y  = msg.ReadFloat();
                                otherPlayerInfo.position.Z  = msg.ReadFloat();
                                otherPlayerInfo.orientation = msg.ReadFloat();

                                Console.WriteLine("{0} {1} {2}", otherPlayerInfo.position.X, otherPlayerInfo.position.Y, otherPlayerInfo.position.Z);
                                opponentWaypoint.Add(new WayPoint(this.game.GraphicsDevice, new Vector3(otherPlayerInfo.position.X,
                                                                                                        otherPlayerInfo.position.Y,
                                                                                                        otherPlayerInfo.position.Z)));
                            }
                        }
                        this.game.gamePlayer.Path.OpponentWayPoints = opponentWaypoint;
                        this.game.SetGameStatus(Shooter.GameStatus.Start);
                        this.game.gamePlayer.StartToMove();
                        break;
                    }
                }
            }


            base.Update(gameTime);
        }