Hey I need to know how to add a function for my fps xna game so my pistol model can shoot and I need a function for adding monsters in the game to shoot with there own health...
my main game.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using FirstPersonCameraWindows.Models;
namespace FirstPersonCameraWindows
{
public class Game : Microsoft.Xna.Framework.Game
{
private const float G = 9.81f;
private const float RotationSpeed = MathHelper.Pi / 300f;
private const float InitRunA = MathHelper.Pi * 6f;
private const float InitRunDecelerateA = MathHelper.Pi * 4f;
private const float InitRunVmax = MathHelper.Pi * 1.2f;
private const float InitJumpV = MathHelper.Pi;
private const float InitForwardSpeed = MathHelper.Pi / 1.5f;
private const float EndOfTheWorld = -2000;
private readonly Vector3 StartPosition = new Vector3(0, 80, 0);
private BaseWorldModel model;
private Camera camera;
private const float avatarBsR = 8;
private Vector3 velocity;
private Vector3? groundNormal;
private float currentYaw;
private float currentPitch;
private float runA = InitRunA;
private float runDecelerateA = InitRunDecelerateA;
private float runVmax = InitRunVmax;
private float jumpV = InitJumpV;
private float forwardSpeed = InitForwardSpeed;
private bool isInAir = false;
private bool isCrouching = false;
private KeyboardState prevKeyboardState;
private MouseState prevMouseState;
private GraphicsDeviceManager graphics;
public Game()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 2560;
graphics.PreferredBackBufferHeight = 1080;
Content.RootDirectory = "Content";
this.Window.Title = "First-person shooter";
}
protected override void Initialize()
{
camera = new Camera(StartPosition, graphics.GraphicsDevice.Viewport.AspectRatio);
Mouse.SetPosition(Window.ClientBounds.Width / 2, Window.ClientBounds.Height / 2);
prevMouseState = Mouse.GetState();
base.Initialize();
}
protected override void LoadContent()
{
model = new WorldModel(Content, GraphicsDevice);
model.SetProjectionMatrix(camera.ProjectionMatrix);
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
{
this.Exit();
}
UpdateCamera(gameTime);
model.update(gameTime, camera);
base.Update(gameTime);
}
private void UpdateCamera(GameTime gameTime)
{
KeyboardState keyboardState = Keyboard.GetState();
MouseState mouseState = Mouse.GetState();
handleKeyboardAndMouse(keyboardState, mouseState);
prevKeyboardState = keyboardState;
prevMouseState = mouseState;
float? moveAngle = getMoveDirection(keyboardState);
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
if (moveAngle != null)
{
Vector3 moveDirection = Vector3.Transform(Vector3.Backward, Matrix.CreateRotationY(moveAngle.Value) * Matrix.CreateRotationY(currentYaw)) * forwardSpeed;
Vector3 newVelocity = velocity + moveDirection * runA * elapsed;
if (newVelocity.Length() <= runVmax)
{
velocity = newVelocity;
}
}
if (!isInAir && !isCrouching && keyboardState.IsKeyDown(Keys.Space))
{
velocity.Y += jumpV;
}
velocity.Y -= G * elapsed;
if (!isInAir)
{
float factor = 1f - runDecelerateA * elapsed;
velocity *= new Vector3(factor, 1, factor);
if (Math.Abs(velocity.X) < 0.01) velocity.X = 0;
if (Math.Abs(velocity.Z) < 0.01) velocity.Z = 0;
}
Vector3 offset = camera.Position + velocity;
int bsCount = isCrouching ? 2 : 3;
BoundingSphere[] bss = new BoundingSphere[bsCount];
bss[bsCount - 1] = new BoundingSphere(offset, avatarBsR);
bss[bsCount - 2] = new BoundingSphere(offset + new Vector3(0, -2f * avatarBsR, 0), avatarBsR);
if (!isCrouching) bss[bsCount - 3] = new BoundingSphere(offset + new Vector3(0, -4f * avatarBsR, 0), avatarBsR);
groundNormal = model.CollitionTest(bss, ref velocity);
camera.Update(velocity, currentYaw, currentPitch);
isInAir = groundNormal == null;
if (camera.Position.Y < EndOfTheWorld)
{
ResetAvatar();
}
}
private void handleKeyboardAndMouse(KeyboardState curKeyboardState, MouseState curMouseState)
{
if (curMouseState.RightButton == ButtonState.Pressed && prevMouseState.RightButton == ButtonState.Released)
{
camera.setViewAngle(MathHelper.Pi / 16);
model.SetProjectionMatrix(camera.ProjectionMatrix);
}
else if (curMouseState.RightButton == ButtonState.Released && prevMouseState.RightButton == ButtonState.Pressed)
{
camera.setViewAngle(Camera.initViewAngle);
model.SetProjectionMatrix(camera.ProjectionMatrix);
}
if (curKeyboardState.IsKeyDown(Keys.LeftShift) && !prevKeyboardState.IsKeyDown(Keys.LeftShift))
{
runA = MathHelper.Pi * 5f / 2f;
runVmax = MathHelper.PiOver2;
}
else if (!curKeyboardState.IsKeyDown(Keys.LeftShift) && prevKeyboardState.IsKeyDown(Keys.LeftShift))
{
runA = InitRunA;
runVmax = InitRunVmax;
}
if (curKeyboardState.IsKeyDown(Keys.LeftControl) && !prevKeyboardState.IsKeyDown(Keys.LeftControl))
{
runA = MathHelper.Pi * 5f / 2f;
runVmax = MathHelper.PiOver2;
isCrouching = true;
camera.Update(new Vector3(0, -2f * avatarBsR, 0), currentYaw, currentPitch);
}
else if (!curKeyboardState.IsKeyDown(Keys.LeftControl) && prevKeyboardState.IsKeyDown(Keys.LeftControl))
{
runA = InitRunA;
runVmax = InitRunVmax;
isCrouching = false;
camera.Update(new Vector3(0, 2 * avatarBsR, 0), currentYaw, currentPitch);
}
currentYaw -= RotationSpeed * (curMouseState.X - prevMouseState.X);
if (currentYaw < 0)
{
currentYaw += 2f * MathHelper.Pi;
}
else if (currentYaw > 2f * MathHelper.Pi)
{
currentYaw -= 2f * MathHelper.Pi;
}
currentPitch += RotationSpeed * (curMouseState.Y - prevMouseState.Y);
if (currentPitch < -0.45f * MathHelper.Pi)
{
currentPitch = -0.45f * MathHelper.Pi;
}
else if (currentPitch > 0.45f * MathHelper.Pi)
{
currentPitch = 0.45f * MathHelper.Pi;
}
}
private float? getMoveDirection(KeyboardState keyboardState)
{
float? moveAngle = null;
if (!isInAir)
{
if (keyboardState.IsKeyDown(Keys.W)) moveAngle = MathHelper.Pi;
if (keyboardState.IsKeyDown(Keys.A)) moveAngle = 1.5f * MathHelper.Pi;
if (keyboardState.IsKeyDown(Keys.S)) moveAngle = 0;
if (keyboardState.IsKeyDown(Keys.D)) moveAngle = MathHelper.PiOver2;
if (keyboardState.IsKeyDown(Keys.W) && keyboardState.IsKeyDown(Keys.A)) moveAngle = 1.25f * MathHelper.Pi;
if (keyboardState.IsKeyDown(Keys.W) && keyboardState.IsKeyDown(Keys.D)) moveAngle = 0.75f * MathHelper.Pi;
if (keyboardState.IsKeyDown(Keys.A) && keyboardState.IsKeyDown(Keys.S)) moveAngle = 1.75f * MathHelper.Pi;
if (keyboardState.IsKeyDown(Keys.S) && keyboardState.IsKeyDown(Keys.D)) moveAngle = MathHelper.PiOver4;
}
return moveAngle;
}
private void ResetAvatar()
{
velocity = Vector3.Zero;
currentPitch = 0;
currentYaw = 0;
camera.Position = StartPosition;
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.DarkRed);
model.Draw(camera.ViewMatrix);
base.Draw(gameTime);
}
}
}
What I have tried:
I have tried nothing so far I need some advice here thanks