176 lines
No EOL
5 KiB
C#
176 lines
No EOL
5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Threading;
|
|
using Tetris.Models;
|
|
using Color = System.Drawing.Color;
|
|
using Grid = Tetris.Models.Grid;
|
|
|
|
namespace Tetris.ViewsModels;
|
|
|
|
public class GameViewModel : INotifyPropertyChanged
|
|
{
|
|
public event PropertyChangedEventHandler? PropertyChanged;
|
|
|
|
protected virtual void OnPropertyChanged(string propertyName)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
|
|
public static readonly Game Game = new("...", new Grid(new Color[15, 30]));
|
|
|
|
private const int RendererHertz = 5;
|
|
private const int GameRendererHertz = (1 / RendererHertz) * 1000;
|
|
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;
|
|
private readonly WriteableBitmap _writeableBitmap;
|
|
private readonly int _colorLine = 0xFFFFFF;
|
|
private uint _countDown;
|
|
private string _scoreText = "";
|
|
private string _userNameText = "";
|
|
|
|
public GameViewModel()
|
|
{
|
|
fetchScoreBoard();
|
|
|
|
_writeableBitmap = BitmapFactory.New(_width, _height);
|
|
|
|
var dispatcherRenderTimer = new DispatcherTimer
|
|
{
|
|
Interval = new TimeSpan(0, 0, 0, 0, GameRendererHertz)
|
|
};
|
|
|
|
dispatcherRenderTimer.Tick += Render;
|
|
dispatcherRenderTimer.Start();
|
|
|
|
var dispatcherUpdateTimer = new DispatcherTimer
|
|
{
|
|
Interval = new TimeSpan(0, 0, 0, 0, 200)
|
|
};
|
|
|
|
dispatcherUpdateTimer.Tick += Update;
|
|
dispatcherUpdateTimer.Start();
|
|
}
|
|
|
|
public ImageSource Source => _writeableBitmap;
|
|
|
|
public string ScoreText
|
|
{
|
|
get => _scoreText;
|
|
set
|
|
{
|
|
_scoreText = value;
|
|
OnPropertyChanged("ScoreText");
|
|
}
|
|
}
|
|
|
|
private List<Score> _scoreBoard;
|
|
|
|
public List<Score> ScoreBoard {
|
|
get {
|
|
return _scoreBoard;
|
|
}
|
|
set {
|
|
_scoreBoard = value;
|
|
OnPropertyChanged("ScoreBoard");
|
|
}
|
|
}
|
|
|
|
public string UserNameText
|
|
{
|
|
get => _userNameText;
|
|
set
|
|
{
|
|
_userNameText = value;
|
|
OnPropertyChanged("UserNameText");
|
|
}
|
|
}
|
|
|
|
private void fetchScoreBoard()
|
|
{
|
|
using var db = new ScoreContext();
|
|
List<Score> scores = db.Scores.ToList();
|
|
scores.Sort((x, y) => y.Points.CompareTo(x.Points));
|
|
ScoreBoard = scores;
|
|
}
|
|
|
|
private void Render(object? sender, EventArgs eventArgs)
|
|
{
|
|
_writeableBitmap.Lock();
|
|
_writeableBitmap.Clear(Colors.Black);
|
|
ScoreText = "Score: " + Game.Score;
|
|
UserNameText = "Username: " + Game.UserName;
|
|
|
|
var colorGrid = Game.Grid.CGrid;
|
|
for (var x = 0; x < Game.Grid.MaxGrid.X + 1; x++)
|
|
{
|
|
var startX = x * Multiplier;
|
|
var endX = startX + Multiplier;
|
|
|
|
for (var y = 0; y < Game.Grid.MaxGrid.Y + 1; y++)
|
|
{
|
|
var startY = y * Multiplier;
|
|
var endY = startY + Multiplier;
|
|
|
|
_writeableBitmap.DrawLine(startX, startY, endX, startY, _colorLine);
|
|
_writeableBitmap.DrawLine(startX, startY, startX, endY, _colorLine);
|
|
if (colorGrid[x, y] == Color.Empty) continue;
|
|
_writeableBitmap.FillRectangle(startX, startY, endX, endY, colorGrid[x, y].ToArgb());
|
|
}
|
|
}
|
|
|
|
var tetrominoeWidth = Game.CurrentTetrominoe?.Shape.GetLength(0);
|
|
var tetrominoeHeight = Game.CurrentTetrominoe?.Shape.GetLength(1);
|
|
|
|
for (int x = 0; x < tetrominoeWidth; x++)
|
|
{
|
|
for (int y = 0; y < tetrominoeHeight; y++)
|
|
{
|
|
var currentPiece = Game.CurrentTetrominoe!;
|
|
if (currentPiece.Shape[x, y] == false) continue;
|
|
var color = currentPiece.Color;
|
|
|
|
var startX = (currentPiece.Coordinates.X + x) * Multiplier;
|
|
var startY = (currentPiece.Coordinates.Y + y) * Multiplier;
|
|
_writeableBitmap.FillRectangle(startX, startY, startX + Multiplier, startY + Multiplier, color.ToArgb());
|
|
}
|
|
}
|
|
|
|
_writeableBitmap.Unlock();
|
|
}
|
|
|
|
private void Update(object? sender, EventArgs eventArgs)
|
|
{
|
|
if (!Game.Playing)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Game.CurrentTetrominoe?.GoDown();
|
|
|
|
if (Game.HitBottom())
|
|
{
|
|
if (_countDown >= 3)
|
|
{
|
|
Game.PrintTetrominoe();
|
|
_countDown = 0;
|
|
}
|
|
else
|
|
_countDown++;
|
|
}
|
|
|
|
Game.ClearLine();
|
|
|
|
if (Game.HitTop())
|
|
{
|
|
Game.Playing = false;
|
|
Game.PrintTetrominoe();
|
|
Game.SaveGame();
|
|
fetchScoreBoard();
|
|
}
|
|
}
|
|
} |