]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Projectile.cs
Improved projectiles
[chaz/carfire] / CarFire / CarFire / CarFire / Projectile.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Content;
7 using Microsoft.Xna.Framework.Graphics;
8 using Microsoft.Xna.Framework.Input;
9
10 namespace CarFire
11 {
12 /// <summary>
13 /// This class represents a projectile object that will be spawned whenever a player or a monster fires.
14 /// </summary>
15 public class Projectile
16 {
17 //Member Variables
18 Map theMap;
19 Vector2 velocity;
20 Texture2D projectileModel;
21 int damage;
22 int gridX;
23 int gridY;
24 //The pixel position should be the pixel position on the map. When a projectile is drawn
25 //these will have to be transformed to the coordinate system that the drawable screen is using.
26 int pixelX;
27 int pixelY;
28
29 /// <summary>
30 /// The Constructor for a projectile object.
31 /// </summary>
32 /// <param name="_currentMap">The map that this character will interact with</param>
33 /// <param name="_projectileModel">The model for this projectile</param>
34 /// <param name="_velocity">How fast the projectile moves</param>
35 /// <param name="_gridX">The starting X position in the map grid</param>
36 /// <param name="_gridY">The starting Y position in the map grid</param>
37 /// <param name="_pixelX">The absolute X pixel position on the map</param>
38 /// <param name="_pixelY"> The absolute Y pixel position on the map</param>
39 public Projectile(Map _currentMap,
40 Texture2D _projectileModel,
41 Vector2 _velocity,
42 int _gridX,
43 int _gridY)
44 /*,
45 int _pixelX,
46 int _pixelY)*/
47 {
48 theMap = _currentMap;
49 projectileModel = _projectileModel;
50 velocity = _velocity;
51 gridX = _gridX;
52 gridY = _gridY;
53 pixelX = _gridX * (int)Map.PixelsToUnitSquares;
54 pixelY = _gridY * (int)Map.PixelsToUnitSquares;
55 damage = 20;
56 }
57 public void Update(TimeSpan timespan)
58 {
59 /*
60 //See if something moved onto this projectile.
61 if(theMap.isOpenSquare(gridX, gridY))
62 {
63 theMap.damageSquare(gridX, gridY, damage);
64 }
65 */
66 //If the projectile will be moving to a new grid position we need to check to see if it is occupied.
67 if ((int)((pixelX + velocity.X) / Map.PixelsToUnitSquares) != gridX || (int)((pixelY + velocity.Y) / Map.PixelsToUnitSquares) != gridY)
68 {
69 //bool open = theMap.IsCellOpen((int)((pixelX + velocity.X) /Map.PixelsToUnitSquares), (int)((pixelY + velocity.Y) / Map.PixelsToUnitSquares));
70 //If open just move this projectile there
71 //***Map doesn't need to know that this projectile is there because players/monsters are allowed
72 // to move into the path of projectiles.
73 //if (open)
74 {
75 //Console.WriteLine("pixelX:" + pixelX + " " + " pixelY: "+ pixelY);
76 pixelX += (int)velocity.X;
77 pixelY += (int)velocity.Y;
78 gridX = (int)(pixelX /Map.PixelsToUnitSquares);
79 gridY = (int)(pixelY / Map.PixelsToUnitSquares);
80 }
81 //If the square isn't open then just damage whatever is there
82 //TODO: A projectile must be deleted after this happens
83 //else
84 {
85 //theMap.damageSquare(gridX, gridY, damage);
86 }
87 }
88 //If it is not moving grid positions just increment pixelX and pixelY
89 else
90 {
91 pixelX += (int)velocity.X;
92 pixelY += (int)velocity.Y;
93 }
94
95 }
96 /// <summary>
97 /// This method will draw a projectile to the screen
98 /// </summary>
99 /// <param name="spriteBatch"></param>
100 /// <param name="topLeftX">The map X pixel position of the topLeft of the display</param>
101 /// <param name="topLeftY">The map Y pixel position of the topLeft of the display</param>
102 public void Draw(SpriteBatch spriteBatch)
103 {
104 //Console.WriteLine(gridX + " " + gridY);
105 //Console.WriteLine(theMap.GetRectangleFromCoordinates(new Vector2(gridX, gridY)));
106 Rectangle position = theMap.GetRectangleFromCoordinates(new Vector2(gridX, gridY));
107 //spriteBatch.Draw(projectileModel, position, Color.White);
108 Rectangle position2 = new Rectangle((int)(pixelX), (int)(pixelY), (int)Map.PixelsToUnitSquares, (int)Map.PixelsToUnitSquares);
109 Rectangle position3 = theMap.GetRectangleFromCoordinates(new Vector2(pixelX / Map.PixelsToUnitSquares, pixelY / Map.PixelsToUnitSquares));
110 spriteBatch.Draw(projectileModel, position3, Color.White);
111 }
112
113 /// <summary>
114 /// Basic getters and setters
115 /// </summary>
116 public int GridX { get { return gridX; } set { gridX = value; } }
117 public int GridY { get { return gridY; } set { gridY = value; } }
118 public int PixelX { get { return pixelX; } set { pixelX = value; } }
119 public int PixelY { get { return pixelY; } set { pixelY = value; } }
120 public Map TheMap { get { return theMap; } set { theMap = value; } }
121 public int Damage { get { return damage; } set { damage = value; } }
122 }
123 }
This page took 0.035542 seconds and 4 git commands to generate.