Commit Description:
Early version of serialization.
Commit Description:
Early version of serialization.
File last commit:
Show/Diff file:
Action:
isometric-park-fna/Tile.cs
165 lines | 6.0 KiB | text/x-csharp | CSharpLexer
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace isometricparkfna
{
public class Tile
{
//blatantly stolen from XNA resources.com
static public int TileWidth = 64;
static public int TileHeight = 64;
static public int TileStepX = 64;
static public int TileStepY = 16;
static public int OddRowXOffset = 32;
static public int HeightTileOffset = 32;
static public int TileSpriteWidth = 64;
static public int TileSpriteHeight = 32;
static public Texture2D TileSetTexture;
public Tile()
{
}
static public Rectangle GetSourceRectangle(int tileIndex)
{
int tileY = tileIndex / (TileSetTexture.Width / TileWidth);
int tileX = tileIndex % (TileSetTexture.Width / TileWidth);
return new Rectangle(tileX * TileWidth, tileY * TileHeight, TileWidth, TileHeight);
}
static public Rectangle GetExtendedSourceRectangle(int tileIndex, int height)
{
int tileY = tileIndex / (TileSetTexture.Width / TileWidth);
int tileX = tileIndex % (TileSetTexture.Width / TileWidth);
return new Rectangle(tileX * TileWidth, tileY * TileHeight, TileWidth, TileHeight*height);
}
static public void OutlineSquare(SpriteBatch batch, float x, float y, Color color)
{
Tile.OutlineSquare(batch, x, y, color, 1);
}
static public void OutlineSquare(SpriteBatch batch, float x, float y, Color color, int size)
{
Vector2 adjust2 = new Vector2(Tile.TileSpriteWidth / 2, Tile.TileSpriteHeight); //TODO figure out why this second value shouldn't be halved
//Upper right
//float x = this.mouseGrid.X;
//float y = this.mouseGrid.Y;
Line.drawLine(batch,
new Vector2(((x - y) * Tile.TileSpriteWidth / 2), (x + y) * Tile.TileSpriteHeight / 2) + adjust2,
//new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight),
new Vector2(((x - y + size) * Tile.TileSpriteWidth / 2), (x + y + size) * Tile.TileSpriteHeight / 2) + adjust2,
color, 0.79f);
//Bottom right
Line.drawLine(batch,
new Vector2(((x + size - y) * Tile.TileSpriteWidth / 2), (x + size + y) * Tile.TileSpriteHeight / 2) + adjust2,
//new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight),
new Vector2(((x + size - (y + size)) * Tile.TileSpriteWidth / 2), (x + size + (y + size)) * Tile.TileSpriteHeight / 2) + adjust2,
color, 0.79f);
//Bottom left
Line.drawLine(batch,
new Vector2(((x - (y + size)) * Tile.TileSpriteWidth / 2), (x + y + size) * Tile.TileSpriteHeight / 2) + adjust2,
//new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight),
new Vector2(((x + size - (y + size)) * Tile.TileSpriteWidth / 2), (x + size + (y + size)) * Tile.TileSpriteHeight / 2) + adjust2,
color, 0.79f);
//Upper left
Line.drawLine(batch,
new Vector2(((x - y) * Tile.TileSpriteWidth / 2), (x + y) * Tile.TileSpriteHeight / 2) + adjust2,
//new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight),
new Vector2(((x - (y + size)) * Tile.TileSpriteWidth / 2), (x + (y + size)) * Tile.TileSpriteHeight / 2) + adjust2,
color, 0.79f);
}
// 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);
// Tile.drawTileAt(x, y, tileIndex, height, depthOffset);
// }
public static void drawTileAt(SpriteBatch batch, 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 height_adjust = 0;
if (height > 1) //not sure why this is necessary :/
{
height_adjust = height;
}
int adjustedx = x - height_adjust;
int adjustedy = y - height_adjust;
int screenx = (adjustedx - adjustedy) * Tile.TileSpriteWidth / 2;
int screeny = (adjustedx + adjustedy) * Tile.TileSpriteHeight / 2;
// if (this.cull(x, y))
// {
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);
// }
}
}
}