void checkForStartingPointPlacement(Vector2 mousePosition) { //if (mouseState.LeftButton == ButtonState.Released) // allowStartingPointPlacement = true; //else if (allowStartingPointPlacement && mouseState.LeftButton == ButtonState.Pressed) //{ // allowStartingPointPlacement = false; int X = (int)MathHelper.Clamp(mousePosition.X / map.TileSize - 2, 0, map.Width - 2); int Y = (int)MathHelper.Clamp(mousePosition.Y / map.TileSize - 2, 0, map.Height - 2); BaseObject startingPoint = new BaseObject(new Rectangle(X * map.TileSize, Y * map.TileSize, 5 * map.TileSize, 5 * map.TileSize)); //startingPoint.Texture = startingPointTexture; blocked = false; foreach (BaseObject r in map.Resources) { if (startingPoint.Rectangle.Intersects(r.Rectangle) || Vector2.Distance(startingPoint.CenterPoint, r.CenterPoint) < map.TileSize * 9) { blocked = true; break; } } foreach (BaseObject s in map.StartingPoints) { if (startingPoint.Rectangle.Intersects(s.Rectangle)) { blocked = true; break; } } int maxX = (int)MathHelper.Clamp(X + 5, 0, map.Width); int maxY = (int)MathHelper.Clamp(Y + 5, 0, map.Height); for (int x = X; x < maxX; x++) { for (int y = Y; y < maxY; y++) { if (!map.Tiles[y, x].Walkable) { blocked = true; break; } } } //if (!blocked) // map.StartingPoints.Add(startingPoint); //} }
void initializeMinimap() { minimapPosX = minimapBorderSize; minimapPosY = uiViewport.Height - minimapSize - minimapBorderSize; minimapToMapRatioX = (float)minimapSize / (map.Width * map.TileSize); minimapToMapRatioY = (float)minimapSize / (map.Height * map.TileSize); minimapToScreenRatioX = (float)minimapSize / worldViewport.Width; minimapToScreenRatioY = (float)minimapSize / worldViewport.Height; minimap = new Rectangle(minimapPosX, minimapPosY, minimapSize, minimapSize); minimapScreenIndicatorBox = new BaseObject(new Rectangle(0, 0, (int)(worldViewport.Width * minimapToMapRatioX), (int)(worldViewport.Height * minimapToMapRatioY))); minimapScreenIndicatorBoxLine = new PrimitiveLine(GraphicsDevice, 1); minimapScreenIndicatorBoxLine.Colour = Color.White; }
void checkForLeftClick(GameTime gameTime) { //if (mouseState.LeftButton == ButtonState.Pressed) // allowMiniMapClick = false; //else if (mouseState.LeftButton == ButtonState.Released) // allowMiniMapClick = true; // clicked on bottom ui if (mouseState.Y > worldViewport.Height) { if (mouseState.LeftButton == ButtonState.Pressed) { if (minimap.Contains(mouseState.X, mouseState.Y)) { Vector2 mousePosition = new Vector2(mouseState.X, mouseState.Y); Vector2 minimapCenterPoint = new Vector2(minimap.X + minimap.Width / 2f, minimap.Y + minimap.Height / 2f); float distance = Vector2.Distance(mousePosition, minimapCenterPoint); float angle = (float)Math.Atan2(mousePosition.Y - minimapCenterPoint.Y, mousePosition.X - minimapCenterPoint.X); mousePosition = new Vector2(minimapCenterPoint.X + distance * (float)Math.Cos(angle - camera.Rotation), minimapCenterPoint.Y + distance * (float)Math.Sin(angle - camera.Rotation)); camera.Pos = new Vector2((mousePosition.X - minimapPosX) / minimapToMapRatioX, (mousePosition.Y - minimapPosY) / minimapToMapRatioY); //camera.Pos = new Vector2((mouseState.X - minimapPosX) / minimapToMapRatioX, (mouseState.Y - minimapPosY) / minimapToMapRatioY); } } } // clicked somewhere above bottom ui else { Vector2 mousePosition = Vector2.Transform(new Vector2(mouseState.X, mouseState.Y), Matrix.Invert(camera.get_transformation(worldViewport))); if (state == State.PlacingTile) checkForTileClick(); else if (state == State.PlacingResource) { checkForResourcePlacement(mousePosition); if (!blocked) { int X = (int)MathHelper.Clamp(mousePosition.X / map.TileSize - 1, 0, map.Width - 3); int Y = (int)MathHelper.Clamp(mousePosition.Y / map.TileSize - 1, 0, map.Height - 3); if (mouseState.LeftButton == ButtonState.Pressed) { BaseObject resource = new BaseObject(new Rectangle(X * map.TileSize, Y * map.TileSize, 3 * map.TileSize, 3 * map.TileSize)); resource.Texture = roksTexture; resource.Type = 0; map.Resources.Add(resource); blocked = true; } } } else if (state == State.PlacingStartingPoint) { checkForStartingPointPlacement(mousePosition); if (!blocked) { int X = (int)MathHelper.Clamp(mousePosition.X / map.TileSize - 2, 0, map.Width - 2); int Y = (int)MathHelper.Clamp(mousePosition.Y / map.TileSize - 2, 0, map.Height - 2); if (mouseState.LeftButton == ButtonState.Pressed) { BaseObject startingPoint = new BaseObject(new Rectangle(X * map.TileSize, Y * map.TileSize, 5 * map.TileSize, 5 * map.TileSize)); startingPoint.Texture = startingPointTexture; map.StartingPoints.Add(startingPoint); blocked = true; } } } else if (state == State.Erasing) { checkForErase(); } } }
public bool IsNearRotated(BaseObject r, float factor) { Rectangle inflatedRectangle = new Rectangle(X, Y, Width, Height); inflatedRectangle.Inflate((int)(Width * factor), (int)(Height * factor)); return r.Intersects(inflatedRectangle); }
private bool IsAxisCollision(BaseObject theRectangle, Vector2 aAxis) { //List<int> aRectangleAScalars = new List<int>(); aRectangleAScalars.Clear(); aRectangleAScalars.Add(GenerateScalar(theRectangle.UpperLeftCorner, aAxis)); aRectangleAScalars.Add(GenerateScalar(theRectangle.UpperRightCorner, aAxis)); aRectangleAScalars.Add(GenerateScalar(theRectangle.LowerLeftCorner, aAxis)); aRectangleAScalars.Add(GenerateScalar(theRectangle.LowerRightCorner, aAxis)); //List<int> aRectangleBScalars = new List<int>(); aRectangleBScalars.Clear(); aRectangleBScalars.Add(GenerateScalar(UpperLeftCorner, aAxis)); aRectangleBScalars.Add(GenerateScalar(UpperRightCorner, aAxis)); aRectangleBScalars.Add(GenerateScalar(LowerLeftCorner, aAxis)); aRectangleBScalars.Add(GenerateScalar(LowerRightCorner, aAxis)); int aRectangleAMinimum = aRectangleAScalars.Min(); int aRectangleAMaximum = aRectangleAScalars.Max(); int aRectangleBMinimum = aRectangleBScalars.Min(); int aRectangleBMaximum = aRectangleBScalars.Max(); if (aRectangleBMinimum <= aRectangleAMaximum && aRectangleBMaximum >= aRectangleAMaximum) { return true; } else if (aRectangleAMinimum <= aRectangleBMaximum && aRectangleAMaximum >= aRectangleBMaximum) { return true; } return false; }
public virtual bool Intersects(BaseObject theRectangle) { List<Vector2> aRectangleAxis = new List<Vector2>(); aRectangleAxis.Add(UpperRightCorner - UpperLeftCorner); aRectangleAxis.Add(UpperRightCorner - LowerRightCorner); aRectangleAxis.Add(theRectangle.UpperLeftCorner - theRectangle.LowerLeftCorner); aRectangleAxis.Add(theRectangle.UpperLeftCorner - theRectangle.UpperRightCorner); foreach (Vector2 aAxis in aRectangleAxis) { if (!IsAxisCollision(theRectangle, aAxis)) { return false; } } return true; }
public bool Intersects(Rectangle theRectangle) { BaseObject o = new BaseObject(theRectangle, 0.0f); o.CalculateCorners(); return Intersects(o); }
public void AddPotentialCollision(BaseObject o) { //lock (PotentialCollisions) //{ potentialCollisions.Add(o); //} }
void loadMap(string mapFilePath) { string[] lines = File.ReadAllLines(mapFilePath); string[] widthAndHeight = lines[0].Split(' '); width = int.Parse(widthAndHeight[0]); height = int.Parse(widthAndHeight[1]); int numberOfResources = int.Parse(widthAndHeight[2]); int numberOfStartingPoints = int.Parse(widthAndHeight[3]); tiles = new MapTile[height, width]; for (int i = 0; i < height; i++) { string[] rowOfTiles = lines[i + 1].Split(' '); for (int s = 0; s < width; s++) { int typeCode = int.Parse(rowOfTiles[s].Substring(0, 1)); int pathingCode = int.Parse(rowOfTiles[s].Substring(1, 1)); tiles[i, s] = new MapTile(s, i, tileSize, tileSize, typeCode, pathingCode); //if (pathingCode == 1) // Walls.Add(tiles[i - 1, s]); } } for (int i = height + 1; i < height + 1 + numberOfResources; i++) { string[] resourceParams = lines[i].Split(' '); if (resourceParams[0] == "0") { BaseObject resource = new BaseObject(new Rectangle(int.Parse(resourceParams[1]) * TileSize, int.Parse(resourceParams[2]) * TileSize, 3 * TileSize, 3 * TileSize)); Resources.Add(resource ); } } for (int i = height + 1 + numberOfResources; i < height + 1 + numberOfResources + numberOfStartingPoints; i++) { string[] resourceParams = lines[i].Split(' '); BaseObject startingPoint = new BaseObject(new Rectangle(int.Parse(resourceParams[0]) * TileSize, int.Parse(resourceParams[1]) * TileSize, 5 * TileSize, 5 * TileSize)); StartingPoints.Add(startingPoint); } //Util.SortByX(Walls); }
/*public void RotateRightToLeft() { Queue<MapTile> tileQueue = new Queue<MapTile>(); for (int y = 0; y < Height; y++) { for (int x = Width / 2; x < Width; x++) { tileQueue.Enqueue(Tiles[y, x]); } } for (int y = Height - 1; y >= 0; y--) { for (int x = Width / 2 - 1; x >= 0; x--) { MapTile tile = tileQueue.Dequeue(); Tiles[y, x].Type = tile.Type; Tiles[y, x].Walkable = tile.Walkable; } } }*/ public void RotateTopToBottom() { Queue<MapTile> tileQueue = new Queue<MapTile>(); for (int y = 0; y < Height / 2; y++) { for (int x = 0; x < Width; x++) { tileQueue.Enqueue(Tiles[y, x]); } } for (int y = Height - 1; y >= Height / 2; y--) { for (int x = Width - 1; x >= 0; x--) { MapTile tile = tileQueue.Dequeue(); Tiles[y, x].Type = tile.Type; Tiles[y, x].Walkable = tile.Walkable; } } List<BaseObject> resourcesToAdd = new List<BaseObject>(); for (int i = 0; i < Resources.Count; ) { BaseObject r = Resources[i]; if (r.Y <= Height * TileSize / 2 - r.Height) { BaseObject resource = new BaseObject(new Rectangle(Width * TileSize - r.X - r.Width, Height * TileSize - r.Y - r.Height, r.Width, r.Height)); resource.Type = r.Type; resourcesToAdd.Add(resource); i++; } else Resources.Remove(r); } foreach (BaseObject r in resourcesToAdd) Resources.Add(r); List<BaseObject> startingPointsToAdd = new List<BaseObject>(); for (int i = 0; i < StartingPoints.Count; ) { BaseObject s = StartingPoints[i]; if (s.Y <= Height * TileSize / 2 - s.Height) { BaseObject startingPoint = new BaseObject(new Rectangle(Width * TileSize - s.X - s.Width, Height * TileSize - s.Y - s.Height, s.Width, s.Height)); startingPointsToAdd.Add(startingPoint); i++; } else StartingPoints.Remove(s); } foreach (BaseObject s in startingPointsToAdd) StartingPoints.Add(s); }