Commit Description:
Fix cursor overlay.
Commit Description:
Fix cursor overlay.
File last commit:
Show/Diff file:
Action:
isometric-park-fna/FNAGame.cs
638 lines | 16.9 KiB | text/x-csharp | CSharpLexer
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
using System;
using System.IO;
using SpriteFontPlus;
using isometricparkfna;
using System.Diagnostics;
using System.Collections.Generic;
class FNAGame : Game
{
private KeyboardState keyboardPrev = new KeyboardState();
private SpriteBatch batch;
private Texture2D texture;
private Texture2D paddle;
private Texture2D ball;
private SoundEffect sound;
private Song music;
private SpriteFont font;
private Camera camera = new Camera(new float[] { 0.25f, 0.5f, 1.0f, 2.0f, 4.0f });
Random random_generator = new Random();
int frameRate = 0;
int frameCounter = 0;
TimeSpan elapsedTime = TimeSpan.Zero;
TimeSpan drawTime = TimeSpan.Zero;
private const int width = 1280;
private const int height = 640;
private const float friction = 0.1f;
private const float inputVelocityDelta = friction + 0.1f;
private int playerScore = 0;
private int aiScore = 0;
//new tile stuff
int squaresAcross = 50;
int squaresDown = 50;
int baseOffsetX = -14;
int baseOffsetY = -14;
GraphicsDevice device;
TileMap map;
Vector2 mouseGrid;
Vector2 original_point;
Vector2 mousePos;
private static void Main(string[] args)
{
using FNAGame g = new FNAGame();
g.Run();
}
private FNAGame()
{
//this.device = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile.)
GraphicsDeviceManager gdm = new GraphicsDeviceManager(this)
{
// Typically you would load a config here...
PreferredBackBufferWidth = width,
PreferredBackBufferHeight = height,
IsFullScreen = false,
SynchronizeWithVerticalRetrace = true
};
//gdm.SynchronizeWithVerticalRetrace = false;
IsFixedTimeStep = false;
this.map = new TileMap(this.squaresAcross, this.squaresDown);
foreach (List<Cell> row in this.map.cells)
{
foreach (Cell cell in row)
{
if (this.random_generator.NextDouble() > 0.75)
{
cell.hasTree = true;
}
}
}
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
/* This is a nice place to start up the engine, after
* loading configuration stuff in the constructor
*/
this.IsMouseVisible = true;
base.Initialize();
}
protected override void LoadContent()
{
// Load textures, sounds, and so on in here...
// Create the batch...
batch = new SpriteBatch(GraphicsDevice);
// ... then load a texture from ./Content/FNATexture.png
texture = Content.Load<Texture2D>("FNATexture");
paddle = Content.Load<Texture2D>("paddle");
ball = Content.Load<Texture2D>("ball");
sound = Content.Load<SoundEffect>("FNASound");
music = Content.Load<Song>("IfImWrong");
Tile.TileSetTexture = Content.Load<Texture2D>(@"part4_tileset");
Line.initialize(GraphicsDevice);
var fontBakeResult = TtfFontBaker.Bake(File.OpenRead(@"Content/DroidSans.ttf"),
25,
1024,
1024,
new[]
{
CharacterRange.BasicLatin,
CharacterRange.Latin1Supplement,
CharacterRange.LatinExtendedA,
CharacterRange.Cyrillic
}
);
font = fontBakeResult.CreateSpriteFont(GraphicsDevice);
//DynamicSpriteFont font = DynamicSpriteFont.FromTtf(File.ReadAllBytes(@"Content/DroidSans.ttf"), 20);
}
protected override void UnloadContent()
{
// Clean up after yourself!
batch.Dispose();
texture.Dispose();
sound.Dispose();
music.Dispose();
}
Vector2 calculateMousegrid(Vector2 normalizedMousePos)
{
//int gridx = (int)(normalizedMousePos.X / Tile.TileSpriteWidth);
//int gridy = (int)(normalizedMousePos.Y / Tile.TileSpriteHeight);
Vector2 adjust = new Vector2(Tile.TileSpriteWidth / 2, Tile.TileSpriteHeight);
Vector2 adjustedMousePos = normalizedMousePos - adjust;
float boardx = ((adjustedMousePos.X / Tile.TileSpriteWidth) + (adjustedMousePos.Y / Tile.TileSpriteHeight));
float boardy = ((adjustedMousePos.Y / Tile.TileSpriteHeight) - (adjustedMousePos.X / Tile.TileSpriteWidth));
return new Vector2(boardx, boardy);
/*
int gridx = (int)((normalizedMousePos.X + (this.baseOffsetX + 4)) / Tile.TileWidth) ;
int gridy = (int)((normalizedMousePos.Y + (this.baseOffsetX + 4) + (2*baseOffsetY)) / (Tile.TileStepY));
int within_gridx = (int)((normalizedMousePos.X) % Tile.TileWidth) - (Tile.TileWidth/2);
int within_gridy = (int)((normalizedMousePos.Y) % Tile.TileHeight) - (Tile.TileHeight / 2);
int middle_distance = Math.Abs(within_gridx) + Math.Abs(within_gridx);
Vector2 adjustment_vector;
return new Vector2(gridx, gridy);
if (middle_distance < (Tile.TileWidth / 2))
{
return new Vector2(gridx, gridy);
}
else if ((Math.Sign(within_gridx) == -1) && (Math.Sign(within_gridy) == 1))
{
adjustment_vector = new Vector2(-1, -1);
return new Vector2(gridx, gridy) + adjustment_vector;
}
else if ((Math.Sign(within_gridx) == -1) && (Math.Sign(within_gridy) == -1))
{
adjustment_vector = new Vector2(-1, 1);
return new Vector2(gridx, gridy) + adjustment_vector;
}
else if ((Math.Sign(within_gridx) == 1) && (Math.Sign(within_gridy) == 1))
{
adjustment_vector = new Vector2(0, -1);
return new Vector2(gridx, gridy) + adjustment_vector;
}
else if ((Math.Sign(within_gridx) == 1) && (Math.Sign(within_gridy) == -1))
{
adjustment_vector = new Vector2(0, 1);
return new Vector2(gridx, gridy) + adjustment_vector;
}
else {
return new Vector2(gridx, gridy);
}
*/
}
protected override void Update(GameTime gameTime)
{
float volume = 1.0f;
float pitch = 0.0f;
float pan = 0.0f;
// Run game logic in here. Do NOT render anything here!
KeyboardState keyboardCur = Keyboard.GetState();
if (keyboardCur.IsKeyDown(Keys.Q) && keyboardPrev.IsKeyUp(Keys.Q))
{
System.Console.WriteLine("Quitting");
Environment.Exit(0);
}
if (keyboardCur.IsKeyDown(Keys.Down))
{
this.camera.Move(new Vector2(0, -2));
}
else if (keyboardCur.IsKeyDown(Keys.Up))
{
this.camera.Move(new Vector2(0, 2));
}
else if (keyboardCur.IsKeyDown(Keys.Left))
{
this.camera.Move(new Vector2(-2, 0));
}
else if (keyboardCur.IsKeyDown(Keys.Right))
{
this.camera.Move(new Vector2(2, 0));
}
else if (keyboardCur.IsKeyDown(Keys.Subtract) && keyboardPrev.IsKeyUp(Keys.Subtract))
{
this.camera.ZoomOut();
}
else if (keyboardCur.IsKeyDown(Keys.Add) && keyboardPrev.IsKeyUp(Keys.Add))
{
this.camera.ZoomIn();
}
if (keyboardCur.IsKeyDown(Keys.Space) && keyboardPrev.IsKeyUp(Keys.Space))
{
sound.Play(volume, pitch, pan);
}
//if (keyboardCur.IsKeyDown(Keys.P) && keyboardPrev.IsKeyUp(Keys.P))
// {
// this.player_state = !this.player_state;
// }
//if (player_state )
//{
// MediaPlayer.Play(music);
//}
MouseState mouseCur = Mouse.GetState();
if (MathUtils.Between(mouseCur.X, 0, 50))
{
this.camera.Move(new Vector2(-4, 0));
}
else if (MathUtils.Between(mouseCur.X, (FNAGame.width - 50), FNAGame.width))
{
this.camera.Move(new Vector2(4, 0));
}
if (MathUtils.Between(mouseCur.Y, 0, 50))
{
this.camera.Move(new Vector2(0, -4));
}
else if (MathUtils.Between(mouseCur.Y, (FNAGame.height - 50), FNAGame.height))
{
this.camera.Move(new Vector2(0, 4));
}
this.original_point = Vector2.Transform(new Vector2(mouseCur.X, mouseCur.Y), Matrix.Invert(camera.get_transformation(GraphicsDevice)));
//int gridx = (int)((this.original_point.X-baseOffsetX) / Tile.TileStepX);
/* int gridx = (int)(this.original_point.Y / Tile.TileHeight + this.original_point.X / Tile.TileWidth); */
//int gridy = (int)((this.original_point.Y-baseOffsetY) / (Tile.TileStepY*2));
/* int gridy = (int)(this.original_point.Y / Tile.TileHeight - this.original_point.X / Tile.TileWidth); */
//this.mouseGrid = new Vector2(gridx, gridy);
this.mouseGrid = this.calculateMousegrid(this.original_point);
elapsedTime += gameTime.ElapsedGameTime;
if (elapsedTime > TimeSpan.FromSeconds(1))
{
elapsedTime -= TimeSpan.FromSeconds(1);
frameRate = frameCounter;
frameCounter = 0;
}
this.keyboardPrev = keyboardCur;
base.Update(gameTime);
}
protected void drawTileAt(int x, int y, int tileIndex, int height)
{
float maxdepth = ((this.squaresAcross + 1) + ((this.squaresDown + 1) * Tile.TileWidth)) * 10;
float depthOffset = 0.7f - ((0 + (0 * Tile.TileWidth)) / maxdepth);
drawTileAt(x, y, tileIndex, height, depthOffset);
}
protected void drawTileAt(int x, int y, int tileIndex, int height, float depth)
{
/*
Vector2 firstSquare = Vector2.Zero;
Vector2 squareOffset = Vector2.Zero;
int offsetX = (int)squareOffset.X;
int offsetY = (int)squareOffset.Y;
int firstX = (int)firstSquare.X;
int firstY = (int)firstSquare.Y;
int rowOffset = 0;
float maxdepth = ((this.squaresAcross + 1) + ((this.squaresDown + 1) * Tile.TileWidth)) * 10;
int mapx = (firstX + x);
int mapy = (firstY + y);
float depthOffset = 0.7f - ((mapx + (mapy * Tile.TileWidth)) / maxdepth);
if ((firstY + y) % 2 == 1)
rowOffset = Tile.OddRowXOffset;
batch.Draw(
Tile.TileSetTexture,
new Rectangle(
(x * Tile.TileStepX) - offsetX + rowOffset + baseOffsetX,
(y * Tile.TileStepY) - offsetY + baseOffsetY-(Tile.TileHeight*(height-1)),
Tile.TileWidth, Tile.TileHeight*height),
Tile.GetExtendedSourceRectangle(tileIndex, height),
Color.White,
0.0f,
Vector2.Zero,
SpriteEffects.None,
depthOffset);
*/
int screenx = (x - y) * Tile.TileSpriteWidth / 2;
int screeny = (x + y) * Tile.TileSpriteHeight / 2;
batch.Draw(
Tile.TileSetTexture,
new Rectangle(
screenx,
screeny,
Tile.TileWidth, Tile.TileHeight * height),
Tile.GetExtendedSourceRectangle(tileIndex, height),
Color.White,
0.0f,
Vector2.Zero,
SpriteEffects.None,
depth);
}
protected override void Draw(GameTime gameTime)
{
// Render stuff in here. Do NOT run game logic in here!
frameCounter++;
string fps = string.Format("fps: {0}", frameRate);
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
GraphicsDevice.Clear(Color.CornflowerBlue);
batch.Begin(SpriteSortMode.BackToFront,
BlendState.AlphaBlend,
null,
null,
null,
null,
camera.get_transformation(GraphicsDevice));
//New tile stuff
/*
Vector2 firstSquare = Vector2.Zero;
int firstX = (int)firstSquare.X;
int firstY = (int)firstSquare.Y;
Vector2 squareOffset = Vector2.Zero;
int offsetX = (int)squareOffset.X;
int offsetY = (int)squareOffset.Y;
for (int y = 0; y < this.squaresDown; y++)
{
int rowOffset = 0;
if ((firstY + y) % 2 == 1)
rowOffset = Tile.OddRowXOffset;
for (int x = 0; x < this.squaresAcross; x++) {
batch.Draw(
Tile.TileSetTexture,
new Rectangle(
((x * Tile.TileStepX) - offsetX + rowOffset + baseOffsetX),
((y * Tile.TileStepY) - offsetY + baseOffsetY),
Tile.TileWidth, Tile.TileHeight),
Tile.GetSourceRectangle(1),
Color.White,
0.0f,
Vector2.Zero,
SpriteEffects.None,
0.9f);
}
}*/
for (int y = 0; y < this.squaresDown; y++)
{
for (int x = 0; x < this.squaresAcross; x++)
{
int screenx = (x - y) * Tile.TileSpriteWidth/2;
int screeny = (x + y) * Tile.TileSpriteHeight / 2;
batch.Draw(
Tile.TileSetTexture,
new Rectangle(
screenx,
screeny,
Tile.TileWidth, Tile.TileHeight),
Tile.GetSourceRectangle(1),
Color.White,
0.0f,
Vector2.Zero,
SpriteEffects.None,
0.9f);
}
}
for (int y = 0; y < this.squaresDown; y++)
{
Vector2 adjust = new Vector2(Tile.TileSpriteWidth / 2, Tile.TileSpriteHeight); //TODO figure out why this second value shouldn't be halved
Line.drawLine(batch,
new Vector2(((0-y) * Tile.TileSpriteWidth/2), (0+y)*Tile.TileSpriteHeight/2) + adjust,
//new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight),
new Vector2((this.squaresAcross - (y)) * Tile.TileSpriteWidth / 2, (this.squaresAcross + (y )) * Tile.TileSpriteHeight / 2) + adjust,
Color.White, 0.8f);
}
for (int x = 0; x < this.squaresAcross; x++)
{
Vector2 adjust = new Vector2(Tile.TileSpriteWidth / 2, Tile.TileSpriteHeight); //TODO figure out why this second value shouldn't be halved
Line.drawLine(batch,
new Vector2(((x - 0) * Tile.TileSpriteWidth / 2), (x + 0) * Tile.TileSpriteHeight / 2) + adjust,
//new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight),
new Vector2((x - this.squaresDown) * Tile.TileSpriteWidth / 2, (x + this.squaresDown) * Tile.TileSpriteHeight / 2) + adjust,
Color.White, 0.8f);
}
//Gridlines
//Lines going down and to the right:
/*
for (int x = (int)(-this.squaresAcross/2); x < this.squaresAcross; x++)
{
int rowOffset = 0;
float startX = (x * Tile.TileStepX) + baseOffsetX - (Tile.TileStepX / 2);
Vector2 start = new Vector2(startX, -baseOffsetY+4);
Vector2 stop = new Vector2(startX + this.squaresAcross* Tile.TileStepX/2,
this.squaresDown*Tile.TileStepY- baseOffsetY+4);
Line.drawLine(batch,
Line.departurePoint(stop, start, this.squaresAcross * Tile.TileWidth, this.squaresDown * Tile.TileHeight),
Line.departurePoint(start, stop, this.squaresAcross * Tile.TileWidth, this.squaresDown * Tile.TileHeight),
Color.White, 0.8f);
}
//Lines going down and to the left:
for (int x = 0; x < (int)(1.5*this.squaresAcross); x++)
{
float startX = (x * Tile.TileStepX) + baseOffsetX - (Tile.TileStepX / 2);
Vector2 start_reverse = new Vector2(startX, -baseOffsetY + 4);
Vector2 stop_reverse = new Vector2(startX + -(this.squaresAcross * Tile.TileStepX / 2),
(this.squaresDown * Tile.TileStepY) - baseOffsetY + 4);
Line.drawLine(batch,
Line.departurePoint(stop_reverse, start_reverse, this.squaresAcross * Tile.TileWidth, this.squaresDown * Tile.TileHeight),
Line.departurePoint(start_reverse, stop_reverse, this.squaresAcross * Tile.TileWidth, this.squaresDown * Tile.TileHeight),
Color.White, 0.8f);
}
*/
drawTileAt(4, 4, 140, 3);
drawTileAt(6, 4, 141, 3);
drawTileAt(8, 4, 142, 2);
drawTileAt(10, 4, 142, 3);
//drawTileAt(0, 0, 22, 1);
drawTileAt((int)this.mouseGrid.X, (int)this.mouseGrid.Y, 2, 1, 0.85f); //between tiles and gridlines
/*
for (int i = 0; i< 80; i++)
{
for (int j = 0; j < 50; j += 1)
{
//Warning: creates a flashing effect because tree positions update every 1/60th of a second
if (this.random_generator.NextDouble() > 0.75)
{
drawTileAt(i, j, 142, 2);
}
if ((i + j) % 3 == 0)
{
drawTileAt(i, j, 142, 2);
}
}
}//*/
for (int i = 0; i < this.map.MapHeight; i++)
{
for (int j = 0; j < this.map.MapWidth; j += 1)
{
//Warning: creates a flashing effect because tree positions update every 1/60th of a second
if (this.map.cells[i][j].hasTree)
{
drawTileAt(i, j, 142, 2);
}
}
}
batch.End();
batch.Begin(SpriteSortMode.BackToFront,
BlendState.AlphaBlend,
null,
null,
null,
null);
batch.DrawString(font, fps, new Vector2(33, 33), Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.5f);
batch.DrawString(font, fps, new Vector2(32, 32), Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.49f);
batch.DrawString(font, this.drawTime.TotalMilliseconds.ToString(), new Vector2(120, 33), Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.5f);
batch.DrawString(font, this.drawTime.TotalMilliseconds.ToString(), new Vector2(119, 32), Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.49f);
batch.DrawString(font, camera.position.ToString(), new Vector2(190, 33), Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.5f);
batch.DrawString(font, camera.position.ToString(), new Vector2(189, 32), Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.49f);
batch.DrawString(font, this.map.tree_count.ToString(), new Vector2(330, 33), Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.5f);
batch.DrawString(font, this.map.tree_count.ToString(), new Vector2(329, 32), Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.49f);
batch.DrawString(font, this.mouseGrid.ToString(), new Vector2(360, 33), Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.5f);
batch.DrawString(font, this.mouseGrid.ToString(), new Vector2(359, 32), Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.49f);
batch.DrawString(font, this.original_point.ToString(), new Vector2(460, 33), Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.5f);
batch.DrawString(font, this.original_point.ToString(), new Vector2(459, 32), Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.49f);
//*/
//Matrix.Multiply()
batch.End();
stopWatch.Stop();
this.drawTime = stopWatch.Elapsed;
base.Draw(gameTime);
}
}