112 lines
2.0 KiB
Go
112 lines
2.0 KiB
Go
package breakout
|
|
|
|
import (
|
|
"log"
|
|
|
|
"git.wisellama.rocks/Wisellama/carpy-breakout/pkg/globject"
|
|
"github.com/go-gl/mathgl/mgl32"
|
|
)
|
|
|
|
type Ball struct {
|
|
Box *globject.Box
|
|
}
|
|
|
|
func NewBall(box *globject.Box) *Ball {
|
|
ball := Ball{
|
|
Box: box,
|
|
}
|
|
|
|
return &ball
|
|
}
|
|
|
|
func (self *Ball) Update() {
|
|
self.Box.Update()
|
|
}
|
|
|
|
func (self *Ball) GLDraw() {
|
|
self.Box.GLDraw()
|
|
}
|
|
|
|
func (self *Ball) GLInit(glProgram uint32) {
|
|
self.Box.GLInit(glProgram)
|
|
}
|
|
|
|
func (self *Ball) ToggleWireframe() {
|
|
self.Box.ToggleWireframe()
|
|
}
|
|
|
|
func (self *Ball) GetAABB() *globject.AABB {
|
|
return self.Box.GetAABB()
|
|
}
|
|
|
|
func (self *Ball) HandlePaddleCollision(paddle *Paddle) {
|
|
aabb := self.GetAABB()
|
|
p := paddle.GetAABB()
|
|
|
|
intersects, diff := aabb.Intersects(p)
|
|
|
|
if intersects {
|
|
newVelocity := self.Box.Velocity
|
|
if globject.XCollision(diff) {
|
|
newVelocity[0] = -1 * newVelocity[0]
|
|
}
|
|
|
|
if globject.YCollision(diff) {
|
|
// Change differently based on which part of the paddle we hit
|
|
// TODO
|
|
newVelocity[1] = -1 * newVelocity[1]
|
|
}
|
|
|
|
self.Box.Velocity = newVelocity
|
|
}
|
|
}
|
|
|
|
func (self *Ball) HandleTargetCollisions(targets *Targets) {
|
|
aabb := self.GetAABB()
|
|
for _, brick := range targets.Bricks {
|
|
if !brick.Broken() {
|
|
b := brick.GetAABB()
|
|
|
|
intersects, diff := aabb.Intersects(b)
|
|
|
|
if intersects {
|
|
// TODO points
|
|
brick.Break()
|
|
|
|
self.UpdateElasticCollision(diff)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (self *Ball) HandleSideWallsCollisions(sidewalls *SideWalls) {
|
|
aabb := self.GetAABB()
|
|
boxes := sidewalls.Boxes
|
|
for _, box := range boxes {
|
|
b := box.GetAABB()
|
|
intersects, diff := aabb.Intersects(b)
|
|
|
|
if intersects {
|
|
self.UpdateElasticCollision(diff)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (self *Ball) UpdateElasticCollision(diff mgl32.Vec3) {
|
|
newVelocity := self.Box.Velocity
|
|
|
|
if globject.XCollision(diff) {
|
|
log.Printf("X collision: %v\n", diff)
|
|
newVelocity[0] = -1 * newVelocity[0]
|
|
}
|
|
|
|
if globject.YCollision(diff) {
|
|
log.Printf("Y collision: %v\n", diff)
|
|
newVelocity[1] = -1 * newVelocity[1]
|
|
}
|
|
|
|
// No Z collisions for this game
|
|
|
|
self.Box.Velocity = newVelocity
|
|
}
|