#region Using Statements // System using statements using System; using System.Collections.Generic; 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.Net; using Microsoft.Xna.Framework.Storage; // Local using statements using XNAEngine; using CollisionDetection; using LinearAlgebra; #endregion // Test Gun Class by Dr. Roger Webster and Dr. Gary Zoppetti namespace XNACollisionDetection { public class Guns : CollidableModel { #region Global variables private bool m_isVisible; int gunNumber; int max_ammo; //max amount of ammo that can be in the gun int damage; //amount of damage that a gun can do bool gun_hide; //flag to hide or show the gun bool gun_moving; // flag to initialize or terminate the moving action bool gun_shooting; // flag to initialize or terminate the shooting action bool WASDenabled; bool recoiling; bool chaingun; bool knife; bool knifeSwinging; float pa; float yawAmount; bool shotgun; bool flamethrower; int fps_instant; // holds frames per second information int gun_movement_counter; //keeps track of current stage in movement animation float hideAngle; float recoil; float delay; float gun_pitch_amt_move; float gun_forward_amt; float gun_yaw_amt; float gun_pitch_reload; float gun_reload_delay; float reloading; bool rld; float gun_move_rate; float gun_fire_rate; float gun_recoil; float gun_firing_delay; //player sets the delay time for each gun float gun_bullet_dist; Vector3 cp, surfaceNormal; bool target_contact; Vector3 gunTipPosition; // used in update to shift the gun into desired location w.r.t camera //should be initialized in myinit(); double gun_shift_x, gun_shift_y, gun_shift_z; double gun_tip_x, gun_tip_y, gun_tip_z; //chaingun variables float chaingun_rot; float chaingun_rotSpeed; float chaingun_cutoffTime; //these variables may be changed to fit preference int clip_size;//the maximum amount of ammo allowed in gun int total_ammo;//the total ammo available int loaded_ammo;//the amount of ammo currently in the gun string homedir = System.IO.Directory.GetCurrentDirectory() + "/sounds/"; string fireSound = "weapons/EmptyMagazine.wav"; string altFireSound = "weapons/EmptyMagazine.wav"; string reloadSound = "weapons/Reload_HandGun.wav"; string emptySound = "weapons/EmptyMagazine.wav"; Sound fire_sound; Sound alt_fire_sound; Sound reload_sound; Sound empty_sound; Sound windup_sound; Sound winddown_sound; bool fthrower_switch; Random random = new Random(); #endregion #region Constructors public Guns(Model model) : base(model) { initVars(); fire_sound = new Sound(homedir + fireSound, false); alt_fire_sound = new Sound(homedir + altFireSound, false); reload_sound = new Sound(homedir + reloadSound, false); empty_sound = new Sound(homedir + emptySound, false); } public Guns(Model model, int gunNum) : base(model) { initVars(); gunNumber = gunNum; fire_sound = new Sound(homedir + fireSound, false); alt_fire_sound = new Sound(homedir + altFireSound, false); reload_sound = new Sound(homedir + reloadSound, false); empty_sound = new Sound(homedir + emptySound, false); } public Guns(Model model, int gunNum, string fire, string altFire, string reload) : base(model) { initVars(); gunNumber = gunNum; fire_sound = new Sound(homedir + fire, false); alt_fire_sound = new Sound(homedir + altFire, false); reload_sound = new Sound(homedir + reload, false); empty_sound = new Sound(homedir + emptySound, false); } #endregion private void initVars() { gunNumber=-1; m_isVisible = true; gun_moving = false; gun_shooting = false; gun_hide = false; WASDenabled = false; recoiling = true; chaingun = false; knife = false; knifeSwinging = false; pa = (90.0f) / (60.0f / 2.0f); yawAmount = 14.0f; shotgun = false; flamethrower = false; fps_instant = 30; gun_shift_x = -0.23; gun_shift_y = -0.53; gun_shift_z = -0.1; gun_tip_x = 0.0; gun_tip_y = 0.0; gun_tip_z = 0.0; gun_movement_counter = 0; gun_firing_delay = 8.0f; gun_recoil = 20; gun_move_rate = 1.0f; gun_fire_rate = 1.0f; gun_pitch_amt_move = 0.1f; gun_forward_amt = 0.07f; gun_yaw_amt = 0.2f; gun_pitch_reload = 0.0f; reloading = 0.0f; gun_bullet_dist = 9999.0f; damage = 10; max_ammo = 200; total_ammo = 50; clip_size = 20; loaded_ammo = 20; // pFlash = 0; //flashOn = true; rld = true; fthrower_switch = false; chaingun_rot = 0; chaingun_rotSpeed = 0; } public void setVisible(bool visible) { m_isVisible = visible; } #region Shift Functions public Vector3 getShift() { Vector3 shift; shift.X = (float)gun_shift_x; shift.Y = (float) gun_shift_y; shift.Z = (float)gun_shift_z; return (shift); } public void setShift(Vector3 shift) { gun_shift_x = shift.X; gun_shift_y = shift.Y; gun_shift_z = shift.Z; } #endregion public void MoveKnife(Camera camera) { if (knifeSwinging) { this.yaw(yawAmount); yawAmount = yawAmount + pa; if (yawAmount > 90.0) { knifeSwinging = false; yawAmount = 0; } } } public new void Draw(Camera camera) { if (m_isVisible) { this.Transform4 = camera.Transform4; this.yaw(180.0f); this.Position = camera.Position; this.moveLocal(this.getShift(), 1.0f); if (chaingun) this.rotateAboutPoint(this.Position, this.Forward, chaingun_rot); if (knife) { MoveKnife(camera); } base.Draw(camera); } } #region FireGun/Alt Fire public void FireGun(int subtract_ammo, Camera camera, CollidableModel world) { if (loaded_ammo != 0 && chaingun) { fireSound = fireSound.Remove(fireSound.Length-5); // fireSound = fireSound + random.Next(1, 4) + ".wav"; fireSound = fireSound + "n.wav"; fire_sound = new Sound(homedir + fireSound, false); } if (loaded_ammo != 0 && flamethrower) { loopFlameThrower(); fireRayCast(camera, world); } if (knife) { knifeSwinging = true; } if (loaded_ammo!=0 && !flamethrower) { try { fire_sound.stop(); } catch (Exception) { } fire_sound.play(); loaded_ammo -= subtract_ammo; if(gunNumber!= 4 && gunNumber!= 7 && gunNumber!= 8) fireRayCast(camera, world); } else { try { empty_sound.stop(); } catch (Exception) { } empty_sound.play(); } } public void AltFireGun(int subtract_ammo) { if (loaded_ammo != 0) { try { alt_fire_sound.stop(); } catch (Exception) { } alt_fire_sound.play(); loaded_ammo -= subtract_ammo; } else { try { empty_sound.stop(); } catch (Exception) { } empty_sound.play(); } } private void fireRayCast(Camera camera, CollidableModel world) { if (gunNumber == 10) gun_bullet_dist = 6.0f; if (gunNumber == 9) gun_bullet_dist = 30.0f; Vector3 temp = camera.Forward; temp.X += random.Next(-100,100)*0.0002f; temp.Y += random.Next(-100,100)*0.0002f; temp.Z += random.Next(-100,100)*0.0002f; if (world.checkRayCollision(camera.Position, temp, gun_bullet_dist, true, out cp, out surfaceNormal)) { target_contact = true; } else target_contact = false; } #endregion public void ReloadGun(int gunNumber) { if (total_ammo != 0 && loaded_ammo != clip_size && rld) { try { reload_sound.stop(); } catch (Exception) { } reload_sound.play(); if (total_ammo >= (clip_size - loaded_ammo)) { total_ammo -= (clip_size - loaded_ammo); loaded_ammo = clip_size; } else { loaded_ammo += total_ammo; total_ammo = 0; } } } public void setDelay(float fdelay) { gun_firing_delay = fdelay; } public float getDelay() { return(gun_firing_delay); } #region Set/Stop Sounds public void setGunSound(string fire) { fireSound = fire; fire_sound = new Sound(homedir + fireSound, false); } public void setGunSound(string fire, string reload) { fireSound = fire; reloadSound = reload; fire_sound = new Sound(homedir + fireSound, false); reload_sound = new Sound(homedir + reloadSound, false); } public void setGunSound(string fire, string altFire, string reload) { fire_sound = new Sound(homedir + fire, false); reload_sound = new Sound(homedir + reload, false); alt_fire_sound = new Sound(homedir + altFire, false); } public void stopSounds() { try { fire_sound.stop(); } catch (Exception) { } try { empty_sound.stop(); } catch (Exception) { } try { alt_fire_sound.stop(); } catch (Exception) { } try { reload_sound.stop(); } catch (Exception) { } if (flamethrower) { Sound end_sound = new Sound(homedir + "weapons/flame_thrower_end.wav", false); end_sound.play(); } } #endregion #region Clip/Ammo set/gets Functions public void setClipSize(int clipSize) { clip_size = clipSize; } public void setTotalAmmo(int addAmmo) { total_ammo = addAmmo; } public void addTotalAmmo() { total_ammo += clip_size; } public void addTotalAmmo(int addAmmo) { total_ammo += addAmmo; } public void setLoadedAmmo(int addAmmo) { loaded_ammo = addAmmo; } public void getAmmo(out int totalAmmo, out int loadedAmmo) { totalAmmo = total_ammo; loadedAmmo = loaded_ammo; } #endregion #region GunTypes public void isChaingun(bool TorF) { chaingun = TorF; if (chaingun) { winddown_sound = new Sound(homedir + "weapons/chaingun/cg_winddown_mix_01.wav", false); windup_sound = new Sound(homedir + "weapons/chaingun/cg_windup_mix_01.wav", false); } } public void isShotgun(bool TorF) { shotgun = TorF; } public void isKnife(bool TorF) { knife = TorF; } public void isFlameThrower(bool TorF) { flamethrower = TorF; } public void reloadable(bool TorF) { rld = TorF; } #endregion #region Chaingun Specific Functions public float getRotSpeed() { return (chaingun_rotSpeed); } public void chaingunAccelerate(float spin) { if (chaingun_rotSpeed < 9.0f) { try { winddown_sound.stop(); } catch (Exception) { } windup_sound.play(); } if (chaingun_rotSpeed < 20.0f) { chaingun_rotSpeed += spin; } chaingun_rot += chaingun_rotSpeed; } public void chaingunDecelerate(float spin) { if (chaingun_rotSpeed > 10.0f) { try { windup_sound.stop(); } catch (Exception) { } winddown_sound.play(); } if (chaingun_rotSpeed > 0.0f) { chaingun_rotSpeed -= spin; } else if (chaingun_rotSpeed < 0.0f) { chaingun_rotSpeed = 0.0f; ; } chaingun_rot += chaingun_rotSpeed; } #endregion #region FlameThrower Specific Functions private void loopFlameThrower() { fire_sound.play(); if (!fthrower_switch) { fire_sound.switchFile(homedir + "weapons/flame_thrower_loop.wav"); fthrower_switch = true; } } #endregion public void debug_getContactPoint(out bool targetC, out Vector3 collisionP, out Vector3 surfaceN) { collisionP = cp; surfaceN = surfaceNormal; targetC = target_contact; } } }