using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace CarFire
{
///
/// This class represents a projectile object that will be spawned whenever a player or a monster fires.
///
public class Projectile
{
//Member Variables
Map theMap;
Vector2 velocity;
Texture2D projectileModel;
int damage;
int gridX;
int gridY;
//The pixel position should be the pixel position on the map. When a projectile is drawn
//these will have to be transformed to the coordinate system that the drawable screen is using.
int pixelX;
int pixelY;
///
/// The Constructor for a projectile object.
///
/// The map that this character will interact with
/// The model for this projectile
/// How fast the projectile moves
/// The starting X position in the map grid
/// The starting Y position in the map grid
/// The absolute X pixel position on the map
/// The absolute Y pixel position on the map
public Projectile(Map _currentMap,
Texture2D _projectileModel,
Vector2 _velocity,
int _gridX,
int _gridY)
/*,
int _pixelX,
int _pixelY)*/
{
theMap = _currentMap;
projectileModel = _projectileModel;
velocity = _velocity;
gridX = _gridX;
gridY = _gridY;
pixelX = _gridX * (int)Map.PixelsToUnitSquares;
pixelY = _gridY * (int)Map.PixelsToUnitSquares;
damage = 20;
}
public void Update(TimeSpan timespan)
{
/*
//See if something moved onto this projectile.
if(theMap.isOpenSquare(gridX, gridY))
{
theMap.damageSquare(gridX, gridY, damage);
}
*/
//If the projectile will be moving to a new grid position we need to check to see if it is occupied.
if ((int)((pixelX + velocity.X) / Map.PixelsToUnitSquares) != gridX || (int)((pixelY + velocity.Y) / Map.PixelsToUnitSquares) != gridY)
{
//bool open = theMap.IsCellOpen((int)((pixelX + velocity.X) /Map.PixelsToUnitSquares), (int)((pixelY + velocity.Y) / Map.PixelsToUnitSquares));
//If open just move this projectile there
//***Map doesn't need to know that this projectile is there because players/monsters are allowed
// to move into the path of projectiles.
//if (open)
{
//Console.WriteLine("pixelX:" + pixelX + " " + " pixelY: "+ pixelY);
pixelX += (int)velocity.X;
pixelY += (int)velocity.Y;
gridX = (int)(pixelX /Map.PixelsToUnitSquares);
gridY = (int)(pixelY / Map.PixelsToUnitSquares);
}
//If the square isn't open then just damage whatever is there
//TODO: A projectile must be deleted after this happens
//else
{
//theMap.damageSquare(gridX, gridY, damage);
}
}
//If it is not moving grid positions just increment pixelX and pixelY
else
{
pixelX += (int)velocity.X;
pixelY += (int)velocity.Y;
}
}
///
/// This method will draw a projectile to the screen
///
///
/// The map X pixel position of the topLeft of the display
/// The map Y pixel position of the topLeft of the display
public void Draw(SpriteBatch spriteBatch)
{
//Console.WriteLine(gridX + " " + gridY);
//Console.WriteLine(theMap.GetRectangleFromCoordinates(new Vector2(gridX, gridY)));
Rectangle position = theMap.GetRectangleFromCoordinates(new Vector2(gridX, gridY));
//spriteBatch.Draw(projectileModel, position, Color.White);
Rectangle position2 = new Rectangle((int)(pixelX), (int)(pixelY), (int)Map.PixelsToUnitSquares, (int)Map.PixelsToUnitSquares);
Rectangle position3 = theMap.GetRectangleFromCoordinates(new Vector2(pixelX / Map.PixelsToUnitSquares, pixelY / Map.PixelsToUnitSquares));
spriteBatch.Draw(projectileModel, position3, Color.White);
}
///
/// Basic getters and setters
///
public int GridX { get { return gridX; } set { gridX = value; } }
public int GridY { get { return gridY; } set { gridY = value; } }
public int PixelX { get { return pixelX; } set { pixelX = value; } }
public int PixelY { get { return pixelY; } set { pixelY = value; } }
public Map TheMap { get { return theMap; } set { theMap = value; } }
public int Damage { get { return damage; } set { damage = value; } }
}
}