/// <summary>
        /// Update the player. Takes input from the keyboard and updates the position of the player.
        /// </summary>
        /// <param name="gameTime">Game time</param>
        public void Update(GameTime gameTime)
        {
            Rectangle visibleArea = VirtualScreenSize.CalculateVisibleArea(_playerModel.Position);
            var       deltaTime   = gameTime.ElapsedGameTime.Milliseconds / 1000f;
            var       state       = Keyboard.GetState();
            var       pressedKeys = state.GetPressedKeys();

            if (_animation != null && !_animation.Done)
            {
                if (pressedKeys.Length == 0)
                {
                    _animation = null;
                    return;
                }
                _animation.Update(deltaTime);
                _playerModel.Position = _animation.Position + new Vector2(deltaTime, 0);
                _playerModel.Frame    = _animation.CurrentFrame;
                return;
            }

            if (_animation != null && _animation.Done)
            {
                _playerModel.Position = _animation.Position;
                _playerModel.Frame    = _animation.CurrentFrame;
                MoveFinished?.Invoke(this, EventArgs.Empty);
            }

            if (!Disabled)
            {
                _animation = null;
                if (pressedKeys.Contains(Keys.W))
                {
                    _animation = new PlayerAnimationController(new int[] { 8, 9, 10, 11, 12, 13, 14, 15 },
                                                               _playerModel.Position,
                                                               new Vector2(0, -_playerModel.Speed),
                                                               MoveCooldownPeriod);
                }
                else if (pressedKeys.Contains(Keys.A))
                {
                    _animation = new PlayerAnimationController(new int[] { 16, 17, 18, 19, 20, 21, 22, 23 },
                                                               _playerModel.Position,
                                                               new Vector2(-_playerModel.Speed, 0),
                                                               MoveCooldownPeriod);
                }
                else if (pressedKeys.Contains(Keys.S))
                {
                    _animation = new PlayerAnimationController(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 },
                                                               _playerModel.Position,
                                                               new Vector2(0, _playerModel.Speed),
                                                               MoveCooldownPeriod);
                }
                else if (pressedKeys.Contains(Keys.D))
                {
                    _animation = new PlayerAnimationController(new int[] { 24, 25, 26, 27, 28, 29, 30, 31 },
                                                               _playerModel.Position,
                                                               new Vector2(_playerModel.Speed, 0),
                                                               MoveCooldownPeriod);
                }
            }
        }
예제 #2
0
        public void Process()
        {
            if (waypoints.Count == 0)
            {
                MoveFinished?.Invoke();
            }
            else
            {
                var waypoint = waypoints.First();

                movableEntity.ParentSceneNode.Position = waypoint;

                waypoints.RemoveAt(0);
            }
        }
예제 #3
0
 private void Reset()
 {
     sTimer.Stop();
     Size = size;
     Dock = dock;
     switch (MDirection)
     {
     case TMDirection.Transparent:
     case TMDirection.T3DLeft:
     case TMDirection.T3DLeftToRight:
     case TMDirection.T3DRight:
     case TMDirection.T3DRightToLeft:
     case TMDirection.T3DUp:
     case TMDirection.T3DUpToDown:
     case TMDirection.T3DDown:
     case TMDirection.T3DDownToUp:
         Controls.Remove(alpha);
         BackgroundImage = image;
         break;
     }
     MoveFinished?.Invoke(this, EventArgs.Empty);
 }
예제 #4
0
        private void RegisterToService()
        {
            _server.HubConnection.On("GameRequested", async(string userName) =>
            {
                await System.Windows.Application.Current.Dispatcher.InvokeAsync(
                    () =>
                {
                    GameRequested?.Invoke(this, new UserActionEventArgs(userName));
                });
            });

            _server.HubConnection.On("GameAccepted", async(Guid gameId, string user) =>
            {
                await System.Windows.Application.Current.Dispatcher.InvokeAsync(
                    () =>
                {
                    GameAccepted?.Invoke(this, new GameAcceptedEventArgs(gameId, user));
                });
            });

            _server.HubConnection.On("GameDeclined", async(string userName) =>
            {
                await System.Windows.Application.Current.Dispatcher.InvokeAsync(
                    () =>
                {
                    GameDeclined?.Invoke(this, new UserActionEventArgs(userName));
                });
            });

            _server.HubConnection.On("MoveMade", async(Game game) =>
            {
                await System.Windows.Application.Current.Dispatcher.InvokeAsync(
                    () =>
                {
                    MoveFinished?.Invoke(this, new MoveMadeEventArgs(game));
                });
            });
        }
