2020-04-07 17:50:31 +02:00
using Microsoft.Xna.Framework ;
using Microsoft.Xna.Framework.Graphics ;
using Microsoft.Xna.Framework.Input ;
2020-04-08 20:04:31 +02:00
using System ;
2020-04-07 17:50:31 +02:00
namespace Game1
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics ;
SpriteBatch spriteBatch ;
SpriteFont Bold ;
2020-05-03 13:05:05 +02:00
private Texture2D [ ] tile = new Texture2D [ 5 ] ;
2020-05-05 16:27:45 +02:00
private Texture2D [ ] Crops = new Texture2D [ 5 ] ;
2020-04-07 17:50:31 +02:00
private Texture2D tractor ;
2020-04-08 20:04:31 +02:00
private Texture2D house ;
2020-05-04 01:17:10 +02:00
private Texture2D markers ;
2020-05-05 16:27:45 +02:00
private Texture2D mouseCursor ;
private Texture2D ProgressionBar ;
private Texture2D ProgressionBarStatus ;
2020-04-07 17:50:31 +02:00
private Tractor tractorUnit = new Tractor ( ) ;
private Input input = new Input ( ) ;
2020-04-08 20:04:31 +02:00
private House houseUnit = new House ( ) ;
2020-05-05 16:27:45 +02:00
private Rectangle mousePosition ;
MouseState state ;
2020-04-07 17:50:31 +02:00
public Game1 ( )
{
graphics = new GraphicsDeviceManager ( this ) ;
Content . RootDirectory = "Content" ;
}
protected override void Initialize ( )
{
// TODO: Add your initialization logic here
base . Initialize ( ) ;
2020-04-08 20:04:31 +02:00
//Generates the map with some random values
2020-05-05 16:09:47 +02:00
input . init ( graphics , new Vector2 ( 32 , 16 ) , 56 , 1 ) ; //Generates the starting size
2020-05-03 13:05:05 +02:00
houseUnit . init ( input . getTileSize ( ) , input . getSpacing ( ) ) ; //Generates the house position
tractorUnit . init ( houseUnit . GetRectangle ( ) , input ) ; //Generates the Tractor
tractorUnit . updateSizing ( input , 0 , houseUnit . getVector ( ) ) ; //Updates the first Size of the Tractor
tractorUnit . setPos ( houseUnit . getVector ( ) ) ; //Changes the position of the tractor to the houses position at the start
2020-04-08 20:04:31 +02:00
2020-04-07 17:50:31 +02:00
graphics . PreferredBackBufferWidth = ( input . getTileSize ( ) + input . getSpacing ( ) ) * ( int ) input . getSize ( ) . X ;
graphics . PreferredBackBufferHeight = ( input . getTileSize ( ) + input . getSpacing ( ) ) * ( int ) input . getSize ( ) . Y + 125 ;
graphics . ApplyChanges ( ) ;
}
protected override void LoadContent ( )
2020-04-08 20:04:31 +02:00
{
2020-04-07 17:50:31 +02:00
spriteBatch = new SpriteBatch ( GraphicsDevice ) ;
2020-04-08 20:04:31 +02:00
//Loads the PNG content and Fonts
2020-05-03 13:05:05 +02:00
tile [ 0 ] = Content . Load < Texture2D > ( "Mountain" ) ;
tile [ 1 ] = Content . Load < Texture2D > ( "tileunplantable" ) ;
tile [ 2 ] = Content . Load < Texture2D > ( "Plantable" ) ;
tile [ 3 ] = Content . Load < Texture2D > ( "Planted" ) ;
tile [ 4 ] = Content . Load < Texture2D > ( "Crop" ) ;
2020-05-05 16:27:45 +02:00
Crops [ 0 ] = Content . Load < Texture2D > ( "CarrotIcon" ) ;
Crops [ 1 ] = Content . Load < Texture2D > ( "WheatIcon" ) ;
Crops [ 2 ] = Content . Load < Texture2D > ( "BerriesIcon" ) ; //Replace 2 and 3 with the new crop Textures
Crops [ 3 ] = Content . Load < Texture2D > ( "TreePlantationIcon" ) ;
ProgressionBar = Content . Load < Texture2D > ( "ProgressionBar" ) ;
ProgressionBarStatus = Content . Load < Texture2D > ( "ProgressionBarStatus" ) ;
2020-04-07 17:50:31 +02:00
tractor = Content . Load < Texture2D > ( "Tractor" ) ;
2020-05-05 16:27:45 +02:00
mouseCursor = Content . Load < Texture2D > ( "MouseCursor" ) ;
2020-04-07 17:50:31 +02:00
Bold = Content . Load < SpriteFont > ( "Font" ) ;
2020-04-08 20:04:31 +02:00
house = Content . Load < Texture2D > ( "house" ) ;
2020-05-04 01:17:10 +02:00
markers = Content . Load < Texture2D > ( "Markers" ) ;
2020-04-07 17:50:31 +02:00
}
2020-04-08 20:04:31 +02:00
2020-04-07 17:50:31 +02:00
protected override void UnloadContent ( )
{
}
2020-04-08 20:04:31 +02:00
protected override void Update ( GameTime gameTime ) //updates every 60 seconds
2020-04-07 17:50:31 +02:00
{
if ( GamePad . GetState ( PlayerIndex . One ) . Buttons . Back = = ButtonState . Pressed | | Keyboard . GetState ( ) . IsKeyDown ( Keys . Escape ) )
Exit ( ) ;
2020-05-04 01:17:10 +02:00
2020-05-05 16:27:45 +02:00
state = Mouse . GetState ( ) ;
mousePosition = new Rectangle ( state . X , state . Y , 1 , 1 ) ;
2020-05-04 01:17:10 +02:00
2020-04-08 20:04:31 +02:00
tractorUnit . updateSizing ( input , 0 , houseUnit . getVector ( ) ) ; //Updates the size
2020-05-03 13:05:05 +02:00
tractorUnit . setSpeed ( input . changeSpeed ( tractorUnit . getSpeed ( ) ) ) ; //Updates the Simulation Speed
tractorUnit . setTractorSpeed ( input . changeTractorSpeed ( tractorUnit . getTractorSpeed ( ) , tractorUnit . getPos ( ) ) ) ; //Updates the Tractor Speed
2020-04-08 20:04:31 +02:00
input . controlWindowSize ( ) ; //Controls the size of the screen depending on the number of tiles
2020-05-03 13:05:05 +02:00
houseUnit . updateRectangle ( input . getSize ( ) , input . getTileSize ( ) , input . getSpacing ( ) ) ; //Updates the position of the house if the house appears out of bound
2020-05-05 16:27:45 +02:00
tractorUnit . updateCrops ( ) ;
2020-05-04 01:17:10 +02:00
2020-04-07 17:50:31 +02:00
base . Update ( gameTime ) ;
}
protected override void Draw ( GameTime gameTime ) //Draw Function
{
GraphicsDevice . Clear ( Color . CornflowerBlue ) ;
spriteBatch . Begin ( ) ;
2020-05-05 16:27:45 +02:00
DrawTiles ( ) ;
2020-05-04 01:17:10 +02:00
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 ) ;
2020-05-03 13:05:05 +02:00
for ( int i = 0 ; i < tractorUnit . getPath ( ) . getCount ( ) + 1 ; i + + )
{
2020-05-04 01:17:10 +02:00
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 ) ;
2020-05-03 13:05:05 +02:00
}
2020-04-07 17:50:31 +02:00
2020-05-04 01:17:10 +02:00
2020-04-08 20:04:31 +02:00
spriteBatch . Draw ( house , houseUnit . GetRectangle ( ) , Color . White ) ;
2020-05-04 01:17:10 +02:00
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 ) ;
2020-05-03 13:05:05 +02:00
spriteBatch . DrawString ( Bold , "Speed:" + tractorUnit . getSpeed ( ) . ToString ( ) , new Vector2 ( 10 , input . getSize ( ) . Y * ( input . getTileSize ( ) + input . getSpacing ( ) ) + 20 ) , Color . White ) ; //Draws the speed value
2020-05-04 01:17:10 +02:00
2020-05-03 13:05:05 +02:00
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
2020-05-04 01:17:10 +02:00
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 ) ;
2020-05-05 16:27:45 +02:00
//spriteBatch.DrawString(Bold, tractorUnit.getCurrentTask(), new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 80), Color.White); //Draws the tile size
2020-05-03 13:05:05 +02:00
spriteBatch . DrawString ( Bold , tractorUnit . getScore ( ) . ToString ( ) , new Vector2 ( 10 , input . getSize ( ) . Y * ( input . getTileSize ( ) + input . getSpacing ( ) ) + 100 ) , Color . White ) ;
2020-05-04 01:17:10 +02:00
2020-05-05 16:27:45 +02:00
spriteBatch . Draw ( mouseCursor , new Rectangle ( ( int ) mousePosition . X , ( int ) mousePosition . Y , 14 , 21 ) , Color . White ) ;
2020-05-04 01:17:10 +02:00
2020-04-07 17:50:31 +02:00
spriteBatch . End ( ) ;
base . Draw ( gameTime ) ;
}
2020-05-05 16:27:45 +02:00
public void DrawTiles ( )
{
for ( int i = 0 ; i < input . getSize ( ) . X ; i + + ) //Draw the tiles
{
for ( int j = 0 ; j < input . getSize ( ) . Y ; j + + )
{
Rectangle tilePos = new Rectangle ( i * ( input . getSpacingTile ( ) ) , j * ( input . getSpacingTile ( ) ) , input . getTileSize ( ) , input . getTileSize ( ) ) ;
spriteBatch . Draw ( tile [ tractorUnit . getFarm ( ) . getCrop ( i , j ) . getStatus ( ) ] , tilePos , Color . White ) ;
if ( tilePos . Intersects ( mousePosition ) )
{
spriteBatch . Draw ( tile [ tractorUnit . getFarm ( ) . getCrop ( i , j ) . getStatus ( ) ] , tilePos , Color . FromNonPremultiplied ( 0 , 0 , 20 , 40 ) ) ;
if ( state . LeftButton = = ButtonState . Pressed & & ! tractorUnit . getFarm ( ) . getCrop ( i , j ) . getHousePos ( ) )
{
tractorUnit . setNewHousePos ( new Vector2 ( i , j ) , true ) ;
tractorUnit . setNewHousePos ( houseUnit . getVector ( ) / input . getSpacingTile ( ) , false ) ;
houseUnit . setRectangle ( i , j , input . getTileSize ( ) , input . getSpacing ( ) ) ;
}
}
if ( ( tractorUnit . getFarm ( ) . getCrop ( i , j ) . getStatus ( ) = = 4 ) )
{
spriteBatch . Draw ( ProgressionBar , new Rectangle ( i * ( input . getSpacingTile ( ) ) + input . getTileSize ( ) - input . getTileSize ( ) / 3 , j * ( input . getSpacingTile ( ) ) , input . getTileSize ( ) / 3 , input . getTileSize ( ) ) , Color . White ) ;
spriteBatch . Draw ( ProgressionBarStatus , new Rectangle ( i * ( input . getSpacingTile ( ) ) + input . getTileSize ( ) - input . getTileSize ( ) / 4 , j * ( input . getSpacingTile ( ) ) + input . getTileSize ( ) / 3 , input . getTileSize ( ) / 4 , tractorUnit . getFarm ( ) . getCrop ( i , j ) . getCropTimerBar ( ( input . getTileSize ( ) ) ) + 1 ) , Color . White ) ;
}
if ( tractorUnit . getFarm ( ) . getCrop ( i , j ) . getStatus ( ) ! = 0 & & tractorUnit . getFarm ( ) . getCrop ( i , j ) . getStatus ( ) ! = 1 )
{
spriteBatch . Draw ( Crops [ tractorUnit . getFarm ( ) . getCrop ( i , j ) . getCropType ( ) ] , new Rectangle ( i * ( input . getSpacingTile ( ) ) + input . getTileSize ( ) - input . getTileSize ( ) / 3 , j * ( input . getSpacingTile ( ) ) , input . getTileSize ( ) / 3 , input . getTileSize ( ) / 3 ) , Color . White ) ;
}
}
}
}
2020-04-07 17:50:31 +02:00
}
}