using System;
using FlatRedBall;
using FlatRedBall.Audio;
using FlatRedBall.Graphics;
using FlatRedBall.Input;
using FlatRedBall.Math;
using FlatRedBall.Math.Geometry;
using FlatRedBall_XNA;
using Lidgren.Network;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

public enum MessageTypes
{
    NONE,                   // None
    //POSITION_DATA,           // Updates to a player's position
    MOVE_UP,
    MOVE_LEFT,
    MOVE_DOWN,
    MOVE_RIGHT,
    VERTICAL_STOP,
    HORIZONTAL_STOP,
    NEW_FIREBALL,            // New fireball shot from other player
    NEW_LIGHTNING,          // New lightning shot from other player
    NEW_ATTRACTOR,           // New attractor shot from other player, data includes float x, float y, float velocity, float length/magnitude
    HEALTH_UPDATE,           // Enemy's health has changed to this
    NEW_IMP
}

namespace Wizard_Duel.Screens
{
    public class GameScreen : Screen
    {
        #region Methods
        #region ClassScope

        float attractorPower = GameSettings.attractorPower, fireballSpeed = GameSettings.fireballSpeed, fireballDamage = GameSettings.fireballDamage, attractorDuration = GameSettings.attractorDuration, wizardSpeed = GameSettings.wizardSpeed, lightningDamage = GameSettings.lightningDamage, lightningSpeed = GameSettings.lightningSpeed, startingHealth = GameSettings.startingHealth, startingMana = GameSettings.startingMana, manaPerSecond = GameSettings.manaRegen;
        float fireballRechargeTime = 1, lightningRechargeTime = 5, attractorRechargeTime = 10, impRechargeTime;
        int player1wizard = GameSettings2.player1wizard, player2wizard = GameSettings2.player2wizard;
        double impRecharge, fireballRecharge, lightningRecharge, attractorRecharge, timeForManaRegen, timeToExecute, timeForAttractorStop, timeForEnemyAttractorStop, timeForAttractorExpiration, timeForManaTextExpiration, timeForSpellTextExpiration;
        Sprite cursor, background;
        Attractor currentAttractor, currentEnemyAttractor;         //current attractor being fired
        bool isHost = NetworkScreen.isHost, isClient = NetworkScreen.isClient, gameEnded = false, vert_check = true, hor_check = true;
        NetPeerConfiguration config = NetworkScreen.config;
        NetServer server = NetworkScreen.server;
        NetClient client = NetworkScreen.client;
        Wizard wizard1, wizard2;
        Text gameOverText, notEnoughManaText, spellIsRechargingText, manaText, manaText2;
        AxisAlignedRectangle border1, border2, border3, border4;
        Polygon manapol = Polygon.CreateRectangle(10, 1), manapol2 = Polygon.CreateRectangle(10, 1);

        FlatRedBall.Math.Geometry.Point topLeftMana = new FlatRedBall.Math.Geometry.Point(0, 1);
        FlatRedBall.Math.Geometry.Point topRightMana = new FlatRedBall.Math.Geometry.Point(1, 1);
        FlatRedBall.Math.Geometry.Point bottomLeftMana = new FlatRedBall.Math.Geometry.Point(0, -1);
        FlatRedBall.Math.Geometry.Point bottomRightMana = new FlatRedBall.Math.Geometry.Point(1, -1);

        FlatRedBall.Math.Geometry.Point topLeftMana2 = new FlatRedBall.Math.Geometry.Point(0, 1);
        FlatRedBall.Math.Geometry.Point topRightMana2 = new FlatRedBall.Math.Geometry.Point(1, 1);
        FlatRedBall.Math.Geometry.Point bottomLeftMana2 = new FlatRedBall.Math.Geometry.Point(0, -1);
        FlatRedBall.Math.Geometry.Point bottomRightMana2 = new FlatRedBall.Math.Geometry.Point(1, -1);

        PositionedObjectList<Fireball> fireballs = new PositionedObjectList<Fireball>();
        PositionedObjectList<Lightning> lightnings = new PositionedObjectList<Lightning>();
        PositionedObjectList<Attractor> attractors = new PositionedObjectList<Attractor>();
        PositionedObjectList<Imp> imps = new PositionedObjectList<Imp>();

        #endregion
        #region Constructor and Initialize

        public GameScreen()
            : base("GameScreen")
        {
            // Don't put initialization code here, do it in the Initialize method below
        }

        public override void Initialize(bool addToManagers)
        {
            // Set the screen up here instead of in the Constructor to avoid exceptions occurring during the constructor
            background = SpriteManager.AddSprite("Emerald Theta Space.jpg");
            background.PixelSize = .025f;


            gameOverText = TextManager.AddText(String.Empty);
            notEnoughManaText = TextManager.AddText(String.Empty);
            spellIsRechargingText = TextManager.AddText(String.Empty);
            manaText = TextManager.AddText(String.Empty);
            manaText2 = TextManager.AddText(String.Empty);

            border_init();
            wizard_init();
            health_init();
            mana_init();
            mList_init();


            FlatRedBallServices.Game.IsMouseVisible = false;
            cursor = SpriteManager.AddSprite("reticule.png");

            // AddToManagers should be called LAST in this method:
            if (addToManagers)
            {
                AddToManagers();
            }
        }

        public override void AddToManagers()
        {
        }

        #endregion
        #region Public Methods

        public override void Activity(bool firstTimeCalled)
        {
            //this next bit of code must come before collision_update()
            routine_init();

            for (int i = fireballs.Count - 1; i > -1; i--)
            {
                fireballs[i].Acceleration = new Vector3(0, 0, 0);
            }

            cursor_update();
            collision_update();
            move_update();
            health_update();
            mana_update();
            TextDestroy_Update();
            shoot_fireballs();
            shoot_lightning();
            shoot_attractor();
            attractor_stop();
            imp_movement();
            summon_imp();
        }

        public override void Destroy()
        {
            base.Destroy();
        }

        private void routine_init()
        {
            if (isHost == true)
            {
                server_routine();
            }
            else if (isClient == true)
            {
                client_routine();
            }
        }

        private void border_init()
        {
            border1 = ShapeManager.AddAxisAlignedRectangle();
            border2 = ShapeManager.AddAxisAlignedRectangle();
            border3 = ShapeManager.AddAxisAlignedRectangle();
            border4 = ShapeManager.AddAxisAlignedRectangle();
            border1.ScaleX = 0.1f;
            border1.ScaleY = 17;
            border2.ScaleX = 23;
            border2.ScaleY = 0.1f;
            border3.ScaleX = 0.1f;
            border3.ScaleY = 17;
            border4.ScaleX = 23;
            border4.ScaleY = 0.1f;
            border1.X = -23;
            border2.Y = 17;
            border3.X = 23;
            border4.Y = -17;
        }

