Collision detection system
This commit is contained in:
32
collision/world.go
Normal file
32
collision/world.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package collision
|
||||
|
||||
type World struct {
|
||||
Entities []Collidable
|
||||
}
|
||||
|
||||
func (w *World) AddEntity(e Collidable) {
|
||||
w.Entities = append(w.Entities, e)
|
||||
}
|
||||
|
||||
func (w *World) RemoveEntity(e Collidable) {
|
||||
for i, entity := range w.Entities {
|
||||
if entity == e {
|
||||
w.Entities = append(w.Entities[:i], w.Entities[i+1:]...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *World) NotifyAboutCollisions() {
|
||||
for i, entity := range w.Entities {
|
||||
for _, other := range w.Entities[i+1:] {
|
||||
if entity.CollisionShape().Overlaps(other.CollisionShape()) {
|
||||
entity.HandleCollisionEvent(other)
|
||||
other.HandleCollisionEvent(entity)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewWorld() *World {
|
||||
return &World{}
|
||||
}
|
||||
Reference in New Issue
Block a user