Show More
Commit Description:
Add rudimentary dropdown for menu button.
Commit Description:
Add rudimentary dropdown for menu button.
References:
File last commit:
Show/Diff file:
Action:
isometric-park-fna/Tile.cs
244 lines | 10.4 KiB | text/x-csharp | CSharpLexer
244 lines | 10.4 KiB | text/x-csharp | CSharpLexer
r0 | using System; | |||
r596 | using System.Diagnostics; | |||
r154 | using System.Collections.Generic; | |||
using System.Linq; | ||||
r0 | ||||
using Microsoft.Xna.Framework; | ||||
using Microsoft.Xna.Framework.Graphics; | ||||
namespace isometricparkfna | ||||
{ | ||||
r153 | ||||
public struct Edge { | ||||
r462 | public Vector2 Start; | |||
public Vector2 End; | ||||
r154 | ||||
r462 | public static bool operator == (Edge left, Edge right) { | |||
return (left.Start == right.Start && left.End == right.End) || | ||||
(left.Start == right.End && left.End == right.Start); | ||||
} | ||||
r154 | ||||
r462 | public static bool operator != (Edge left, Edge right) { | |||
return !(left == right); | ||||
} | ||||
r154 | ||||
r462 | public override bool Equals(Object other) { | |||
return ((other != null) | ||||
&& this.GetType().Equals(other.GetType()) | ||||
&& this == (Edge)other); //we just confirmed the types match so this is okay | ||||
} | ||||
r155 | ||||
r462 | public override int GetHashCode() { | |||
return (Start, End).GetHashCode(); | ||||
} | ||||
r153 | ||||
} | ||||
r0 | public class Tile | |||
{ | ||||
//blatantly stolen from XNA resources.com | ||||
static public int TileWidth = 64; | ||||
static public int TileHeight = 64; | ||||
r124 | // static public int TileStepX = 64; | |||
// static public int TileStepY = 16; | ||||
//static public int OddRowXOffset = 32; | ||||
r0 | ||||
r12 | static public int TileSpriteWidth = 64; | |||
static public int TileSpriteHeight = 32; | ||||
r0 | static public Texture2D TileSetTexture; | |||
r564 | public const float DEPTH = 0.79f; | |||
r206 | ||||
r0 | public Tile() | |||
{ | ||||
r206 | ||||
r0 | } | |||
r206 | ||||
r0 | 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); | ||||
} | ||||
r2 | 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); | ||||
} | ||||
r206 | ||||
r67 | 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, | ||||
r462 | 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, | ||||
r578 | color, 0.77f); | |||
r67 | ||||
//Bottom right | ||||
Line.drawLine(batch, | ||||
r462 | 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, | ||||
r578 | color, 0.77f); | |||
r67 | //Bottom left | |||
Line.drawLine(batch, | ||||
r462 | 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, | ||||
r578 | color, 0.77f); | |||
r67 | //Upper left | |||
Line.drawLine(batch, | ||||
r462 | 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, | ||||
r578 | color, 0.77f); | |||
r67 | } | |||
r68 | ||||
r153 | static public void OutlineSquare2(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; | ||||
r462 | // | |||
int size_less_one = size - 1; | ||||
r153 | ||||
Line.drawLine(batch, | ||||
r462 | 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_less_one) * Tile.TileSpriteWidth / 2), (x + y + size_less_one) * Tile.TileSpriteHeight / 2) + adjust2, | ||||
r564 | color, Tile.DEPTH); | |||
r153 | ||||
//Bottom right | ||||
Line.drawLine(batch, | ||||
r462 | new Vector2(((x + size - y) * Tile.TileSpriteWidth / 2), (x + size_less_one + 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_less_one + (y + size_less_one)) * Tile.TileSpriteHeight / 2) + adjust2, | ||||
r564 | color, Tile.DEPTH); | |||
r153 | //Bottom left | |||
Line.drawLine(batch, | ||||
r462 | 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, | ||||
r564 | color, Tile.DEPTH); | |||
r153 | //Upper left | |||
Line.drawLine(batch, | ||||
r462 | 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, | ||||
r564 | color, Tile.DEPTH); | |||
r153 | } | |||
r462 | public static void drawOutline(SpriteBatch batch, Color color) { | |||
Vector2[] squares = {new Vector2(15, 1), new Vector2(15,2)}; | ||||
r153 | ||||
r462 | foreach (Vector2 square in squares) { | |||
} | ||||
} | ||||
r153 | ||||
r462 | public static void drawEdge(SpriteBatch batch, Edge edge, Color color) { | |||
r153 | ||||
r462 | //x = edge.Start.X | |||
//y = edge.Start.y | ||||
//y + size = edge.End.y | ||||
//x + size = edge.End.x | ||||
r153 | ||||
Vector2 adjust2 = new Vector2(Tile.TileSpriteWidth / 2, Tile.TileSpriteHeight); //TODO figure out why this second value shouldn't be halved | ||||
Line.drawLine(batch, | ||||
r462 | new Vector2(((edge.Start.X - edge.Start.Y) * Tile.TileSpriteWidth / 2), (edge.Start.X + edge.Start.Y) * Tile.TileSpriteHeight / 2) + adjust2, | |||
//new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight), | ||||
new Vector2(((edge.End.X - edge.End.Y) * Tile.TileSpriteWidth / 2), (edge.End.X + edge.End.Y) * Tile.TileSpriteHeight / 2) + adjust2, | ||||
color, 0.80f); //Just behind the default | ||||
} | ||||
r153 | ||||
r462 | public static void drawEdges(SpriteBatch batch, Edge[] edges, Color color) { | |||
foreach (Edge edge in edges) { | ||||
drawEdge(batch, edge, color); | ||||
} | ||||
} | ||||
r153 | ||||
r462 | public static void DrawOutlinedSquares(SpriteBatch batch, Vector2[] squares, Color color) { | |||
List<Edge> edges = new List<Edge>(); | ||||
r154 | ||||
r462 | foreach (Vector2 square in squares) { | |||
//upper right | ||||
edges.Add(new Edge {Start=square, End=square + new Vector2(1, 0)}); | ||||
//lower right | ||||
edges.Add(new Edge {Start=square + new Vector2(1, 0), End=square + new Vector2(1, 1)}); | ||||
//lower left | ||||
edges.Add(new Edge {Start=square + new Vector2(0, 1), End=square + new Vector2(1, 1)}); | ||||
//upper left | ||||
edges.Add(new Edge {Start=square, End=square + new Vector2(0, 1)}); | ||||
} | ||||
r154 | ||||
r596 | //Seems to be v. slow | |||
r462 | edges = edges.GroupBy(x => x).Where(grp => grp.Count() == 1).Select(grp => grp.Key).ToList<Edge>(); | |||
r154 | ||||
r462 | drawEdges(batch, edges.ToArray(), color); | |||
} | ||||
r154 | ||||
r569 | public static void drawTileAt(SpriteBatch batch, int x, int y, int tileIndex, int height, float depth) { | |||
drawTileAt(batch, x, y, tileIndex, height, depth, Color.White); | ||||
} | ||||
r153 | ||||
r569 | ||||
public static void drawTileAt(SpriteBatch batch, int x, int y, int tileIndex, int height, float depth, Color color) | ||||
{ | ||||
//new Color(100, 100, 100, 100) | ||||
r68 | ||||
r462 | int height_adjust = 0; | |||
r68 | ||||
r462 | if (height > 1) //not sure why this is necessary :/ | |||
{ | ||||
height_adjust = height; | ||||
} | ||||
r68 | ||||
r462 | int adjustedx = x - height_adjust; | |||
int adjustedy = y - height_adjust; | ||||
r68 | ||||
r462 | int screenx = (adjustedx - adjustedy) * Tile.TileSpriteWidth / 2; | |||
int screeny = (adjustedx + adjustedy) * Tile.TileSpriteHeight / 2; | ||||
r68 | ||||
r462 | // if (this.cull(x, y)) | |||
// { | ||||
batch.Draw( | ||||
r68 | Tile.TileSetTexture, | |||
r462 | new Rectangle( | |||
screenx, | ||||
screeny, | ||||
Tile.TileWidth, Tile.TileHeight * height), | ||||
r68 | Tile.GetExtendedSourceRectangle(tileIndex, height), | |||
r569 | color, | |||
r462 | 0.0f, | |||
Vector2.Zero, | ||||
SpriteEffects.None, | ||||
depth); | ||||
// } | ||||
r68 | ||||
r462 | } | |||
r68 | ||||
r0 | } | |||
} | ||||