Example #1
0
        void ReadInData()
        {
            NetworkMessage sm = new NetworkMessage();

            NetIncomingMessage im;

            while ((im = client.ReadMessage()) != null)
            {
                switch (im.MessageType)
                {
                case NetIncomingMessageType.Data:
                    byte[] msg = im.ReadBytes(im.LengthBytes);
                    sm = NetworkMessage.Deserialize(msg);

                    OutputQueue.AddToQueue(sm.GetData());

                    break;

                case NetIncomingMessageType.DiscoveryResponse:
                    Debug.WriteLine("Found server at " + im.SenderEndPoint + " name: " + im.ReadString());
                    client.Connect(im.SenderEndPoint);
                    break;

                case NetIncomingMessageType.UnconnectedData:
                    Debug.WriteLine("Received from " + im.SenderEndPoint + ": " + im.ReadString() + Environment.NewLine);
                    break;
                }
                client.Recycle(im);
            }
        }
Example #2
0
        void ReadInData()
        {
            NetworkMessage client_input = new NetworkMessage();

            NetIncomingMessage im;

            while ((im = server.ReadMessage()) != null)
            {
                switch (im.MessageType)
                {
                case NetIncomingMessageType.Data:
                    byte[] msg = im.ReadBytes(im.LengthBytes);
                    client_input = NetworkMessage.Deserialize(msg);

                    OutputQueue.AddToQueue(client_input.GetData());

                    Debug.WriteLine(msg);
                    break;

                case NetIncomingMessageType.DiscoveryRequest:
                    // Create a response
                    NetOutgoingMessage om = server.CreateMessage();
                    om.Write("Connecting to DOG server");
                    server.SendDiscoveryResponse(om, im.SenderEndPoint);
                    break;

                case NetIncomingMessageType.UnconnectedData:
                    Debug.WriteLine("Received from " + im.SenderEndPoint + ": " + im.ReadString() + Environment.NewLine);
                    break;
                }
                server.Recycle(im);
            }
        }
Example #3
0
 public static OutputQueue Instance()
 {
     if (instance == null)
     {
         instance = new OutputQueue();
     }
     return(instance);
 }
Example #4
0
        //-----------------------------------------------------------------------------
        // Game::LoadContent()
        //		Allows you to load all content needed for your engine,
        //	    such as objects, graphics, etc.
        //-----------------------------------------------------------------------------
        public override void LoadContent()
        {
            PhysicWorld.Instance();
            GameManager.Instance();
            ParticleSpawner.Instance();
            AudioManager.Instance();
            InputQueue.Instance();
            OutputQueue.Instance();
            MyClient.Instance();

            prevTime = GetTime();

            GameManager.Start();
        }
Example #5
0
        public static void Process()
        {
            while (instance.inputQueue.Count > 0)
            {
                DataMessage msg = instance.inputQueue.Dequeue();

                if (msg.sendType == SEND_TYPE.LOCAL)
                {
                    OutputQueue.AddToQueue(msg);
                }
                else
                {
                    MyClient.Instance.SendMessageToServer(msg);
                }
            }
        }
Example #6
0
        //-----------------------------------------------------------------------------
        // Game::Update()
        //      Called once per frame, update data, tranformations, etc
        //      Use this function to control process order
        //      Input, AI, Physics, Animation, and Graphics
        //-----------------------------------------------------------------------------

        // static int number = 0;
        public override void Update()
        {
            float curTime         = GetTime();
            float gameElapsedTime = curTime - prevTime;

            PhysicWorld.Update(gameElapsedTime);
            GameManager.Update(gameElapsedTime);

            InputManager.Update();
            InputTest.KeyboardTest();

            MyClient.Instance().Update();

            OutputQueue.Process();
            InputQueue.Process();



            GameManager.CleanUp();

            prevTime = curTime;
        }
