1
0
Fork 0

feat: lock and spawn shapes

This commit is contained in:
Henrik Hautakoski 2025-09-14 22:31:21 +02:00
parent 3c847094a0
commit a6072e5b05

25
main.go
View file

@ -14,10 +14,11 @@ import (
) )
var ( var (
shape = game.NewShape(game.SHAPE_O) shape game.Shape
shape_pos = core.Vec2i8{X: 4, Y: 0} shape_pos core.Vec2i8
dropTimer = core.NewIntervalTimer(0.3) dropTimer = core.NewIntervalTimer(0.3)
grid = game.Grid{} grid = game.Grid{}
nextShape game.ShapeType
r = draw.Renderer{ r = draw.Renderer{
Theme: &draw.Theme{ Theme: &draw.Theme{
@ -30,13 +31,29 @@ var (
} }
) )
func SpawnShape() {
shape = game.NewShape(nextShape)
shape_pos = core.Vec2i8{X: 4, Y: 0}
nextShape = (nextShape + 1) % 7
}
func LockShape() {
for _, block := range shape.Coordinates() {
block = shape_pos.Add(block)
grid.Set(byte(block.X), byte(block.Y), shape.GetBlock())
}
}
func Update(delta float32) { func Update(delta float32) {
if dropTimer.UpdateReset(delta) { if dropTimer.UpdateReset(delta) {
new_pos := shape_pos new_pos := shape_pos
new_pos.Y += 1 new_pos.Y += 1
// Update position if it does not collide // Update position if it does not collide
if !game.CheckShapeCollision(new_pos, &shape, &grid) { if game.CheckShapeCollision(new_pos, &shape, &grid) {
LockShape()
SpawnShape()
} else {
shape_pos = new_pos shape_pos = new_pos
} }
} }
@ -71,6 +88,8 @@ func main() {
render.SetTexture(texture) render.SetTexture(texture)
render.SetFont(&assets.Font) render.SetFont(&assets.Font)
SpawnShape()
for !rl.WindowShouldClose() { for !rl.WindowShouldClose() {
Update(rl.GetFrameTime()) Update(rl.GetFrameTime())
Render() Render()