<commit_before>﻿using System;
using System.Collections.Generic;
using GameEngine;
using GameEngine.GameObjects;
using Microsoft.Xna.Framework;
using Some2DRPG.GameObjects.Misc;

namespace Some2DRPG.GameObjects.Characters
{

    // Could possibly be renamed to AiRpgEntity if is generic enough.
    public class NPC : RPGEntity
    {
        double _attackDelay = 1500;
        double _lastAttack = 0;

        bool _startedDeathAnim = false;
        float _attackDistance = 40f;
        float _agroDistance = 200;
        RPGEntity _target;

        public NPC()
        {
            this.CurrentState = EntityStates.Idle;
            this.AttackPriority = 6;
            this.MaxHP = 50;
            this.HP = this.MaxHP;
            this.Strength = 5;
            this.CollisionGroup = "Shadow";
            this.Interact += NPC_Interact;
        }

        private void NPC_Interact(RPGEntity sender, Entity invoker, GameTime gameTime, TeeEngine engine)
        {
            SpeechBubble speech = new SpeechBubble(this, "Hello there Adventurer! Whats you're name?");

            engine.AddEntity(speech);

            Vector2 distance = this.Pos - invoker.Pos;
            if (distance.X > distance.Y)
                Direction = (distance.X > 0) ? Direction.Left : Direction.Right;
            else
                Direction = (distance.Y > 0) ? Direction.Up : Direction.Down;
        }

        private void AggressiveNpcAI(GameTime gameTime, TeeEngine engine)
        {
            _target = GetHighestPriorityTarget(gameTime, engine, _agroDistance);

            if (CurrentState == EntityStates.Idle)
            {
                if (_target != null) CurrentState = EntityStates.Alert;
            }
            else if (CurrentState == EntityStates.Alert)
            {
                if (_target == null) CurrentState = EntityStates.Idle;
                else
                {
                    if (Vector2.Distance(this.Pos, _target.Pos) < _attackDistance)
                    {
                        // Only Attack during specific delayed intervals.
                        if (gameTime.TotalGameTime.TotalMilliseconds - _lastAttack > _attackDelay)
                        {
                            _lastAttack = gameTime.TotalGameTime.TotalMilliseconds;

                            CurrentDrawableState = "Slash_" + Direction;
                            CurrentState = EntityStates.Attacking;

                            ClearHitList();
                            Drawables.ResetState(CurrentDrawableState, gameTime);
                        }
                    }
                    else Approach(_target.Pos);<commit_msg>NPCs will now face their targets when attacking.<commit_after>﻿using System;
using System.Collections.Generic;
using GameEngine;
using GameEngine.GameObjects;
using Microsoft.Xna.Framework;
using Some2DRPG.GameObjects.Misc;

namespace Some2DRPG.GameObjects.Characters
{

    // Could possibly be renamed to AiRpgEntity if is generic enough.
    public class NPC : RPGEntity
    {
        double _attackDelay = 1500;
        double _lastAttack = 0;

        bool _startedDeathAnim = false;
        float _attackDistance = 40f;
        float _agroDistance = 200;
        RPGEntity _target;

        public NPC()
        {
            this.CurrentState = EntityStates.Idle;
            this.AttackPriority = 6;
            this.MaxHP = 50;
            this.HP = this.MaxHP;
            this.Strength = 5;
            this.CollisionGroup = "Shadow";
            this.Interact += NPC_Interact;
        }

        private void NPC_Interact(RPGEntity sender, Entity invoker, GameTime gameTime, TeeEngine engine)
        {
            SpeechBubble speech = new SpeechBubble(this, "Hello there Adventurer! Whats you're name?");

            engine.AddEntity(speech);

            Vector2 distance = this.Pos - invoker.Pos;
            if (distance.X > distance.Y)
                Direction = (distance.X > 0) ? Direction.Left : Direction.Right;
            else
                Direction = (distance.Y > 0) ? Direction.Up : Direction.Down;
        }

        private void AggressiveNpcAI(GameTime gameTime, TeeEngine engine)
        {
            _target = GetHighestPriorityTarget(gameTime, engine, _agroDistance);

            if (CurrentState == EntityStates.Idle)
            {
                if (_target != null) CurrentState = EntityStates.Alert;
            }
            else if (CurrentState == EntityStates.Alert)
            {
                if (_target == null) CurrentState = EntityStates.Idle;
                else
                {
                    if (Vector2.Distance(this.Pos, _target.Pos) < _attackDistance)
                    {
                        // Only Attack during specific delayed intervals.
                        if (gameTime.TotalGameTime.TotalMilliseconds - _lastAttack > _attackDelay)
                        {
                            _lastAttack = gameTime.TotalGameTime.TotalMilliseconds;

                            CurrentDrawableState = "Slash_" + Direction;
                            CurrentState = EntityStates.Attacking;

                            ClearHitList();
                            Drawables.ResetState(CurrentDrawableState, gameTime);
                        }
                    }
                    else Approach(_target.Pos);

                    Vector2 difference = this.Pos - _target.Pos;
                    if (Math.Abs(difference.X) > Math.Abs(difference.Y))
                        Direction = (difference.X > 0) ? Direction.Left : Direction.Right;
                    else
                        Direction = (difference.Y > 0) ? Direction.Up : Direction.Down;
{'fraction_non_alphanumeric': 0.07715289982425308, 'fraction_numerical': 0.007557117750439367, 'mean_word_length': 1.5797824116047143, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 14, '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': '6098693', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1403, 'n_words': 332}