Example #7
0
        //-----------------------------------------------------------------------------
        // Game::Update()
        //      Called once per frame, update data, tranformations, etc
        //      Use this function to control process order
        //      Input, AI, Physics, Animation, and Graphics
        //-----------------------------------------------------------------------------

        // static int number = 0;
        public override void Update()
        {
            float curTime         = GetTime();
            float gameElapsedTime = curTime - prevTime;

            PhysicWorld.Update(gameElapsedTime);

            InputManager.Update();
            CheckForInput();

            GameManager.Update(gameElapsedTime);

            MyServer.Instance.Update();

            InputQueue.Process();

            OutputQueue.Process();



            GameManager.CleanUp();

            prevTime = curTime;
        }
Example #8
0
        private void checkInput()
        {
            newState      = Keyboard.GetState();
            P1newPadState = GamePad.GetState(PlayerIndex.One);
            P2newPadState = GamePad.GetState(PlayerIndex.Two);

            if (oldState.IsKeyDown(Keys.D) || P1oldPadState.IsButtonDown(Buttons.DPadRight))
            {
                player1.playerShip.physicsObj.body.Rotation += 0.1f;
                Ship_Rot_Message msg = new Ship_Rot_Message(player1, 0.1f);
                Debug.WriteLine("D player1.playerShip.physicsObj.body.Rotation = " + player1.playerShip.physicsObj.body.Rotation);
                OutputQueue.add(msg);
            }

            if (oldState.IsKeyDown(Keys.A) || P1oldPadState.IsButtonDown(Buttons.DPadLeft))
            {
                player1.playerShip.physicsObj.body.Rotation -= 0.1f;
                Ship_Rot_Message msg = new Ship_Rot_Message(player1, -0.1f);
                Debug.WriteLine("A player1.playerShip.physicsObj.body.Rotation = " + player1.playerShip.physicsObj.body.Rotation);
                OutputQueue.add(msg);
            }

            if (oldState.IsKeyDown(Keys.W) || P1oldPadState.IsButtonDown(Buttons.DPadUp))
            {
                Ship Player1Ship = player1.playerShip;

                Vector2 direction = new Vector2((float)(Math.Cos(Player1Ship.physicsObj.body.GetAngle())), (float)(Math.Sin(Player1Ship.physicsObj.body.GetAngle())));
                Debug.WriteLine("direction - W - = " + direction);
                //Vector2 direction = new Vector2(.6f, .5f);
                direction.Normalize();
                float x = (float)(Math.Cos(Player1Ship.physicsObj.body.GetAngle()));
                float y = (float)(Math.Sin(Player1Ship.physicsObj.body.GetAngle()));
                Debug.WriteLine("x = " + x);
                Debug.WriteLine("y = " + y);
                direction *= shipSpeed;
                Debug.WriteLine("direction - W - afterNormal = " + direction);


                // No action, send a message thru queue
                //Player1Ship.physicsObj.body.ApplyLinearImpulse(direction, Player1Ship.physicsObj.body.GetWorldCenter());

                Ship_Impulse_Message msg = new Ship_Impulse_Message(player1, direction);
                OutputQueue.add(msg);
            }

            if ((oldState.IsKeyDown(Keys.X) && newState.IsKeyUp(Keys.X)) || (P1oldPadState.IsButtonDown(Buttons.A) && P1newPadState.IsButtonUp(Buttons.A)))
            {
                if (player1.state == PlayerState.alive && player1.missileAvailable())
                {
                    // player1.createMissile();

                    Ship_Create_Missile_Message msg = new Ship_Create_Missile_Message(player1);
                    OutputQueue.add(msg);
                }
            }

            if (oldState.IsKeyDown(Keys.C) && newState.IsKeyUp(Keys.C) || (P1oldPadState.IsButtonDown(Buttons.B) && P1newPadState.IsButtonUp(Buttons.B)))
            {
                if (player1.state == PlayerState.alive && BombManager.Instance().bombAvailable(PlayerID.one))
                {
                    // GameObjManager.Instance().createBomb(PlayerID.one);

                    Ship_Create_Bomb_Message msg = new Ship_Create_Bomb_Message(player1);
                    OutputQueue.add(msg);
                }
            }

            if (oldState.IsKeyDown(Keys.Right) || P2oldPadState.IsButtonDown(Buttons.DPadRight))
            {
                // player2.playerShip.physicsObj.body.Rotation += 0.1f;
                Ship_Rot_Message msg = new Ship_Rot_Message(player2, 0.1f);
                Debug.WriteLine("Right player2.playerShip.physicsObj.body.Rotation = " + player2.playerShip.physicsObj.body.Rotation);
                OutputQueue.add(msg);
            }

            if (oldState.IsKeyDown(Keys.Left) || P2oldPadState.IsButtonDown(Buttons.DPadLeft))
            {
                //player2.playerShip.physicsObj.body.Rotation -= 0.1f;
                Ship_Rot_Message msg = new Ship_Rot_Message(player2, -0.1f);
                Debug.WriteLine("Left player1.playerShip.physicsObj.body.Rotation = " + player2.playerShip.physicsObj.body.Rotation);
                OutputQueue.add(msg);
            }


            if (oldState.IsKeyDown(Keys.Up) || P2oldPadState.IsButtonDown(Buttons.DPadUp))
            {
                Ship Player2Ship = player2.playerShip;

                Vector2 direction = new Vector2((float)(Math.Cos(Player2Ship.physicsObj.body.GetAngle())), (float)(Math.Sin(Player2Ship.physicsObj.body.GetAngle())));
                //Vector2 direction = new Vector2(-.5f, .5f);
                Debug.WriteLine("direction - up - = " + direction);
                float x = (float)(Math.Cos(Player2Ship.physicsObj.body.GetAngle()));
                float y = (float)(Math.Sin(Player2Ship.physicsObj.body.GetAngle()));
                Debug.WriteLine("x = " + x);
                Debug.WriteLine("y = " + y);
                direction.Normalize();

                direction *= shipSpeed;

                // Player2Ship.physicsObj.body.ApplyLinearImpulse(direction, Player2Ship.physicsObj.body.GetWorldCenter());

                // No action, send a message thru queue
                //Player1Ship.physicsObj.body.ApplyLinearImpulse(direction, Player1Ship.physicsObj.body.GetWorldCenter());

                Ship_Impulse_Message msg = new Ship_Impulse_Message(player2, direction);
                OutputQueue.add(msg);
            }

            if ((oldState.IsKeyDown(Keys.OemQuestion) && newState.IsKeyUp(Keys.OemQuestion)) || (P2oldPadState.IsButtonDown(Buttons.A) && P2newPadState.IsButtonUp(Buttons.A)))
            {
                if (player2.state == PlayerState.alive && player2.missileAvailable())
                {
                    // player2.createMissile();
                    Ship_Create_Missile_Message msg = new Ship_Create_Missile_Message(player2);
                    OutputQueue.add(msg);
                }
            }

            if (oldState.IsKeyDown(Keys.OemPeriod) && newState.IsKeyUp(Keys.OemPeriod) || (P2oldPadState.IsButtonDown(Buttons.B) && P2newPadState.IsButtonUp(Buttons.B)))
            {
                if (player2.state == PlayerState.alive && BombManager.Instance().bombAvailable(PlayerID.two))
                {
                    // GameObjManager.Instance().createBomb(PlayerID.two);

                    Ship_Create_Bomb_Message msg = new Ship_Create_Bomb_Message(player2);
                    OutputQueue.add(msg);
                }
            }


            else
            {
            }



            P1oldPadState = P1newPadState;
            P2oldPadState = P2newPadState;
            oldState      = newState;
        }
