Czoko_Smieciarka/Trunk/MonoGameView/Game1.cs

159 lines
5.5 KiB
C#
Raw Normal View History

using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans;
using CzokoŚmieciarka.MonoGameView.DataModels.Models;
2019-04-22 03:16:30 +02:00
using Microsoft.Xna.Framework;
2019-04-21 02:43:36 +02:00
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
2019-04-22 03:16:30 +02:00
using MonoGameView;
2019-04-21 02:43:36 +02:00
using System;
2019-04-22 03:16:30 +02:00
using System.Collections.Generic;
using System.Linq;
using CzokoŚmieciarka.MonoGameView.Algorithms;
2019-04-21 02:43:36 +02:00
2019-04-22 03:16:30 +02:00
namespace CzokoŚmieciarka.MonoGameView
2019-04-21 02:43:36 +02:00
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
2019-04-22 03:16:30 +02:00
Texture2D road1;
Texture2D road2;
Texture2D grass;
Texture2D house;
2019-04-21 02:43:36 +02:00
Vector2 roadPos;
2019-04-22 03:16:30 +02:00
float timer;
const float TIMER = 0.2f;
int stepN;
List<IStep> steps;
GarbageCollector collector;
object[,] grid = new object[10, 10];
2019-04-21 02:43:36 +02:00
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 500;
graphics.PreferredBackBufferHeight = 500;
}
2019-04-22 03:16:30 +02:00
2019-04-21 02:43:36 +02:00
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
roadPos = new Vector2(0, 0);
2019-04-22 03:16:30 +02:00
timer = 0.2f;
for (int x=0;x<10;x++)
{
for (int y=0;y<10;y++)
{
if (x % 2 == 0 || y % 2 == 0) grid[x, y] = new Road1();
else grid[x, y] = null;
}
}
grid[4, 0] = new House();
grid[6, 4] = new House();
2019-04-22 03:16:30 +02:00
collector = new GarbageCollector(Content,new Coords(0, 0), new List<AGarbageCollectorContainer>(), 10, 10);
stepN = 0;
var dfs = new DFS();
steps = dfs.BestPath(collector, grid);
2019-04-21 02:43:36 +02:00
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
2019-04-22 03:16:30 +02:00
road1 = Content.Load<Texture2D>("road1");
road2 = Content.Load<Texture2D>("road2");
grass = Content.Load<Texture2D>("grass");
house = Content.Load<Texture2D>("house");
2019-04-21 02:43:36 +02:00
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific content.
/// </summary>
protected override void UnloadContent()
{
Content.Unload();
2019-04-21 02:43:36 +02:00
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
2019-04-22 03:16:30 +02:00
var elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
timer -= elapsed;
if (timer<0)
{
timer = TIMER;
if (steps.Any())
{
steps.First().Invoke(collector, grid);
steps.RemoveAt(0);
}
}
2019-04-21 02:43:36 +02:00
// TODO: Add your update logic here
2019-04-22 03:16:30 +02:00
2019-04-21 02:43:36 +02:00
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
2019-04-22 03:16:30 +02:00
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
if (grid[x, y] is Road1) spriteBatch.Draw(road1, new Vector2(x*50,y*50), Color.White);
else if (grid[x,y] is Road2) spriteBatch.Draw(road2, new Vector2(x * 50, y * 50), Color.White);
else if (grid[x, y] is House) spriteBatch.Draw(house, new Vector2(x*50, y*50), Color.White);
2019-04-22 03:16:30 +02:00
else spriteBatch.Draw(grass, new Vector2(x * 50, y * 50), Color.White);
}
}
collector.Draw(spriteBatch);
2019-04-21 02:43:36 +02:00
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}