Пример #1
0
        private double protagonistWeight = 1, enemyWeight = 1; //Default weights

        /// <summary>
        /// Every 1/4 second we check for the new angle of the maze to determine wheter to open/close exits
        /// </summary>
        /// <param name="s">The sender object (unused)</param>
        /// <param name="e">The arguments (used)</param>
        private void RotationTimer_Tick(object s, EventArgs e)
        {
            Console.WriteLine("Angle = {0} degrees", ServerDataManager.Instance.currentAngle);
            //Check for updates in rotation and hence exits openings
            double[]   charactersXPositions = { ServerDataManager.Instance.character1.CharacterPosition.x, ServerDataManager.Instance.character2.CharacterPosition.x };
            Position[] charactersPositions  = { ServerDataManager.Instance.character1.CharacterPosition, ServerDataManager.Instance.character2.CharacterPosition };
            double[]   charactersWeights    = { protagonistWeight, enemyWeight };
            int        multiplier           = Rotation.RotationMultiplier(charactersXPositions, charactersWeights);

            double angleDelta = Rotation.AbsAngleDelta(charactersPositions, 0.25, charactersWeights);

            double newAngle = ServerDataManager.Instance.currentAngle + angleDelta * multiplier;

            //If the angle is too large or small set it to the max/min value respectively
            if (newAngle < -90)
            {
                newAngle = -90;
            }
            else if (newAngle > 90)
            {
                newAngle = 90;
            }


            //Check for exit opening/closing
            if (multiplier == 0)
            {
                return;
                //Dont bother if it isn't rotating
            }

            if (multiplier > 0)
            {
                //Positive rotation
                for (int i = 0; i < ExitingManager.Instance.AnglesToOpen.Count; i++)
                {
                    if (ExitingManager.Instance.AnglesToOpen[i] < ServerDataManager.Instance.currentAngle)
                    {
                        ServerDataManager.Instance.ExitsOpen[i] = true;
                    }

                    if (ExitingManager.Instance.AnglesToClose[i] < ServerDataManager.Instance.currentAngle)
                    {
                        ServerDataManager.Instance.ExitsOpen[i] = false;
                    }
                }
            }
            else /*if (rotationMultiplier > 0)*/
            {
                //Negative rotation
                for (int i = 0; i < ExitingManager.Instance.AnglesToOpen.Count; i++)
                {
                    if (ExitingManager.Instance.AnglesToClose[i] > newAngle)
                    {
                        ServerDataManager.Instance.ExitsOpen[i] = true;
                    }

                    if (ExitingManager.Instance.AnglesToOpen[i] > ServerDataManager.Instance.currentAngle)
                    {
                        ServerDataManager.Instance.ExitsOpen[i] = false;
                    }
                }
            }

            ServerDataManager.Instance.currentAngle = newAngle;
            //Update currentangle
        }
Пример #2
0
        /// <summary>
        /// Creates an instance of GamePage
        /// </summary>
        /// <param name="pt">The type of protagonist to generate</param>
        /// <param name="et">The type of enemy to generate</param>
        public GamePage(ProtagonistType pt, EnemyType et, Level.Level _level, double protagonistWeight = 1, double enemyWeight = 1)
        {
            InitializeComponent();
            level = _level;

            Protagonist = pt;
            Enemy       = et;

            //Sets up the grid by decoding the int array and placing everything on the canvas
            level.SetupGrid(ref cvsPlayArea, ref cvsExitArea, pt, et);

            //Set the characters weights (for turning moments)
            GameGridManager.Instance.Characters[0].Weight = protagonistWeight;
            GameGridManager.Instance.Characters[1].Weight = enemyWeight;

            //Set the canvas of the singleton for easier access to the canvas (so the canvas does
            //not need to be referenced every tick for the collision detection visualisation to work)
            GameGridManager.Instance.GameCanvas = cvsPlayArea;

            //Setup the angles that open the exits
            ExitingManager.FindAnglesNeededToOpen(level.ExitLocation.HeightFromAnchor, level.ExitLocation.Length);

            keyboardInputTimer = new DispatcherTimer()
            {
                //Every ~1/1000 of a second update
                Interval = new TimeSpan(0, 0, 0, 0, 1)
            };
            //Have the timer use the timertick event
            keyboardInputTimer.Tick += KeyboardInputTimerTick;

            rotationTimer = new DispatcherTimer()
            {
                //Update every 1/4 second
                Interval = new TimeSpan(0, 0, 0, 0, 250)
            };
            rotationTimer.Tick += (s, e) =>
            {
                double rotation = Rotation.AbsAngleDelta() *
                                  Algorithms.Rotation.RotationMultiplier(GameGridManager.Instance.Characters);

                GameGridManager.RotateStoryBoard((int)rotation);
            };

            //If there is some networking involved within characters then start the communication manager and tie it to the message manager
            if (gameType == GameType.Networked)
            {
                CommunicationManager.Instance.SetupEnemyTypes(pt, et);
                //Also tell the server that it has received and loaded the map

                messageInstance = MessageManager.Instance;
                messageInstance.MessageHandler += HandleMessage;
                messageInstance.SendMessage("received");


                //Also start the timers
                StartTimers();
            }

            //Setups up AI timer if this is a singleplayer game
            if (gameType == GameType.Singleplayer)
            {
                aiTimer = new DispatcherTimer()
                {
                    Interval = new TimeSpan(0, 0, 0, 0, 400)
                };
                aiTimer.Tick += AiTimerOnTick;
                //Also show path tickbox
                chkShowPath.Visibility = Visibility.Visible;
            }

            //Allow keydown so that starts the game etc
            allowKeyDown = true;
        }