Example #9
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // TODO: Add your update logic here
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            GraphicsDevice.Clear(Color.Black);

            HandleInput();

            if (networkSession == null)
            {
                // If we are not in a network session, update the
                // menu screen that will let us create or join one.
                UpdateMenuScreen();
            }
            else
            {
                // If we are in a network session, update it.
                UpdateNetworkSession();
            }

            base.Update(gameTime);

            if (state == gameState.game)
            {
                //Input Queue -> msg process
                InputQueue.Process3();

                world.Step((float)gameTime.ElapsedGameTime.TotalSeconds, 5, 8);


                // Out 0) contact listener fires collisions
                // events created in world step

                //Out 1) physics event(contact listener) msg -> Output queue

                if (Game1.networkSession != null)
                {
                    if (Game1.networkSession.IsHost)
                    {
                        // create Buffer
                        PhysicsBuffer[] physicsBuff = new PhysicsBuffer[PhysicsMan.Instance().getCount()];
                        int             num         = PhysicsMan.Instance().getCount();


                        // Update PhysicsObject to PhysicsBuffer for a message transmission
                        PhysicsMan.PushToBuffer(ref physicsBuff);

                        //// out 2) physics buffer msg => OutputQueue
                        PhysicsBuffer_Message msg = new PhysicsBuffer_Message(ref physicsBuff);
                        PhysicsBuffer_Message_outQueue.add(msg);
                    }
                }

                // ON remote ---------------------------------------------
                // read Physics Buffer from OutputQueue
                // no physics simulator

                // On Both----------------------------------------------

                // Out 3) input msg -> OutputQueue
                // InputQueue.Update ?? maybe this is checkinput()???

                checkInput();

                //OutputQueue -> InputQueue
                OutputQueue.PushToNetwork();

                // I put this down here to get pBuffGlobal to have the same amount of items as number of physics bodies
                // InputQueue.Process3();

                //Both----------------------------------------------

                //PhsicsBuffer to GameObject
                PhysicsMan.Update(ref PhysicsBuffer_Message.pBuffGlobal);

                //PhysicsMan.Instance().Update();

                ScoreManager.Instance().Update();

                GameObjManager.Instance().Update(world);

                Timer.Process(gameTime);
            }

            Game1.Camera.Update(gameTime);
        }
