From ff30178f505c69bc70b0770a3220ebd6d4706c74 Mon Sep 17 00:00:00 2001 From: aldrikboy Date: Thu, 14 Dec 2017 16:39:16 +0100 Subject: dab --- Penguloon/Objects/ObjectBase.cs | 78 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 Penguloon/Objects/ObjectBase.cs (limited to 'Penguloon/Objects/ObjectBase.cs') diff --git a/Penguloon/Objects/ObjectBase.cs b/Penguloon/Objects/ObjectBase.cs new file mode 100644 index 0000000..9834b39 --- /dev/null +++ b/Penguloon/Objects/ObjectBase.cs @@ -0,0 +1,78 @@ +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); + } + } +} \ No newline at end of file -- cgit v1.2.3-70-g09d2