        private void wizard_init()
        {
            switch (player1wizard)
            {
                case 0:
                    wizard1 = new Wizard(FlatRedBallServices.GlobalContentManager, "Wizard2.png", .020f);
                    break;

                case 1:

                    wizard1 = new Wizard(FlatRedBallServices.GlobalContentManager, "Hovering Wizard.png", .036f);
                    break;

                case 2:
                    wizard1 = new Wizard(FlatRedBallServices.GlobalContentManager, "Green Wizard.png", .025f);
                    break;

                case 3:
                    wizard1 = new Wizard(FlatRedBallServices.GlobalContentManager, "Hooded Wizard.png", .045f);
                    break;

                case 4:
                    wizard1 = new Wizard(FlatRedBallServices.GlobalContentManager, "KH Wizard.png", .030f);
                    break;

                case 5:
                    wizard1 = new Wizard(FlatRedBallServices.GlobalContentManager, "Maple Wizard.png", .035f);
                    break;

                case 6:
                    wizard1 = new Wizard(FlatRedBallServices.GlobalContentManager, "Caped Wizard.png", .042f);
                    break;

                case 7:
                    wizard1 = new Wizard(FlatRedBallServices.GlobalContentManager, "Old Wizard.png", .048f);
                    break;

                case 8:
                    wizard1 = new Wizard(FlatRedBallServices.GlobalContentManager, "Tall Wizard.png", .037f);
                    break;

                case 9:
                    wizard1 = new Wizard(FlatRedBallServices.GlobalContentManager, "wizard.png", .020f);
                    break;

                case 10:
                    wizard1 = new Wizard(FlatRedBallServices.GlobalContentManager, "Wizard2.png", .020f);
                    break;
            }

            wizard1.X = -15;
            wizard1.health = startingHealth;
            wizard1.healthbar = startingHealth;
            wizard1.mana = startingMana;


            switch (player2wizard)
            {
                case 0:
                    wizard2 = new Wizard(FlatRedBallServices.GlobalContentManager, "Wizard2.png", .020f);
                    break;

                case 1:

                    wizard2 = new Wizard(FlatRedBallServices.GlobalContentManager, "Hovering Wizard.png", .036f);
                    break;

                case 2:
                    wizard2 = new Wizard(FlatRedBallServices.GlobalContentManager, "Green Wizard.png", .025f);
                    break;

                case 3:
                    wizard2 = new Wizard(FlatRedBallServices.GlobalContentManager, "Hooded Wizard.png", .045f);
                    break;

                case 4:
                    wizard2 = new Wizard(FlatRedBallServices.GlobalContentManager, "KH Wizard.png", .030f);
                    break;

                case 5:
                    wizard2 = new Wizard(FlatRedBallServices.GlobalContentManager, "Maple Wizard.png", .035f);
                    break;

                case 6:
                    wizard2 = new Wizard(FlatRedBallServices.GlobalContentManager, "Caped Wizard.png", .042f);
                    break;

                case 7:
                    wizard2 = new Wizard(FlatRedBallServices.GlobalContentManager, "Old Wizard.png", .048f);
                    break;

                case 8:
                    wizard2 = new Wizard(FlatRedBallServices.GlobalContentManager, "Tall Wizard.png", .037f);
                    break;

                case 9:
                    wizard2 = new Wizard(FlatRedBallServices.GlobalContentManager, "wizard.png", .020f);
                    break;

                case 10:
                    wizard2 = new Wizard(FlatRedBallServices.GlobalContentManager, "Wizard2.png", .020f);
                    break;
            }

            wizard2.X = 15;
            wizard2.health = startingHealth;
            wizard2.healthbar = startingHealth;
            wizard2.mana = startingMana;
        }

        private void health_init()
        {
            wizard1.healthpol = Polygon.CreateRectangle(10, 1);
            wizard1.topLeftHealth = new FlatRedBall.Math.Geometry.Point(0, 1);
            wizard1.topRightHealth = new FlatRedBall.Math.Geometry.Point(startingHealth, 1);
            wizard1.bottomLeftHealth = new FlatRedBall.Math.Geometry.Point(0, -1);
            wizard1.bottomRightHealth = new FlatRedBall.Math.Geometry.Point(startingHealth, -1);

            wizard2.healthpol = Polygon.CreateRectangle(10, 1);
            wizard2.topLeftHealth = new FlatRedBall.Math.Geometry.Point(0, 1);
            wizard2.topRightHealth = new FlatRedBall.Math.Geometry.Point(startingHealth, 1);
            wizard2.bottomLeftHealth = new FlatRedBall.Math.Geometry.Point(0, -1);
            wizard2.bottomRightHealth = new FlatRedBall.Math.Geometry.Point(startingHealth, -1);

            wizard1.healthpol.SetPoint(0, wizard1.topLeftHealth);
            wizard1.healthpol.SetPoint(1, wizard1.topRightHealth);
            wizard1.healthpol.SetPoint(2, wizard1.bottomRightHealth);
            wizard1.healthpol.SetPoint(3, wizard1.bottomLeftHealth);
            wizard1.healthpol.SetPoint(4, wizard1.topLeftHealth);
            wizard1.healthpol.X = -20;
            wizard1.healthpol.Y = 15;

            wizard2.healthpol.SetPoint(0, wizard2.topLeftHealth);
            wizard2.healthpol.SetPoint(1, wizard2.topRightHealth);
            wizard2.healthpol.SetPoint(2, wizard2.bottomRightHealth);
            wizard2.healthpol.SetPoint(3, wizard2.bottomLeftHealth);
            wizard2.healthpol.SetPoint(4, wizard2.topLeftHealth);
            wizard2.healthpol.X = 10;
            wizard2.healthpol.Y = 15;

            ShapeManager.AddPolygon(wizard1.healthpol);
            ShapeManager.AddPolygon(wizard2.healthpol);
            wizard1.healthpol.Color = Color.Green;
            wizard2.healthpol.Color = Color.Green;

            wizard1.healthText = TextManager.AddText(String.Empty);
            wizard2.healthText = TextManager.AddText(String.Empty);

            wizard1.healthText.DisplayText = string.Concat(wizard1.health);
            wizard2.healthText.DisplayText = string.Concat(wizard2.health);

            wizard1.healthText.Scale = 0.5f;
            wizard1.healthText.Spacing = 0.7f;
            wizard1.healthText.X = -15.5f;
            wizard1.healthText.Y = 15;
            wizard1.healthText.Red = 1f;
            wizard1.healthText.Green = .25f;
            wizard1.healthText.Blue = .25f;

            wizard2.healthText.Scale = 0.5f;
            wizard2.healthText.Spacing = 0.7f;
            wizard2.healthText.X = 14.5f;
            wizard2.healthText.Y = 15;
            wizard2.healthText.Red = 1f;
            wizard2.healthText.Green = .25f;
            wizard2.healthText.Blue = .25f;
        }

        private void mana_init()
        {
            manapol.SetPoint(0, topLeftMana);
            manapol.SetPoint(1, topRightMana);
            manapol.SetPoint(2, bottomRightMana);
            manapol.SetPoint(3, bottomLeftMana);
            manapol.SetPoint(4, topLeftMana);
            manapol.X = -20;
            manapol.Y = 12.9F;

            manapol2.SetPoint(0, topLeftMana2);
            manapol2.SetPoint(1, topRightMana2);
            manapol2.SetPoint(2, bottomRightMana2);
            manapol2.SetPoint(3, bottomLeftMana2);
            manapol2.SetPoint(4, topLeftMana2);
            manapol2.X = 10;
            manapol2.Y = 12.9F;

            ShapeManager.AddPolygon(manapol);
            ShapeManager.AddPolygon(manapol2);
            manapol.Color = Color.Blue;
            manapol2.Color = Color.Blue;

            manaText.Scale = 0.5f;
            manaText.Spacing = 0.7f;
            manaText.X = -15.5f;
            manaText.Y = 12.9F;
            manaText.Red = 1f;
            manaText.Green = .25f;
            manaText.Blue = .25f;

            manaText2.Scale = 0.5f;
            manaText2.Spacing = 0.7f;
            manaText2.X = 14.5f;
            manaText2.Y = 12.9F;
            manaText2.Red = 1f;
            manaText2.Green = .25f;
            manaText2.Blue = .25f;
        }
        private void mList_init()
        {
            mTexts.Add(gameOverText);
            mTexts.Add(notEnoughManaText);
            mTexts.Add(spellIsRechargingText);
            mSprites.Add(cursor);
            mSprites.Add(background);
            mPolygons.Add(wizard1.healthpol);
            mPolygons.Add(wizard2.healthpol);
            mPolygons.Add(manapol);
            mPolygons.Add(manapol2);
        }

        private void cursor_update()
        {
            cursor.X = InputManager.Mouse.WorldXAt(0);
            cursor.Y = InputManager.Mouse.WorldYAt(0);
        }

