Game-Design / Sprint3_actual / ActiveItems / MagicBoomerang.cs
MagicBoomerang.cs
Raw
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Sprint3_actual.StateFiles;
using System;
using System.Collections.Generic;
using System.Text;
using Sprint3_actual.SoundManagement;

namespace Sprint3_actual.ActiveItems
{
    public class MagicBoomerang : IActiveItem
    {
        private ILink link;
        private Game1 game;
        private Texture2D texture;
        private Vector2 ItemPos;
        private Vector2 ItemSpeed;

        public Rectangle itemRectangle { get; set; }
        private int SpriteHeight;
        private int SpriteWidth;
        private AItemSprites ItemSprite;
        private bool Removed, hasCollided;
        private const int shift4 = 4;
        private const int shift28 = 28;
        private const int shift36 = 36;
        public MagicBoomerang(ILink link, Game1 game)
        {
            this.game = game;
            this.link = link;
            texture = game.PlayerTexture;
            Removed = true;
            SpriteHeight = 24;
            SpriteWidth = 24;
        }
        public void ActiveItemDown()
        {
            if (Removed)
            {
                ItemPos = link.Position();
                ItemPos.X += shift4;
                ItemPos.Y += shift36;
                ItemSpeed = new Vector2(0, 5);
                Removed = false;
                hasCollided = false;
                ItemSprite = new MagicBoomerangSprite(this, texture);

                game.currentRoom.LinkItems.Add(this);
            }
        }

        public void ActiveItemLeft()
        {
            if (Removed)
            {
                ItemPos = link.Position();
                ItemPos.X -= shift28;
                ItemPos.Y += shift4;
                ItemSpeed = new Vector2(-5, 0);
                Removed = false;
                hasCollided = false;
                ItemSprite = new MagicBoomerangSprite(this, texture);

                game.currentRoom.LinkItems.Add(this);
            }
        }

        public void ActiveItemRight()
        {
            if (Removed)
            {
                ItemPos = link.Position();
                ItemPos.X += shift36;
                ItemPos.Y += shift4;
                ItemSpeed = new Vector2(5, 0);
                Removed = false;
                hasCollided = false;
                ItemSprite = new MagicBoomerangSprite(this, texture);
                game.currentRoom.LinkItems.Add(this);
            }
        }

        public void ActiveItemUp()
        {
            if (Removed)
            {
                ItemPos = link.Position();
                ItemPos.X += shift4;
                ItemPos.Y -= shift28;
                ItemSpeed = new Vector2(0, -5);
                Removed = false;
                hasCollided = false;
                ItemSprite = new MagicBoomerangSprite(this, texture);
                game.currentRoom.LinkItems.Add(this);
            }
        }

        public float Collide()
        {
            if (!hasCollided)
            {
                ItemSpeed *= -1;
                hasCollided = true;
            }
            return 1.0f;
        }

        public void Draw()
        {
            if (!Removed)
                ItemSprite.draw();
        }

        public bool IsRemoved()
        {
            return Removed;
        }

        public Rectangle PositionRectangle()
        {
            return itemRectangle;
        }

        public void Update()
        {

            if (!Removed)
            {
                ItemSprite.update(GlobalVars.gameTime);
                Vector2 TowardLink = link.Position() + new Vector2(4, 4) - ItemPos;
                if (TowardLink.Length() < 8)
                {
                    Removed = true;
                    itemRectangle = Rectangle.Empty;
                }
                if (TowardLink.Length() > 0)
                {
                    TowardLink.Normalize();
                    TowardLink *= 5;
                }
                if (!hasCollided)
                {
                    ItemPos += ItemSpeed;
                }
                else
                {
                    ItemPos += TowardLink;
                }
                itemRectangle = new Rectangle((int)ItemPos.X, (int)ItemPos.Y, SpriteWidth , SpriteHeight);
            }
        }

        public void Remove()
        {
            Removed = true;
        }

        public void ItemSound()
        {
            SoundEngine.Instance.playBoomerang();
        }
    }
}