From 656487903e282507c0bfabac5657c124a63c4d7f Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 14 Sep 2025 21:46:17 +0200 Subject: [PATCH] feat: add shape struct --- game/shape.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++ game/shape_type.go | 34 +++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 game/shape.go create mode 100644 game/shape_type.go diff --git a/game/shape.go b/game/shape.go new file mode 100644 index 0000000..cf0df6f --- /dev/null +++ b/game/shape.go @@ -0,0 +1,58 @@ +package game + +import ( + "tetris/engine/core" + "tetris/engine/graphics" +) + +type Shape struct { + typ ShapeType + coordinates [3]core.Vec2i8 +} + +func NewShape(typ ShapeType) Shape { + var coordinates [3]core.Vec2i8 + + switch typ { + case SHAPE_O: + coordinates = [3]core.Vec2i8{{X: 0, Y: 1}, {X: 1, Y: 0}, {X: 1, Y: 1}} + case SHAPE_L: + coordinates = [3]core.Vec2i8{{X: 0, Y: -1}, {X: 0, Y: 1}, {X: 1, Y: 1}} + case SHAPE_J: + coordinates = [3]core.Vec2i8{{X: 0, Y: -1}, {X: 0, Y: 1}, {X: -1, Y: 1}} + case SHAPE_T: + coordinates = [3]core.Vec2i8{{X: -1, Y: 0}, {X: 1, Y: 0}, {X: 0, Y: 1}} + case SHAPE_Z: + coordinates = [3]core.Vec2i8{{X: 1, Y: 0}, {X: 0, Y: -1}, {X: -1, Y: -1}} + case SHAPE_S: + coordinates = [3]core.Vec2i8{{X: -1, Y: 0}, {X: 0, Y: -1}, {X: 1, Y: -1}} + case SHAPE_I: + coordinates = [3]core.Vec2i8{{X: 0, Y: 1}, {X: 0, Y: -1}, {X: 0, Y: -2}} + } + + return Shape{ + typ: typ, + coordinates: coordinates, + } +} + +func (s Shape) Type() ShapeType { + return s.typ +} + +func (s Shape) GetBlock() Block { + return s.typ.GetBlock() +} + +func (s Shape) Tile() graphics.Tile { + return s.GetBlock().Tile() +} + +func (s Shape) Coordinates() [4]core.Vec2i8 { + return [4]core.Vec2i8{ + {}, // first coordinate is always zero + s.coordinates[0], + s.coordinates[1], + s.coordinates[2], + } +} diff --git a/game/shape_type.go b/game/shape_type.go new file mode 100644 index 0000000..e6074fc --- /dev/null +++ b/game/shape_type.go @@ -0,0 +1,34 @@ +package game + +type ShapeType byte + +const ( + SHAPE_O ShapeType = iota + SHAPE_L + SHAPE_J + SHAPE_T + SHAPE_Z + SHAPE_S + SHAPE_I +) + +func (s ShapeType) GetBlock() Block { + switch s { + case SHAPE_O: + return BLOCK_GREEN + case SHAPE_L: + return BLOCK_RED + case SHAPE_J: + return BLOCK_BLUE + case SHAPE_T: + return BLOCK_MAGENTA + case SHAPE_Z: + return BLOCK_YELLOW + case SHAPE_S: + return BLOCK_ORANGE + case SHAPE_I: + return BLOCK_TEAL + default: + return BLOCK_EMPTY + } +}