From ced3d03bdb3ce866d832e03fb212865140905a9a Mon Sep 17 00:00:00 2001 From: Thomas Leyh Date: Sun, 24 Jul 2016 08:14:18 +0200 Subject: Add project files. --- V3/Objects/AbstractBuilding.cs | 110 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 V3/Objects/AbstractBuilding.cs (limited to 'V3/Objects/AbstractBuilding.cs') diff --git a/V3/Objects/AbstractBuilding.cs b/V3/Objects/AbstractBuilding.cs new file mode 100644 index 0000000..bc6061c --- /dev/null +++ b/V3/Objects/AbstractBuilding.cs @@ -0,0 +1,110 @@ +using System; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; + +namespace V3.Objects +{ + public abstract class AbstractBuilding : IBuilding + { + private readonly string mTextureName; + private Texture2D mTexture; + private readonly BuildingFace mFacing; + private Rectangle mTilesetRectangle; + + public Vector2 Position { get; set; } + protected abstract int MaxRobustness { get; } + public abstract int Robustness { get; protected set; } + public abstract string Name { get; } + public virtual int MaxGivesWeapons { get; protected set; } + private bool mIsDestroyed; + public Rectangle BoundaryRectangle { get; } + public int Id { get; } + + protected AbstractBuilding(Vector2 position, Rectangle size, string textureName, BuildingFace facing) + { + Position = position; + Id = IdGenerator.GetNextId(); + // TODO: Hella ugly code + mTilesetRectangle = size; + // Boundary rectangle is smaller than the texture size: + if (this is Castle) + { + BoundaryRectangle = new Rectangle(size.X + 96, size.Y + 296, 900, 500); + } + else + { + BoundaryRectangle = new Rectangle(size.X, size.Y + size.Height / 2, size.Width * 4 / 5, size.Height / 2); + } + mTextureName = textureName; + mFacing = facing; + } + + public void Draw(SpriteBatch spriteBatch) + { + int status = 0; + if (mIsDestroyed) + { + status = 2; + } + else if (Robustness < MaxRobustness / 2) + { + status = 1; + } + Rectangle source = new Rectangle(status * mTilesetRectangle.Width, (mFacing == BuildingFace.SW ? 0 : 1) * mTilesetRectangle.Height + (this is Forge ? 384 : 0), mTilesetRectangle.Width, mTilesetRectangle.Height); + spriteBatch.Draw(mTexture, mTilesetRectangle, source, Color.White); + } + + + public void LoadContent(ContentManager contentManager) + { + mTexture = contentManager.Load("Textures/" + mTextureName); + //mOnePixelTexture = contentManager.Load("Sprites/WhiteRectangle"); + } + + public void TakeDamage(int damage) + { + if (Robustness > 0) + { + Robustness -= damage; + } + + if (Robustness <= 0) + { + Destroyed(); + } + } + + public void UpgradeCounter() + { + MaxGivesWeapons -= 1; + } + + + private void Destroyed() + { + mIsDestroyed = true; + } + + public IGameObject GetSelf() + { + return this; + } + + public override bool Equals(Object obj) + { + if (obj == null) + return false; + if (obj == this) + return true; + if (!(obj is IGameObject)) + return false; + return Id.Equals(((IGameObject) obj).Id); + } + + public override int GetHashCode() + { + return Id; + } + } +} -- cgit v1.2.1