feat: add shape struct
This commit is contained in:
parent
93874028b0
commit
656487903e
2 changed files with 92 additions and 0 deletions
58
game/shape.go
Normal file
58
game/shape.go
Normal file
|
|
@ -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],
|
||||||
|
}
|
||||||
|
}
|
||||||
34
game/shape_type.go
Normal file
34
game/shape_type.go
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue