<commit_before>﻿#if XNA
using Microsoft.Xna.Framework;
#endif

using System.Collections.Generic;
using Newtonsoft.Json;
using SadConsole.SerializedTypes;

namespace SadConsole.Entities
{
    // todo Fix serialization
    /// <summary>
    /// A positionable and animated game object.
    /// </summary>
    [JsonConverter(typeof(EntityJsonConverter))]
    public class Entity : Console
    {
        /// <summary>
        /// Automatically forwards the <see cref="AnimatedConsole.AnimationStateChanged"/> event.
        /// </summary>
        public event System.EventHandler<AnimatedConsole.AnimationStateChangedEventArgs> AnimationStateChanged;

        /// <summary>
        /// Triggered when the entity changes position.
        /// </summary>
        public event System.EventHandler<EntityMovedEventArgs> Moved;

        /// <summary>
        /// Animation for the game object.
        /// </summary>
        protected AnimatedConsole animation;

        /// <summary>
        /// The offset to render this object at.
        /// </summary>
        protected Point positionOffset;

        /// <summary>
        /// A friendly name of the game object.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// The current animation.
        /// </summary>
        public AnimatedConsole Animation
        {
            get => animation;
            set
            {
                if (animation != null)
                {
                    animation.State = AnimatedConsole.AnimationState.Deactivated;
                    animation.AnimationStateChanged -= OnAnimationStateChanged;
                    Children.Remove(animation);
                }

                animation = value;
                animation.Font = Font;

                animation.AnimationStateChanged += OnAnimationStateChanged;
                animation.State = AnimatedConsole.AnimationState.Activated;
                Children.Add(animation);
            }
        }

        /// <summary>
        /// Collection of animations associated with this game object.
        /// </summary>
        public Dictionary<string, AnimatedConsole> Animations { get; protected set; } = new Dictionary<string, AnimatedConsole>();

        /// <summary>
        /// Offsets the position by this amount.
        /// </summary>
        public Point PositionOffset
        {
            get => positionOffset;
            set
            {
                positionOffset = value;
                OnCalculateRenderPosition();
            }
        }

        /// <summary>
        /// Creates a new Entity with the default font.
        /// </summary>
        public Entity(int width, int height) : this(width, height, Global.FontDefault) { }

        /// <summary>
        /// Creates a new Entity.
        /// </summary>
        public Entity(int width, int height, Font font)
        {
            IsCursorDisabled = true;
            Animation = new AnimatedConsole("default", width, height, Global.FontDefault);
            animation.CreateFrame();
            Animations.Add("default", animation);
        }

        /// <summary>
        /// Creates a new 1x1 entity with the specified foreground, background, and glyph.
        /// </summary><commit_msg>Entity position offset SET does not trigger if the value is the same<commit_after>﻿#if XNA
using Microsoft.Xna.Framework;
#endif

using System.Collections.Generic;
using Newtonsoft.Json;
using SadConsole.SerializedTypes;

namespace SadConsole.Entities
{
    // todo Fix serialization
    /// <summary>
    /// A positionable and animated game object.
    /// </summary>
    [JsonConverter(typeof(EntityJsonConverter))]
    public class Entity : Console
    {
        /// <summary>
        /// Automatically forwards the <see cref="AnimatedConsole.AnimationStateChanged"/> event.
        /// </summary>
        public event System.EventHandler<AnimatedConsole.AnimationStateChangedEventArgs> AnimationStateChanged;

        /// <summary>
        /// Triggered when the entity changes position.
        /// </summary>
        public event System.EventHandler<EntityMovedEventArgs> Moved;

        /// <summary>
        /// Animation for the game object.
        /// </summary>
        protected AnimatedConsole animation;

        /// <summary>
        /// The offset to render this object at.
        /// </summary>
        protected Point positionOffset;

        /// <summary>
        /// A friendly name of the game object.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// The current animation.
        /// </summary>
        public AnimatedConsole Animation
        {
            get => animation;
            set
            {
                if (animation != null)
                {
                    animation.State = AnimatedConsole.AnimationState.Deactivated;
                    animation.AnimationStateChanged -= OnAnimationStateChanged;
                    Children.Remove(animation);
                }

                animation = value;
                animation.Font = Font;

                animation.AnimationStateChanged += OnAnimationStateChanged;
                animation.State = AnimatedConsole.AnimationState.Activated;
                Children.Add(animation);
            }
        }

        /// <summary>
        /// Collection of animations associated with this game object.
        /// </summary>
        public Dictionary<string, AnimatedConsole> Animations { get; protected set; } = new Dictionary<string, AnimatedConsole>();

        /// <summary>
        /// Offsets the position by this amount.
        /// </summary>
        public Point PositionOffset
        {
            get => positionOffset;
            set
            {
                if (positionOffset == value) return;

                positionOffset = value;
                OnCalculateRenderPosition();
            }
        }

        /// <summary>
        /// Creates a new Entity with the default font.
        /// </summary>
        public Entity(int width, int height) : this(width, height, Global.FontDefault) { }

        /// <summary>
        /// Creates a new Entity.
        /// </summary>
        public Entity(int width, int height, Font font)
        {
            IsCursorDisabled = true;
            Animation = new AnimatedConsole("default", width, height, Global.FontDefault);
            animation.CreateFrame();
            Animations.Add("default", animation);
        }

        /// <summary>
        /// Creates a new 1x1 entity with the specified foreground, background, and glyph.
        /// </summary>
{'fraction_non_alphanumeric': 0.1036869826937547, 'fraction_numerical': 0.0006019563581640331, 'mean_word_length': 1.640444974175606, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 65, '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': '1445916', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1498, 'n_words': 435}