Czoko_Smieciarka/Trunk/Interface/CzokoŚmieciarka.WPF/Models/Board.cs

100 lines
3.4 KiB
C#
Raw Normal View History

2019-03-13 16:45:38 +01:00
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
2019-03-13 16:45:38 +01:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using CzokoŚmieciarka.DataModels.GeneralModels.Models;
using CzokoŚmieciarka.DataModels.Models;
2019-03-13 16:45:38 +01:00
namespace CzokoŚmieciarka.WPF.Models
{
public class Board
{
int _rows;
int _columns;
List<Tile> _tiles = new List<Tile>();
private GarbageCollectorWPF garbageCollectorWPF = new GarbageCollectorWPF(new Coords(0,0), AppDomain.CurrentDomain.BaseDirectory + @"..\..\Images\garbageCollector.png");
2019-03-13 16:45:38 +01:00
public Board(int rows, int columns, List<IObject> objects, GarbageCollector garbageCollector)
2019-03-13 16:45:38 +01:00
{
_rows = rows;
_columns = columns;
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < columns; c++)
{
Road road = new Road(AppDomain.CurrentDomain.BaseDirectory + @"..\..\Images\intersection.png");
2019-03-13 16:45:38 +01:00
_tiles.Add(new Tile()
{
Data = string.Format(""),
Object = road
2019-03-13 16:45:38 +01:00
});
}
}
2019-03-20 12:34:03 +01:00
foreach(var item in objects)
2019-03-20 12:34:03 +01:00
{
_tiles[item.Location.X + item.Location.Y * _columns].Object = item;
2019-03-20 12:34:03 +01:00
}
garbageCollectorWPF.Move(_columns, garbageCollector);
_tiles[garbageCollectorWPF.Location.X + garbageCollectorWPF.Location.Y].Object.Image = MergedBitmaps(
new Bitmap(_tiles[garbageCollectorWPF.Location.X + garbageCollectorWPF.Location.Y].Object.ImagePath),
new Bitmap(garbageCollectorWPF.ImagePath));
2019-03-13 16:45:38 +01:00
}
public Board BoardRefresh(List<IObject> objects, GarbageCollector garbageCollector)
{
foreach (var item in objects)
{
_tiles[item.Location.X + item.Location.Y * _columns].Object = item;
}
garbageCollectorWPF.Move(_columns, garbageCollector);
_tiles[garbageCollectorWPF.Location.X + garbageCollectorWPF.Location.Y].Object.Image = MergedBitmaps(
new Bitmap(_tiles[garbageCollectorWPF.Location.X + garbageCollectorWPF.Location.Y].Object.ImagePath),
new Bitmap(garbageCollectorWPF.ImagePath));
return this;
}
2019-03-13 16:45:38 +01:00
public int Rows
{
get { return _rows; }
set { _rows = value; }
}
public int Columns
{
get { return _columns; }
set { _columns = value; }
}
public List<Tile> Tiles
{
get { return _tiles; }
set { _tiles = value; }
}
private ImageBrush MergedBitmaps(Bitmap bmp1, Bitmap bmp2)
{
using (Graphics g = Graphics.FromImage(bmp1))
{
g.DrawImage(bmp2, new Point(0,0));
}
MemoryStream ms = new MemoryStream();
((System.Drawing.Bitmap)bmp1).Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
BitmapImage image = new BitmapImage();
image.BeginInit();
ms.Seek(0, SeekOrigin.Begin);
image.StreamSource = ms;
image.EndInit();
ImageBrush result = new ImageBrush(image);
return result;
}
2019-03-13 16:45:38 +01:00
}
}