1
0
Fork 0

feat: add line clear animation

This commit is contained in:
Henrik Hautakoski 2025-10-18 11:28:26 +02:00
parent fdab0a1992
commit db9ec53d35
4 changed files with 112 additions and 20 deletions

View file

@ -16,21 +16,24 @@ import (
)
type GamePlay struct {
shape game.Shape
shape_pos core.Vec2i8
score game.Score
dropTimer core.IntervalTimer
moveTimer core.IntervalTimer
grid game.Grid
nextShape game.Shape
shapeQueue *game.ShapeQueue
r draw.Renderer
shape game.Shape
shape_pos core.Vec2i8
score game.Score
dropTimer core.IntervalTimer
moveTimer core.IntervalTimer
lineClearTimer core.IntervalTimer
grid game.Grid
nextShape game.Shape
shapeQueue *game.ShapeQueue
r draw.Renderer
lineClearAnimation game.LineClearAnimation
}
func NewGamePlay() *GamePlay {
return &GamePlay{
dropTimer: core.NewIntervalTimer(0.3),
moveTimer: core.NewIntervalTimer(0.1),
dropTimer: core.NewIntervalTimer(0.3),
moveTimer: core.NewIntervalTimer(0.1),
lineClearTimer: core.NewIntervalTimer(0.05),
r: draw.Renderer{
Theme: &draw.Theme{
FrameBG: color.RGBA{R: 30, G: 30, B: 46, A: 255},
@ -50,6 +53,7 @@ func (gp *GamePlay) Enter() {
gp.shapeQueue = game.NewShapeQueue()
gp.nextShape = gp.shapeQueue.Next()
gp.SpawnShape()
gp.lineClearAnimation.Reset()
}
func (GamePlay) Exit() {
@ -81,6 +85,13 @@ func (gp *GamePlay) Update(fsm state.Transitioner, delta float32) {
gp.dropTimer.SetInterval(0.3)
}
if !gp.lineClearAnimation.Completed() {
if gp.lineClearTimer.UpdateReset(delta) {
gp.lineClearAnimation.Update(&gp.grid)
}
return
}
if rl.IsKeyPressed(rl.KeyUp) {
rotated := gp.shape.RotateCW()
if !game.CheckShapeCollision(gp.shape_pos, &rotated, &gp.grid) {
@ -107,15 +118,16 @@ func (gp *GamePlay) Update(fsm state.Transitioner, delta float32) {
// Update position if it does not collide
if game.CheckShapeCollision(new_pos, &gp.shape, &gp.grid) {
gp.LockShape()
num_rows := gp.grid.ClearFullRows()
if num_rows > 0 {
audio.Play(assets.SFX_ROW_CLEARED)
gp.score.Lines(num_rows)
}
gp.SpawnShape()
if game.CheckShapeCollision(gp.shape_pos, &gp.shape, &gp.grid) {
fsm.Switch("gameover")
lines := gp.grid.FullRows()
if len(lines) > 0 {
gp.lineClearAnimation.SetLines(lines)
gp.score.Lines(byte(len(lines)))
audio.Play(assets.SFX_ROW_CLEARED)
} else {
if game.CheckShapeCollision(gp.shape_pos, &gp.shape, &gp.grid) {
fsm.Switch("gameover")
}
}
} else {
gp.shape_pos = new_pos