Hello, I’m building a space game where i have a spaceship moving around so far, but i can’t really figure out why my Weapon/shooting class wont work. I have tried finding a solution for this for quite some time now but can’t find anything that solves my problem. Can anyone see what I’m missing out? I’m new to this forum and couldn’t find any similar thread with a similar problem.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media;
namespace SpaceGame { class Weapon { private List<string> WEAPON_ASSETNAME = new List<string>(); //Stores the ship img string private List<Texture2D> THE_WEAPON = new List<Texture2D>(); //Stores the Weapon GFX img private List<Vector2> POSITION = new List<Vector2>(); //Position of shots private List<Vector2> DIRECTION = new List<Vector2>(); //Shot Direction private List<float> WEAPON_SPEED = new List<float>(); //Every weapons moving speed private float ANGLE = 0; public int ACTIVE_WEAPON = 1; //Decides which weapon is active in the list private List<int> SHOT_DELAY = new List<int>(); //Adds delays for each weapon private int SHOT_TIME; //Cooldown after a shot
GraphicsDeviceManager GFX; private Ship ship;
KeyboardState mPreviousKeyboardState;
enum State { Shooting } State mCurrentState = State.Shooting;
public Weapon(Ship player, GraphicsDeviceManager graphics) { ship = player; GFX = graphics; }
public void LoadContent(ContentManager theContentManager) { //Blue bullet WEAPON_ASSETNAME.Add("weapon/bullet_blue"); SHOT_DELAY.Add(500); WEAPON_SPEED.Add(2);
//Red Bullet WEAPON_ASSETNAME.Add("weapon/bullet_red"); SHOT_DELAY.Add(150); WEAPON_SPEED.Add(5);
foreach (var asset in WEAPON_ASSETNAME) { THE_WEAPON.Add(theContentManager.Load<Texture2D>(asset)); }
}
public void Update(GameTime theGameTime) { ANGLE = ship.ANGLE; //Get the updated ship angle
KeyboardState aCurrentKeyboardState = Keyboard.GetState(); UpdateMovement(aCurrentKeyboardState); mPreviousKeyboardState = aCurrentKeyboardState;
if (SHOT_TIME > 0) { SHOT_TIME -= theGameTime.ElapsedGameTime.Milliseconds; }
for (int i = 0; i < POSITION.Count; i++) //Move the shot { POSITION[i] += DIRECTION[i]; if (POSITION[i].X < -100 || POSITION[i].X > GFX.GraphicsDevice.Viewport.Width + 100 || POSITION[i].Y < -100 || POSITION[i].Y > GFX.GraphicsDevice.Viewport.Height + 100) { POSITION.RemoveAt(i); //Removes the shot once it's outside the map DIRECTION.RemoveAt(i); //Removes the shot speed outside the map continue; }
} }
public void Draw(SpriteBatch theSpriteBatch) {
for (int i = 0; i < POSITION.Count; i++) //Draw out shots { theSpriteBatch.Draw( THE_WEAPON[ACTIVE_WEAPON], POSITION[i], new Rectangle(0, 0, THE_WEAPON[ACTIVE_WEAPON].Width, THE_WEAPON[ACTIVE_WEAPON].Height), Color.White, 0f, new Vector2(THE_WEAPON[ACTIVE_WEAPON].Width / 2, THE_WEAPON[ACTIVE_WEAPON].Height / 2), 1.0f, SpriteEffects.None, 0f); } }
private void UpdateMovement(KeyboardState aCurrentKeyboardState) { if (mCurrentState == State.Shooting) { if (aCurrentKeyboardState.IsKeyDown(Keys.Space) == true /*&& SHOT_TIME == 0*/) { SHOT_TIME = SHOT_DELAY[ACTIVE_WEAPON]; //Activates cooldown for current weapon
POSITION.Add(new Vector2(ship.POSITION.X, ship.POSITION.Y)); DIRECTION.Add(new Vector2( WEAPON_SPEED[ACTIVE_WEAPON] * (float)Math.Cos(ANGLE), WEAPON_SPEED[ACTIVE_WEAPON] * (float)Math.Sin(ANGLE)) );
} } }
} }