예제 #5
0
        /// <summary>
        /// Update the player. Takes input from the keyboard and updates the position of the
        /// player's frog.
        /// </summary>
        /// <param name="deltaTime">Delta time</param>
        public void Update(float deltaTime)
        {
            if (_cooldown != null)
            {
                _cooldown.Update(deltaTime);
                if (!_cooldown.Complete)
                {
                    return;
                }
                _cooldown = null;
            }

            if (_animation != null && !_animation.Done)
            {
                _animation.Update(deltaTime);
                _model.Position = _animation.Position + new Vector2(_force * deltaTime, 0);
                _model.Frame    = _animation.CurrentFrame;
                return;
            }
            else
            {
                _model.Position = _model.Position + new Vector2(_force * deltaTime, 0);
            }

            if (_animation != null && _animation.Done)
            {
                if (_inDeathAnimation)
                {
                    _animation        = null;
                    _model.Frame      = 34;
                    _force            = 0f;
                    _model.Flip       = SpriteEffects.None;
                    _model.Position   = new Vector2((16 * 7), 224);
                    _inDeathAnimation = false;
                }
                else
                {
                    _model.Position = _animation.Position;
                    _model.Frame    = _animation.CurrentFrame;
                    _model.Score   += 10;
                    if (_model.Score > _model.HiScore)
                    {
                        _model.HiScore = _model.Score;
                    }
                    MoveFinished?.Invoke(this, EventArgs.Empty);
                }
            }

            if (!Disabled && !_inDeathAnimation)
            {
                _animation = null;

                var state       = Keyboard.GetState();
                var pressedKeys = state.GetPressedKeys();
                if (pressedKeys.Contains(Keys.Up) && _model.Position.Y > 16)
                {
                    _model.Flip = SpriteEffects.None;
                    _animation  = new FrogAnimation(new int[] { 34, 33, 32, 34 },
                                                    _model.Position,
                                                    new Vector2(0, -Distance),
                                                    MoveCooldownPeriod);
                }
                else if (pressedKeys.Contains(Keys.Down) && _model.Position.Y < 240)
                {
                    _model.Flip = SpriteEffects.FlipVertically;
                    _animation  = new FrogAnimation(new int[] { 34, 33, 32, 34 },
                                                    _model.Position,
                                                    new Vector2(0, Distance),
                                                    MoveCooldownPeriod);
                }
                else if (pressedKeys.Contains(Keys.Left) && _model.Position.X > 0)
                {
                    _model.Flip = SpriteEffects.None;
                    _animation  = new FrogAnimation(new int[] { 37, 36, 35, 37 },
                                                    _model.Position,
                                                    new Vector2(-Distance, 0),
                                                    MoveCooldownPeriod);
                }
                else if (pressedKeys.Contains(Keys.Right) && _model.Position.X < 208)
                {
                    _model.Flip = SpriteEffects.FlipHorizontally;
                    _animation  = new FrogAnimation(new int[] { 37, 36, 35, 37 },
                                                    _model.Position,
                                                    new Vector2(Distance, 0),
                                                    MoveCooldownPeriod);
                }
            }
        }
예제 #6
0
파일: Game.cs 프로젝트: r-alex98/SnakeAI
 private void OnMoveFinished(MoveResult moveResult)
 {
     MoveFinished?.Invoke(moveResult);
 }
예제 #7
0
        private void MovementLoop()
        {
            FocusMoveResult result;

            while (!StopMove)
            {
                // Check the current status and report any failures
                L6470Status status = Driver.GetStatus();
                try
                {
                    CheckL6470State(status);
                }
                catch (Exception ex)
                {
                    Driver.SoftHiZ();
                    MoveTask = null;
                    result   = new FocusMoveResult(false, ex.Message);
                    MoveFinished?.Invoke(this, result);
                    return;
                }

                // Get the current position and distance to the target
                int currentPosition = Encoder.GetPosition();
                int delta           = Math.Abs(currentPosition - DesiredPosition);

                // See which direction we're supposed to go, or if we're close enough
                MotorAction action = MotorAction.Stop;
                if (delta < TargetThreshold)
                {
                    action = MotorAction.Stop;
                }
                else if (DesiredPosition > currentPosition)
                {
                    action = MotorAction.Forward;
                    delta  = Math.Min(delta, UpperEncoderBound - BoundSafetyThreshold - currentPosition);
                }
                else if (DesiredPosition < currentPosition)
                {
                    action = MotorAction.Reverse;
                    delta  = Math.Min(delta, currentPosition - LowerEncoderBound - BoundSafetyThreshold);
                }

                // See if the move would take us out of bounds, and if so, don't do it!
                if (action == MotorAction.Forward && currentPosition >= (UpperEncoderBound - BoundSafetyThreshold))
                {
                    action = MotorAction.Stop;
                }
                else if (action == MotorAction.Reverse && currentPosition <= (LowerEncoderBound + BoundSafetyThreshold))
                {
                    action = MotorAction.Stop;
                }

                double newRpm = GetSpeedSetting(delta);

                // Perform the requested action
                switch (action)
                {
                // Shut it down and consider this move a success
                case MotorAction.Stop:
                    Driver.SoftHiZ();
                    MoveTask = null;
                    result   = new FocusMoveResult(true, null);
                    MoveFinished?.Invoke(this, result);
                    return;

                // Go forward if it isn't already going forward
                case MotorAction.Forward:
                    if (Driver.Direction == MotorDirection.Reverse ||
                        status.MotorState == MotorActivity.Stopped ||
                        newRpm != CurrentSpeed)
                    {
                        Driver.Direction    = MotorDirection.Forward;
                        Driver.DesiredSpeed = newRpm;
                        Driver.Run();
                        CurrentSpeed = newRpm;
                    }
                    break;

                // Go backward if it isn't already going backward
                case MotorAction.Reverse:
                    if (Driver.Direction == MotorDirection.Forward ||
                        status.MotorState == MotorActivity.Stopped ||
                        newRpm != CurrentSpeed)
                    {
                        Driver.Direction    = MotorDirection.Reverse;
                        Driver.DesiredSpeed = newRpm;
                        Driver.Run();
                        CurrentSpeed = newRpm;
                    }
                    break;
                }

                Thread.Sleep(MoveLoopDelay);
            }

            // If we get here, the system requested a stop.
            Driver.SoftHiZ();
            MoveTask = null;
            result   = new FocusMoveResult(true, null);
            MoveFinished?.Invoke(this, result);
            return;
        }