2022-04-07 17:24:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2022-04-07 15:30:48 +02:00
|
|
|
|
using System.ComponentModel;
|
2022-04-07 17:24:00 +02:00
|
|
|
|
using System.Drawing;
|
2022-05-04 10:40:15 +02:00
|
|
|
|
using System.Windows.Input;
|
2022-04-07 15:30:48 +02:00
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
using System.Windows.Threading;
|
2022-04-07 17:24:00 +02:00
|
|
|
|
using Tetris.Models;
|
|
|
|
|
using Color = System.Drawing.Color;
|
2022-04-07 15:30:48 +02:00
|
|
|
|
|
|
|
|
|
namespace Tetris.ViewsModels;
|
|
|
|
|
|
|
|
|
|
public class GameViewModel : INotifyPropertyChanged
|
|
|
|
|
{
|
|
|
|
|
public event PropertyChangedEventHandler? PropertyChanged;
|
|
|
|
|
|
|
|
|
|
private const int RendererHertz = 5;
|
|
|
|
|
private const int GameRendererHertz = (1 / RendererHertz) * 1000;
|
2022-05-04 10:40:15 +02:00
|
|
|
|
private const int Width = 25;
|
|
|
|
|
private const int Height = 50;
|
2022-04-07 15:30:48 +02:00
|
|
|
|
private readonly WriteableBitmap _writeableBitmap;
|
|
|
|
|
|
2022-05-04 10:40:15 +02:00
|
|
|
|
private const int GridWidth = 25;
|
|
|
|
|
private const int GridHeight = 50;
|
2022-04-07 17:24:00 +02:00
|
|
|
|
private static readonly Grid Grid = new(new Color[GridWidth, GridHeight]);
|
2022-05-04 10:40:15 +02:00
|
|
|
|
private readonly Tetrominoe _currentTetrominoe = new(Grid, "J", new Point(25 / 2, 0), 0, Color.Aqua);
|
2022-04-07 17:24:00 +02:00
|
|
|
|
|
2022-04-07 15:30:48 +02:00
|
|
|
|
public GameViewModel()
|
|
|
|
|
{
|
2022-05-04 10:40:15 +02:00
|
|
|
|
_writeableBitmap = BitmapFactory.New(Width, Height);
|
2022-04-07 15:30:48 +02:00
|
|
|
|
|
|
|
|
|
var dispatcherRenderTimer = new DispatcherTimer
|
|
|
|
|
{
|
|
|
|
|
Interval = new TimeSpan(0, 0, 0, 0, GameRendererHertz)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dispatcherRenderTimer.Tick += Render;
|
|
|
|
|
dispatcherRenderTimer.Start();
|
|
|
|
|
|
|
|
|
|
var dispatcherUpdateTimer = new DispatcherTimer
|
|
|
|
|
{
|
2022-05-04 10:40:15 +02:00
|
|
|
|
Interval = new TimeSpan(0, 0, 0, 0, 100)
|
2022-04-07 15:30:48 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dispatcherUpdateTimer.Tick += Update;
|
|
|
|
|
dispatcherUpdateTimer.Start();
|
|
|
|
|
}
|
2022-05-04 10:40:15 +02:00
|
|
|
|
|
2022-04-07 15:30:48 +02:00
|
|
|
|
public ImageSource Source => _writeableBitmap;
|
2022-05-04 10:40:15 +02:00
|
|
|
|
public Tetrominoe CurrentTetrominoe => _currentTetrominoe;
|
2022-04-07 15:30:48 +02:00
|
|
|
|
|
|
|
|
|
private void Render(object? sender, EventArgs eventArgs)
|
|
|
|
|
{
|
|
|
|
|
_writeableBitmap.Lock();
|
|
|
|
|
_writeableBitmap.Clear(Colors.Black);
|
|
|
|
|
|
2022-04-07 17:24:00 +02:00
|
|
|
|
for (var x = 0; x < GridWidth; x++)
|
|
|
|
|
{
|
|
|
|
|
for (var y = 0; y < GridHeight; y++)
|
|
|
|
|
{
|
|
|
|
|
var color = Grid.CGrid[x, y];
|
|
|
|
|
_writeableBitmap.SetPixel(x, y, color.R, color.G, color.B);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tetrominoeWidth = _currentTetrominoe.Shape.GetLength(0);
|
|
|
|
|
var tetrominoeHeight = _currentTetrominoe.Shape.GetLength(1);
|
|
|
|
|
|
|
|
|
|
for (int x = 0; x < tetrominoeWidth; x++)
|
|
|
|
|
{
|
|
|
|
|
for (int y = 0; y < tetrominoeHeight; y++)
|
|
|
|
|
{
|
|
|
|
|
if (!_currentTetrominoe.Shape[x, y]) continue;
|
|
|
|
|
var color = _currentTetrominoe.Color;
|
2022-05-04 10:40:15 +02:00
|
|
|
|
_writeableBitmap.SetPixel(_currentTetrominoe.Coordinates.X + x, _currentTetrominoe.Coordinates.Y + y, color.R, color.G, color.B);
|
2022-04-07 17:24:00 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 15:30:48 +02:00
|
|
|
|
_writeableBitmap.Unlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update(object? sender, EventArgs eventArgs)
|
|
|
|
|
{
|
2022-04-07 17:24:00 +02:00
|
|
|
|
_currentTetrominoe.GoDown();
|
2022-04-07 15:30:48 +02:00
|
|
|
|
}
|
2022-04-06 11:30:44 +02:00
|
|
|
|
}
|