Exemplo n.º 1
0
        private void HandleKeyboard(KeyboardData data)
        {
            bool aDown = false;
            bool dDown = false;

            foreach (Keys key in data.KeysDown)
            {
                switch (key)
                {
                case Keys.A:
                    aDown = true;
                    break;

                case Keys.D:
                    dDown = true;
                    break;
                }
            }

            if (aDown ^ dDown)
            {
                int multiplier = aDown ? -1 : 1;

                Body.Accelerating = true;
                Body.Decelerating = false;
                Body.ApplyRawForce(new Vector2(PhysicsConvert.ToMeters(playerData.Acceleration * multiplier), 0));

                FlipHorizontally = aDown;

                if (idle)
                {
                    idle = false;
                    PlayAnimation("Running");
                }
            }
            else if (Math.Abs(Body.LinearVelocity.X) > 0)
            {
                int multiplier = Body.LinearVelocity.X > 0 ? -1 : 1;

                Body.Accelerating = false;
                Body.Decelerating = true;
                Body.ApplyRawForce(new Vector2(PhysicsConvert.ToMeters(playerData.Deceleration * multiplier), 0));
            }
            else
            {
                Body.Accelerating = false;
                Body.Decelerating = false;

                if (!idle)
                {
                    idle = true;
                    PlayAnimation("Idle");
                }
            }
        }
Exemplo n.º 2
0
        public Player() : base(EntityTypes.Player)
        {
            LoadAnimations("PlayerAnimations.json");
            PlayAnimation("Idle");

            playerData = new PlayerData();

            Body                    = PhysicsFactory.CreateRectangle(PlayerWidth, PlayerHeight, Units.Pixels, BodyType.Dynamic, this);
            Body.Friction           = 0;
            Body.ManuallyControlled = true;
            Body.MaximumSpeed       = new Vector2(PhysicsConvert.ToMeters(playerData.MaxSpeed), float.PositiveInfinity);

            idle = true;

            MessageSystem.Subscribe(MessageTypes.Keyboard, this);
            MessageSystem.Subscribe(MessageTypes.Mouse, this);
        }
Exemplo n.º 3
0
        public virtual void Update(float dt)
        {
            if (Body != null)
            {
                if (OnGround)
                {
                    Body.Position = PhysicsConvert.ToMeters(Position);
                    Body.Rotation = Rotation;
                }
                else
                {
                    selfUpdate = true;
                    Position   = PhysicsConvert.ToPixels(Body.Position);
                    Rotation   = Body.Rotation;
                    selfUpdate = false;
                }
            }

            timers?.ForEach(t => t.Update(dt));
            components?.ForEach(c => c.Update(dt));
        }