        private void move_update()
        {
            switch (isHost)
            {
                case true:

                    if (InputManager.Keyboard.KeyDown(Keys.W) && vert_check == true)
                    {
                        wizard1.YVelocity = wizardSpeed;
                        NetConnection player = server.Connections[0];
                        NetOutgoingMessage om = server.CreateMessage();
                        om.Write((byte)MessageTypes.MOVE_UP);
                        //om.Write(wizard1.YVelocity);
                        server.SendMessage(om, player, NetDeliveryMethod.Unreliable);
                        vert_check = false;
                    }
                    if (InputManager.Keyboard.KeyDown(Keys.S) && vert_check == true)
                    {
                        wizard1.YVelocity = -wizardSpeed;
                        NetConnection player = server.Connections[0];
                        NetOutgoingMessage om = server.CreateMessage();
                        om.Write((byte)MessageTypes.MOVE_DOWN);
                        //om.Write(wizard1.YVelocity);
                        server.SendMessage(om, player, NetDeliveryMethod.Unreliable);
                        vert_check = false;
                    }
                    if (InputManager.Keyboard.KeyReleased(Keys.W) | InputManager.Keyboard.KeyReleased(Keys.S))
                    {
                        wizard1.YVelocity = 0;
                        NetConnection player = server.Connections[0];
                        NetOutgoingMessage om = server.CreateMessage();
                        om.Write((byte)MessageTypes.VERTICAL_STOP);
                        //om.Write(wizard1.YVelocity);
                        server.SendMessage(om, player, NetDeliveryMethod.Unreliable);
                        vert_check = true;
                    }

                    if (InputManager.Keyboard.KeyDown(Keys.A) && hor_check == true)
                    {
                        wizard1.XVelocity = -wizardSpeed;
                        NetConnection player = server.Connections[0];
                        NetOutgoingMessage om = server.CreateMessage();
                        om.Write((byte)MessageTypes.MOVE_LEFT);
                        //om.Write(wizard1.XVelocity);
                        server.SendMessage(om, player, NetDeliveryMethod.Unreliable);
                        hor_check = false;
                    }
                    else if (InputManager.Keyboard.KeyDown(Keys.D) && hor_check == true)
                    {
                        wizard1.XVelocity = wizardSpeed;
                        NetConnection player = server.Connections[0];
                        NetOutgoingMessage om = server.CreateMessage();
                        om.Write((byte)MessageTypes.MOVE_RIGHT);
                        //om.Write(wizard1.XVelocity);
                        server.SendMessage(om, player, NetDeliveryMethod.Unreliable);
                        hor_check = false;
                    }
                    if (InputManager.Keyboard.KeyReleased(Keys.A) | InputManager.Keyboard.KeyReleased(Keys.D))
                    {
                        wizard1.XVelocity = 0;
                        NetConnection player = server.Connections[0];
                        NetOutgoingMessage om = server.CreateMessage();
                        om.Write((byte)MessageTypes.HORIZONTAL_STOP);
                        //om.Write(wizard1.XVelocity);
                        server.SendMessage(om, player, NetDeliveryMethod.Unreliable);
                        hor_check = true;
                    }
                    break;

                case false:

                    if (InputManager.Keyboard.KeyDown(Keys.W) && vert_check == true)
                    {
                        wizard2.YVelocity = wizardSpeed;
                        NetOutgoingMessage om = client.CreateMessage();
                        om.Write((byte)MessageTypes.MOVE_UP);
                        //om.Write(wizard2.YVelocity);
                        client.SendMessage(om, NetDeliveryMethod.Unreliable);
                        vert_check = false;
                    }
                    else if (InputManager.Keyboard.KeyDown(Keys.S) && vert_check == true)
                    {
                        wizard2.YVelocity = -wizardSpeed;
                        NetOutgoingMessage om = client.CreateMessage();
                        om.Write((byte)MessageTypes.MOVE_DOWN);
                        //om.Write(wizard2.YVelocity);
                        client.SendMessage(om, NetDeliveryMethod.Unreliable);
                        vert_check = false;
                    }
                    if (InputManager.Keyboard.KeyReleased(Keys.W) | InputManager.Keyboard.KeyReleased(Keys.S))
                    {
                        wizard2.YVelocity = 0;
                        NetOutgoingMessage om = client.CreateMessage();
                        om.Write((byte)MessageTypes.VERTICAL_STOP);
                        //om.Write(wizard2.YVelocity);
                        client.SendMessage(om, NetDeliveryMethod.Unreliable);
                        vert_check = true;
                    }

                    if (InputManager.Keyboard.KeyDown(Keys.A) && hor_check == true)
                    {
                        wizard2.XVelocity = -wizardSpeed;
                        NetOutgoingMessage om = client.CreateMessage();
                        om.Write((byte)MessageTypes.MOVE_LEFT);
                        //om.Write(wizard2.XVelocity);
                        client.SendMessage(om, NetDeliveryMethod.Unreliable);
                        hor_check = false;
                    }
                    else if (InputManager.Keyboard.KeyDown(Keys.D) && hor_check == true)
                    {
                        wizard2.XVelocity = wizardSpeed;
                        NetOutgoingMessage om = client.CreateMessage();
                        om.Write((byte)MessageTypes.MOVE_RIGHT);
                        //om.Write(wizard2.XVelocity);
                        client.SendMessage(om, NetDeliveryMethod.Unreliable);
                        hor_check = false;
                    }
                    if (InputManager.Keyboard.KeyReleased(Keys.A) | InputManager.Keyboard.KeyReleased(Keys.D))
                    {
                        wizard2.XVelocity = 0;
                        NetOutgoingMessage om = client.CreateMessage();
                        om.Write((byte)MessageTypes.HORIZONTAL_STOP);
                        //om.Write(wizard2.XVelocity);
                        client.SendMessage(om, NetDeliveryMethod.Unreliable);
                        hor_check = true;
                    }

                    break;
            }

            //if (isInput)
            //{
            //    // If there's input; send it to server
            //    if (isHost)
            //    {
            //        NetConnection player = server.Connections[0];
            //        NetOutgoingMessage om = server.CreateMessage();
            //        om.Write((byte)MessageTypes.POSITION_DATA);
            //        om.Write(wizard1.X); // very inefficient to send a full Int32 (4 bytes) but we'll use this for simplicity
            //        om.Write(wizard1.Y);
            //        server.SendMessage(om, player, NetDeliveryMethod.Unreliable);
            //    }
            //    else
            //    {
            //        NetOutgoingMessage om = client.CreateMessage();
            //        om.Write((byte)MessageTypes.POSITION_DATA);
            //        om.Write(wizard2.X); // very inefficient to send a full Int32 (4 bytes) but we'll use this for simplicity
            //        om.Write(wizard2.Y);
            //        client.SendMessage(om, NetDeliveryMethod.Unreliable);
            //    }
            //}
        }

        private void health_update()
        {
            if (wizard1.health < 1)
            {
                wizard1.health = 0;
                GameOver("Wizard 2");
            }

            if (wizard2.health < 1)
            {
                wizard2.health = 0;
                GameOver("Wizard 1");
            }

            if (wizard1.healthbar < wizard1.health)
            {
                wizard1.healthbar += 1 * TimeManager.SecondDifference;
                healthbar_redraw(wizard1);
            }
            else if (wizard1.healthbar > wizard1.health)
            {
                wizard1.healthbar -= 1 * TimeManager.SecondDifference;
                healthbar_redraw(wizard1);
            }
            if (wizard2.healthbar < wizard2.health)
            {
                wizard2.healthbar += 1 * TimeManager.SecondDifference;
                healthbar_redraw(wizard2);
            }
            else if (wizard2.healthbar > wizard2.health)
            {
                wizard2.healthbar -= 1 * TimeManager.SecondDifference;
                healthbar_redraw(wizard2);
            }
        }

        private void healthbar_redraw(Wizard player)
        {
            player.healthbar -= 1 * TimeManager.SecondDifference;
            player.topRightHealth = new FlatRedBall.Math.Geometry.Point(player.healthbar, 1);
            player.bottomRightHealth = new FlatRedBall.Math.Geometry.Point(player.healthbar, -1);
            player.healthpol.SetPoint(1, player.topRightHealth);
            player.healthpol.SetPoint(2, player.bottomRightHealth);
            player.healthText.DisplayText = string.Concat(player.health);
        }

