Archived
1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
Tetris/ViewsModels/GameViewModel.cs

96 lines
3 KiB
C#
Raw Permalink Normal View History

2022-04-07 17:24:00 +02:00

using System;
2022-04-07 15:30:48 +02:00
using System.ComponentModel;
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;
2022-05-04 11:34:46 +02:00
public static readonly Game Game = new("...", new Grid(new Color[20, 40]));
2022-05-04 11:08:04 +02:00
2022-04-07 15:30:48 +02:00
private const int RendererHertz = 5;
private const int GameRendererHertz = (1 / RendererHertz) * 1000;
2022-05-04 11:08:04 +02:00
private const int Multiplier = 10;
private readonly int _width = (Game.Grid.MaxGrid.X + 1) * Multiplier;
private readonly int _height = (Game.Grid.MaxGrid.Y + 1) * Multiplier;
2022-04-07 15:30:48 +02:00
private readonly WriteableBitmap _writeableBitmap;
public GameViewModel()
{
2022-05-04 11:08:04 +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;
private void Render(object? sender, EventArgs eventArgs)
{
_writeableBitmap.Lock();
_writeableBitmap.Clear(Colors.Black);
2022-05-04 11:34:46 +02:00
var colorGrid = Game.Grid.CGrid;
2022-05-04 11:08:04 +02:00
for (var x = 0; x < Game.Grid.MaxGrid.X + 1; x++)
2022-04-07 17:24:00 +02:00
{
2022-05-04 11:08:04 +02:00
for (var y = 0; y < Game.Grid.MaxGrid.Y + 1; y++)
2022-04-07 17:24:00 +02:00
{
2022-05-04 11:34:46 +02:00
if (colorGrid[x, y] == Color.Empty) continue;
var startX = x * Multiplier;
var startY = y * Multiplier;
_writeableBitmap.FillRectangle(startX, startY, startX + Multiplier, startY + Multiplier, colorGrid[x, y].ToArgb());
2022-04-07 17:24:00 +02:00
}
}
2022-05-04 11:08:04 +02:00
var tetrominoeWidth = Game.CurrentTetrominoe?.Shape.GetLength(0);
var tetrominoeHeight = Game.CurrentTetrominoe?.Shape.GetLength(1);
2022-04-07 17:24:00 +02:00
for (int x = 0; x < tetrominoeWidth; x++)
{
for (int y = 0; y < tetrominoeHeight; y++)
{
2022-05-04 11:08:04 +02:00
var currentPiece = Game.CurrentTetrominoe!;
if (currentPiece.Shape[x, y] == false) continue;
var color = currentPiece.Color;
2022-05-04 11:34:46 +02:00
var startX = (currentPiece.Coordinates.X + x) * Multiplier;
var startY = (currentPiece.Coordinates.Y + y) * Multiplier;
_writeableBitmap.FillRectangle(startX, startY, startX + Multiplier, startY + Multiplier, color.ToArgb());
2022-04-07 17:24:00 +02:00
}
}
2022-05-04 11:34:46 +02:00
2022-04-07 15:30:48 +02:00
_writeableBitmap.Unlock();
}
private void Update(object? sender, EventArgs eventArgs)
{
2022-05-04 11:08:04 +02:00
Game.CurrentTetrominoe?.GoDown();
if (Game.HitBottom())
{
Game.PrintTetrominoe();
}
2022-04-07 15:30:48 +02:00
}
2022-04-06 11:30:44 +02:00
}