示例#1
0
        /// <summary>
        /// Rotation storyboard setup for the canvas
        /// </summary>
        /// <param name="angleDiff">The how much to rotate in what direction</param>
        public static void RotateStoryBoard(int angleDiff)
        {
            //Get the angle that the grid will change to
            int newAngle = Instance.PreviousAngle + angleDiff;

            //Make sure that it doesn't rotate past +/- 90 degrees
            if (newAngle >= 90)
            {
                newAngle = 90;
            }
            else if (newAngle <= -90)
            {
                newAngle = -90;
            }

            //Make sure the rotation storyboard is available to be overwritten (if above 90% complete that is assumed to be
            //a good time to do this)
            if (Instance.rotationStoryboard is null || Instance.rotationStoryboard?.GetCurrentProgress() >= 0.9)
            {
                //Create a storyboard and set its time
                Instance.rotationStoryboard          = new Storyboard();
                Instance.rotationStoryboard.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 250));
                //Make sure the animation goes from the current to the next angle smoothly and set its length
                DoubleAnimation animation = new DoubleAnimation()
                {
                    From     = Instance.PreviousAngle,
                    To       = newAngle,
                    Duration = Instance.rotationStoryboard.Duration
                };
                Instance.rotationStoryboard.Children.Add(animation);
                //Actually set the storyboard to work on the canvas
                Storyboard.SetTarget(animation, Instance.GameCanvas);
                Storyboard.SetTargetProperty(animation,
                                             new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));

                //Start the storyboard
                Instance.rotationStoryboard.Begin();

                //Set the current angle to be the next angle for future calculations
                Instance.PreviousAngle = newAngle;
            }

            //Check to see if the exits need to be updated (unnecessary in networked since this is sent with every transmission)
            if (!CommunicationManager.Instance.IsNetworked)
            {
                ExitingManager.CheckForUpdates(Instance.PreviousAngle, angleDiff);
            }
        }
示例#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;
        }