carpy-breakout/pkg/breakout/targets.go

86 lines
1.8 KiB
Go

package breakout
import (
"git.wisellama.rocks/Wisellama/carpy-breakout/pkg/globject"
"github.com/go-gl/mathgl/mgl32"
)
type Targets struct {
Bricks []*Brick
TopRight mgl32.Vec3
BottomLeft mgl32.Vec3
Rows int
Columns int
}
func NewTargets(numRows, numColumns int, adjustPos mgl32.Vec3) *Targets {
targets := Targets{
Rows: numRows,
Columns: numColumns,
Bricks: make([]*Brick, numRows*numColumns),
}
var brickWidth float32 = 4.0
var brickHeight float32 = 1.0
var brickDepth float32 = 4.0
var widthOffset float32 = 0.5
var heightOffset float32 = 1
bw := brickWidth + widthOffset
bh := brickHeight + heightOffset
bottomLeftPos := mgl32.Vec3{
bw/2 - float32(numColumns)*(bw)/2,
bh/2 - float32(numRows)*(bh)/2,
0,
}
bottomLeftPos = bottomLeftPos.Add(adjustPos)
topRightPos := mgl32.Vec3{
bw/2 + float32(numColumns)*(bw)/2,
bh/2 + float32(numRows)*(bh)/2,
0,
}
topRightPos = topRightPos.Add(adjustPos)
targets.TopRight = topRightPos
targets.BottomLeft = bottomLeftPos
materials := make([]*globject.Material, numColumns)
for i := 0; i < numColumns; i++ {
red := 0 + float32(i)/float32(numColumns)
blue := 1 - float32(i)/float32(numColumns)
m := globject.NewMaterial()
m.Color = mgl32.Vec4{red, 0, blue, 1}
materials[i] = m
}
n := 0
for i := 0; i < numRows; i++ {
for j := 0; j < numColumns; j++ {
position := bottomLeftPos.Add(mgl32.Vec3{
float32(j) * bw,
float32(i) * bh,
0})
targets.Bricks[n] = NewBrick(brickWidth, brickHeight, brickDepth, position, materials[i])
n++
}
}
return &targets
}
func (t *Targets) GLInit(glProgram uint32) {
for _, brick := range t.Bricks {
brick.GLInit(glProgram)
}
}
func (t *Targets) GLDraw() {
for _, brick := range t.Bricks {
brick.GLDraw()
}
}