示例#1
0
        // Methoden
        public void move(clsGameObjectMoveable obj, TimeSpan deltaTime)
        {
            try
            {
                // automatisch
                obj.X += (float)(Math.Cos(obj.Angle) * obj.MaxSpeed * deltaTime.TotalSeconds);
                obj.Y += (float)(Math.Sin(obj.Angle) * obj.MaxSpeed * deltaTime.TotalSeconds);

                //obj.X = (int)(obj.X + (obj.Velocity.X * deltaTime.TotalSeconds));
                //obj.Y = (int)(obj.Y + (obj.Velocity.Y * deltaTime.TotalSeconds));

                if (_BorderRectangle.Width != 0 && _BorderRectangle.Height != 0)
                {
                    // Grenzen abchecken, wenn über das Game-Objekt Grenzen vorhanden sin
                    if (obj.X < 0)
                    {
                        obj.X        = 0;
                        obj.Velocity = new Vector2(obj.Velocity.X * -1, obj.Velocity.Y);
                    }
                    if (obj.Y < 0)
                    {
                        obj.Y        = 0;
                        obj.Velocity = new Vector2(obj.Velocity.X, obj.Velocity.Y * -1);
                    }
                    if (obj.X + obj.Width > _BorderRectangle.Width)
                    {
                        obj.X     = _BorderRectangle.Width - obj.Width;
                        obj.Angle = (float)Math.PI * 2 - obj.Angle;
                        //obj.Velocity = new Vector2(obj.Velocity.X * -1, obj.Velocity.Y);
                    }
                    if (obj.Y + obj.Height > _BorderRectangle.Height)
                    {
                        obj.Y     = _BorderRectangle.Height - obj.Height;
                        obj.Angle = (float)Math.PI * 2 - obj.Angle;
                        //obj.Velocity = new Vector2(obj.Velocity.X, obj.Velocity.Y * -1);
                    }
                }
            }
            catch (Exception ex)
            {
                new clsError(ex);
            }
        }
示例#2
0
        // Methoden
        public void move(clsGameObjectMoveable obj, TimeSpan deltaTime)
        {
            Vector2 velocity = new Vector2(
                (obj.Input.West ? -1f : 0f) + (obj.Input.East ? 1f : 0f),
                (obj.Input.North ? -1f : 0f) + (obj.Input.South ? 1f : 0f));

            if (velocity.Length() > 0f)
            {
                // Vektor normalisieren
                velocity = Vector2.Normalize(velocity);

                //State = PlayerState.Walk;
                obj.Angle = (float)Math.Atan2(velocity.Y, velocity.X);
                obj.X    += (velocity.X * obj.MaxSpeed * (float)deltaTime.TotalSeconds);
                obj.Y    += (velocity.Y * obj.MaxSpeed * (float)deltaTime.TotalSeconds);
            }
            else
            {
                //State = PlayerState.Idle;
            }
        }