diff --git a/Game1/Content/Content.mgcb b/Game1/Content/Content.mgcb index be7215e..0b600c6 100644 --- a/Game1/Content/Content.mgcb +++ b/Game1/Content/Content.mgcb @@ -44,6 +44,18 @@ /processorParam:TextureFormat=Color /build:house.png +#begin Markers.png +/importer:TextureImporter +/processor:TextureProcessor +/processorParam:ColorKeyColor=255,0,255,255 +/processorParam:ColorKeyEnabled=True +/processorParam:GenerateMipmaps=False +/processorParam:PremultiplyAlpha=True +/processorParam:ResizeToPowerOfTwo=False +/processorParam:MakeSquare=False +/processorParam:TextureFormat=Color +/build:Markers.png + #begin Mountain.png /importer:TextureImporter /processor:TextureProcessor diff --git a/Game1/Content/Markers.png b/Game1/Content/Markers.png new file mode 100644 index 0000000..947c42c Binary files /dev/null and b/Game1/Content/Markers.png differ diff --git a/Game1/Content/Tractor.png b/Game1/Content/Tractor.png index 479d069..d0b8c06 100644 Binary files a/Game1/Content/Tractor.png and b/Game1/Content/Tractor.png differ diff --git a/Game1/Game1.cs b/Game1/Game1.cs index b87c073..e9478bc 100644 --- a/Game1/Game1.cs +++ b/Game1/Game1.cs @@ -16,9 +16,11 @@ namespace Game1 private Texture2D[] tile = new Texture2D[5]; private Texture2D tractor; private Texture2D house; + private Texture2D markers; private Tractor tractorUnit = new Tractor(); private Input input = new Input(); private House houseUnit = new House(); + private Vector2 mousePosition; public Game1() @@ -63,6 +65,7 @@ namespace Game1 tractor = Content.Load("Tractor"); Bold = Content.Load("Font"); house = Content.Load("house"); + markers = Content.Load("Markers"); } @@ -77,12 +80,19 @@ namespace Game1 { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); + + + MouseState state = Mouse.GetState(); + mousePosition = new Vector2(state.X, state.Y); + tractorUnit.updateSizing(input, 0, houseUnit.getVector()); //Updates the size tractorUnit.setSpeed(input.changeSpeed(tractorUnit.getSpeed())); //Updates the Simulation Speed tractorUnit.setTractorSpeed(input.changeTractorSpeed(tractorUnit.getTractorSpeed(), tractorUnit.getPos())); //Updates the Tractor Speed input.controlWindowSize(); //Controls the size of the screen depending on the number of tiles houseUnit.updateRectangle(input.getSize(), input.getTileSize(), input.getSpacing()); //Updates the position of the house if the house appears out of bound + + base.Update(gameTime); } @@ -100,21 +110,32 @@ namespace Game1 spriteBatch.Draw(tile[tractorUnit.getFarm().getCrop(i, j).Status], new Rectangle(i * (input.getSpacingTile()), j * (input.getSpacingTile()), input.getTileSize(), input.getTileSize()), Color.White); } } - spriteBatch.Draw(tractor, new Rectangle((int)tractorUnit.getTargetPosition().X / input.getSpacingTile() * (input.getTileSize() + input.getSpacing()) + input.getTileSize() / 4, (int)tractorUnit.getTargetPosition().Y / input.getSpacingTile() * (input.getTileSize() + input.getSpacing()) + input.getTileSize() / 4, input.getTileSize()/2, input.getTileSize()/2), Color.Green); + spriteBatch.Draw(markers, new Rectangle((int)tractorUnit.getTargetPosition().X / input.getSpacingTile() * (input.getTileSize() + input.getSpacing()) + input.getTileSize() / 4, (int)tractorUnit.getTargetPosition().Y / input.getSpacingTile() * (input.getTileSize() + input.getSpacing()) + input.getTileSize() / 4, input.getTileSize()/2, input.getTileSize()/2), Color.Green); for (int i = 0; i < tractorUnit.getPath().getCount() + 1; i++) { - spriteBatch.Draw(tractor, new Rectangle((int)tractorUnit.getPath().getByIndex(i).getCords().X * (input.getSpacingTile()) + input.getTileSize() / 4, (int)tractorUnit.getPath().getByIndex(i).getCords().Y * (input.getSpacingTile()) + input.getTileSize() / 4, input.getTileSize()/2, input.getTileSize()/2), Color.Green); + spriteBatch.Draw(markers, new Rectangle((int)tractorUnit.getPath().getByIndex(i).getCords().X * (input.getSpacingTile()) + input.getTileSize() / 4, (int)tractorUnit.getPath().getByIndex(i).getCords().Y * (input.getSpacingTile()) + input.getTileSize() / 4, input.getTileSize()/2, input.getTileSize()/2), Color.Green); } - spriteBatch.Draw(tractor, new Rectangle((int)tractorUnit.getPos().X, (int)tractorUnit.getPos().Y, input.getTileSize(), input.getTileSize()), Color.White); + + + spriteBatch.Draw(house, houseUnit.GetRectangle(), Color.White); + spriteBatch.Draw(markers, new Rectangle((int)tractorUnit.getPath().getFinalDest().getCords().X * (input.getSpacingTile()) + Convert.ToInt32(input.getTileSize() / 6), (int)tractorUnit.getPath().getFinalDest().getCords().Y * (input.getSpacingTile()) + Convert.ToInt32(input.getTileSize() / 6), Convert.ToInt32(input.getTileSize() / 1.5), Convert.ToInt32(input.getTileSize() / 1.5)), Color.Red); //Draws the current target of the tractor + spriteBatch.Draw(tractor, new Vector2((int)tractorUnit.getPos().X + input.getTileSize() / 2, (int)tractorUnit.getPos().Y + input.getTileSize() / 2), new Rectangle(0, 0, input.getTileSize(), input.getTileSize()), Color.White, tractorUnit.getRotation(), new Vector2(input.getTileSize() / 2, input.getTileSize() / 2), 1.0f, SpriteEffects.None, 1); + + + spriteBatch.DrawString(Bold, "Speed:" + tractorUnit.getSpeed().ToString(), new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 20) , Color.White); //Draws the speed value - spriteBatch.Draw(tractor, new Rectangle((int)tractorUnit.getPath().getFinalDest().getCords().X * (input.getSpacingTile()) + Convert.ToInt32(input.getTileSize() / 6), (int)tractorUnit.getPath().getFinalDest().getCords().Y * (input.getSpacingTile()) + Convert.ToInt32(input.getTileSize() / 6), Convert.ToInt32(input.getTileSize() / 1.5), Convert.ToInt32(input.getTileSize() / 1.5)), Color.Red); //Draws the current target of the tractor + spriteBatch.DrawString(Bold, "Tractor Speed:" + tractorUnit.getTractorSpeed().ToString(), new Vector2(100, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 20), Color.White); spriteBatch.DrawString(Bold, "Tile Size:" + input.getTileSize().ToString() + "pix", new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 40), Color.White); //Draws the tile size - spriteBatch.DrawString(Bold, "Matrix Size: " + input.getSize().X.ToString() + " X " + input.getSize().Y.ToString(), new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 60), Color.White); + spriteBatch.DrawString(Bold, "Matrix Size: " + input.getSize().X.ToString() + " X " + input.getSize().Y.ToString(), new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 60), Color.White);spriteBatch.DrawString(Bold, "Tile Size:" + input.getTileSize().ToString() + "pix", new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 40), Color.White); + spriteBatch.DrawString(Bold, "Tractor Rotation:" + tractorUnit.getRotation().ToString() + " Degrees", new Vector2(250, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 20), Color.White); spriteBatch.DrawString(Bold, tractorUnit.getCurrentTask(), new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 80), Color.White); //Draws the tile size spriteBatch.DrawString(Bold, tractorUnit.getScore().ToString(), new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 100), Color.White); + + spriteBatch.Draw(tractor, new Rectangle((int)mousePosition.X, (int)mousePosition.Y, input.getTileSize() / 4, input.getTileSize() / 4), Color.White); + spriteBatch.End(); base.Draw(gameTime); diff --git a/Game1/Game1.csproj b/Game1/Game1.csproj index 6d43d60..2416809 100644 --- a/Game1/Game1.csproj +++ b/Game1/Game1.csproj @@ -69,6 +69,7 @@ + diff --git a/Game1/Sources/Crops/Crops.cs b/Game1/Sources/Crops/Crops.cs index ccbdcb7..64cc648 100644 --- a/Game1/Sources/Crops/Crops.cs +++ b/Game1/Sources/Crops/Crops.cs @@ -41,7 +41,7 @@ class Crops } else if (Status == 3) { - return 20; + return 15; } else { diff --git a/Game1/Sources/Objects/Tractor.cs b/Game1/Sources/Objects/Tractor.cs index 0eb1c83..97a6813 100644 --- a/Game1/Sources/Objects/Tractor.cs +++ b/Game1/Sources/Objects/Tractor.cs @@ -9,7 +9,7 @@ class Tractor - private int Spacing, sizeTile, Speed = 1; + private int Spacing, sizeTile, Speed = 1, Rotation = 180, rotationSpeed = 5; private float tractorSpeed = 1; private String currentTask; @@ -46,6 +46,7 @@ class Tractor private void updateDirection(Vector2 Size, Vector2 newPosition) { Vector2 DeltaPosition = TargetPosition - Position; + if (DeltaPosition.X == 0) { if (DeltaPosition.Y == 0) @@ -54,20 +55,20 @@ class Tractor } else if (DeltaPosition.Y > 0) { - Direction = new Vector2(0, 1) * tractorSpeed; + updateRotation(0); } else if (DeltaPosition.Y < 0) { - Direction = new Vector2(0, -1) * tractorSpeed; + updateRotation(1); } } else if (DeltaPosition.X > 0) { - Direction = new Vector2(1, 0) * tractorSpeed; + updateRotation(2); } else if (DeltaPosition.X < 0) { - Direction = new Vector2(-1, 0) * tractorSpeed; + updateRotation(3); } } @@ -79,8 +80,8 @@ class Tractor farm.updateSize(Size, sizeTile, Spacing); for (int i = 0; i < Speed; i++) //Where all the choices the tractor does comes from { - smartTractor.updateMap(Position, housePos, farm.getCrops(), Size, sizeTile, Spacing, scoreSystem.getScore()); - Position = Position + Direction; + smartTractor.updateMap(Position, housePos, farm.getCrops(), Size, sizeTile, Spacing, scoreSystem.getScore(), Rotation); + updateDirection(Size, Position); } @@ -120,7 +121,121 @@ class Tractor } } + private void updateRotation(int Destination) + { + if (Destination == 0) + { + if (Rotation == 0) + { + Direction = new Vector2(0, 1) * tractorSpeed; + Position = Position + Direction; + } + else + { + if (Rotation == 0) + { + //Do nothing + } + else if (Rotation > 180) + { + if (Rotation >= 360) + { + Rotation = 0; + } + Rotation = Rotation + rotationSpeed; + } + else if (Rotation <= 180 && Rotation > 0) + { + Rotation = Rotation - rotationSpeed; + } + } + } + else if (Destination == 1) + { + if (Rotation == 180) + { + Direction = new Vector2(0, -1) * tractorSpeed; + Position = Position + Direction; + } + else + { + if (Rotation == 180) + { + //Do nothing + } + else if (Rotation >= 0 && Rotation < 180) + { + Rotation = Rotation + rotationSpeed; + } + else if (Rotation < 360 && Rotation > 180) + { + Rotation = Rotation - rotationSpeed; + } + } + } + else if (Destination == 2) + { + if (Rotation == 270) + { + Direction = new Vector2(1, 0) * tractorSpeed; + Position = Position + Direction; + } + else + { + if (Rotation == 270) + { + //Do nothing + } + else if (Rotation > 90 && Rotation < 270) + { + Rotation = Rotation + rotationSpeed; + } + else if (Rotation < 90 || Rotation < 360) + { + if (Rotation <= 0) + { + Rotation = 360; + } + Rotation = Rotation - rotationSpeed; + } + } + } + else if (Destination == 3) + { + if (Rotation == 90) + { + Direction = new Vector2(-1, 0) * tractorSpeed; + Position = Position + Direction; + } + else + { + if (Rotation == 90) + { + //Do nothing + } + else if ( Rotation < 270 && Rotation > 90) + { + Rotation = Rotation - rotationSpeed; + } + else if (Rotation >= 0 || Rotation > 270) + { + if (Rotation >= 360) + { + Rotation = 0; + } + Rotation = Rotation + rotationSpeed; + } + } + + } + } + + public float getRotation() + { + return MathHelper.ToRadians(Rotation); + + } public Vector2 getPos() { diff --git a/Game1/Sources/Pathing/A-Star/Astar.cs b/Game1/Sources/Pathing/A-Star/Astar.cs index a089a44..808129f 100644 --- a/Game1/Sources/Pathing/A-Star/Astar.cs +++ b/Game1/Sources/Pathing/A-Star/Astar.cs @@ -14,15 +14,16 @@ class Astar private Vector2 Size; private PriorityQueue allPaths; private Vector2 targetPos; + private int Rotation; - public void update(Crops[,] newCrops, Vector2 newSize, Vector2 newTractorPos, Vector2 newHousePos, Vector2 newtargetPos) + public void update(Crops[,] newCrops, Vector2 newSize, Vector2 newTractorPos, Vector2 newHousePos, Vector2 newtargetPos, int rotation) { tractorPos = new Vector2((int)newTractorPos.X, (int)newTractorPos.Y); housePos = new Vector2((int)newHousePos.X, (int)newHousePos.Y); targetPos = newtargetPos; crops = newCrops; Size = newSize; - + Rotation = rotation; } public Nodes getOptimalPath() @@ -30,7 +31,7 @@ class Astar return allPaths.Peek(); } - private List GetAdjacentNodes(Vector2 currentPos) + private List GetAdjacentNodes(Vector2 currentPos) { var adjacentNodes = new List() @@ -42,7 +43,7 @@ class Astar }; //check if out of range - for (int i = 3; i >=0; i--) + for (int i = 3; i >= 0; i--) { if (adjacentNodes[i].getCords().X < 0 || adjacentNodes[i].getCords().Y < 0) adjacentNodes.Remove(adjacentNodes[i]); @@ -68,12 +69,26 @@ class Astar if (currDir == newDir) return 0; else if (Math.Abs(currDir - newDir) == 1 || Math.Abs(currDir - newDir) == 3) - return 1; + return 3; else if (Math.Abs(currDir - newDir) == 0 || Math.Abs(currDir - newDir) == 2) //its for turning back, so it never should happen anyway, remove this check then? - return 10; + return 9; return 0; } + public int ConvertRotation() + { + int rotation = 0; + if (Rotation == 180) + rotation = 0; + else if (Rotation == 270) + rotation = 1; + else if (Rotation == 0) + rotation = 2; + else if (Rotation == 90) + rotation = -1; + return rotation; + } + public Path FindPath() { Path path = new Path(); @@ -82,10 +97,11 @@ class Astar //PriorityQueue closedList = new PriorityQueue(); MinHeap closedList = new MinHeap(); Nodes target = new Nodes(targetPos); - Nodes startPos = new Nodes (tractorPos); + int direction = ConvertRotation(); + Nodes startPos = new Nodes(tractorPos, direction); Nodes current = null; - int direction = 6; int g = 0; + //int direction = SmartTractor.update //openList.Enqueue(startPos); openList.Insert(startPos); @@ -103,7 +119,7 @@ class Astar direction = current.getDirection(); if (current.getCords() == target.getCords()) - break; + break; //if (closedList.Exists(target.getCords())) @@ -111,14 +127,14 @@ class Astar var adjacentNodes = GetAdjacentNodes(current.getCords()); //g++; - foreach(var adjacentNode in adjacentNodes) + foreach (var adjacentNode in adjacentNodes) { if (closedList.Exists(adjacentNode.getCords())) continue; g = current.getG() + crops[(int)adjacentNode.getCords().X, (int)adjacentNode.getCords().Y].getCostOnMovement() + CalculateRotationCost(direction, adjacentNode.getDirection()); if (!(openList.Exists(adjacentNode.getCords()))) { - + adjacentNode.setG(g); adjacentNode.setH(ComputeHScore(adjacentNode.getCords(), target.getCords())); adjacentNode.calculateF(); @@ -128,7 +144,7 @@ class Astar } else { - if(g + adjacentNode.getH() < adjacentNode.getF()) + if (g + adjacentNode.getH() < adjacentNode.getF()) { adjacentNode.setG(g); adjacentNode.calculateF(); @@ -154,4 +170,4 @@ class Astar return path; } -} \ No newline at end of file +} \ No newline at end of file diff --git a/Game1/Sources/Pathing/A-Star/PathSaver/Nodes.cs b/Game1/Sources/Pathing/A-Star/PathSaver/Nodes.cs index 7413965..76450ad 100644 --- a/Game1/Sources/Pathing/A-Star/PathSaver/Nodes.cs +++ b/Game1/Sources/Pathing/A-Star/PathSaver/Nodes.cs @@ -27,7 +27,7 @@ class Nodes Direction = direction; } - public Nodes (Nodes node) + public Nodes(Nodes node) { F = node.F; G = node.G; @@ -94,4 +94,4 @@ class Nodes { return Direction; } -} +} \ No newline at end of file diff --git a/Game1/Sources/Smart/SmartTractor.cs b/Game1/Sources/Smart/SmartTractor.cs index ea35145..fa7112c 100644 --- a/Game1/Sources/Smart/SmartTractor.cs +++ b/Game1/Sources/Smart/SmartTractor.cs @@ -15,6 +15,7 @@ class SmartTractor private int Spacing; private Random r = new Random(); private Astar astar = new Astar(); + private int Rotation; //What to do next @@ -30,7 +31,7 @@ class SmartTractor //To the fields getTargetPosition(r.Next(0, (int)Size.X), r.Next(0, (int)Size.Y)); } - astar.update(crops, Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Target/(tileSize+Spacing)); + astar.update(crops, Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Target/(tileSize+Spacing), Rotation); //astar.FindPath(); return astar.FindPath(); } @@ -41,7 +42,7 @@ class SmartTractor //Updates the variables every frame - public void updateMap(Vector2 newTractorPos, Vector2 newHousePos, Crops[,] newCropsStatus, Vector2 newSize, int newTileSize, int newSpacing, int newScore) + public void updateMap(Vector2 newTractorPos, Vector2 newHousePos, Crops[,] newCropsStatus, Vector2 newSize, int newTileSize, int newSpacing, int newScore, int rotation) { crops = newCropsStatus; housePos = newHousePos; @@ -50,8 +51,8 @@ class SmartTractor tileSize = newTileSize; Spacing = newSpacing; Score = newScore; + Rotation = rotation; - } private void getTargetPosition(int x, int y)