﻿#region File Description
/*
Copyright (c) 2009 Craig Mesdag, Neil Grey, Patrick Reding

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion

namespace Sheep_Cannon
{
    //Objects that fall under gravity
    abstract public class MovingObject
    {
        protected const float gravity = 800f;
        protected const int playfieldTop = 0;

        protected MovingObjectObserver observer;
        protected Level level;

        protected float mass = 1f;

        public Vector2 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }
        protected Vector2 velocity;

        public Rectangle BoundingRectangle
        {
            get { return boundingRectangle; }
            set { boundingRectangle = value; }
        }
        protected Rectangle boundingRectangle;

        public MovingObject(Level currentLevel, Rectangle initialBoundingRectangle, Vector2 initialVelocity, MovingObjectObserver myObserver)
        {
            level = currentLevel;
            velocity = initialVelocity;
            observer = myObserver;
            boundingRectangle = initialBoundingRectangle;
        }

        public void MoveObject(GameTime gameTime, Farmer farmer, Level level)
        {
            float elapsedTime = (float)gameTime.ElapsedGameTime.Ticks / 10000000;

            velocity.Y += elapsedTime * gravity;
            if (mass != 0) velocity.X += elapsedTime * (level.Wind.X / mass);

            Vector2 velocityVector = velocity * elapsedTime;
            boundingRectangle.Location = new Point(
                (int)Math.Round(velocityVector.X) + boundingRectangle.Location.X,
                (int)Math.Round(velocityVector.Y) + boundingRectangle.Location.Y
            );          

            if (boundingRectangle.Intersects(level.Ground))
            {
                boundingRectangle.Location = new Point(boundingRectangle.Left, level.Ground.Y - boundingRectangle.Height);
                velocity.X = 0f;
                velocity.Y = 0f;
                hitBottom(farmer, gameTime);
            }

            if (CollidesWithFarmer(farmer))
                hitFarmer(farmer, gameTime);
        }

        public virtual bool CollidesWithFarmer(Farmer farmer)
        {
            Rectangle farmerBounds = farmer.BoundingRectangle;
            Rectangle objBounds = BoundingRectangle;

            return farmerBounds.Intersects(objBounds);
        }

        abstract protected void hitFarmer(Farmer farmer, GameTime gameTime);
        abstract protected void hitBottom(Farmer farmer, GameTime gameTime);
        abstract public void Draw(GameTime gameTime, SpriteBatch spriteBatch);
    }
}