summaryrefslogtreecommitdiff
path: root/engine/CoreObjects/Collider.go
diff options
context:
space:
mode:
Diffstat (limited to 'engine/CoreObjects/Collider.go')
-rw-r--r--engine/CoreObjects/Collider.go85
1 files changed, 85 insertions, 0 deletions
diff --git a/engine/CoreObjects/Collider.go b/engine/CoreObjects/Collider.go
new file mode 100644
index 0000000..93cbee6
--- /dev/null
+++ b/engine/CoreObjects/Collider.go
@@ -0,0 +1,85 @@
+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
+}