        private void mana_update()
        {
            if (TimeManager.CurrentTime >= timeForManaRegen)
            {
                timeForManaRegen = TimeManager.CurrentTime + 1 / manaPerSecond;
                wizard1.mana += manaPerSecond;
                wizard2.mana += manaPerSecond;

                if (wizard1.mana > startingMana)
                {
                    wizard1.mana = startingMana;
                }

                if (wizard2.mana > startingMana)
                {
                    wizard2.mana = startingMana;
                }

                wizard1.manabar = wizard1.mana;
                wizard2.manabar = wizard2.mana;
            }

            wizard1.manabar += manaPerSecond * TimeManager.SecondDifference;
            wizard2.manabar += manaPerSecond * TimeManager.SecondDifference;

            if (wizard1.manabar > startingMana)
            {
                wizard1.manabar = startingMana;
            }

            if (wizard2.manabar > startingMana)
            {
                wizard2.manabar = startingMana;
            }

            FlatRedBall.Math.Geometry.Point topRightMana = new FlatRedBall.Math.Geometry.Point(wizard1.manabar, 1);
            FlatRedBall.Math.Geometry.Point bottomRightMana = new FlatRedBall.Math.Geometry.Point(wizard1.manabar, -1);
            manapol.SetPoint(1, topRightMana);
            manapol.SetPoint(2, bottomRightMana);
            manaText.DisplayText = string.Concat(wizard1.mana);

            FlatRedBall.Math.Geometry.Point topRightMana2 = new FlatRedBall.Math.Geometry.Point(wizard2.manabar, 1);
            FlatRedBall.Math.Geometry.Point bottomRightMana2 = new FlatRedBall.Math.Geometry.Point(wizard2.manabar, -1);
            manapol2.SetPoint(1, topRightMana2);
            manapol2.SetPoint(2, bottomRightMana2);
            manaText2.DisplayText = string.Concat(wizard2.mana);
        }

        #region Collision
        private void collision_update()
        {
            for (int i = fireballs.Count - 1; i > -1; i--)
            {
                Fireball newFireball = fireballs[i];
                if (newFireball.Collision.CollideAgainst(border1) | newFireball.Collision.CollideAgainst(border2) | newFireball.Collision.CollideAgainst(border3) | newFireball.Collision.CollideAgainst(border4))
                {
                    newFireball.Destroy();
                }
            }

            for (int i = lightnings.Count - 1; i > -1; i--)
            {
                Lightning newLightning = lightnings[i];
                if (newLightning.Collision.CollideAgainst(border1) | newLightning.Collision.CollideAgainst(border2) | newLightning.Collision.CollideAgainst(border3) | newLightning.Collision.CollideAgainst(border4))
                {
                    newLightning.Destroy();
                }
            }

            //for (int i = attractors.Count - 1; i > -1; i--)
            //{
            //    Attractor newAttractor = attractors[i];
            //    if (newAttractor.Collision.CollideAgainst(border1) | newAttractor.Collision.CollideAgainst(border2) | newAttractor.Collision.CollideAgainst(border3) | newAttractor.Collision.CollideAgainst(border4))
            //    {
            //        newAttractor.Destroy();
            //    }
            //}

            wizard1.Collision.CollideAgainstBounce(border1, 0, 1, 0);
            wizard2.Collision.CollideAgainstBounce(border1, 0, 1, 0);
            wizard1.Collision.CollideAgainstBounce(border2, 0, 1, 0);
            wizard2.Collision.CollideAgainstBounce(border2, 0, 1, 0);
            wizard1.Collision.CollideAgainstBounce(border3, 0, 1, 0);
            wizard2.Collision.CollideAgainstBounce(border3, 0, 1, 0);
            wizard1.Collision.CollideAgainstBounce(border4, 0, 1, 0);
            wizard2.Collision.CollideAgainstBounce(border4, 0, 1, 0);

            if (isHost)
            {
                for (int i = fireballs.Count - 1; i > -1; i--)
                {
                    Fireball newFireball = fireballs[i];
                    for (int k = attractors.Count - 1; k > -1; k--)
                    {
                        if (newFireball.Collision.CollideAgainst(attractors[k].Collision))
                        {
                            Vector3 position = newFireball.Position;
                            Vector3 target = new Vector3(attractors[k].X, attractors[k].Y, 0);
                            Vector3 connectingVector = target - position;
                            float distance = connectingVector.Length();
                            if (distance == 0)
                            {
                                distance = 0.000000001f;

                            }
                            float newMagnitude = 2.5f / distance;
                            newMagnitude *= attractorPower;
                            connectingVector.Normalize();
                            connectingVector *= newMagnitude;
                            newFireball.Acceleration += connectingVector;
                        }
                    }
                    if (newFireball.owner == wizard2)
                    {
                        if (newFireball.Collision.CollideAgainst(wizard1.Collision))
                        {
                            newFireball.Destroy();
                            wizard1.health = wizard1.health - fireballDamage;
                            NetConnection player = server.Connections[0];
                            NetOutgoingMessage om = server.CreateMessage();
                            om.Write((byte)MessageTypes.HEALTH_UPDATE);
                            om.Write(wizard1.health); // very inefficient to send a full Int32 (4 bytes) but we'll use this for simplicity
                            server.SendMessage(om, player, NetDeliveryMethod.ReliableUnordered);
                        }
                    }
                    else
                    {
                        //newfireball.owner = wizard1
                        if (newFireball.Collision.CollideAgainst(wizard2.Collision))
                        {
                            newFireball.Destroy();
                            //health of other wizard is not subtracted because that will be sent from the client if it
                            //hits there 
                        }

                        if (!newFireball.Collision.CollideAgainst(wizard1.Collision))
                        {
                            newFireball.colliding = false;
                        }
                        if (newFireball.colliding == false)
                        {
                            if (newFireball.Collision.CollideAgainst(wizard1.Collision))
                            {
                                newFireball.Destroy();
                                wizard1.health = wizard1.health - fireballDamage;
                                NetConnection player = server.Connections[0];
                                NetOutgoingMessage om = server.CreateMessage();
                                om.Write((byte)MessageTypes.HEALTH_UPDATE);
                                om.Write(wizard1.health); // very inefficient to send a full Int32 (4 bytes) but we'll use this for simplicity
                                server.SendMessage(om, player, NetDeliveryMethod.ReliableUnordered);
                            }
                        }
                    }
                }

                for (int i = lightnings.Count - 1; i > -1; i--)
                {
                    Lightning newLightning = lightnings[i];
                    for (int k = attractors.Count - 1; k > -1; k--)
                    {
                        if (newLightning.Collision.CollideAgainst(attractors[k].Collision))
                        {
                            Vector3 position = newLightning.Position;
                            Vector3 target = new Vector3(attractors[k].X, attractors[k].Y, 0);
                            Vector3 connectingVector = target - position;
                            float distance = connectingVector.Length();
                            if (distance == 0)
                            {
                                distance = 0.000000001f;

                            }
                            float newMagnitude = 2.5f / distance;
                            newMagnitude *= attractorPower;
                            connectingVector.Normalize();
                            connectingVector *= newMagnitude;
                            newLightning.Acceleration += connectingVector;
                        }
                    }
                    if (newLightning.owner == wizard2)
                    {
                        if (newLightning.Collision.CollideAgainst(wizard1.Collision))
                        {
                            newLightning.Destroy();
                            wizard1.health = wizard1.health - lightningDamage;
                            NetConnection player = server.Connections[0];
                            NetOutgoingMessage om = server.CreateMessage();
                            om.Write((byte)MessageTypes.HEALTH_UPDATE);
                            om.Write(wizard1.health); // very inefficient to send a full Int32 (4 bytes) but we'll use this for simplicity
                            server.SendMessage(om, player, NetDeliveryMethod.ReliableUnordered);
                        }
                    }
                    else
                    {
                        //newfireball.owner = wizard1
                        if (newLightning.Collision.CollideAgainst(wizard2.Collision))
                        {
                            newLightning.Destroy();
                            //health of other wizard is not subtracted because that will be sent from the client if it
                            //hits there 
                        }
                    }
                }
            }
            else
            {
                for (int i = fireballs.Count - 1; i > -1; i--)
                {
                    Fireball newFireball = fireballs[i];
                    for (int k = attractors.Count - 1; k > -1; k--)
                    {
                        if (newFireball.Collision.CollideAgainst(attractors[k].Collision))
                        {
                            Vector3 position = newFireball.Position;
                            Vector3 target = new Vector3(attractors[k].X, attractors[k].Y, 0);
                            Vector3 connectingVector = target - position;
                            float distance = connectingVector.Length();
                            if (distance == 0)
                            {
                                distance = 0.000000001f;

                            }
                            float newMagnitude = 2.5f / distance;
                            newMagnitude *= attractorPower;
                            connectingVector.Normalize();
                            connectingVector *= newMagnitude;
                            newFireball.Acceleration += connectingVector;
                        }
                    }
                    if (newFireball.owner == wizard1)
                    {
                        if (newFireball.Collision.CollideAgainst(wizard2.Collision))
                        {
                            newFireball.Destroy();
                            wizard2.health = wizard2.health - fireballDamage;
                            NetOutgoingMessage om = client.CreateMessage();
                            om.Write((byte)MessageTypes.HEALTH_UPDATE);
                            om.Write(wizard2.health); // very inefficient to send a full Int32 (4 bytes) but we'll use this for simplicity
                            client.SendMessage(om, NetDeliveryMethod.ReliableUnordered);
                        }
                        if (!newFireball.Collision.CollideAgainst(wizard2.Collision))
                        {
                            newFireball.colliding = false;
                        }
                        if (newFireball.colliding == false)
                        {
                            if (newFireball.Collision.CollideAgainst(wizard2.Collision))
                            {
                                newFireball.Destroy();
                            }
                        }

                    }
                    else
                    {
                        //newfireball.owner = wizard1
                        if (newFireball.Collision.CollideAgainst(wizard1.Collision))
                        {
                            newFireball.Destroy();
                            //health of other wizard is not subtracted because that will be sent from the server if it
                            //hits there         
                        }
                        if (!newFireball.Collision.CollideAgainst(wizard2.Collision))
                        {
                            newFireball.colliding = false;
                        }
                        if (newFireball.colliding == false)
                        {
                            if (newFireball.Collision.CollideAgainst(wizard2.Collision))
                            {
                                newFireball.Destroy();
                                newFireball.Destroy();
                                wizard2.health = wizard2.health - fireballDamage;
                                NetOutgoingMessage om = client.CreateMessage();
                                om.Write((byte)MessageTypes.HEALTH_UPDATE);
                                om.Write(wizard2.health); // very inefficient to send a full Int32 (4 bytes) but we'll use this for simplicity
                                client.SendMessage(om, NetDeliveryMethod.ReliableUnordered);
                            }
                        }

                    }
                }

                for (int i = lightnings.Count - 1; i > -1; i--)
                {
                    Lightning newLightning = lightnings[i];
                    for (int k = attractors.Count - 1; k > -1; k--)
                    {
                        if (newLightning.Collision.CollideAgainst(attractors[k].Collision))
                        {
                            Vector3 position = newLightning.Position;
                            Vector3 target = new Vector3(attractors[k].X, attractors[k].Y, 0);
                            Vector3 connectingVector = target - position;
                            float distance = connectingVector.Length();
                            if (distance == 0)
                            {
                                distance = 0.000000001f;

                            }
                            float newMagnitude = 2.5f / distance;
                            newMagnitude *= attractorPower;
                            connectingVector.Normalize();
                            connectingVector *= newMagnitude;
                            newLightning.Acceleration += connectingVector;
                        }
                    }
                    if (newLightning.owner == wizard1)
                    {
                        if (newLightning.Collision.CollideAgainst(wizard2.Collision))
                        {
                            newLightning.Destroy();
                            wizard2.health = wizard2.health - lightningDamage;
                            NetOutgoingMessage om = client.CreateMessage();
                            om.Write((byte)MessageTypes.HEALTH_UPDATE);
                            om.Write(wizard2.health); // very inefficient to send a full Int32 (4 bytes) but we'll use this for simplicity
                            client.SendMessage(om, NetDeliveryMethod.ReliableUnordered);
                        }
                    }
                    else
                    {
                        //newfireball.owner = wizard1
                        if (newLightning.Collision.CollideAgainst(wizard1.Collision))
                        {
                            newLightning.Destroy();
                            //health of other wizard is not subtracted because that will be sent from the client if it
                            //hits there 
                        }
                    }
                }
            }
        }