Example #10
0
        public void BeginContact(Contact contact)
        {
            GameObject A = (GameObject)contact.GetFixtureA().GetUserData();
            GameObject B = (GameObject)contact.GetFixtureB().GetUserData();

            Manifold  manifold;
            Transform xfA;
            Transform xfB;
            float     radiusA;
            float     radiusB;

            contact.GetFixtureA().GetBody().GetTransform(out xfA);
            contact.GetFixtureB().GetBody().GetTransform(out xfB);
            radiusA = contact.GetFixtureA().GetShape()._radius;
            radiusB = contact.GetFixtureB().GetShape()._radius;

            contact.GetManifold(out manifold);

            WorldManifold worldManifold = new WorldManifold(ref manifold, ref xfA, radiusA, ref xfB, radiusB);

            Vector2 ptA = worldManifold._points[0];
            Vector2 ptB = worldManifold._points[1];

            // adding this for networking
            if (A.GameID == 1)
            {
                int x = 0;
            }

            System.Console.Write("v--> sending mc point A:{0} B:{1} {2} {3}\n", A.GameID, B.GameID, ptA, ptB);

            Debug.Assert(A != null);
            Debug.Assert(B != null);

            Event_Message msg = new Event_Message(A.GameID, B.GameID, ptA);

            OutputQueue.add(msg);

            //System.Console.Write(" point {0} {1}\n", ptA, ptB);

            // if (A.CollideAvailable == true && B.CollideAvailable == true)
            {/*
              * if (A.type < B.type)
              * {
              *     A.Accept(B, ptA);
              * }
              * else
              * {
              *     B.Accept(A, ptA);
              * }*/
            }

            //if (A.type == GameObjType.p1missiles || A.type == GameObjType.p2missiles)
            //{
            //    A.CollideAvailable = false;
            //}

            //if (B.type == GameObjType.p1missiles || B.type == GameObjType.p2missiles)
            //{
            //    B.CollideAvailable = false;
            //}
        }