<commit_before>using System;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Xplore
{
    public class Enemy : Ship, IShip
    {
        private bool _destroyAnimationStarted;
        private readonly HealthBar _healthBar;
        private Vector2 _destination;
       
        private const float WanderRotationSpeed = 0.99f;
        private float SeekDistance = 100f;
        private float _fleeDistance = 200f;
        public event EventHandler Destroyed;
        private double _lastWanderDecision;
        private const float CircleDistance = 2f;
        private const float CircleRadius = 400f;
        private float _wanderAngle = 5f;
        private float _angleChange = 0.1f;
        private ShipBehaviour _shipBehaviour;
        private float maxForce = 1f;

        public Enemy(Texture2D texture, Vector2 position, ShipType shipType) : base(texture, position, shipType)
        {
            _lastWanderDecision = 0;
            Speed = 3.5f;
            DirectionVector = new Vector2(0, 1);
            RotationSpeed = 0.95f;
            _healthBar = new HealthBar(new ShipHealthBar(this));
        }

        public void Flee(Vector2 location, float distance)
        {
            _fleeDistance = distance;
            Speed = 3.5f;
            _shipBehaviour = ShipBehaviour.Flee;
            _destination = location;
        }

        public void Seek(Vector2 location)
        {
            Speed = 3.5f;
            _shipBehaviour = ShipBehaviour.Seek;
            _destination = location;
        }

        public void Wander()
        {
            Speed = 2f;
            _shipBehaviour = ShipBehaviour.Wander;
        }

        private Vector2 WanderBehaviour(GameTime gameTime)
        {
            var newVector = Wander(gameTime);
            
            var normailzed = new Vector2(newVector.X, newVector.Y);
            normailzed.Normalize();
            //Debug.WriteLine(normailzed);
            return normailzed;

        }

        private Vector2 SeekBehaviour()
        {
            var directionVector = _destination - Center;
            float vectorLength = directionVector.Length();
            if (vectorLength < SeekDistance)
            {
                _shipBehaviour = ShipBehaviour.Wander;
                return Vector2.Zero;
            }
            directionVector.Normalize();
            return directionVector;
        }

        private Vector2 FleeBehaviour()
        {
            var directionVector = -(_destination - Center);
            float vectorLength = directionVector.Length();
            if (vectorLength > _fleeDistance)
            {

                _shipBehaviour = ShipBehaviour.Wander;

            }
            directionVector.Normalize();
            return directionVector;

        }

        public override void Update(GameTime gameTime)
        {
            
            //DEBUGGING HELP
            var keyboardState = Keyboard.GetState();
            var mouseState = Mouse.GetState();
            if (keyboardState.IsKeyDown(Keys.Space))
            {
                var mouseWorldPosition = Camera.GetWorldPosition(new Vector2(mouseState.X, mouseState.Y));
                Seek(mouseWorldPosition);
            }
            if (keyboardState.IsKeyDown(Keys.G))
            {
                var mouseWorldPosition = Camera.GetWorldPosition(new Vector2(mouseState.X, mouseState.Y));
                Flee(mouseWorldPosition, 2000f);
            }
            //END DEBUGGING HELP

            Vector2 steering = Vector2.Zero;
            if (_shipBehaviour == ShipBehaviour.Seek)
            {
                steering += SeekBehaviour();
            }
            if (_shipBehaviour == ShipBehaviour.Wander)
            {
                steering += WanderBehaviour(gameTime);
            }
            if (_shipBehaviour == ShipBehaviour.Flee)
            {
                steering += FleeBehaviour();
            }

            steering = Vector2.Normalize(steering*maxForce);
            steering = steering/Mass;
            DirectionVector = Vector2.Normalize(steering);
            Velocity = DirectionVector*Speed;
            //Debug.WriteLine(DirectionVector);


            //if (_shipBehaviour == ShipBehaviour.Wander)
            //{
            //    DirectionVector = Vector2.Lerp(DirectionGoalVector, DirectionVector, WanderRotationSpeed);
            //}
            //else
            //{
            //    DirectionVector = Vector2.Lerp(DirectionGoalVector, DirectionVector, RotationSpeed);<commit_msg>enemy direction change now is linked to descion time.<commit_after>using System;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Xplore
{
    public class Enemy : Ship, IShip
    {
        private bool _destroyAnimationStarted;
        private readonly HealthBar _healthBar;
        private Vector2 _destination;
       
        private const float WanderRotationSpeed = 0.99f;
        private float SeekDistance = 100f;
        private float _fleeDistance = 200f;
        public event EventHandler Destroyed;
        private double _lastWanderDecision;
        private const float CircleDistance = 2f;
        private const float CircleRadius = 400f;
        private float _wanderAngle = 5f;
        private float _angleChange = 2f;
        private ShipBehaviour _shipBehaviour;
        private float maxForce = 1f;

        public Enemy(Texture2D texture, Vector2 position, ShipType shipType) : base(texture, position, shipType)
        {
            _lastWanderDecision = 0;
            Speed = 3.5f;
            DirectionVector = new Vector2(0, 1);
            RotationSpeed = 0.95f;
            _healthBar = new HealthBar(new ShipHealthBar(this));
        }

        public void Flee(Vector2 location, float distance)
        {
            _fleeDistance = distance;
            Speed = 3.5f;
            _shipBehaviour = ShipBehaviour.Flee;
            _destination = location;
        }

        public void Seek(Vector2 location)
        {
            Speed = 3.5f;
            _shipBehaviour = ShipBehaviour.Seek;
            _destination = location;
        }

        public void Wander()
        {
            Speed = 2f;
            _shipBehaviour = ShipBehaviour.Wander;
        }

        private Vector2 WanderBehaviour(GameTime gameTime)
        {
            //TODO: Direction vector needs to lerp to show smoother direction changes
            var newVector = new Vector2(DirectionVector.X, DirectionVector.Y);
            if(gameTime.TotalGameTime.TotalMilliseconds + 300 > _lastWanderDecision)
            {
                _lastWanderDecision = gameTime.TotalGameTime.TotalMilliseconds;
                newVector = Wander(gameTime);
            }
            
            
            var normailzed = new Vector2(newVector.X, newVector.Y);
            normailzed.Normalize();
            //Debug.WriteLine(normailzed);
            return normailzed;

        }

        private Vector2 SeekBehaviour()
        {
            var directionVector = _destination - Center;
            float vectorLength = directionVector.Length();
            if (vectorLength < SeekDistance)
            {
                _shipBehaviour = ShipBehaviour.Wander;
                return Vector2.Zero;
            }
            directionVector.Normalize();
            return directionVector;
        }

        private Vector2 FleeBehaviour()
        {
            var directionVector = -(_destination - Center);
            float vectorLength = directionVector.Length();
            if (vectorLength > _fleeDistance)
            {

                _shipBehaviour = ShipBehaviour.Wander;

            }
            directionVector.Normalize();
            return directionVector;

        }

        public override void Update(GameTime gameTime)
        {
            
            //DEBUGGING HELP
            var keyboardState = Keyboard.GetState();
            var mouseState = Mouse.GetState();
            if (keyboardState.IsKeyDown(Keys.Space))
            {
                var mouseWorldPosition = Camera.GetWorldPosition(new Vector2(mouseState.X, mouseState.Y));
                Seek(mouseWorldPosition);
            }
            if (keyboardState.IsKeyDown(Keys.G))
            {
                var mouseWorldPosition = Camera.GetWorldPosition(new Vector2(mouseState.X, mouseState.Y));
                Flee(mouseWorldPosition, 2000f);
            }
            //END DEBUGGING HELP

            Vector2 steering = Vector2.Zero;
            if (_shipBehaviour == ShipBehaviour.Seek)
            {
                steering += SeekBehaviour();
            }
            if (_shipBehaviour == ShipBehaviour.Wander)
            {
                steering += WanderBehaviour(gameTime);
            }
            if (_shipBehaviour == ShipBehaviour.Flee)
            {
                steering += FleeBehaviour();
            }

            steering = Vector2.Normalize(steering*maxForce);
            steering = steering/Mass;
            DirectionGoalVector = Vector2.Normalize(steering);
            DirectionVector = Vector2.Lerp(DirectionGoalVector, DirectionVector, 0.99f);
            Velocity = DirectionVector*Speed;
            //Debug.WriteLine(DirectionVector);


            //if (_shipBehaviour == ShipBehaviour.Wander)
            //{
            //    DirectionVector = Vector2.Lerp(DirectionGoalVector, DirectionVector, WanderRotationSpeed);
            //}
            //else
            //{
            //    DirectionVector = Vector2.Lerp(DirectionGoalVector, DirectionVector, RotationSpeed);
{'fraction_non_alphanumeric': 0.07504111842105263, 'fraction_numerical': 0.011615953947368422, 'mean_word_length': 1.706258692628651, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '6293802', 'n_tokens_mistral': 2620, 'n_tokens_neox': 2434, 'n_words': 537}