        #endregion

        #region projectileCreation
        private void shoot_fireballs()
        {
            if (InputManager.Mouse.ButtonPushed(FlatRedBall.Input.Mouse.MouseButtons.LeftButton) && gameEnded == false)
            {
                if (isHost == true)
                {
                    if (wizard1.mana >= 1)
                    {
                        if (TimeManager.CurrentTime >= fireballRecharge)
                        {
                            fireballRecharge = TimeManager.CurrentTime + fireballRechargeTime;
                            AudioManager.PlaySound("fireball");

                            Fireball newFireball = new Fireball(FlatRedBallServices.GlobalContentManager);
                            newFireball.X = wizard1.X;
                            newFireball.Y = wizard1.Y;
                            newFireball.owner = wizard1;
                            wizard1.mana--;
                            wizard1.manabar--;
                            fireballs.Add(newFireball);

                            newFireball.colliding = true;

                            Vector3 position = newFireball.Position;
                            Vector3 target = new Vector3(cursor.X, cursor.Y, 0);
                            Vector3 connectingVector = target - position;
                            connectingVector.Normalize();
                            connectingVector *= fireballSpeed;
                            newFireball.Velocity = connectingVector;

                            NetConnection player = server.Connections[0];
                            NetOutgoingMessage om = server.CreateMessage();
                            om.Write((byte)MessageTypes.NEW_FIREBALL);
                            om.Write(newFireball.X);
                            om.Write(newFireball.Y);
                            om.Write(connectingVector.X);
                            om.Write(connectingVector.Y);
                            om.Write(wizard1.mana);
                            om.Write(wizard1.manabar);
                            server.SendMessage(om, player, NetDeliveryMethod.Unreliable);
                        }
                        else
                        {
                            spellIsRecharging();
                        }
                    }
                    else
                    {
                        notEnoughMana();
                    }
                }
                else
                {
                    if (wizard2.mana >= 1)
                    {
                        if (TimeManager.CurrentTime >= fireballRecharge)
                        {
                            fireballRecharge = TimeManager.CurrentTime + fireballRechargeTime;
                            AudioManager.PlaySound("fireball");
                            Fireball newFireball = new Fireball(FlatRedBallServices.GlobalContentManager);
                            newFireball.X = wizard2.X;
                            newFireball.Y = wizard2.Y;
                            newFireball.owner = wizard2;
                            wizard2.mana--;
                            wizard2.manabar--;
                            fireballs.Add(newFireball);

                            newFireball.colliding = true;

                            Vector3 position = newFireball.Position;
                            Vector3 target = new Vector3(cursor.X, cursor.Y, 0);
                            Vector3 connectingVector = target - position;
                            connectingVector.Normalize();
                            connectingVector *= fireballSpeed;
                            newFireball.Velocity = connectingVector;

                            NetOutgoingMessage om = client.CreateMessage();
                            om.Write((byte)MessageTypes.NEW_FIREBALL);
                            om.Write(newFireball.X);
                            om.Write(newFireball.Y);
                            om.Write(connectingVector.X);
                            om.Write(connectingVector.Y);
                            om.Write(wizard2.mana);
                            om.Write(wizard2.manabar);
                            client.SendMessage(om, NetDeliveryMethod.Unreliable);
                        }
                        else
                        {
                            spellIsRecharging();
                        }

                    }
                    else
                    {
                        notEnoughMana();
                    }
                }
            }
        }

