Esempio n. 1
0
        public void Draw()
        {
            target.BeginDraw();

            try {
                this.map.Draw(this.target);
            }
            catch {
                Console.WriteLine("Exception in graphic engine");
            }

            foreach (var item in FloatingObjectRegistry.GetMovingObjects())
            {
                item.Draw(this.target);
            }
            foreach (var player in players)
            {
                if (player.ShouldDraw())
                {
                    player.Draw(this.target);
                }
            }

            target.EndDraw();
        }
Esempio n. 2
0
        /// <summary>
        /// Main gameloop, updates all objects necassary
        /// </summary>
        private void GameLoop()
        {
            double t  = 0.0;
            double dt = 1.0 / 60.0;
            double delta;

            timer.Start();
            currentTime = ((double)timer.ElapsedMilliseconds) / 1000;

            //myPlayerInstance.SetLocation(new PointF(500, 500));

            while (true)
            {
                //
                // Step 1: determine timestep values
                //
                newTime            = ((double)timer.ElapsedMilliseconds) / 1000;
                timeUntilNextFrame = newTime - currentTime;
                currentTime        = newTime;

                //
                // Step 2: Check keyboard input and replicate to other players
                //
                if (myPlayerInstance.isAlive())
                {
                    Input newInput = inputHandler.CheckInput();
                    myPlayerInstance.UpdateInput(newInput, currentTime);

                    //if (inputHandler.UpdatedInput) {
                    //    myPlayerInstance.NewMovementDirection(inputHandler.PressedDirection, currentTime);
                    //    //communicationHandler.Broadcast(PacketBuilder.Build_PlayerMovement(myID, inputHandler.PressedDirection));
                    //    inputHandler.UpdatedInput = false;
                    //}
                    //if (inputHandler.DeployBomb) {
                    //    if (myPlayerInstance.BombCap > 0)
                    //        myPlayerInstance.DeployBomb();
                    //    //inputHandler.DeployBomb = false;
                    //    communicationHandler.Broadcast(PacketBuilder.Build_DeployBomb(myID));
                    //}
                    //communicationHandler.Broadcast(PacketBuilder.Build_PlayerMovement(myID, inputHandler.PressedDirection, myPlayerInstance.GetLocation()));
                }
                //communicationHandler.Broadcast(PacketBuilder.Build_Ready(myID));

                //readySignal.WaitOne();

                while (timeUntilNextFrame > 0.0)
                {
                    delta = Math.Min(timeUntilNextFrame, dt);

                    //
                    // Step 3: Update fixed objects on the game map
                    //
                    map.Update(currentTime, delta);

                    //
                    // Step 4: Update floating objects
                    //
                    foreach (var player in players)
                    {
                        player.Value.Update(currentTime);
                        //checkPlayerCollision(player.Value);
                    }

                    // Remove dead players, do this better
                    foreach (var player in players.Where(x => x.Value.PendingDestroy).ToList())
                    {
                        players.Remove(player.Key);
                    }

                    // Check if game is over
                    //CheckIfGameOver();


                    foreach (var item in FloatingObjectRegistry.GetMovingObjects())
                    {
                        item.Update(currentTime);
                    }
                    FloatingObjectRegistry.RemoveDestroyedObjects();
                    //
                    // Step 5: Execute netwok syncronizations messages,
                    //         and wait for them to finish
                    //

                    //foreach (var command in networkCommands) {
                    //    command.Start();
                    //}
                    //Task.WaitAll(networkCommands.ToArray());

                    timeUntilNextFrame -= delta;
                    t += delta;
                }

                //
                // Step 6: Draw current game state
                //
                gEngine.Draw();
                //communicationHandler.Broadcast(PacketBuilder.Build_Ready(myID));
                //readySignal.WaitOne();
            }
        }