using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Penguloon.Levels; using System; using System.Collections.Generic; using System.Linq; namespace Penguloon.Objects { public abstract class ObjectBase { public int AttackSpeedMS { get; set; } public DateTime LastAttack { get; set; } public float Range { get; set; } public Texture2D Texture { get; set; } public float Rotation { get; set; } public Vector2 Position { get; set; } public Vector2 Center { get; set; } public int TileSpanX { get; set; } public int TileSpanY { get; set; } public Map Map { get; set; } public ObjectBase(Vector2 position, Map map) { this.Map = map; this.Position = position; } public void Update(float deltaTime) { this.Center = new Vector2(Position.X + ((TileSpanX * Map.TileWidth) / 2), Position.Y + ((TileSpanY * Map.TileHeight) / 2)); for (int i = 0; i < Map.Enemies.Count; i++) { if (Contains(Map.Enemies[i].Box.Location.ToVector2())) { Rotation = (float)GetRotation(Map.Enemies[i].Box.Center.ToVector2()); return; } } } public void Draw(float deltaTime) { float rotation = ((Rotation)); Rectangle rec = new Rectangle( (int)Position.X + ((int)(Map.TileWidth * TileSpanX)) / 2, (int)Position.Y + ((int)(Map.TileHeight * TileSpanY)) / 2, Map.TileWidth * TileSpanX, Map.TileHeight * TileSpanY); Map.ParentScene.Main.SpriteBatch.Draw(Texture, destinationRectangle: rec, rotation: rotation, origin: new Vector2(Texture.Width / 2, Texture.Height / 2)); Map.ParentScene.Main.SpriteBatch.DrawString(ContentManager.GetFont("Fonts/GWENT/24"), Rotation.ToString(), rec.Location.ToVector2(), Color.Red); } private double GetRotation(Vector2 enemyPos) { double radians = Math.Atan2(enemyPos.Y - Center.Y, enemyPos.X - Center.X); return (radians / MathHelper.PiOver2) * MathHelper.Pi; } // The concise version... private bool Contains(Vector2 point) { return ((point - Center).Length() <= Range); } } }