        private void shoot_lightning()
        {
            if (InputManager.Mouse.ButtonPushed(FlatRedBall.Input.Mouse.MouseButtons.RightButton) && gameEnded == false)
            {
                if (isHost == true)
                {
                    if (wizard1.mana >= 5)
                    {
                        if (TimeManager.CurrentTime >= lightningRecharge)
                        {
                            AudioManager.PlaySound("lightning");
                            lightningRecharge = TimeManager.CurrentTime + lightningRechargeTime;
                            Lightning newLightning = new Lightning(FlatRedBallServices.GlobalContentManager);
                            newLightning.owner = wizard1;
                            newLightning.X = wizard1.X;
                            newLightning.Y = wizard1.Y;
                            wizard1.mana = wizard1.mana - 5;
                            wizard1.manabar = wizard1.manabar - 5;
                            lightnings.Add(newLightning);

                            Vector3 position = newLightning.Position;
                            Vector3 target = new Vector3(cursor.X, cursor.Y, 0);
                            Vector3 connectingVector = target - position;
                            connectingVector.Normalize();
                            connectingVector *= lightningSpeed;
                            newLightning.Velocity = connectingVector;

                            NetConnection player = server.Connections[0];
                            NetOutgoingMessage om = server.CreateMessage();
                            om.Write((byte)MessageTypes.NEW_LIGHTNING);
                            om.Write(newLightning.X);
                            om.Write(newLightning.Y);
                            om.Write(connectingVector.X);
                            om.Write(connectingVector.Y);
                            om.Write(wizard1.mana);
                            om.Write(wizard1.manabar);
                            server.SendMessage(om, player, NetDeliveryMethod.Unreliable);
                        }
                        else
                        {
                            spellIsRecharging();
                        }
                    }
                    else
                    {
                        notEnoughMana();
                    }
                }
                else
                {
                    if (wizard2.mana >= 5)
                    {
                        if (TimeManager.CurrentTime >= lightningRecharge)
                        {
                            AudioManager.PlaySound("lightning");
                            lightningRecharge = TimeManager.CurrentTime + lightningRechargeTime;
                            Lightning newLightning = new Lightning(FlatRedBallServices.GlobalContentManager);
                            newLightning.owner = wizard2;
                            newLightning.X = wizard2.X;
                            newLightning.Y = wizard2.Y;
                            wizard2.mana = wizard2.mana - 5;
                            wizard2.manabar = wizard2.manabar - 5;
                            lightnings.Add(newLightning);

                            Vector3 position = newLightning.Position;
                            Vector3 target = new Vector3(cursor.X, cursor.Y, 0);
                            Vector3 connectingVector = target - position;
                            connectingVector.Normalize();
                            connectingVector *= lightningSpeed;
                            newLightning.Velocity = connectingVector;

                            NetOutgoingMessage om = client.CreateMessage();
                            om.Write((byte)MessageTypes.NEW_LIGHTNING);
                            om.Write(newLightning.X);
                            om.Write(newLightning.Y);
                            om.Write(connectingVector.X);
                            om.Write(connectingVector.Y);
                            om.Write(wizard2.mana);
                            om.Write(wizard2.manabar);
                            client.SendMessage(om, NetDeliveryMethod.Unreliable);
                        }
                        else
                        {
                            spellIsRecharging();
                        }
                    }
                    else
                    {
                        notEnoughMana();
                    }
                }

            }
        }

        private void shoot_attractor()
        {
            if (InputManager.Mouse.ButtonPushed(FlatRedBall.Input.Mouse.MouseButtons.MiddleButton) && gameEnded == false)
            {
                if (isHost == true)
                {
                    if (wizard1.mana >= 5)
                    {
                        if (TimeManager.CurrentTime >= attractorRecharge)
                        {
                            AudioManager.PlaySound("blurp");
                            attractorRecharge = TimeManager.CurrentTime + attractorRechargeTime;
                            Attractor newAttractor = new Attractor(FlatRedBallServices.GlobalContentManager);
                            currentAttractor = newAttractor;
                            newAttractor.X = wizard1.X;
                            newAttractor.Y = wizard1.Y;
                            wizard1.mana = wizard1.mana - 5;
                            wizard1.manabar = wizard1.manabar - 5;
                            attractors.Add(newAttractor);

                            Vector3 position = newAttractor.Position;
                            Vector3 target = new Vector3(cursor.X, cursor.Y, 0);
                            Vector3 connectingVector = target - position;
                            double length = connectingVector.Length();
                            connectingVector.Normalize();
                            connectingVector *= fireballSpeed;
                            newAttractor.Velocity = connectingVector;

                            timeForAttractorStop = TimeManager.CurrentTime + (length / fireballSpeed);
                            timeForAttractorExpiration = TimeManager.CurrentTime + attractorDuration;

                            NetConnection player = server.Connections[0];
                            NetOutgoingMessage om = server.CreateMessage();
                            om.Write((byte)MessageTypes.NEW_ATTRACTOR);
                            om.Write(newAttractor.X);
                            om.Write(newAttractor.Y);
                            om.Write(connectingVector.X);
                            om.Write(connectingVector.Y);
                            om.Write(length / fireballSpeed);
                            om.Write(wizard1.mana);
                            om.Write(wizard1.manabar);
                            server.SendMessage(om, player, NetDeliveryMethod.ReliableUnordered);
                        }
                        else
                        {
                            spellIsRecharging();
                        }
                    }

                    else
                    {
                        notEnoughMana();
                    }
                }
                else
                {
                    if (wizard2.mana >= 5)
                    {
                        if (TimeManager.CurrentTime >= attractorRecharge)
                        {
                            AudioManager.PlaySound("blurp");
                            attractorRecharge = TimeManager.CurrentTime + attractorRechargeTime;
                            Attractor newAttractor = new Attractor(FlatRedBallServices.GlobalContentManager);
                            currentAttractor = newAttractor;
                            newAttractor.X = wizard2.X;
                            newAttractor.Y = wizard2.Y;
                            wizard2.mana = wizard2.mana - 5;
                            wizard2.manabar = wizard2.manabar - 5;
                            attractors.Add(newAttractor);

                            Vector3 position = newAttractor.Position;
                            Vector3 target = new Vector3(cursor.X, cursor.Y, 0);
                            Vector3 connectingVector = target - position;
                            double length = connectingVector.Length();
                            connectingVector.Normalize();
                            connectingVector *= fireballSpeed;
                            newAttractor.Velocity = connectingVector;
                            timeForAttractorStop = TimeManager.CurrentTime + (length / fireballSpeed);
                            timeForAttractorExpiration = TimeManager.CurrentTime + attractorDuration;

                            NetOutgoingMessage im = client.CreateMessage();
                            im.Write((byte)MessageTypes.NEW_ATTRACTOR);
                            im.Write(newAttractor.X);
                            im.Write(newAttractor.Y);
                            im.Write(connectingVector.X);
                            im.Write(connectingVector.Y);
                            im.Write(length / fireballSpeed);
                            im.Write(wizard1.mana);
                            im.Write(wizard1.manabar);
                            client.SendMessage(im, NetDeliveryMethod.ReliableUnordered);
                        }
                        else
                        {
                            spellIsRecharging();
                        }
                    }
                    else
                    {
                        notEnoughMana();
                    }
                }
            }

            for (int k = attractors.Count - 1; k > -1; k--)
            {
                Attractor newAttractor = attractors[k];
                if (TimeManager.CurrentTime >= timeForAttractorExpiration)
                { newAttractor.Destroy(); }
            }
        }

