public DeadObject(Texture2D texture, Vector2 position, IsoGrid grid) { this.tex = texture; this.gridposition = position; this.rect = new Rectangle(0,0,tex.Width, tex.Height); this.position = grid.gridCoords(gridposition) - new Vector2(tex.Width/2.0f,tex.Height); }
public void StartRoute(ModCity city, ModCity homeCity, IsoGrid isogrid, Path path) { // calculate a path for movement running = true; drawPath = path.PathList(homeCity.gridposition + new Vector2(2, 2), city.gridposition + new Vector2(2, 2), isogrid); pathStep = 1; walkingTarget = drawPath[pathStep]; gridPosition = drawPath[0]; position = isogrid.gridCoords( homeCity.position); }
public void Draw(SpriteBatch sbatch, Path path, GraphicsDevice graphicsDevice,IsoGrid isogrid) { if (running == true) { if (drawPaths == true) { path.Draw(graphicsDevice, sbatch, drawPath, Vector2.Zero, isogrid); } sbatch.Draw(tex, position + drawOffset, rect, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.5f); } }
public GridTileRegion(Texture2D texture, Vector2 gridposition,Vector2 region, IsoGrid grid, Random random) { this.tex = texture; this.rect = new Rectangle(0, 0, tex.Width/size, tex.Height/size); this.gridPosition = gridposition; this.region = region; for (int x = 0; x < region.X; x++) { for (int y = 0; y < region.Y; y++) { nums.Add(new Vector2(random.Next(10),random.Next(10))); pos.Add(grid.gridCoords(gridposition + new Vector2(x,y))); } } }
public void Draw(GraphicsDevice graphicsDevice, SpriteBatch sbatch, List<Vector2> path, Vector2 offset, IsoGrid grid) { Texture2D blank = new Texture2D(graphicsDevice, 1, 1, false, SurfaceFormat.Color); blank.SetData(new[] { Microsoft.Xna.Framework.Color.White }); for (int x = 0; x < (path.Count() - 1); x++) { Vector2 startPoint = grid.gridCoords(path[x]); Vector2 endPoint = grid.gridCoords(path[x + 1]); float angle = (float)Math.Atan2(endPoint.Y - startPoint.Y, endPoint.X - startPoint.X); float length = Vector2.Distance(startPoint, endPoint); sbatch.Draw(blank, startPoint + offset, null, Microsoft.Xna.Framework.Color.Blue, angle, Vector2.Zero, new Vector2(length, 3.0f), SpriteEffects.None, 0); } }
public City(string name, Texture2D texture,Texture2D highlightTexture, Texture2D clickedTexture, Vector2 position, IsoGrid grid, Tuple<float,float,float> politicalStanding,List<Ship> shipList, List<Material> materialList, bool playerCity = false) { this.lowTex = texture; this.highTex = highlightTexture; this.clickTex = clickedTexture; this.gridposition = position; this.rect = new Rectangle(0, 0, lowTex.Width, lowTex.Height); this.position = grid.gridCoords(gridposition) - new Vector2(0, lowTex.Height); this.name = name; this.player = playerCity; this.politics = politicalStanding; this.ships = shipList; this.materials = materialList; this.tex = lowTex; }
// The public instance class public ReachableArea(IsoGrid grid, List<ModCity> citiesList) { this.width = (int)grid.size.X; this.height = (int)grid.size.Y; this.reachable = new int[width, height]; for (int x = 0; x < (width); x++) { for (int y = 0; y < (height); y++) { reachable[x, y] = 0; } } this.cityList = citiesList; foreach (ModCity city in cityList) { reachable[(int)city.gridposition.X , (int)city.gridposition.Y ] = 1; } }
public List<Vector2> PathList(Vector2 start, Vector2 end, IsoGrid grid) { Vector2 startPosition = start; Vector2 endPosition = end; Vector2 currentPosition = startPosition; List<Vector2> openList = new List<Vector2>(); // squares to check openList.Add(startPosition); List<Vector2> openListParent = new List<Vector2>(); // shortest path back from a square openListParent.Add(startPosition); List<float> openListF = new List<float>(); // usefulness of a square openListF.Add(0); List<float> openListG = new List<float>(); // distance of a square from start openListG.Add(0); List<Vector2> closedList = new List<Vector2>(); // checked squares List<Vector2> closedParents = new List<Vector2>(); List<Vector2> path = new List<Vector2>(); // list of vectors between points int currentIndex; // --------- path finding loop ------------------------------- if (startPosition != endPosition) { while (currentPosition != endPosition) { // calculate adjacent points Vector2[,] currentAdjacent = Adjacent(currentPosition); currentIndex = openList.IndexOf(currentPosition); // for each square adjacent to the current position... for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { // if the square is on the screen and reachable... if (currentAdjacent[x, y] != currentPosition && currentAdjacent[x, y].X >= 1 && currentAdjacent[x, y].Y >= 1 && currentAdjacent[x, y].X <= grid.size.X && currentAdjacent[x, y].Y <= grid.size.Y && closedList.IndexOf(currentAdjacent[x, y]) < 0) { if (Check((int)currentAdjacent[x, y].X, (int)currentAdjacent[x, y].Y) == true) { // calculate usefulness of the square... Vector2 Cvector = currentAdjacent[x, y] - currentPosition; Vector2 Hvector = currentAdjacent[x, y] - endPosition; double C = Math.Sqrt(Math.Pow((double)Cvector.X,2.0)+ Math.Pow((double)Cvector.Y,2.0)); // distance from current position float S = openListG[currentIndex]; // distance to current position float G = (float)C + S; // distance from start position float H = Hvector.Length(); // direct distance to final position //float H = Math.Abs(Hvector.X) + Math.Abs(Hvector.Y); // 'Manhatten' distance estimate to final position (gives smoother paths) float F = G + H; // usefulness of this square // add to open array if not already present if (openList.IndexOf(currentAdjacent[x, y]) < 0) { openList.Add(currentAdjacent[x, y]); openListParent.Add(currentPosition); openListF.Add(F); openListG.Add(G); } // if present in open array, check if path from current position is shorter if (openList.IndexOf(currentAdjacent[x, y]) >= 0) { int adjacentIndex = openList.IndexOf(currentAdjacent[x, y]); if (G < openListG[adjacentIndex]) { openListParent[adjacentIndex] = currentPosition; openListF[adjacentIndex] = F; openListG[adjacentIndex] = G; } } } } } } // add current psition to closed list closedList.Add(currentPosition); closedParents.Add(openListParent[currentIndex]); // remove current position from open list openList.Remove(currentPosition); openListF.Remove(openListF[currentIndex]); openListG.Remove(openListG[currentIndex]); openListParent.Remove(openListParent[currentIndex]); // find coordinate with smallest F value, set to current position (if there's a path!) if (openListF.Count > 0) { float minF = openListF.Min(); int minFIndex = openListF.IndexOf(minF); currentPosition = openList[minFIndex]; } else { break; } } Vector2 pathCurrentPosition = closedList[closedList.Count - 1]; int pathCurrentIndex = 0; path.Add(endPosition); //////////////// while (pathCurrentPosition != startPosition) { path.Add(pathCurrentPosition); pathCurrentIndex = closedList.IndexOf(pathCurrentPosition); pathCurrentPosition = closedParents[pathCurrentIndex]; } path.Add(startPosition); /////////////// for (int x = 0; x < path.Count; x++) { path[x] = new Vector2(path[x].X, path[x].Y); } } path.Reverse(); return path; }
protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // grid & camera isoGrid = new IsoGrid(GraphicsDevice,Math.PI/6.0,30,new Vector2(100,100)); cameraPosition = isoGrid.cameraCentre(new Vector2(11,12),graphics); // initial camera position! scrollSpeed = 10.0f; // fonts font = Content.Load<SpriteFont>("font"); // cursor cursor = new Cursor(Content.Load<Texture2D>("cursor")); // HUD buttons = new List<Button> { }; // random seed random = new Random(); // spacescape tiles = new GridTileRegion(Content.Load<Texture2D>("starTiles"), new Vector2(-1, 0),new Vector2(100,100), isoGrid,random); // --- game objects ---------- // ships trader = new Ship("Trading Ship", Content.Load<Texture2D>("cargoShip")); transport = new Ship("Transport Ship", Content.Load<Texture2D>("cargoShip")); // materials metal = new Material("Metal"); rock = new Material("Rock"); gems = new Material("Gemstones"); // buildings dwellBlock = new Building("Dwelling Block",Content.Load<Texture2D>("building2"),3,new Vector2(0,0),new Vector2(1,1)); dwellBlockLarge = new Building("Large Dwelling Block",Content.Load<Texture2D>("building5"), 3, new Vector2(0, 0), new Vector2(1, 1)); govTower = new Building("Goverment Central Tower",Content.Load<Texture2D>("building3"), 1, new Vector2(0, 0), new Vector2(3, 3)); hugeGovTower = new Building("Huge Governemnt Central Tower", Content.Load<Texture2D>("hugeGovernmentTower"), 1, new Vector2(0, 0), new Vector2(12, 12)); Factory = new Building("Factory",Content.Load<Texture2D>("building4"), 2, new Vector2(0, 0), new Vector2(2, 1)); Arena = new Building("Entertainment Arena",Content.Load<Texture2D>("building1"), 1, new Vector2(0, 0), new Vector2(2, 2)); // cities modCity = new ModCity("Customisable City", Content.Load<Texture2D>("smallcity"), Content.Load<Texture2D>("smallcityHigh"), Content.Load<Texture2D>("smallcityClick"), new Vector2(20, 3),new Vector2(20, 85),8, isoGrid, new Tuple<float, float, float>(0.33f, 0.33f, 0.34f), new List<Ship> { trader }, new List<Material> { metal }, new List<Tuple<Building, int>> { new Tuple<Building, int>(dwellBlock, 4), new Tuple<Building, int>(dwellBlockLarge, 4), new Tuple<Building, int>(govTower, 1), new Tuple<Building, int>(Arena, 1),new Tuple<Building,int>(Factory,3) }, GraphicsDevice, random); modCity2 = new ModCity("Another Customisable City", Content.Load<Texture2D>("smallcity"), Content.Load<Texture2D>("smallcityHigh"), Content.Load<Texture2D>("smallcityClick"), new Vector2(30, 5),new Vector2(20, 85),8, isoGrid, new Tuple<float, float, float>(0.33f, 0.33f, 0.34f), new List<Ship> { trader }, new List<Material> { metal }, new List<Tuple<Building, int>> { new Tuple<Building, int>(dwellBlock, 4), new Tuple<Building, int>(dwellBlockLarge, 4), new Tuple<Building, int>(govTower, 1), new Tuple<Building, int>(Arena, 1), new Tuple<Building, int>(Factory, 3) }, GraphicsDevice, random); homeModCity = new ModCity("Home Customisable City", Content.Load<Texture2D>("smallcity"), Content.Load<Texture2D>("smallcityHigh"), Content.Load<Texture2D>("smallcityClick"), new Vector2(10, 10),new Vector2(20, 85),8, isoGrid, new Tuple<float, float, float>(0.33f, 0.33f, 0.34f), new List<Ship> { trader }, new List<Material> { metal }, new List<Tuple<Building, int>> { new Tuple<Building, int>(dwellBlock, 4), new Tuple<Building, int>(dwellBlockLarge, 4), new Tuple<Building, int>(govTower, 1), new Tuple<Building, int>(Arena, 1), new Tuple<Building, int>(Factory, 3) }, GraphicsDevice, random, true); hugeCity = new ModCity("Huge City", Content.Load<Texture2D>("hugecity"), Content.Load<Texture2D>("hugecityHigh"), Content.Load<Texture2D>("hugecityClick"), new Vector2(30, 30), new Vector2(-100,280), 90, isoGrid, new Tuple<float, float, float>(0.30f, 0.30f, 0.4f), new List<Ship> { trader }, new List<Material> { metal }, new List<Tuple<Building, int>> {new Tuple<Building, int>(hugeGovTower,1), new Tuple<Building, int>(dwellBlock, 1000), new Tuple<Building, int>(dwellBlockLarge, 400), new Tuple<Building, int>(govTower, 1), new Tuple<Building, int>(Arena, 50), new Tuple<Building, int>(Factory, 300) }, GraphicsDevice, random); fullCity = new ModCity("A Full City", Content.Load<Texture2D>("smallcity"), Content.Load<Texture2D>("smallcityHigh"), Content.Load<Texture2D>("smallcityClick"), new Vector2(10, 15), new Vector2(7, 90), 9, isoGrid, new Tuple<float, float, float>(0.33f, 0.33f, 0.34f), new List<Ship> { trader }, new List<Material> { metal }, new List<Tuple<Building, int>> { new Tuple<Building, int>(dwellBlock, 64)}, GraphicsDevice, random); modCities = new List<ModCity> { modCity, modCity2,homeModCity,hugeCity,fullCity }; universe = new DeadObject(Content.Load<Texture2D>("universe"), new Vector2(90, 94), isoGrid); uiball = new DeadUIObject(Content.Load<Texture2D>("rball"), new Vector2(10, 10)); // accesible region and paths reach = new ReachableArea(isoGrid, modCities); path = new Path(reach); }
public void Update(Cursor cursor, Matrix cameraTransform, List<Button> buttons, GraphicsDevice graphicsDevice, Path path,IsoGrid isogrid, City homeCity) { Vector2 translation = new Vector2(cameraTransform.Translation.X,cameraTransform.Translation.Y); screenPosition = position + translation; if (cursor.position.X >= screenPosition.X && cursor.position.X <= screenPosition.X + rect.Width && cursor.position.Y >= screenPosition.Y && cursor.position.Y <= screenPosition.Y + rect.Height) { tex = highTex; if (cursor.mstate.LeftButton == ButtonState.Pressed) { if (cursor.click == false) { cursor.click = true; clicked = true; if (player == false && tradeDeal == false) { tradeButton = new Button(new Vector2(10, 350), 150, 40, "Trade", graphicsDevice); buttons.Add(tradeButton); } } } } else { tex = lowTex; if (tradeButton != null) { if (tradeButton.pressed == true) { buttons.Clear(); tradeDeal = true; tradeButton = null; ships[0].StartRoute(this,homeCity,isogrid,path); } else if (cursor.mstate.LeftButton == ButtonState.Pressed) { if (cursor.click == false) { cursor.click = true; if (clicked == true) { clicked = false; buttons.Clear(); } } } } else if (cursor.mstate.LeftButton == ButtonState.Pressed) { if (cursor.click == false) { if (clicked == true) { cursor.click = true; clicked = false; buttons.Clear(); } } } } if (clicked == true) { tex = clickTex; } }
public ModCity(string name, Texture2D texture, Texture2D highlightTexture, Texture2D clickedTexture, Vector2 gridposition,Vector2 gridOffset,float size, IsoGrid grid, Tuple<float, float, float> politicalStanding, List<Ship> shipList, List<Material> materialList, List<Tuple<Building, int>> buildingList, GraphicsDevice graphicsDevice,Random random, bool playerCity = false) { this.lowTex = texture; this.highTex = highlightTexture; this.clickTex = clickedTexture; this.gridposition = gridposition; this.rect = new Rectangle(0, 0, lowTex.Width, lowTex.Height); this.position = grid.gridCoords(gridposition) - new Vector2(0, lowTex.Height); this.name = name; this.player = playerCity; this.politics = politicalStanding; this.ships = shipList; this.materials = materialList; this.buildingTypes = buildingList; this.tex = lowTex; this.isogrid = new CityIsoGrid(graphicsDevice, Math.PI / 6.0, 8, size ,position + gridOffset); // fill grid with buildings in random positions List<Vector2> positions = new List<Vector2>{}; // list of possible positions... for (int i=0;i<isogrid.size;i++) { for (int j=0;j<isogrid.size;j++) { if (isogrid.usage[i,j] == 0) { positions.Add(new Vector2(i,j)); } } } List<Vector2> rpositions = new List<Vector2>{}; // randomised list of possible positions... foreach (Vector2 p in positions) { int i = random.Next(rpositions.Count); rpositions.Insert(i,p); } foreach (Tuple<Building, int> buildingType in buildingList) // fit them all in somewhere... { for (int n = 0; n < buildingType.Item2; n++) // for the number in the city... { Vector2 pos = Vector2.Zero; for (int j = 0; j < rpositions.Count; j++) // find a suitable position... { pos = rpositions[j]; bool fit = true; for (int l = 0; l < buildingType.Item1.footprint.X; l ++) { for (int k = 0; k < buildingType.Item1.footprint.Y; k++) { if ((int)pos.X + l >= isogrid.usage.GetLength(0) | (int)pos.Y + k >= isogrid.usage.GetLength(1)) { fit = false; } else if (isogrid.usage[(int)pos.X + l, (int)pos.Y + k] == 1) { fit = false; } } } if (fit == true) { break; } } int type = random.Next(1, buildingType.Item1.variations); buildings.Add(new Tuple<Building, int, Vector2>(buildingType.Item1, type, pos)); for (int x = (int)pos.X; x < buildingType.Item1.footprint.X + (int)pos.X; x++) { for (int y = (int)pos.Y; y < buildingType.Item1.footprint.Y + (int)pos.Y; y++) { isogrid.usage[x, y] = 1; rpositions.Remove(pos + new Vector2(x, y)); } } } } }
public void Update(ModCity homeCity, IsoGrid isogrid, GameTime gametime) { // continuous movement if (running == true) { direction = (isogrid.gridCoords(walkingTarget) - isogrid.gridCoords(gridPosition)); targetDistance = direction.Length(); direction.Normalize(); walkingOffset += direction * gametime.ElapsedGameTime.Milliseconds * 0.02f; distanceGone = walkingOffset.Length(); if (distanceGone < targetDistance) { position = isogrid.gridCoords(gridPosition) + walkingOffset; } else { pathStep += 1; if (pathStep < drawPath.Count) { gridPosition = drawPath[pathStep - 1]; walkingTarget = drawPath[pathStep]; position = isogrid.gridCoords(gridPosition); } else { drawPath.Reverse(); pathStep = 1; walkingTarget = drawPath[pathStep]; gridPosition = drawPath[pathStep - 1]; position = isogrid.gridCoords(gridPosition); } walkingOffset = Vector2.Zero; targetDistance = 0; distanceGone = 0; } } // set direction anim (8 directions!) double dot = Vector2.Dot(new Vector2(0, -1), direction); // dot product of direction with vertical double angle = Math.Acos(dot); // angle from vertical if (angle == 0) { dir = 0; } else if (0 < angle && angle < Math.PI / 2 && direction.X > 0) { dir = 1; } else if (angle == Math.PI / 2 && direction.X > 0) { dir = 2; } else if (Math.PI / 2 < angle && angle < Math.PI && direction.X > 0) { dir = 3; } else if (angle == Math.PI) { dir = 4; } else if (Math.PI / 2 < angle && angle < Math.PI && direction.X < 0) { dir = 5; } else if (angle == Math.PI / 2 && direction.X < 0) { dir = 6; } else if (0 < angle && angle < Math.PI/2 && direction.X < 0) { dir = 7; } if (dir != 0) { } // update animation timer += gametime.ElapsedGameTime.Milliseconds; if (timer >= msecsTweenFrames) { timer = 0; if (currentFrame++ == numberOfFrames - 1) { currentFrame = 0; } } rect.X = currentFrame * rect.Width; rect.Y = dir * rect.Height; }