public void MoveInDirection(CGPoint direction, double timeInterval)
        {
            var curPosition = Position;
            var dx          = MovementSpeed * direction.X;
            var dy          = MovementSpeed * direction.Y;
            var ds          = MovementSpeed * (float)timeInterval;

            var targetPosition = new CGPoint(curPosition.X + dx, curPosition.Y + dy);

            var angle = GraphicsUtilities.RadiansBetweenCGPoints(targetPosition, curPosition);
            var ang   = GraphicsUtilities.PalarAdjust(angle);

            ZRotation = ang;

            var distRemaining = GraphicsUtilities.Hypotenuse(dx, dy);

            if (distRemaining < ds)
            {
                Position = targetPosition;
            }
            else
            {
                float x = (float)(curPosition.X - Math.Sin(ang) * ds);
                float y = (float)(curPosition.Y + Math.Cos(ang) * ds);
                Position = new CGPoint(x, y);
            }

            // Don't change to a walk animation if we planning an attack.
            if (!Attacking)
            {
                RequestedAnimation = AnimationState.Walk;
            }
        }
        public void MoveTowards(CGPoint position, double timeInterval)
        {
            CGPoint curPosition = Position;
            var     dx          = position.X - curPosition.X;
            var     dy          = position.Y - curPosition.Y;
            var     ds          = MovementSpeed * (float)timeInterval;

            var angle = GraphicsUtilities.RadiansBetweenCGPoints(position, curPosition);

            angle     = GraphicsUtilities.PalarAdjust(angle);
            ZRotation = angle;

            var distRemaining = GraphicsUtilities.Hypotenuse(dx, dy);

            if (distRemaining < ds)
            {
                Position = position;
            }
            else
            {
                var x = (float)(curPosition.X - Math.Sin(angle) * ds);
                var y = (float)(curPosition.Y + Math.Cos(angle) * ds);
                Position = new CGPoint(x, y);
            }

            RequestedAnimation = AnimationState.Walk;
        }
Esempio n. 3
0
        void ConfigureController(GCController controller, Player player)
        {
            Console.WriteLine("Assigning {0} to player {1} [{2}]", controller.VendorName, player, players.IndexOf(player));

            // Assign the controller to the player.
            player.Controller = controller;

            GCControllerDirectionPadValueChangedHandler dpadMoveHandler = (dpad, xValue, yValue) => {
                var length = GraphicsUtilities.Hypotenuse(xValue, yValue);
                if (length > 0f)
                {
                    var invLength = 1 / length;
                    player.HeroMoveDirection = new CGPoint(xValue * invLength, yValue * invLength);
                }
                else
                {
                    player.HeroMoveDirection = CGPoint.Empty;
                }
            };

            // Use either the dpad or the left thumbstick to move the character.
            controller.ExtendedGamepad.LeftThumbstick.ValueChangedHandler = dpadMoveHandler;
            controller.Gamepad.DPad.ValueChangedHandler = dpadMoveHandler;

            GCControllerButtonValueChanged fireButtonHandler = (button, value, pressed) => {
                player.FireAction = pressed;
            };

            controller.Gamepad.ButtonA.SetValueChangedHandler(fireButtonHandler);
            controller.Gamepad.ButtonB.SetValueChangedHandler(fireButtonHandler);

            if (player != DefaultPlayer && player.Hero == null)
            {
                AddHeroFor(player);
            }
        }
Esempio n. 4
0
        public override void Update(double currentTime)
        {
            // Handle time delta.
            // If we drop below 60fps, we still want everything to move the same distance.
            double timeSinceLast = currentTime - lastUpdateTimeInterval;

            lastUpdateTimeInterval = currentTime;

            // more than a second since last update
            if (timeSinceLast > 1)
            {
                timeSinceLast       = MIN_TIME_INTERVAL;
                WorldMovedForUpdate = true;
            }

            UpdateWithTimeSinceLastUpdate(timeSinceLast);

            var           defaultPlayer = DefaultPlayer;
            HeroCharacter hero          = Heroes.Count > 0 ? defaultPlayer.Hero : null;

                        #if __IOS__
            if (hero != null && !hero.Dying &&
                defaultPlayer.TargetLocation != CGPoint.Empty)
            {
                if (defaultPlayer.FireAction)
                {
                    hero.FaceTo(defaultPlayer.TargetLocation);
                }

                if (defaultPlayer.MoveRequested)
                {
                    if (defaultPlayer.TargetLocation != hero.Position)
                    {
                        hero.MoveTowards(defaultPlayer.TargetLocation, timeSinceLast);
                    }
                    else
                    {
                        defaultPlayer.MoveRequested = false;
                    }
                }
            }
                        #endif

            foreach (var player in players)
            {
                if (player == null)
                {
                    continue;
                }

                hero = player.Hero;
                if (hero == null || hero.Dying)
                {
                    continue;
                }

                // heroMoveDirection is used by game controllers.
                CGPoint heroMoveDirection = player.HeroMoveDirection;
                if (GraphicsUtilities.Hypotenuse(heroMoveDirection.X, heroMoveDirection.Y) > 0f)
                {
                    hero.MoveInDirection(heroMoveDirection, timeSinceLast);
                }
                else
                {
                    if (player.MoveForward)
                    {
                        hero.Move(MoveDirection.Forward, timeSinceLast);
                    }
                    else if (player.MoveBack)
                    {
                        hero.Move(MoveDirection.Back, timeSinceLast);
                    }

                    if (player.MoveLeft)
                    {
                        hero.Move(MoveDirection.Left, timeSinceLast);
                    }
                    else if (player.MoveRight)
                    {
                        hero.Move(MoveDirection.Right, timeSinceLast);
                    }
                }

                if (player.FireAction)
                {
                    hero.PerformAttackAction();
                }
            }
        }