<commit_before>﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Scott.Dungeon.ComponentModel
{
    /// <summary>
    /// Tracks movement information
    /// </summary>
    public class Movement : AbstractGameObjectComponent
    {
        /// <summary>
        /// The speed (in units/sec) that the movement is occurring
        /// </summary>
        public int Speed { get; set; }

        /// <summary>
        /// The direction that the movement is occurring
        /// </summary>
        public Direction Direction { get; set; }

        public bool IsMoving
        {
            get
            {
                return ( Speed < 1 );
            }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public Movement()
        {
            Speed = 0;
            Direction = Direction.South;
        }

        /// <summary>
        /// Update movement game components
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update( GameTime gameTime )
        {
            // Only perform the movement math and logic if we were actually requested
            // to move.
            if ( Speed > 0 )
            {
                float timeDelta = (float) gameTime.ElapsedGameTime.TotalSeconds;

                // Determine axis of motion
                Vector2 movementAxis = Vector2.Zero;

                switch ( Direction )
                {
                    case Direction.North:
                        movementAxis = new Vector2( 0, -1 );
                        break;

                    case Direction.South:
                        movementAxis = new Vector2( 0, 1 );
                        break;

                    case Direction.West:
                        movementAxis = new Vector2( -1, 0 );
                        break;

                    case Direction.East:
                        movementAxis = new Vector2( 1, 0 );
                        break;
                }

                // Calculate the new position
                Vector2 position = Owner.Position + ( movementAxis * Speed * timeDelta );

                // Don't let the object go out of bounds (TODO: Do this smarter when we get real levels)
                Collider collider = Owner.GetComponent<Collider>();

                int screenWidth  = GameRoot.ViewportWidth;
                int screenHeight = GameRoot.ViewportHeight;
                int objectWidth  = (int) collider.Width;
                int objectHeight = (int) collider.Height;

                bool isOutOfBounds =
                    position.X < 0 || position.X + (int) collider.Width > screenWidth ||
                    position.Y < 0 || position.Y + (int) collider.Height > screenHeight;

                // Update the movement information
                if ( !isOutOfBounds )
                {
                    Owner.Direction = Direction;
                    Owner.Position = position;
                }

            }

            // Reset movement back to zero for the next update cycle.
            Speed = 0;
        }
    }
}<commit_msg>Tighter out of bounds detection<commit_after>﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Scott.Dungeon.ComponentModel
{
    /// <summary>
    /// Tracks movement information
    /// </summary>
    public class Movement : AbstractGameObjectComponent
    {
        /// <summary>
        /// The speed (in units/sec) that the movement is occurring
        /// </summary>
        public int Speed { get; set; }

        /// <summary>
        /// The direction that the movement is occurring
        /// </summary>
        public Direction Direction { get; set; }

        public bool IsMoving
        {
            get
            {
                return ( Speed < 1 );
            }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public Movement()
        {
            Speed = 0;
            Direction = Direction.South;
        }

        /// <summary>
        /// Update movement game components
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update( GameTime gameTime )
        {
            // Only perform the movement math and logic if we were actually requested
            // to move.
            if ( Speed > 0 )
            {
                float timeDelta = (float) gameTime.ElapsedGameTime.TotalSeconds;

                // Determine axis of motion
                Vector2 movementAxis = Vector2.Zero;

                switch ( Direction )
                {
                    case Direction.North:
                        movementAxis = new Vector2( 0, -1 );
                        break;

                    case Direction.South:
                        movementAxis = new Vector2( 0, 1 );
                        break;

                    case Direction.West:
                        movementAxis = new Vector2( -1, 0 );
                        break;

                    case Direction.East:
                        movementAxis = new Vector2( 1, 0 );
                        break;
                }

                // Calculate the new position
                Vector2 position = Owner.Position + ( movementAxis * Speed * timeDelta );

                // Don't let the object go out of bounds (TODO: Do this smarter when we get real levels)
                Collider collider = Owner.GetComponent<Collider>();

                int screenWidth  = GameRoot.ViewportWidth;
                int screenHeight = GameRoot.ViewportHeight;

                bool isOutOfBounds =
                    position.X + collider.UpperLeft.X  < 0           ||
                    position.X + collider.UpperRight.X > screenWidth ||
                    position.Y + collider.UpperLeft.Y  < 0 ||
                    position.Y + collider.LowerRight.Y > screenHeight;

                // Update the movement information
                if ( !isOutOfBounds )
                {
                    Owner.Direction = Direction;
                    Owner.Position = position;
                }

            }

            // Reset movement back to zero for the next update cycle.
            Speed = 0;
        }
    }
}
{'fraction_non_alphanumeric': 0.08523973675963648, 'fraction_numerical': 0.006581009088060169, 'mean_word_length': 1.1578769438810006, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '130686', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1430, 'n_words': 442}