rendered paste bodyusing 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;
namespace Game1
{
public class Game1 : Microsoft.Xna.Framework.Game
{
struct Bullet
{
public Vector3 position;
public Vector3 chasedirection;
}
private struct VertexPointSprite
{
private Vector3 position;
private float pointSize;
public VertexPointSprite(Vector3 position, float pointSize)
{
this.position = position;
this.pointSize = pointSize;
}
public static readonly VertexElement[] VertexElements =
{
new VertexElement(0, 0, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Position, 0),
new VertexElement(0, sizeof(float)*3, VertexElementFormat.Single, VertexElementMethod.Default, VertexElementUsage.PointSize, 0),
};
public static int SizeInBytes = sizeof(float) * (3 + 1);
}
enum CollisionType { None, Building, Boundary, Target }
const int maxTargets = 50;
int[] buildingHeights = new int[] { 0, 2, 2, 6, 5, 4 };
GraphicsDeviceManager graphics;
GraphicsDevice device;
Effect effect;
Vector3 lightDirection = new Vector3(3, -2, 5);
//Matrix viewMatrix;
//Matrix projectionMatrix;
Texture2D sceneryTexture;
int[,] floorPlan;
const float cityBuildingWidth = 100f;
Vector3 cityOffset = new Vector3(0, 0, 0);
BoundingBox[,] cityBuildings;
List<BoundingBox> buildingBoundingBoxes;
BoundingBox completeCityBox;
VertexBuffer cityVertexBuffer;
VertexDeclaration texturedVertexDeclaration;
VertexDeclaration pointSpriteVertexDeclaration;
SpriteBatch spriteBatch;
SpriteFont spriteFont;
SpriteFont myFont;
Texture2D texCrosshair;
Vector2 mPosition = new Vector2(250, 100);
KeyboardState lastKeyboardState = new KeyboardState();
GamePadState lastGamePadState = new GamePadState();
KeyboardState currentKeyboardState = new KeyboardState();
GamePadState currentGamePadState = new GamePadState();
ChaseCamera camera;
Ship ship = new Ship();
Model shipModel;
CoordCross cCross;
//Matrix viewMatrix;
//Matrix projectionMatrix;
Model skydomeModel;
Vector3 skydomeRotation;
Texture2D skydomeTexture;
AudioEngine audioEngine;
SoundBank soundBank;
WaveBank waveBank;
Texture2D bulletTexture;
Texture2D mHealthBar;
int mCurrentHealth = 5;
bool firstTime = true;
Texture2D MinimapTex;
Color MinimapBGCol = new Color(150, 150, 150, 150);
Rectangle MinimapBGRect = new Rectangle(9, 9, 100, 100);
Rectangle NextMinimapItem = new Rectangle(0, 0, 3, 3);
//made a 3x3 rectangle so that the objects are more visible on your minimap
//Vector3 shipPosition = new Vector3(8, 1, -3);
// Quaternion shipRotation = Quaternion.Identity;
List<BoundingSphere> targetList = new List<BoundingSphere>();
List<Bullet> bulletList = new List<Bullet>(); double lastBulletTime = 0;
bool cameraSpringEnabled = true;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
this.IsMouseVisible = false;
// Create the chase camera
camera = new ChaseCamera();
// Set the camera offsets
camera.DesiredPositionOffset = new Vector3(0.0f, 20.0f, 95.0f);
camera.LookAtOffset = new Vector3(0.0f, 150.0f, 0.0f);
// Set camera perspective
camera.NearPlaneDistance = 10.0f;
camera.FarPlaneDistance = 100000.0f;
}
protected override void Initialize()
{
camera.AspectRatio = (float)graphics.GraphicsDevice.Viewport.Width /
graphics.GraphicsDevice.Viewport.Height;
UpdateCameraChaseTarget();
graphics.PreferredBackBufferWidth = 1024;
graphics.PreferredBackBufferHeight = 768;
graphics.IsFullScreen = false;
graphics.ApplyChanges();
Window.Title = "Targa";
LoadFloorPlan();
lightDirection.Normalize();
AddTargets();
base.Initialize();
}
protected override void LoadContent()
{
device = graphics.GraphicsDevice;
cCross = new CoordCross(device);
spriteBatch = new SpriteBatch(GraphicsDevice);
spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
audioEngine = new AudioEngine("Content\\Audio\\AudioFile.xgs");
waveBank = new WaveBank(audioEngine, "Content\\Audio\\Wave Bank.xwb");
soundBank = new SoundBank(audioEngine, "Content\\Audio\\Sound Bank.xsb");
spriteFont = Content.Load<SpriteFont>("Arial");
shipModel = Content.Load<Model>("Ship");
texCrosshair = Content.Load<Texture2D>("crosshair");
myFont = Content.Load<SpriteFont>("myFont");
skydomeModel = Content.Load<Model>("Skydome");
skydomeTexture = Content.Load<Texture2D>("SkydomeTex");
skydomeTexture = Content.Load<Texture2D>("SkydomeTex2");
skydomeRotation = Vector3.Zero;
bulletTexture = Content.Load<Texture2D>("bullet");
mHealthBar = Content.Load<Texture2D>("HealthBar");
effect = Content.Load<Effect>("effects");
sceneryTexture = Content.Load<Texture2D>("texturemap");
SetUpVertices();
// SetUpCamera();
MinimapTex = new Texture2D(graphics.GraphicsDevice, 1, 1); //create a new texture in memory
Color[] texcol = new Color[1]; //array to store our colour values
MinimapTex.GetData(texcol); //get our colour data from our texture
texcol[0] = Color.White; //set the single pixel to white
MinimapTex.SetData(texcol); //set the textures colour data to the new data
pointSpriteVertexDeclaration = new VertexDeclaration(device, VertexPointSprite.VertexElements);
}
private void LoadFloorPlan()
{
floorPlan = new int[,]
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,1,1,0,0,0,1,1,0,0,1,0,1},
{1,0,0,1,1,0,0,0,1,0,0,0,1,0,1},
{1,0,0,0,1,1,0,1,1,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,1,0,0,0,1,0,0,0,0,0,0,1},
{1,0,1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,0,0,0,0,0,1,0,0,0,0,1},
{1,0,1,1,0,0,0,0,1,1,0,0,0,1,1},
{1,0,0,0,0,0,0,0,1,1,0,0,0,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
Random random = new Random();
int differentBuildings = buildingHeights.Length - 1;
for (int x = 0; x < floorPlan.GetLength(0); x++)
for (int y = 0; y < floorPlan.GetLength(1); y++)
if (floorPlan[x, y] == 1)
floorPlan[x, y] = random.Next(differentBuildings) + 1;
SetUpBoundingBoxes();
}
private void SetUpBoundingBoxes()
{
int cityWidth = floorPlan.GetLength(0);
int cityLength = floorPlan.GetLength(1);
cityBuildings = new BoundingBox[cityWidth, cityLength];
buildingBoundingBoxes = new List<BoundingBox>();
for (int x = 0; x < cityWidth; x++)
{
for (int z = 0; z < cityLength; z++)
{
int buildingType = floorPlan[x, z];
if (buildingType != 0)
{
int buildingHeight = buildingHeights[buildingType];
Vector3[] buildingPoints = new Vector3[2];
buildingPoints[0] = new Vector3(x, 1, -z);
buildingPoints[1] = new Vector3(x + 1, buildingHeight, -z - 1);
buildingPoints[0] = (buildingPoints[0] * cityBuildingWidth) + cityOffset;
buildingPoints[1] = (buildingPoints[1] * cityBuildingWidth) + cityOffset;
BoundingBox buildingBox = BoundingBox.CreateFromPoints(buildingPoints);
buildingBoundingBoxes.Add(buildingBox);
cityBuildings[x, z] = buildingBox;
}
}
}
Vector3[] boundaryPoints = new Vector3[2];
boundaryPoints[0] = new Vector3(0, 0, 0);
boundaryPoints[1] = new Vector3(cityWidth, 20, -cityLength);
boundaryPoints[0] = (boundaryPoints[0] * cityBuildingWidth) + cityOffset;
boundaryPoints[1] = (boundaryPoints[1] * cityBuildingWidth) + cityOffset;
completeCityBox = BoundingBox.CreateFromPoints(boundaryPoints);
}
private void AddTargets()
{
int cityWidth = floorPlan.GetLength(0);
int cityLength = floorPlan.GetLength(1);
Random random = new Random();
while (targetList.Count < maxTargets)
{
int x = random.Next(cityWidth);
int z = -random.Next(cityLength);
float y = (float)random.Next(2000) / 1000f + 1;
float radius = (float)random.Next(1000) / 1000f * 0.2f + 0.01f;
BoundingSphere newTarget = new BoundingSphere(new Vector3(x, y, z), radius);
if (CheckCollision(newTarget) == CollisionType.None)
targetList.Add(newTarget);
}
}
#if not_used
private void SetUpCamera()
{
viewMatrix = Matrix.CreateLookAt(new Vector3(20, 13, -5), new Vector3(8, 0, -7), new Vector3(0, 0, 0));
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 0.2f, 500.0f);
}
#endif
private void SetUpVertices()
{
int differentBuildings = buildingHeights.Length - 1;
float imagesInTexture = 1 + differentBuildings * 2;
int cityWidth = floorPlan.GetLength(0);
int cityLength = floorPlan.GetLength(1);
List<VertexPositionNormalTexture> verticesList = new List<VertexPositionNormalTexture>();
for (int x = 0; x < cityWidth; x++)
{
for (int z = 0; z < cityLength; z++)
{
BoundingBox currentbuildingBox = cityBuildings[x, z];
int currentbuilding = floorPlan[x, z];
if (currentbuilding == 0)
{
Vector3[] buildingPoints = new Vector3[2];
buildingPoints[0] = new Vector3(x, 1, -z);
buildingPoints[1] = new Vector3(x + 1, 1 , -z - 1);
buildingPoints[0] = (buildingPoints[0] * cityBuildingWidth) + cityOffset;
buildingPoints[1] = (buildingPoints[1] * cityBuildingWidth) + cityOffset;
currentbuildingBox = BoundingBox.CreateFromPoints(buildingPoints);
}
//floor or ceiling
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Min.X, currentbuildingBox.Max.Y, currentbuildingBox.Max.Z), new Vector3(0, 1, 0), new Vector2(currentbuilding * 2 / imagesInTexture, 1)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Min.X, currentbuildingBox.Max.Y, currentbuildingBox.Min.Z), new Vector3(0, 1, 0), new Vector2(currentbuilding * 2 / imagesInTexture, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Max.X, currentbuildingBox.Max.Y, currentbuildingBox.Max.Z), new Vector3(0, 1, 0), new Vector2((currentbuilding * 2 + 1) / imagesInTexture, 1)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Min.X, currentbuildingBox.Max.Y, currentbuildingBox.Min.Z), new Vector3(0, 1, 0), new Vector2(currentbuilding * 2 / imagesInTexture, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Max.X, currentbuildingBox.Max.Y, currentbuildingBox.Min.Z), new Vector3(0, 1, 0), new Vector2((currentbuilding * 2 + 1) / imagesInTexture, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Max.X, currentbuildingBox.Max.Y, currentbuildingBox.Max.Z), new Vector3(0, 1, 0), new Vector2((currentbuilding * 2 + 1) / imagesInTexture, 1)));
if (currentbuilding != 0)
{
//front wall
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Max.X, currentbuildingBox.Min.Y, currentbuildingBox.Min.Z), new Vector3(0, 0, -1), new Vector2((currentbuilding * 2) / imagesInTexture, 1)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Min.X, currentbuildingBox.Max.Y, currentbuildingBox.Min.Z), new Vector3(0, 0, -1), new Vector2((currentbuilding * 2 - 1) / imagesInTexture, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Min.X, currentbuildingBox.Min.Y, currentbuildingBox.Min.Z), new Vector3(0, 0, -1), new Vector2((currentbuilding * 2 - 1) / imagesInTexture, 1)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Min.X, currentbuildingBox.Max.Y, currentbuildingBox.Min.Z), new Vector3(0, 0, -1), new Vector2((currentbuilding * 2 - 1) / imagesInTexture, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Max.X, currentbuildingBox.Min.Y, currentbuildingBox.Min.Z), new Vector3(0, 0, -1), new Vector2((currentbuilding * 2) / imagesInTexture, 1)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Max.X, currentbuildingBox.Max.Y, currentbuildingBox.Min.Z), new Vector3(0, 0, -1), new Vector2((currentbuilding * 2) / imagesInTexture, 0)));
//back wall
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Max.X, currentbuildingBox.Min.Y, currentbuildingBox.Max.Z), new Vector3(0, 0, 1), new Vector2((currentbuilding * 2) / imagesInTexture, 1)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Min.X, currentbuildingBox.Min.Y, currentbuildingBox.Max.Z), new Vector3(0, 0, 1), new Vector2((currentbuilding * 2 - 1) / imagesInTexture, 1)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Min.X, currentbuildingBox.Max.Y, currentbuildingBox.Max.Z), new Vector3(0, 0, 1), new Vector2((currentbuilding * 2 - 1) / imagesInTexture, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Min.X, currentbuildingBox.Max.Y, currentbuildingBox.Max.Z), new Vector3(0, 0, 1), new Vector2((currentbuilding * 2 - 1) / imagesInTexture, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Max.X, currentbuildingBox.Max.Y, currentbuildingBox.Max.Z), new Vector3(0, 0, 1), new Vector2((currentbuilding * 2) / imagesInTexture, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Max.X, currentbuildingBox.Min.Y, currentbuildingBox.Max.Z), new Vector3(0, 0, 1), new Vector2((currentbuilding * 2) / imagesInTexture, 1)));
//left wall
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Min.X, currentbuildingBox.Min.Y, currentbuildingBox.Max.Z), new Vector3(-1, 0, 0), new Vector2((currentbuilding * 2) / imagesInTexture, 1)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Min.X, currentbuildingBox.Min.Y, currentbuildingBox.Min.Z), new Vector3(-1, 0, 0), new Vector2((currentbuilding * 2 - 1) / imagesInTexture, 1)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Min.X, currentbuildingBox.Max.Y, currentbuildingBox.Min.Z), new Vector3(-1, 0, 0), new Vector2((currentbuilding * 2 - 1) / imagesInTexture, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Min.X, currentbuildingBox.Max.Y, currentbuildingBox.Min.Z), new Vector3(-1, 0, 0), new Vector2((currentbuilding * 2 - 1) / imagesInTexture, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Min.X, currentbuildingBox.Max.Y, currentbuildingBox.Max.Z), new Vector3(-1, 0, 0), new Vector2((currentbuilding * 2) / imagesInTexture, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Min.X, currentbuildingBox.Min.Y, currentbuildingBox.Max.Z), new Vector3(-1, 0, 0), new Vector2((currentbuilding * 2) / imagesInTexture, 1)));
//right wall
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Max.X, currentbuildingBox.Min.Y, currentbuildingBox.Max.Z), new Vector3(1, 0, 0), new Vector2((currentbuilding * 2) / imagesInTexture, 1)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Max.X, currentbuildingBox.Max.Y, currentbuildingBox.Min.Z), new Vector3(1, 0, 0), new Vector2((currentbuilding * 2 - 1) / imagesInTexture, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Max.X, currentbuildingBox.Min.Y, currentbuildingBox.Min.Z), new Vector3(1, 0, 0), new Vector2((currentbuilding * 2 - 1) / imagesInTexture, 1)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Max.X, currentbuildingBox.Max.Y, currentbuildingBox.Min.Z), new Vector3(1, 0, 0), new Vector2((currentbuilding * 2 - 1) / imagesInTexture, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Max.X, currentbuildingBox.Min.Y, currentbuildingBox.Max.Z), new Vector3(1, 0, 0), new Vector2((currentbuilding * 2) / imagesInTexture, 1)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(currentbuildingBox.Max.X, currentbuildingBox.Max.Y, currentbuildingBox.Max.Z), new Vector3(1, 0, 0), new Vector2((currentbuilding * 2) / imagesInTexture, 0)));
}
}
}
cityVertexBuffer = new VertexBuffer(device, verticesList.Count * VertexPositionNormalTexture.SizeInBytes, BufferUsage.WriteOnly);
cityVertexBuffer.SetData<VertexPositionNormalTexture>(verticesList.ToArray());
texturedVertexDeclaration = new VertexDeclaration(device, VertexPositionNormalTexture.VertexElements);
}
protected override void UnloadContent()
{
Content.Unload();
}
protected override void Update(GameTime gameTime)
{
{
/*
if (CheckCollision(ship.Sphere) != CollisionType.None)
{
ship.Reset();
//gameSpeed /= 1.1f;
}*/
if (firstTime)
{
// Play the sound.
soundBank.PlayCue("gamesong");
firstTime = false;
}
lastKeyboardState = currentKeyboardState;
lastGamePadState = currentGamePadState;
currentKeyboardState = Keyboard.GetState();
currentGamePadState = GamePad.GetState(PlayerIndex.One);
// Exit when the Escape key or Back button is pressed
if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
currentGamePadState.Buttons.Back == ButtonState.Pressed)
{
Exit();
}
// Pressing the A button or key toggles the spring behavior on and off
if (lastKeyboardState.IsKeyUp(Keys.A) &&
(currentKeyboardState.IsKeyDown(Keys.A)) ||
(lastGamePadState.Buttons.A == ButtonState.Released &&
currentGamePadState.Buttons.A == ButtonState.Pressed))
{
cameraSpringEnabled = !cameraSpringEnabled;
}
if (currentKeyboardState.IsKeyDown(Keys.H))
{
double currentTime = gameTime.TotalGameTime.TotalMilliseconds;
if (currentTime - lastBulletTime > 100)
{
Bullet newBullet = new Bullet();
newBullet.position = ship.Position;
newBullet.chasedirection = camera.ChaseDirection;
bulletList.Add(newBullet);
lastBulletTime = currentTime;
}
}
// Reset the ship on R key or right thumb stick clicked
if (currentKeyboardState.IsKeyDown(Keys.R) ||
currentGamePadState.Buttons.RightStick == ButtonState.Pressed)
{
ship.Reset();
camera.Reset();
}
//if (currentKeyboardState.IsKeyDown(Keys.C) ||
// currentGamePadState.Buttons.RightStick == ButtonState.Pressed)
//{
// mute = 0;
//}
// Update the ship
ship.Update(gameTime, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
// Update the camera to chase the new target
UpdateCameraChaseTarget();
// The chase camera's update behavior is the springs, but we can
// use the Reset method to have a locked, spring-less camera
if (cameraSpringEnabled)
camera.Update(gameTime);
else
camera.Reset();
skydomeRotation += new Vector3(0, (float)gameTime.ElapsedGameTime.TotalSeconds * 0.05f, 0);
if (CheckCollision(ship.Sphere) != CollisionType.None || ship.Position.Y <= ship.Ground)
{
mCurrentHealth -= 1;
soundBank.PlayCue("explosion");
ship.Reset();
}
#if not_used
UpdateCamera();
UpdateBulletPositions(moveSpeed);
#endif
audioEngine.Update();
base.Update(gameTime);
}
}
#if not_used
private void UpdateCamera()
{
Vector3 campos = new Vector3(0, 0.1f, 0.6f);
campos = Vector3.Transform(campos, Matrix.CreateFromQuaternion(shipRotation));
campos += shipPosition;
Vector3 camup = new Vector3(0, 1, 0);
camup = Vector3.Transform(camup, Matrix.CreateFromQuaternion(shipRotation));
viewMatrix = Matrix.CreateLookAt(campos, shipPosition, camup);
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 0.2f, 500.0f);
}
#endif
private void UpdateCameraChaseTarget()
{
camera.ChasePosition = ship.Position;
camera.ChaseDirection = ship.Direction;
camera.Up = ship.Up;
if (camera.Mode == 1)
{
if (Mouse.GetState().LeftButton == ButtonState.Pressed)
{
camera.UpdateNewCamera(
(float)Mouse.GetState().X / (float)graphics.GraphicsDevice.Viewport.Width,
(float)Mouse.GetState().Y / (float)graphics.GraphicsDevice.Viewport.Height
//.5f
);
}
else
{
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One, GamePadDeadZone.Circular);
if (gamePadState.ThumbSticks.Left.X != 0)
{
float sx = (gamePadState.ThumbSticks.Left.X / 2.0f) + .5f; // was /2.0f
float sy = (gamePadState.ThumbSticks.Left.Y / 2.0f) + .5f;
//dbgtext = "thumbstick x:" + sx;
camera.UpdateNewCamera(
sx,
sy
);
}
else
{
camera.UpdateNewCamera(
.5f,
.5f
);
}
}
}
}
/// <summary>
/// Draws the ship and ground.
/// </summary>
private CollisionType CheckCollision(BoundingSphere sphere)
{
for (int i = 0; i < buildingBoundingBoxes.Count; i++)
if (buildingBoundingBoxes[i].Contains(sphere) != ContainmentType.Disjoint)
return CollisionType.Building;
if (completeCityBox.Contains(sphere) != ContainmentType.Contains)
return CollisionType.Boundary;
return CollisionType.None;
}
private void UpdateBulletPositions(float moveSpeed)
{
for (int i = 0; i < bulletList.Count; i++)
{
Bullet currentBullet = bulletList[i];
ship.acceleration(ref currentBullet.position, currentBullet.chasedirection, moveSpeed * 2.0f);
bulletList[i] = currentBullet;
}
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.CornflowerBlue, 1, 0);
GraphicsDevice device = graphics.GraphicsDevice;
//draw the skydome
device.RenderState.DepthBufferWriteEnable = false;
foreach (ModelMesh mesh in skydomeModel.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.Texture = skydomeTexture;
effect.TextureEnabled = true;
effect.World = Matrix.CreateRotationY(skydomeRotation.Y) *
Matrix.CreateTranslation(camera.Position.X, camera.Position.Y - 500.0f, camera.Position.Z);
effect.View = camera.View;
effect.Projection = camera.Projection;
}
mesh.Draw();
}
//device.RenderState.DepthBufferWriteEnable = true;
foreach (ModelMesh mesh in skydomeModel.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.Texture = skydomeTexture;
effect.TextureEnabled = true;
effect.World = Matrix.CreateRotationY(-skydomeRotation.Y) * Matrix.CreateRotationX(3.14f) *
Matrix.CreateTranslation(camera.Position.X, camera.Position.Y + 500.0f, camera.Position.Z);
effect.View = camera.View;
effect.Projection = camera.Projection;
}
mesh.Draw();
}
device.RenderState.DepthBufferWriteEnable = true;
DrawCity();
DrawBullets();
//DrawBoundingBoxes();
//DrawBoundingSphere(ship.Sphere, Color.Green);
//draw the model
//myModel.CopyAbsoluteBoneTransformsTo(modelTransforms);
//foreach (ModelMesh mesh in myModel.Meshes)
//{
// foreach (BasicEffect effect in mesh.Effects)
// {
// effect.EnableDefaultLighting();
// effect.World = modelTransforms[mesh.ParentBone.Index] * Matrix.CreateScale(0.003f);
// effect.View = camera.View;
// effect.Projection = camera.Projection;
// }
// mesh.Draw();
//}
//device.Clear(Color.CornflowerBlue);
//draw coordcross
DrawModel(shipModel, ship.World);
cCross.Draw(camera.View, camera.Projection);
//DrawModel(groundModel, Matrix.Identity);
//cCross.Draw(camera.ViewMatrix, camera.ProjectionMatrix);
//terrain.Draw(Matrix.Identity, camera.View, camera.Projection);
DrawOverlayText();
spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront, SaveStateMode.SaveState);
string myString = "Elapsed seconds: " + gameTime.TotalGameTime.Seconds.ToString();
spriteBatch.DrawString(myFont, myString, new Vector2(50, 20), Color.Tomato);
spriteBatch.End();
spriteBatch.Begin();
spriteBatch.Draw(MinimapTex, MinimapBGRect, MinimapBGCol);
//foreach (Vector2 tmp in ship)
//{
// NextMinimapItem.X = (int)tmp.X + 9;
// NextMinimapItem.Y = (int)tmp.Y + 9;
// spriteBatch.Draw(MinimapTex, NextMinimapItem, Color.Blue);
//}
//foreach (Vector2 tmp in Enemies)
//{
// NextMinimapItem.X = (int)tmp.X + 9;
// NextMinimapItem.Y = (int)tmp.Y + 9;
// spriteBatch.Draw(MinimapTex, NextMinimapItem, Color.Red);
//}
//Insert the ally/enemy draw code here!!
string myHealth = "Health Level";
spriteBatch.DrawString(myFont, myHealth, new Vector2(877, 30), Color.Red);
//Draw the negative space for the health bar
spriteBatch.Draw(mHealthBar, new Rectangle(this.Window.ClientBounds.Width / 1 - mHealthBar.Width / 1, 10, mHealthBar.Width, 20), new Rectangle(0, 45, mHealthBar.Width, 44), Color.Gray);
//Draw the current health level based on the current Health
spriteBatch.Draw(mHealthBar, new Rectangle(this.Window.ClientBounds.Width / 1 - mHealthBar.Width / 1, 10, (int)(mHealthBar.Width * ((double)mCurrentHealth / 5)), 20), new Rectangle(0, 45, mHealthBar.Width, 44), Color.Red);
//Draw the box around the health bar
spriteBatch.Draw(mHealthBar, new Rectangle(this.Window.ClientBounds.Width / 1 - mHealthBar.Width / 1, 10, mHealthBar.Width, 20), new Rectangle(0, 0, mHealthBar.Width, 44), Color.White);
spriteBatch.End();
spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront, SaveStateMode.SaveState);
spriteBatch.DrawString(myFont, ship.Position.ToString(), new Vector2(8, 1), Color.Blue);
spriteBatch.DrawString(myFont, camera.Position.ToString(), new Vector2(8, 50), Color.Blue);
//spriteBatch.Draw(texcircle, mPosition, Color.White);
spriteBatch.End();
graphics.GraphicsDevice.RenderState.DepthBufferEnable = true;
graphics.GraphicsDevice.RenderState.AlphaBlendEnable = false;
graphics.GraphicsDevice.RenderState.AlphaTestEnable = false;
GraphicsDevice.SamplerStates[0].AddressU = TextureAddressMode.Wrap;
GraphicsDevice.SamplerStates[0].AddressV = TextureAddressMode.Wrap;
base.Draw(gameTime);
}
private void DrawModel(Model model, Matrix world)
{
Matrix worldMatrix = Matrix.CreateRotationZ(ship.Direction.Z) * Matrix.CreateRotationX(ship.Direction.X) * Matrix.CreateRotationY(ship.Direction.Y) * Matrix.CreateScale(0.0005f, 0.0005f, 0.0005f) * Matrix.CreateTranslation(ship.Position);
Matrix[] transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;
effect.World = transforms[mesh.ParentBone.Index] * world;
// Use the matrices provided by the chase camera
effect.View = camera.View;
effect.Projection = camera.Projection;
}
mesh.Draw();
}
}
private void DrawOverlayText()
{
spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred,
SaveStateMode.SaveState);
string text = "Right Trigger or Spacebar = thrust\n" +
"Left Thumb Stick or Arrow keys = steer\n" +
"A = toggle camera spring (" + (cameraSpringEnabled ?
"on" : "off") + ")";
// Draw the string twice to create a drop shadow, first colored black
// and offset one pixel to the bottom right, then again in white at the
// intended position. This makes text easier to read over the background.
spriteBatch.DrawString(spriteFont, text, new Vector2(65, 65), Color.Black);
spriteBatch.DrawString(spriteFont, text, new Vector2(64, 64), Color.White);
//spriteBatch.DrawString(spriteFont, ChaseCamera.dbgtext, new Vector2(64, 140), Color.White);
if (camera.Mode == 2)
spriteBatch.Draw(texCrosshair, new Vector2(graphics.PreferredBackBufferWidth / 2 - 16, graphics.PreferredBackBufferHeight / 2 - 16), Color.White);
else
spriteBatch.Draw(texCrosshair, new Vector2(Mouse.GetState().X - 16, Mouse.GetState().Y - 16), Color.White);
spriteBatch.End();
}
//public static string dbgtext = "";
private void DrawCity()
{
effect.CurrentTechnique = effect.Techniques["Textured"];
effect.Parameters["xWorld"].SetValue(Matrix.Identity);//*Matrix.CreateTranslation(new Vector3(-2, 1, 2)) * Matrix.CreateScale(250.0f));
//effect.Parameters["xWorld"].SetValue(Matrix.Identity);
effect.Parameters["xView"].SetValue(camera.View);
effect.Parameters["xProjection"].SetValue(camera.Projection);
effect.Parameters["xTexture"].SetValue(sceneryTexture);
effect.Parameters["xEnableLighting"].SetValue(true);
effect.Parameters["xLightDirection"].SetValue(lightDirection);
effect.Parameters["xAmbient"].SetValue(0.5f);
effect.Begin();
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Begin();
device.VertexDeclaration = texturedVertexDeclaration;
device.Vertices[0].SetSource(cityVertexBuffer, 0, VertexPositionNormalTexture.SizeInBytes);
device.DrawPrimitives(PrimitiveType.TriangleList, 0, cityVertexBuffer.SizeInBytes / VertexPositionNormalTexture.SizeInBytes / 3);
pass.End();
}
effect.End();
}
readonly int[] indices = { 0, 1, 5, 1, 3, 7, 3, 2, 6, 2, 0, 4, 5, 7, 6, 4 };
public void DrawBoundingBoxes()
{
effect.CurrentTechnique = effect.Techniques["Textured"];
effect.Parameters["xWorld"].SetValue(Matrix.Identity);
effect.Parameters["xView"].SetValue(camera.View);
effect.Parameters["xProjection"].SetValue(camera.Projection);
effect.Parameters["xTexture"].SetValue(sceneryTexture);
effect.Parameters["xEnableLighting"].SetValue(true);
effect.Parameters["xLightDirection"].SetValue(lightDirection);
effect.Parameters["xAmbient"].SetValue(0.5f);
effect.Begin();
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
VertexPositionColor[] vertices = new VertexPositionColor[8];
pass.Begin();
for (int i = 0; i < buildingBoundingBoxes.Count; i++)
{
vertices[0] = new VertexPositionColor(buildingBoundingBoxes[i].Min, Color.GreenYellow);
vertices[1] = new VertexPositionColor(new Vector3(buildingBoundingBoxes[i].Max.X, buildingBoundingBoxes[i].Min.Y, buildingBoundingBoxes[i].Min.Z), Color.GreenYellow);
vertices[2] = new VertexPositionColor(new Vector3(buildingBoundingBoxes[i].Min.X, buildingBoundingBoxes[i].Min.Y, buildingBoundingBoxes[i].Max.Z), Color.GreenYellow);
vertices[3] = new VertexPositionColor(new Vector3(buildingBoundingBoxes[i].Max.X, buildingBoundingBoxes[i].Min.Y, buildingBoundingBoxes[i].Max.Z), Color.GreenYellow);
vertices[4] = new VertexPositionColor(new Vector3(buildingBoundingBoxes[i].Min.X, buildingBoundingBoxes[i].Max.Y, buildingBoundingBoxes[i].Min.Z), Color.GreenYellow);
vertices[5] = new VertexPositionColor(new Vector3(buildingBoundingBoxes[i].Max.X, buildingBoundingBoxes[i].Max.Y, buildingBoundingBoxes[i].Min.Z), Color.GreenYellow);
vertices[6] = new VertexPositionColor(new Vector3(buildingBoundingBoxes[i].Min.X, buildingBoundingBoxes[i].Max.Y, buildingBoundingBoxes[i].Max.Z), Color.GreenYellow);
vertices[7] = new VertexPositionColor(buildingBoundingBoxes[i].Max, Color.GreenYellow);
device.VertexDeclaration = new VertexDeclaration(device, VertexPositionColor.VertexElements);
device.DrawUserIndexedPrimitives<VertexPositionColor>
(PrimitiveType.LineStrip, vertices, 0, 8, indices, 0, 15);
}
vertices[0] = new VertexPositionColor(completeCityBox.Min, Color.Red);
vertices[1] = new VertexPositionColor(new Vector3(completeCityBox.Max.X, completeCityBox.Min.Y, completeCityBox.Min.Z), Color.Red);
vertices[2] = new VertexPositionColor(new Vector3(completeCityBox.Min.X, completeCityBox.Min.Y, completeCityBox.Max.Z), Color.Red);
vertices[3] = new VertexPositionColor(new Vector3(completeCityBox.Max.X, completeCityBox.Min.Y, completeCityBox.Max.Z), Color.Red);
vertices[4] = new VertexPositionColor(new Vector3(completeCityBox.Min.X, completeCityBox.Max.Y, completeCityBox.Min.Z), Color.Red);
vertices[5] = new VertexPositionColor(new Vector3(completeCityBox.Max.X, completeCityBox.Max.Y, completeCityBox.Min.Z), Color.Red);
vertices[6] = new VertexPositionColor(new Vector3(completeCityBox.Min.X, completeCityBox.Max.Y, completeCityBox.Max.Z), Color.Red);
vertices[7] = new VertexPositionColor(completeCityBox.Max, Color.Red);
device.VertexDeclaration = new VertexDeclaration(device, VertexPositionColor.VertexElements);
device.DrawUserIndexedPrimitives<VertexPositionColor>
(PrimitiveType.LineStrip, vertices, 0, 8, indices, 0, 15);
pass.End();
}
effect.End();
}
/// <summary>
/// from http://xnawiki.com/index.php?title=Rendering_Bounding_Spheres
/// </summary>
VertexBuffer vertBuffer;
VertexDeclaration vertDecl;
int sphereResolution;
BasicEffect basicEffect = null;
/// <summary>
/// Initializes the graphics objects for rendering the spheres. If this method isn't
/// run manually, it will be called the first time you render a sphere.
/// </summary>
/// <param name="graphicsDevice">The graphics device to use when rendering.</param>
/// <param name="sphereResolution">The number of line segments
/// to use for each of the three circles.</param>
public void InitializeGraphics(int sphereResolution)
{
basicEffect = new BasicEffect(device, null);
basicEffect.LightingEnabled = false;
basicEffect.VertexColorEnabled = false;
this.sphereResolution = sphereResolution;
vertDecl = new VertexDeclaration(device, VertexPositionColor.VertexElements);
VertexPositionColor[] verts = new VertexPositionColor[(sphereResolution + 1) * 3];
int index = 0;
float step = MathHelper.TwoPi / (float)sphereResolution;
//create the loop on the XY plane first
for (float a = 0f; a <= MathHelper.TwoPi; a += step)
{
verts[index++] = new VertexPositionColor(
new Vector3((float)Math.Cos(a), (float)Math.Sin(a), 0f),
Color.White);
}
//next on the XZ plane
for (float a = 0f; a <= MathHelper.TwoPi; a += step)
{
verts[index++] = new VertexPositionColor(
new Vector3((float)Math.Cos(a), 0f, (float)Math.Sin(a)),
Color.White);
}
//finally on the YZ plane
for (float a = 0f; a <= MathHelper.TwoPi; a += step)
{
verts[index++] = new VertexPositionColor(
new Vector3(0f, (float)Math.Cos(a), (float)Math.Sin(a)),
Color.White);
}
vertBuffer = new VertexBuffer(
device,
verts.Length * VertexPositionColor.SizeInBytes,
BufferUsage.None);
vertBuffer.SetData(verts);
}
/// <summary>
/// Renders a bounding sphere using a single color for all three axis.
/// </summary>
/// <param name="sphere">The sphere to render.</param>
/// <param name="graphicsDevice">The graphics device to use when rendering.</param>
/// <param name="view">The current view matrix.</param>
/// <param name="projection">The current projection matrix.</param>
/// <param name="color">The color to use for rendering the circles.</param>
public void DrawBoundingSphere(
BoundingSphere sphere,
Color color)
{
if (vertBuffer == null)
InitializeGraphics(30);
device.VertexDeclaration = vertDecl;
device.Vertices[0].SetSource(
vertBuffer,
0,
VertexPositionColor.SizeInBytes);
basicEffect.World =
Matrix.CreateScale(sphere.Radius) *
Matrix.CreateTranslation(sphere.Center);
basicEffect.View = camera.View;
basicEffect.Projection = camera.Projection;
basicEffect.DiffuseColor = color.ToVector3();
basicEffect.Begin();
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Begin();
//render each circle individually
device.DrawPrimitives(
PrimitiveType.LineStrip,
0,
sphereResolution);
device.DrawPrimitives(
PrimitiveType.LineStrip,
sphereResolution + 1,
sphereResolution);
device.DrawPrimitives(
PrimitiveType.LineStrip,
(sphereResolution + 1) * 2,
sphereResolution);
pass.End();
}
basicEffect.End();
}
private void DrawBullets()
{
if (bulletList.Count > 0)
{
VertexPointSprite[] spriteArray = new VertexPointSprite[bulletList.Count];
for (int i = 0; i < bulletList.Count; i++)
spriteArray[i] = new VertexPointSprite(bulletList[i].position, 50);
effect.CurrentTechnique = effect.Techniques["PointSprites"];
Matrix worldMatrix = Matrix.Identity;
effect.Parameters["xWorld"].SetValue(worldMatrix);
effect.Parameters["xView"].SetValue(camera.View);
effect.Parameters["xProjection"].SetValue(camera.Projection);
effect.Parameters["xTexture"].SetValue(bulletTexture);
device.RenderState.PointSpriteEnable = true;
effect.Begin();
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Begin();
device.VertexDeclaration = pointSpriteVertexDeclaration;
device.DrawUserPrimitives(PrimitiveType.PointList, spriteArray, 0, spriteArray.Length);
pass.End();
}
effect.End();
device.RenderState.PointSpriteEnable = false;
}
}
}
}