You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
902 B
48 lines
902 B
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 |
|
}
|
|
|