221 lines
8.2 KiB
C#
221 lines
8.2 KiB
C#
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;
|
|
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using Microsoft.Xna.Framework.Input;
|
|
using System;
|
|
using System.CodeDom;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using CzokoŚmieciarka.MonoGameView.Algorithms;
|
|
using MonoGameView.DataModels;
|
|
using MonoGameView.DataModels.Models;
|
|
using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models;
|
|
using CzokoŚmieciarka.MonoGameView.DataModels.Enums;
|
|
using System.Threading.Tasks;
|
|
using System.Threading;
|
|
using MonoGameView.Algorithms;
|
|
|
|
namespace CzokoŚmieciarka.MonoGameView
|
|
{
|
|
/// <summary>
|
|
/// This is the main type for your game.
|
|
/// </summary>
|
|
public class Game1 : Game
|
|
{
|
|
private static int size;
|
|
GraphicsDeviceManager graphics;
|
|
SpriteBatch spriteBatch;
|
|
private SpriteFont font;
|
|
private int score = 0;
|
|
MapLoader mapLoader = new MapLoader();
|
|
Vector2 roadPos;
|
|
float timer;
|
|
const float TIMER = 0.1f;
|
|
int stepN;
|
|
List<IStep> steps;
|
|
GarbageCollector collector;
|
|
IDrawables[,] grid;
|
|
Displayer displayer = new Displayer();
|
|
|
|
|
|
public Game1()
|
|
{
|
|
graphics = new GraphicsDeviceManager(this);
|
|
Content.RootDirectory = "Content";
|
|
graphics.PreferredBackBufferWidth =1400;
|
|
graphics.PreferredBackBufferHeight =1000;
|
|
}
|
|
|
|
|
|
|
|
/// <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()
|
|
{
|
|
|
|
ImageContainer.InitContainer(Content);
|
|
// TODO: Add your initialization logic here
|
|
timer = 1f;
|
|
|
|
mapLoader.Load(out size,out grid,"mapa4.xml");
|
|
var containers = new List<GarbageCollectorContainer>()
|
|
{
|
|
new GarbageCollectorContainer(
|
|
new TypeOfGarbage(GarbageType.Glass,1),
|
|
1000,
|
|
new BasicGarbage(new TypeOfGarbage(GarbageType.Glass,1),0)
|
|
),
|
|
new GarbageCollectorContainer(
|
|
new TypeOfGarbage(GarbageType.Paper,1),
|
|
1000,
|
|
new BasicGarbage(new TypeOfGarbage(GarbageType.Paper,1),0)
|
|
),
|
|
new GarbageCollectorContainer(
|
|
new TypeOfGarbage(GarbageType.Organic,1),
|
|
1000,
|
|
new BasicGarbage(new TypeOfGarbage(GarbageType.Organic,1),0)
|
|
),
|
|
new GarbageCollectorContainer(
|
|
new TypeOfGarbage(GarbageType.PlasticMetal,1),
|
|
1000,
|
|
new BasicGarbage(new TypeOfGarbage(GarbageType.PlasticMetal,1),0)
|
|
),
|
|
|
|
};
|
|
collector = new GarbageCollector(new Coords(1,1), containers, size, size,0);
|
|
|
|
|
|
stepN = 0;
|
|
var dfs = new BestFirstSearch(collector,grid);
|
|
var r = dfs.BestPath(Content, collector, grid);
|
|
steps = r.Item1;
|
|
Console.WriteLine(r);
|
|
//displayer.Add("Count", r.Value.ToString());
|
|
//new Thread(delegate() {
|
|
// var x = dfs.BestPath(Content, collector, grid);
|
|
|
|
//}).Start();
|
|
|
|
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);
|
|
font = Content.Load<SpriteFont>("arial");
|
|
|
|
// 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();
|
|
// 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();
|
|
|
|
var elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
|
|
timer -= elapsed;
|
|
if (timer<0)
|
|
{
|
|
timer = TIMER;
|
|
if (steps.Any())
|
|
{
|
|
steps.First().Invoke(collector, grid);
|
|
steps.RemoveAt(0);
|
|
}
|
|
}
|
|
|
|
// TODO: Add your update logic here
|
|
var barsDictionary = new Dictionary<string, int>();
|
|
var collectorDictionary = new Dictionary<string, string>
|
|
{
|
|
{"Garbage Collector Info", ""}
|
|
};
|
|
foreach (GarbageCollectorContainer container in collector.TrashContainers)
|
|
{
|
|
collectorDictionary.Add(container.TypeOfGarbage.GarbageType.ToString(), container.Garbage.Weight.ToString());
|
|
barsDictionary.Add(container.TypeOfGarbage.GarbageType.ToString(), (int) Math.Round(container.FillPercent * 100));
|
|
}
|
|
|
|
displayer.info = collectorDictionary;
|
|
displayer.barsPercent = barsDictionary;
|
|
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();
|
|
|
|
for (int x = 0; x < size; x++)
|
|
{
|
|
for (int y = 0; y < size; y++)
|
|
{
|
|
grid[x, y].Draw(spriteBatch, size, graphics.PreferredBackBufferHeight);
|
|
}
|
|
}
|
|
collector.Draw(spriteBatch, size, graphics.PreferredBackBufferHeight);
|
|
Display(displayer.info, displayer.barsPercent);
|
|
|
|
spriteBatch.End();
|
|
// TODO: Add your drawing code here
|
|
|
|
base.Draw(gameTime);
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="info"></param>
|
|
public void Display(Dictionary<string, string> info, Dictionary<string,int> barInfo)
|
|
{
|
|
int width = graphics.PreferredBackBufferHeight;
|
|
int x = width + width/50;
|
|
int y = width/50;
|
|
foreach (KeyValuePair<string, string> item in info)
|
|
{
|
|
if (barInfo.ContainsKey(item.Key))
|
|
{
|
|
var bar = barInfo[item.Key];
|
|
spriteBatch.Draw(ImageContainer.GetImage(item.Key + "Bar"), new Rectangle(x, y, bar * width / 500, width / 50), Color.White);
|
|
y += width / 50;
|
|
}
|
|
spriteBatch.DrawString(font, item.Key, new Vector2(x, y), Color.Black);
|
|
y += width*2/100;
|
|
spriteBatch.DrawString(font, item.Value, new Vector2(x, y), Color.White);
|
|
y += width*3/50;
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|