Newer
Older
Gola2 / Gola2 / Actor.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Gola2
{
    public abstract class Actor
    {

        private PerPixelAlphaForm form = new PerPixelAlphaForm();
        private bool alive = true;
        private PointF position;

        #region Instance management
        protected Actor()
        {
            ActorEngine.AddActor(this);
            form.Show();
        }

        ~Actor()
        {
            Kill();
        }

        public virtual void Kill()
        {
            if (alive)
                ActorEngine.RemoveActor(this);
            alive = false;
        }
        #endregion

        #region Lifecycle

        protected abstract void Step();

        protected virtual void Update()
        {
            form.SetPosition(new Point((int)position.X, (int)position.Y));
        }

        internal void ExecuteActor()
        {
            Step();
            Update();
        }
        #endregion

        #region Graphigs management
        protected void SetBitmap(Bitmap bmp)
        {
            form.SelectBitmap(bmp);
        }
        #endregion

        #region Utilities (Static)

        public static int rnd(int min, int max)
        {
            return Random.Next(min, max);
        }

        public static float rndf()
        {
            return (float)Random.NextDouble();
        }

        public static float rndf(float max)
        {
            return rndf() * max;
        }

        public static float rndf(float min, float max)
        {
            return min + rndf(max - min);
        }

        #endregion

        #region Utilities

        public bool IsNearestTo(Actor a, float distance)
        {
            return (Math.Pow(X - a.X, 2) + Math.Pow(Y - a.Y, 2)) < (distance * distance);
        }

        #endregion

        public PointF Position { get => position; set => position = value; }
        public float X { get => position.X; set => position.X = value; }
        public float Y { get => position.Y; set => position.Y = value; }

        public Screen Screen => form.Screen;
        public static Random Random => ActorEngine.Random;
    }
}