48 lines
902 B
Go
48 lines
902 B
Go
package world
|
|
|
|
import (
|
|
"strconv"
|
|
"sync"
|
|
)
|
|
|
|
// Coordinates returns the provided coordinates as a comma-delimited string.
|
|
func Coordinates(x, y, z int) string {
|
|
return strconv.Itoa(x) + "," + strconv.Itoa(y) + "," + strconv.Itoa(z)
|
|
}
|
|
|
|
// Map represents a collection of entities in a 3D space.
|
|
type Map struct {
|
|
contents map[string]*Entity
|
|
|
|
sync.RWMutex
|
|
}
|
|
|
|
// NewMap returns a new Map.
|
|
func NewMap() *Map {
|
|
return &Map{}
|
|
}
|
|
|
|
// Contents returns the entities contained in the Map.
|
|
func (m *Map) Contents() map[string]*Entity {
|
|
m.RLock()
|
|
defer m.RUnlock()
|
|
|
|
c := make(map[string]*Entity)
|
|
for key, value := range m.contents {
|
|
c[key] = value
|
|
}
|
|
return c
|
|
}
|
|
|
|
// Add adds an entity to the Map.
|
|
func (m *Map) Add(e *Entity, x, y, z int) {
|
|
m.Lock()
|
|
defer m.Unlock()
|
|
|
|
if m.contents == nil {
|
|
m.contents = make(map[string]*Entity)
|
|
}
|
|
|
|
e.x, e.y, e.z = x, y, z
|
|
m.contents[Coordinates(x, y, z)] = e
|
|
}
|