package coreobjects import ( "image/color" settings "github.com/DegustatorPonos/RuinesOfRafdolon/Settings" 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 Clone() Collider 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 { return rl.CheckCollisionPointRec(rl.Vector2 { X: x, Y: y }, base.Location) } 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() { if !settings.State.DrawColliders { return } 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 } func (base *BoxCollider) Clone() Collider { var clone = (&BoxCollider{}).Init ( base.Location.X, base.Location.Y, base.Location.Width, base.Location.Height) return clone }