Star Wings
playermodel.cs | |
File Size: | 16 kb |
File Type: | cs |
User Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Media;
namespace StarWingsVer2
{
public class PlayerModel : BasicModel
{
CrosshairModel crosshair;
//Holds the movment for
float f_cordinateY, f_cordinateX, f_cordinateZ;
//Initial roation
Vector3 vec3_modelDirection;
//BulletInfo
float f_bulletSpeed = 12; //Speed Multiplier
int int_bulletDelay = 100; //100milsec/per shot
const int c_rateOfFire = 100;
int int_bulletCountDown = 0; // reset timer
Model m_bulletModel; // Holds the Model for a bullet, till called
float m_bulletCollRadius = 1.2f; //Colloision Sphere radius
Effect m_flashEffect;
public List bulletList = new List();
//HealthBar
GaugeBarTexture m_healthBar;
GaugeBarTexture m_laserBar;
public int m_hp;
int m_lp;
int m_maxHp;
int m_maxLp;
int m_pointsPerFrame;
float m_distanceFromCamera;
//HUD Decals
TextureBase HUDtexture;
public bool b_boost;
public BlueShpere m_blueSphere;
Model m_sphereModel;
//Get bonus points for consecutive ringThroughs
public BonusPoints m_bonusPoints;
private const int c_bonusValue = 10; // increment by 10
public PlayerModel(Model model, Vector3 position, float distanceFromCamera, Vector3 rotation,
GraphicsDevice graphicsDevice, Model crosshairModel, float collRadius, Model bulletModel, Texture2D gaugeBar, int viewportWidth
, Texture2D HUDtexture, Effect effect, Model sphereModel)
: base(model, position, collRadius)
{
m_sphereModel = sphereModel;
m_blueSphere = new BlueShpere(m_sphereModel, Vector3.Zero, 1);
this.vec3_modelDirection = rotation;
//Initialize e the crosshair and Scale
this.crosshair = new CrosshairModel(crosshairModel, position, 1);
this.crosshair.MatrixFromPosRotScale(crosshair.ModelMatrix.Translation, Vector3.Zero, 5);
this.m_bonusPoints = new BonusPoints(c_bonusValue);
this.m_bulletModel = bulletModel;
this.f_cordinateZ = position.Z;
this.m_distanceFromCamera = distanceFromCamera;
this.m_hp = m_maxHp = 100;
this.m_lp = m_maxLp = 100;
this.b_boost = false;
this.m_flashEffect = effect;
this.HUDtexture = new TextureBase(HUDtexture, Vector2.Zero);
this.m_healthBar = new GaugeBarTexture(gaugeBar, new Vector2((HUDtexture.Width / 2) - 7, (HUDtexture.Height / 2) - 5), m_maxHp, Color.LimeGreen);
this.m_laserBar = new GaugeBarTexture(gaugeBar, new Vector2((HUDtexture.Width / 3), (HUDtexture.Height) - 12), m_maxLp, Color.DodgerBlue);
}
public void Update(GameTime gameTime, CameraManger camera, List AstroidModelList, List powerUpModelList, Explosion astroidExplosion, Explosion lpPowerUpExplosion, Explosion hpPowerUpExplosion)
{
//Reset points
m_pointsPerFrame = 0;
//UpdateCrossHair
crosshair.Update(gameTime, camera, m_matrix.Translation);
//Movement
// m_matrix.Translation = Movment(camera);
Vector3 pos = m_matrix.Translation;
Vector3 target = m_distanceFromCamera * crosshair.cameraSpace.Translation / crosshair.cameraSpace.Translation.Z;
target = Vector3.Transform(target, camera.GetMatrix());
Vector3 delta = target - pos;
delta *= 0.07f;
// delta.Z = cameraPos.Z - pos.Z;
pos += delta;
Vector3 cameraPos = -m_distanceFromCamera * camera.GetMatrix().Forward + camera.GetMatrix().Translation;
pos.Z = cameraPos.Z;
m_matrix.Translation = pos;
m_matrix.Translation += crosshair.controls.Boost(crosshair.controls.Accelerate(.1f));
//Set boost Length and toggle back to False after words
b_boost = crosshair.controls.BoostTimer(gameTime, b_boost, 2);
//Adjust the pitch slightly
Matrix rotationPitchOffset = Matrix.Identity;
rotationPitchOffset = Matrix.CreateRotationX(MathHelper.ToRadians(15.0f));
//Rotate the Model's origin to look at crosshair
m_matrix = Matrix.Invert(LookAtCrosshair() * rotationPitchOffset);
//CreateBullet
FireShots(gameTime);
//UpdateBullets
UpdateBullets(gameTime, AstroidModelList, astroidExplosion, lpPowerUpExplosion, hpPowerUpExplosion);
//Check if Player Collided with astroid
CheckAstroidCollision(AstroidModelList);
//Check if player when through the ring
CheckCollisionWithPowerUp(powerUpModelList, camera, lpPowerUpExplosion, hpPowerUpExplosion);
//Upadate the HUD gauges
UpdateGaugeBars();
//UpdateSphere
m_blueSphere.Update(gameTime);
base.Update(gameTime);
}
public void Draw(Matrix viewMat, Matrix projMat, SpriteBatch spritebatch)
{
//Draw Crosshair
crosshair.Draw(viewMat, projMat);
//DrawHud
m_healthBar.Draw(spritebatch);
m_laserBar.Draw(spritebatch);
HUDtexture.Draw(spritebatch);
Game1.graphics.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
base.Draw(viewMat, projMat);
}
void FireRateCheck()
{
int lpDepleted = 500;
if (m_lp <= 0)
{
int_bulletDelay = lpDepleted;
}
else
int_bulletDelay = c_rateOfFire;
}
public Matrix LookAtCrosshair()
{
return Matrix.CreateLookAt(m_matrix.Translation, crosshair.ModelMatrix.Translation, Vector3.Up) *
Matrix.CreateRotationY(MathHelper.Pi);
}
protected void FireShots(GameTime gameTime)
{
if (int_bulletCountDown <= 0)
{
//Did player press the SpaceBar or right trigger
if (Keyboard.GetState().IsKeyDown(Keys.Space) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.RightTrigger))
{
//Add shots and play sound
if (m_bonusPoints.GetBonusPoints >= m_bonusPoints.GetThreshold)
AddBullets();
else
AddBullet();
//Reset bullet count down
int_bulletCountDown = int_bulletDelay;
//Deplete ammo
m_lp -= 1;
}
}
else int_bulletCountDown -= gameTime.ElapsedGameTime.Milliseconds;
FireRateCheck();
}
void AddBullet()
{
Vector3 bulletDirection = crosshair.ModelMatrix.Translation - m_matrix.Translation;
bulletList.Add(new BulletModel(m_bulletModel, m_matrix.Translation, m_bulletCollRadius, bulletDirection, f_bulletSpeed, 1, m_flashEffect));
AudioComponent.Get().PlaySound("Bullet");
m_blueSphere.ResetCountDown(m_matrix.Translation);
}
///
/// Add 2 bullets in the left and right barrel
///
void AddBullets()
{
Vector3 rightBarrel = m_matrix.Translation;
Vector3 leftBarrel = m_matrix.Translation;
float xOffset = 1.6f;
rightBarrel.X += xOffset;
leftBarrel.X -= xOffset;
float yOffset = 1.2f;
rightBarrel.Y -= yOffset;
leftBarrel.Y -= yOffset;
//Right Barrel
Vector3 bulletDirection = crosshair.GetMatrix().Translation - rightBarrel;
bulletList.Add(new BulletModel(m_bulletModel, rightBarrel, m_bulletCollRadius, bulletDirection, f_bulletSpeed, 1, m_flashEffect));
//Left Barrel
bulletDirection = crosshair.GetMatrix().Translation - leftBarrel;
bulletList.Add(new BulletModel(m_bulletModel, leftBarrel, m_bulletCollRadius, bulletDirection, f_bulletSpeed, 1, m_flashEffect));
AudioComponent.Get().PlaySound("TwoBullets");
}
void UpdateBullets(GameTime gameTime, List astroidModelList, Explosion astroidExplosion, Explosion lpExplosion, Explosion hpExplosion)
{
//Loop through shots
for (int i = 0; i < bulletList.Count; i++)
{
BulletModel thisBullet = bulletList[i];
thisBullet.Update(gameTime);
//If bullet is out of bounds, remove it from the game
if (thisBullet.f_timeOut < 0)
{
bulletList.RemoveAt(i);
i--;
}
else
{
//if shot is still in play, chck for collision for astroid
for (int j = 0; j < astroidModelList.Count; j++)
{
AstroidModel astroidModel = astroidModelList[j];
if (thisBullet.CollidesWithSphere(astroidModel.m_collSphere))
{
if (astroidModel.m_value > 1)
{
astroidModel.m_value -= 1;
bulletList.RemoveAt(i);
AudioComponent.Get().PlaySound("NullHit");
astroidModel.b_isCollided = true;
i--;
break;
}
else
{
//Collisions remove the ship and the shot
AudioComponent.Get().PlaySound("AstroidExplosion");
m_pointsPerFrame += astroidModel.m_pointValue;
if (m_bonusPoints.GetBonusPoints >=m_bonusPoints.GetHighThreshold )
{
//Expload Red
astroidExplosion.CreateParticleExplosion(astroidModel.GetMatrix().Translation);
}
else if (m_bonusPoints.GetBonusPoints >= m_bonusPoints.GetThreshold)
{
//Explaod blue
lpExplosion.CreateParticleExplosion(astroidModel.GetMatrix().Translation);
}
else//Explaod Green
hpExplosion.CreateParticleExplosion(astroidModel.GetMatrix().Translation);
astroidModelList.RemoveAt(j);
bulletList.RemoveAt(i);
i--;
break;
}
}
}
}
}
}
void CheckAstroidCollision(List astroidModelList)
{
for (int i = 0; i < astroidModelList.Count; i++)
{
AstroidModel thisAstroidModel = astroidModelList[i];
if (CollidesWithSphere(thisAstroidModel.m_collSphere))
{
RumblePlayer(.5f);
m_hp -= (int)thisAstroidModel.m_value + 15;
AudioComponent.Get().PlaySound("AstroidCollision");
astroidModelList.RemoveAt(i);
i--;
break;
}
}
}
Vector3 Movment(Camera camera)
{
UpdateCordinatesXY(camera);
Vector3 outputMovement = Vector3.Zero;
outputMovement.Y = f_cordinateY;
outputMovement.Z = m_distanceFromCamera;
outputMovement.X = f_cordinateX;
outputMovement = Vector3.Transform(outputMovement, camera.GetMatrix());
return outputMovement;
}
void UpdateCordinatesXY(Camera camera)
{
// Update 2/3 offset to crosshair with simlar triagles from camera to model. from model crosshair
f_cordinateY = (((2 * crosshair.cameraSpace.Translation.Y) / 3) * (f_cordinateZ / (crosshair.cameraSpace.Translation.Z)));
f_cordinateX = (((2 * crosshair.cameraSpace.Translation.X) / 3) * (f_cordinateZ / (crosshair.cameraSpace.Translation.Z)));
}
void CheckCollisionWithPowerUp(List powerUpList, Camera camera, Explosion lpPowerUpExplosion, Explosion hpPowerUpExplosion)
{
for (int i = 0; i < powerUpList.Count; i++)
{
PowerUpModel thisPowerUp = powerUpList[i];
if (CollidesWithSphere(thisPowerUp.m_collSphere))
{
Vector3 distance = Vector3.Transform(m_matrix.Translation, Matrix.Invert(thisPowerUp.GetMatrix()));
//workIn progress
if (distance.Z <= 0)
{
if (thisPowerUp.GetWhichPowerUp == powerUp.hp)
{
m_hp += (int)thisPowerUp.m_value;
AudioComponent.Get().PlaySound("GreenRingPickup");
}
if (thisPowerUp.GetWhichPowerUp == powerUp.lp)
{
m_lp += (int)thisPowerUp.m_value;
AudioComponent.Get().PlaySound("BlueRingPickup");
}
Vector3 pos = new Vector3(thisPowerUp.GetMatrix().Translation.X,
thisPowerUp.GetMatrix().Translation.Y, thisPowerUp.GetMatrix().Translation.Z);
pos = Vector3.Transform(new Vector3(0, 20, 0), m_matrix);
m_bonusPoints.AddBonusPoints(pos);
powerUpList.RemoveAt(i);
i--;
}
}
}
}
void UpdateGaugeBars()
{
//prevention step Of going Over or Less
//than the allowed
if (m_hp > m_maxHp)
m_hp = m_maxHp;
if (m_hp < 0)
m_hp = 0;
if (m_lp > m_maxLp)
m_lp = m_maxLp;
if (m_lp < 0)
m_lp = 0;
m_healthBar.m_current = m_hp;
m_laserBar.m_current = m_lp;
}
public int GetPoints
{
get { return m_pointsPerFrame; }
set { m_pointsPerFrame = value; }
}
///
/// Set between .3 - 1.5
///
///
public void RumblePlayer(float timer)
{
crosshair.controls.RumbleGamePad(timer);
}
}
}