        private void summon_imp()
        {
            if (InputManager.Keyboard.KeyPushed(Keys.Q))
            {
                if (isHost == true)
                {
                    if (wizard1.mana >= 6)
                    {
                        if (TimeManager.CurrentTime >= impRecharge)
                        {
                            impRecharge = TimeManager.CurrentTime + impRechargeTime;
                            //AudioManager.PlaySound("fireball");

                            Imp newImp = new Imp(FlatRedBallServices.GlobalContentManager);
                            newImp.X = cursor.X;
                            newImp.Y = cursor.Y;
                            newImp.owner = wizard1;
                            wizard1.mana = wizard1.mana - 6;
                            wizard1.manabar = wizard1.mana - 6;
                            imps.Add(newImp);



                            NetConnection player = server.Connections[0];
                            NetOutgoingMessage om = server.CreateMessage();
                            om.Write((byte)MessageTypes.NEW_IMP);
                            om.Write(newImp.X);
                            om.Write(newImp.Y);
                            om.Write(wizard1.mana);
                            om.Write(wizard1.manabar);
                            server.SendMessage(om, player, NetDeliveryMethod.Unreliable);
                        }
                        else
                        {
                            spellIsRecharging();
                        }
                    }
                    else
                    {
                        notEnoughMana();
                    }
                }
                else
                {
                    if (wizard2.mana >= 1)
                    {
                        if (TimeManager.CurrentTime >= impRecharge)
                        {
                            impRecharge = TimeManager.CurrentTime + impRechargeTime;
                            //AudioManager.PlaySound("fireball");

                            Imp newImp = new Imp(FlatRedBallServices.GlobalContentManager);
                            newImp.X = cursor.X;
                            newImp.Y = cursor.Y;
                            newImp.owner = wizard2;
                            wizard2.mana = wizard2.mana - 6;
                            wizard2.manabar = wizard2.mana - 6;
                            imps.Add(newImp);

                            NetOutgoingMessage om = client.CreateMessage();
                            om.Write((byte)MessageTypes.NEW_IMP);
                            om.Write(newImp.X);
                            om.Write(newImp.Y);
                            om.Write(wizard2.mana);
                            om.Write(wizard2.manabar);
                            client.SendMessage(om, NetDeliveryMethod.Unreliable);
                        }
                        else
                        {
                            spellIsRecharging();
                        }

                    }
                    else
                    {
                        notEnoughMana();
                    }
                }
            }
        }

        private void imp_movement()
        {
            for (int i = imps.Count - 1; i > -1; i--)
            {
                Imp newImp = imps[i];

                if (newImp.YVelocity == 0 | newImp.Y <= -15)
                {
                    newImp.YVelocity = 5;
                }
                else if (newImp.Y >= 15)
                {
                    newImp.YVelocity = -5;
                }

            }
        }

        private void notEnoughMana()
        {
            timeForManaTextExpiration = TimeManager.CurrentTime + 1;
            if (notEnoughManaText.DisplayText != "You need more mana!")
            {
                notEnoughManaText.DisplayText = "You need more mana!";
                notEnoughManaText.Scale = 0.5f;
                notEnoughManaText.Spacing = 0.7f;
                notEnoughManaText.HorizontalAlignment = HorizontalAlignment.Center;
                notEnoughManaText.Y = 15;
                notEnoughManaText.Red = 1f;
                notEnoughManaText.Green = .25f;
                notEnoughManaText.Blue = .25f;
                notEnoughManaText.Visible = true;
            }
            else
            {
                if (notEnoughManaText.Visible != true)
                {
                    notEnoughManaText.Visible = true;
                }
            }
        }

        private void spellIsRecharging()
        {
            timeForSpellTextExpiration = TimeManager.CurrentTime + 1;
            if (spellIsRechargingText.DisplayText != "That spell is still recharging!")
            {
                spellIsRechargingText.DisplayText = "That spell is still recharging!";
                spellIsRechargingText.Scale = 0.5f;
                spellIsRechargingText.Spacing = 0.7f;
                spellIsRechargingText.HorizontalAlignment = HorizontalAlignment.Center;
                spellIsRechargingText.Y = 15;
                spellIsRechargingText.Red = 1f;
                spellIsRechargingText.Green = .25f;
                spellIsRechargingText.Blue = .25f;
                spellIsRechargingText.Visible = true;
            }
            else
            {
                if (spellIsRechargingText.Visible != true)
                {
                    spellIsRechargingText.Visible = true;
                }
            }
        }

        private void TextDestroy_Update()
        {
            if (TimeManager.CurrentTime >= timeForManaTextExpiration)
            {
                notEnoughManaText.Visible = false;
            }
            if (TimeManager.CurrentTime >= timeForSpellTextExpiration)
            {
                spellIsRechargingText.Visible = false;
            }
        }

        private void attractor_stop()
        {
            if ((TimeManager.CurrentTime >= timeForAttractorStop) && (timeForAttractorStop > 0))
            {
                currentAttractor.Velocity = new Vector3(0, 0, 0);
                currentAttractor.Acceleration = new Vector3(0, 0, 0);
                currentAttractor = null;
                timeForAttractorStop = 0;
            }
            if ((TimeManager.CurrentTime >= timeForEnemyAttractorStop) && (timeForEnemyAttractorStop > 0))
            {
                currentEnemyAttractor.Velocity = new Vector3(0, 0, 0);
                currentEnemyAttractor.Acceleration = new Vector3(0, 0, 0);
                currentEnemyAttractor = null;
                timeForEnemyAttractorStop = 0;
            }
        }

        #endregion
        private void GameOver(string wizardName)
        {
            if (gameEnded == false)
            {
                gameEnded = true;
                if (wizardName == "Wizard 1")
                {
                    wizard2.Destroy();
                    gameOverText.DisplayText = "Wizard 1 wins!";
                }
                else
                {
                    wizard1.Destroy();
                    gameOverText.DisplayText = "Wizard 2 wins!";
                }

                gameOverText.Font = new BitmapFont(@"Content\Kristen_32",
    @"Content\Kristen_32.fnt", FlatRedBallServices.GlobalContentManager);
                gameOverText.HorizontalAlignment = HorizontalAlignment.Center;
                gameOverText.ColorOperation = ColorOperation.Modulate;
                gameOverText.Scale = 1.5f;
                gameOverText.Spacing = 2f;
                gameOverText.Red = 1f;
                gameOverText.Green = .25f;
                gameOverText.Blue = .25f;

                timeToExecute = TimeManager.CurrentTime + 5;
            }

            if (TimeManager.CurrentTime >= timeToExecute)
            {
                wizard1.Destroy();
                wizard2.Destroy();
                TextManager.RemoveText(gameOverText);
                TextManager.RemoveText(notEnoughManaText);
                TextManager.RemoveText(spellIsRechargingText);
                TextManager.RemoveText(manaText);
                TextManager.RemoveText(manaText2);
                TextManager.RemoveText(wizard1.healthText);
                TextManager.RemoveText(wizard2.healthText);
                SpriteManager.RemoveSprite(cursor);
                SpriteManager.RemoveSprite(background);
                ShapeManager.Remove(manapol);
                ShapeManager.Remove(manapol2);
                ShapeManager.Remove(border1);
                ShapeManager.Remove(border2);
                ShapeManager.Remove(border3);
                ShapeManager.Remove(border4);

                for (int i = fireballs.Count - 1; i > -1; i--)
                {
                    Fireball newFireball = fireballs[i];
                    newFireball.Destroy();
                }

                for (int i = lightnings.Count - 1; i > -1; i--)
                {
                    Lightning newLightning = lightnings[i];
                    newLightning.Destroy();
                }

                for (int i = attractors.Count - 1; i > -1; i--)
                {
                    Attractor newAttractor = attractors[i];
                    newAttractor.Destroy();
                }

                MoveToScreen(typeof(GameScreen).FullName);
            }
        }

