138 lines
No EOL
3 KiB
C#
138 lines
No EOL
3 KiB
C#
using System.ComponentModel;
|
|
using System.Drawing;
|
|
|
|
namespace Tetris.Models;
|
|
|
|
public class Tetrominoe
|
|
{
|
|
private Color _color;
|
|
|
|
private Point _coordinates;
|
|
|
|
private readonly Grid _grid;
|
|
|
|
private short _orientation;
|
|
|
|
private readonly bool[,] _shape;
|
|
|
|
public Tetrominoe(Grid grid, bool[,] shape, Point coordinates, short orientation, Color color)
|
|
{
|
|
_grid = grid;
|
|
_shape = shape;
|
|
_coordinates = coordinates;
|
|
_orientation = orientation;
|
|
_color = color;
|
|
// ToDo: UML: Sequand, classe, déploiement
|
|
// ToDo: pb techniques
|
|
// ToDO: ascpect technique test
|
|
}
|
|
|
|
public bool[,] Shape
|
|
{
|
|
get
|
|
{
|
|
var shape = (bool[,])_shape.Clone();
|
|
for (var i = 0; i < _orientation; i++)
|
|
shape = _rotateLeft(shape);
|
|
return shape;
|
|
}
|
|
}
|
|
|
|
public Point Coordinates
|
|
{
|
|
get => _coordinates;
|
|
set
|
|
{
|
|
if (!_grid.CanGo(this, value))
|
|
return;
|
|
_coordinates = value;
|
|
OnPropertyChanged("Coordinates");
|
|
}
|
|
}
|
|
|
|
public short Orientation
|
|
{
|
|
get => _orientation;
|
|
set
|
|
{
|
|
if (value > 3)
|
|
value = 0;
|
|
else if (value < 0)
|
|
value = 3;
|
|
|
|
var oldOrientation = _orientation;
|
|
_orientation = value;
|
|
|
|
var newCoords = _coordinates;
|
|
//ToDo edit position ?
|
|
//ToDo move position when hot a wall
|
|
|
|
if (!_grid.CanGo(this, newCoords))
|
|
_orientation = oldOrientation;
|
|
else
|
|
OnPropertyChanged("Orientation");
|
|
}
|
|
}
|
|
|
|
public Color Color { get; }
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
private bool[,] _rotateLeft(bool[,] shape)
|
|
{
|
|
var rotatedArr = new bool[shape.GetLength(1), shape.GetLength(0)];
|
|
|
|
for (var i = shape.GetLength(0) - 1; i >= 0; --i)
|
|
for (var j = 0; j < shape.GetLength(1); ++j)
|
|
rotatedArr[j, shape.GetLength(0) - 1 - i] = shape[i, j];
|
|
|
|
return rotatedArr;
|
|
}
|
|
|
|
public void RotateLeft()
|
|
{
|
|
Orientation -= 1;
|
|
}
|
|
|
|
public void RotateRight()
|
|
{
|
|
Orientation += 1;
|
|
}
|
|
|
|
public void GoRight()
|
|
{
|
|
Coordinates += new Size(1, 0);
|
|
}
|
|
|
|
public void GoLeft()
|
|
{
|
|
Coordinates -= new Size(1, 0);
|
|
}
|
|
|
|
public void GoDown()
|
|
{
|
|
Coordinates += new Size(0, 1);
|
|
}
|
|
|
|
protected virtual void OnPropertyChanged(string propertyName)
|
|
{
|
|
if (PropertyChanged != null)
|
|
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
var s = "";
|
|
for (uint y = 0; y < Shape.GetLength(1); y++)
|
|
{
|
|
for (uint x = 0; x < Shape.GetLength(0); x++)
|
|
if (Shape[x, y])
|
|
s += "x";
|
|
else
|
|
s += " ";
|
|
s += "\n";
|
|
}
|
|
|
|
return s;
|
|
}
|
|
} |