Skip to content

Commit

Permalink
feat: show next piece
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanJosipovic committed Jan 19, 2024
1 parent e5ef1fc commit c03c716
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 5 deletions.
73 changes: 68 additions & 5 deletions src/AvaloniaTetris/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,22 @@ public Game()
Positions[x, y] = new GridPoint();
}
}

for (int y = 0; y <= 1; y++)
{
for (int x = 0; x <= 3; x++)
{
NextPiecePositions[x, y] = new GridPoint();
}
}
}

[ObservableProperty]
private GridPoint[,] _positions = new GridPoint[10,20];

[ObservableProperty]
private GridPoint[,] _nextPiecePositions = new GridPoint[4, 2];

[ObservableProperty]
private int _level;

Expand All @@ -43,34 +54,50 @@ public Game()

private Piece? activePiece;

private Piece? nextPiece;

private void Timer_Tick(object? sender, EventArgs e)
{
MoveDown();
}

readonly Random randomPiece = new();

private void AddNewPiece()
private Piece GetRandom()
{
// Pick random Piece
Piece? newPiece = randomPiece.Next(1, 6) switch
{
1 => new Straight(),
2 => new Square(),
3 => new T(),
4 => randomPiece.Next(1,3) switch
4 => randomPiece.Next(1, 3) switch

Check warning on line 74 in src/AvaloniaTetris/Game.cs

View workflow job for this annotation

GitHub Actions / Create Release

The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '0' is not covered.
{
1 => new L1(),
2=> new L2(),
} ,
2 => new L2(),
},
5 => randomPiece.Next(1, 3) switch

Check warning on line 79 in src/AvaloniaTetris/Game.cs

View workflow job for this annotation

GitHub Actions / Create Release

The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '0' is not covered.
{
1 => new S1(),
2 => new S2(),
},
_ => throw new Exception(),
};
activePiece = newPiece;

return newPiece;
}

private void AddNewPiece()
{
if (nextPiece == null)
{
nextPiece = GetRandom();
}

activePiece = nextPiece;

nextPiece = GetRandom();
SetNextPiece();
}

private void ClearActivePiece()
Expand Down Expand Up @@ -115,6 +142,42 @@ private void SetActivePiece(bool isActive = true)
}
}

private void SetNextPiece()
{
int shape = 0;
if (nextPiece is Straight)
{
shape = 1;
}
else if (nextPiece is Square)
{
shape = 2;
}
else if (nextPiece is S1 or S2)
{
shape = 3;
}
else if (nextPiece is T)
{
shape = 4;
}
else if (nextPiece is L1 or L2)
{
shape = 5;
}

foreach (var item in NextPiecePositions)
{
item.Type = 0;
}

foreach (var point in nextPiece.GetUsedCoords(-nextPiece.X, -nextPiece.Y))

Check warning on line 174 in src/AvaloniaTetris/Game.cs

View workflow job for this annotation

GitHub Actions / Create Release

Dereference of a possibly null reference.
{
var pos = NextPiecePositions[(int)point.X, (int)point.Y];
pos.Type = shape;
}
}

private void RemoveFullRow()
{
//Check if a row needs to be removed
Expand Down
12 changes: 12 additions & 0 deletions src/AvaloniaTetris/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace AvaloniaTetris.ViewModels;
public partial class MainViewModel : ViewModelBase
{
public IEnumerable<GridPoint> OrderedPoints => OrderPoints();
public IEnumerable<GridPoint> OrderedNextPoints => OrderNextPoints();

public Game Game { get; set; } = new Game();

Expand All @@ -25,4 +26,15 @@ private IEnumerable<GridPoint> OrderPoints()
}
}
}

private IEnumerable<GridPoint> OrderNextPoints()
{
for (int y = 1; y >= 0; y--)
{
for (int x = 0; x <= 3; x++)
{
yield return Game.NextPiecePositions[x, y];
}
}
}
}
24 changes: 24 additions & 0 deletions src/AvaloniaTetris/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,30 @@
<Button Command="{Binding Game.MoveDownCommand}" HotKey="Down">↓</Button>
<Button Command="{Binding Game.MoveRightCommand}" HotKey="Right">→</Button>
</StackPanel>

<TextBlock></TextBlock>

<TextBlock>Next Piece</TextBlock>
<ItemsControl ItemsSource="{Binding OrderedNextPoints}" Padding="2" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Height="30"
Width="30"
BorderThickness="1"
BorderBrush="White"
CornerRadius="4"
BoxShadow="inset 1 1 1 1 DarkGray"
Background="{Binding Type, Converter={StaticResource intToColorConverter}}"
>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="2" Columns="4" HorizontalAlignment="Left" VerticalAlignment="Top" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>

</Grid>
Expand Down

0 comments on commit c03c716

Please sign in to comment.