package coreobjects import ( "image/color" rl "github.com/gen2brain/raylib-go/raylib" ) var ColliderColour color.RGBA = color.RGBA{ 154, 20, 184, 255 / 2 } type Collider interface { Init(x float32, y float32, height float32, width float32) Collider MoveIfPossible(colliders []*Collider, dx float32, dy float32) Move(dx float32, dy float32) MoveTo(x float32, y float32) Intersects(x float32, y float32) bool CanMove(colliders []*Collider, dx float32, dy float32) bool Draw() } type BoxCollider struct { Location rl.Rectangle } func (base *BoxCollider) Init(x float32, y float32, height float32, width float32) Collider { base.Location = rl.Rectangle{ X: x, Y: y, Width: width, Height: height, } return base } func (base *BoxCollider) Intersects(x float32, y float32) bool { if base.Location.X < x || base.Location.Y < y { return false } if base.Location.X + base.Location.Width > x || base.Location.Y + base.Location.Height > y { return false } return true } func (base *BoxCollider) Move(dx float32, dy float32) { base.Location.X += dx base.Location.Y += dy } func (base *BoxCollider) MoveTo(x float32, y float32) { base.Location.X = x base.Location.Y = y } func (base *BoxCollider) MoveIfPossible(colliders []*Collider, dx float32, dy float32) { if base.CanMove(colliders, dx, dy) { base.Move(dx, dy) } } func (base *BoxCollider) Draw() { rl.DrawRectangle(base.Location.ToInt32().X, base.Location.ToInt32().Y, base.Location.ToInt32().Width, base.Location.ToInt32().Height, ColliderColour) } func (base *BoxCollider) CanMove(colliders []*Collider, dx float32, dy float32) bool { var x = base.Location.X + dx var y = base.Location.Y + dy var width = base.Location.Width var height = base.Location.Height // TODO: optimise for _, v := range colliders { if (*v).Intersects(x, y) || (*v).Intersects(x + width, y) || (*v).Intersects(x, y + height) || (*v).Intersects(x + width, y + height) { return false } } return true }