        #region Networking
        public void server_routine()
        {
            NetIncomingMessage im;
            while ((im = server.ReadMessage()) != null)
            {
                // handle incoming message
                if (im.MessageType == NetIncomingMessageType.Data)
                {
                    MessageTypes msgType = (MessageTypes)im.ReadByte();
                    switch (msgType)
                    {
                        //case MessageTypes.POSITION_DATA:
                        //    float xPos = im.ReadFloat();
                        //    float yPos = im.ReadFloat();
                        //    wizard2.X = xPos;
                        //    wizard2.Y = yPos;
                        //    break;
                        case MessageTypes.MOVE_UP:
                            wizard2.YVelocity = wizardSpeed;
                            break;
                        case MessageTypes.MOVE_DOWN:
                            wizard2.YVelocity = -wizardSpeed;
                            break;
                        case MessageTypes.MOVE_LEFT:
                            wizard2.XVelocity = -wizardSpeed;
                            break;
                        case MessageTypes.MOVE_RIGHT:
                            wizard2.XVelocity = wizardSpeed;
                            break;
                        case MessageTypes.HORIZONTAL_STOP:
                            wizard2.XVelocity = 0;
                            break;
                        case MessageTypes.VERTICAL_STOP:
                            wizard2.YVelocity = 0;
                            break;
                        case MessageTypes.NEW_FIREBALL:
                            float fireX = im.ReadFloat();
                            float fireY = im.ReadFloat();
                            float fireVelX = im.ReadFloat();
                            float fireVelY = im.ReadFloat();
                            wizard2.mana = im.ReadFloat();
                            wizard2.manabar = im.ReadFloat();
                            Fireball newFireball = new Fireball(FlatRedBallServices.GlobalContentManager);
                            newFireball.X = fireX;
                            newFireball.Y = fireY;
                            newFireball.owner = wizard2;
                            fireballs.Add(newFireball);
                            newFireball.Velocity = new Vector3(fireVelX, fireVelY, 0);
                            break;
                        case MessageTypes.NEW_LIGHTNING:
                            float lightningX = im.ReadFloat();
                            float lightningY = im.ReadFloat();
                            float lightningVelX = im.ReadFloat();
                            float lightningVelY = im.ReadFloat();
                            wizard2.mana = im.ReadFloat();
                            wizard2.manabar = im.ReadFloat();
                            Lightning newLightning = new Lightning(FlatRedBallServices.GlobalContentManager);
                            newLightning.X = lightningX;
                            newLightning.Y = lightningY;
                            newLightning.owner = wizard2;
                            lightnings.Add(newLightning);
                            newLightning.Velocity = new Vector3(lightningVelX, lightningVelY, 0);
                            break;
                        case MessageTypes.NEW_IMP:
                            Imp newImp = new Imp(FlatRedBallServices.GlobalContentManager);
                            newImp.X = im.ReadFloat();
                            newImp.Y = im.ReadFloat();
                            wizard2.mana = im.ReadFloat();
                            wizard2.manabar = im.ReadFloat();
                            newImp.owner = wizard2;
                            imps.Add(newImp);
                            break;
                        case MessageTypes.HEALTH_UPDATE:
                            float newHealth = im.ReadFloat();
                            wizard2.health = newHealth;
                            break;
                        case MessageTypes.NEW_ATTRACTOR:
                            float attX = im.ReadFloat();
                            float attY = im.ReadFloat();
                            float attVelX = im.ReadFloat();
                            float attVelY = im.ReadFloat();
                            double distStop = im.ReadDouble();
                            wizard2.mana = im.ReadFloat();
                            wizard2.manabar = im.ReadFloat();
                            Attractor newAttractor = new Attractor(FlatRedBallServices.GlobalContentManager);
                            newAttractor.X = attX;
                            newAttractor.Y = attY;
                            newAttractor.owner = wizard2;
                            attractors.Add(newAttractor);
                            newAttractor.Velocity = new Vector3(attVelX, attVelY, 0);
                            currentEnemyAttractor = newAttractor;
                            timeForEnemyAttractorStop = TimeManager.CurrentTime + distStop;
                            break;
                    }
                }
            }
        }

        public void client_routine()
        {
            NetIncomingMessage im;
            while ((im = client.ReadMessage()) != null)
            {
                if (im.MessageType == NetIncomingMessageType.Data)
                {
                    MessageTypes msgType = (MessageTypes)im.ReadByte();
                    switch (msgType)
                    {
                        //case MessageTypes.POSITION_DATA:
                        //    float xPos = im.ReadFloat();
                        //    float yPos = im.ReadFloat();
                        //    wizard1.X = xPos;
                        //    wizard1.Y = yPos;
                        //    break;
                        case MessageTypes.MOVE_UP:
                            wizard1.YVelocity = wizardSpeed;
                            break;
                        case MessageTypes.MOVE_DOWN:
                            wizard1.YVelocity = -wizardSpeed;
                            break;
                        case MessageTypes.MOVE_LEFT:
                            wizard1.XVelocity = -wizardSpeed;
                            break;
                        case MessageTypes.MOVE_RIGHT:
                            wizard1.XVelocity = wizardSpeed;
                            break;
                        case MessageTypes.HORIZONTAL_STOP:
                            wizard1.XVelocity = 0;
                            break;
                        case MessageTypes.VERTICAL_STOP:
                            wizard1.YVelocity = 0;
                            break;
                        case MessageTypes.NEW_FIREBALL:
                            float fireX = im.ReadFloat();
                            float fireY = im.ReadFloat();
                            float fireVelX = im.ReadFloat();
                            float fireVelY = im.ReadFloat();
                            wizard1.mana = im.ReadFloat();
                            wizard1.manabar = im.ReadFloat();
                            Fireball newFireball = new Fireball(FlatRedBallServices.GlobalContentManager);
                            newFireball.X = fireX;
                            newFireball.Y = fireY;
                            newFireball.owner = wizard1;
                            fireballs.Add(newFireball);
                            newFireball.Velocity = new Vector3(fireVelX, fireVelY, 0);
                            break;
                        case MessageTypes.NEW_LIGHTNING:
                            float lightningX = im.ReadFloat();
                            float lightningY = im.ReadFloat();
                            float lightningVelX = im.ReadFloat();
                            float lightningVelY = im.ReadFloat();
                            wizard1.mana = im.ReadFloat();
                            wizard1.manabar = im.ReadFloat();
                            Lightning newLightning = new Lightning(FlatRedBallServices.GlobalContentManager);
                            newLightning.X = lightningX;
                            newLightning.Y = lightningY;
                            newLightning.owner = wizard1;
                            lightnings.Add(newLightning);
                            newLightning.Velocity = new Vector3(lightningVelX, lightningVelY, 0);
                            break;
                        case MessageTypes.NEW_IMP:
                            Imp newImp = new Imp(FlatRedBallServices.GlobalContentManager);
                            newImp.X = im.ReadFloat();
                            newImp.Y = im.ReadFloat();
                            wizard1.mana = im.ReadFloat();
                            wizard1.manabar = im.ReadFloat();
                            newImp.owner = wizard1;
                            imps.Add(newImp);
                            break;
                        case MessageTypes.HEALTH_UPDATE:
                            float newHealth = im.ReadFloat();
                            wizard1.health = newHealth;
                            break;
                        case MessageTypes.NEW_ATTRACTOR:
                            float attX = im.ReadFloat();
                            float attY = im.ReadFloat();
                            float attVelX = im.ReadFloat();
                            float attVelY = im.ReadFloat();
                            double distStop = im.ReadDouble();
                            wizard1.mana = im.ReadFloat();
                            wizard1.manabar = im.ReadFloat();
                            Attractor newAttractor = new Attractor(FlatRedBallServices.GlobalContentManager);
                            newAttractor.X = attX;
                            newAttractor.Y = attY;
                            newAttractor.owner = wizard1;
                            attractors.Add(newAttractor);
                            newAttractor.Velocity = new Vector3(attVelX, attVelY, 0);
                            currentEnemyAttractor = newAttractor;
                            timeForEnemyAttractorStop = TimeManager.CurrentTime + distStop;
                            break;
                    }
                }
            }
        }
        #endregion
        #endregion
        #endregion
    }
}