2018-10-19 20:31:58 +00:00
|
|
|
package level
|
|
|
|
|
2018-10-20 22:42:49 +00:00
|
|
|
import (
|
2019-12-23 02:21:58 +00:00
|
|
|
"git.kirsle.net/go/render"
|
2019-12-22 22:11:01 +00:00
|
|
|
"github.com/google/uuid"
|
2018-10-20 22:42:49 +00:00
|
|
|
)
|
2018-10-19 20:31:58 +00:00
|
|
|
|
|
|
|
// ActorMap holds the doodad information by their ID in the level data.
|
|
|
|
type ActorMap map[string]*Actor
|
|
|
|
|
|
|
|
// Inflate assigns each actor its ID from the hash map for their self reference.
|
|
|
|
func (m ActorMap) Inflate() {
|
|
|
|
for id, actor := range m {
|
|
|
|
actor.id = id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 22:42:49 +00:00
|
|
|
// Add a new Actor to the map. If it doesn't already have an ID it will be
|
|
|
|
// given a random UUIDv4 ID.
|
|
|
|
func (m ActorMap) Add(a *Actor) {
|
|
|
|
if a.id == "" {
|
2019-12-22 22:11:01 +00:00
|
|
|
a.id = uuid.Must(uuid.NewRandom()).String()
|
2018-10-20 22:42:49 +00:00
|
|
|
}
|
|
|
|
m[a.id] = a
|
|
|
|
}
|
|
|
|
|
2018-10-21 00:08:20 +00:00
|
|
|
// Remove an Actor from the map. The ID must be set at the very least, so to
|
|
|
|
// remove by ID just create an Actor{id: x}
|
|
|
|
func (m ActorMap) Remove(a *Actor) bool {
|
|
|
|
if _, ok := m[a.id]; ok {
|
|
|
|
delete(m, a.id)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
// Actor is an instance of a Doodad in the level.
|
|
|
|
type Actor struct {
|
|
|
|
id string // NOTE: read only, use ID() to access.
|
|
|
|
Filename string `json:"filename"` // like "exit.doodad"
|
|
|
|
Point render.Point `json:"point"`
|
2019-06-23 23:15:09 +00:00
|
|
|
Links []string `json:"links,omitempty"` // IDs of linked actors
|
2018-10-19 20:31:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the actor's ID.
|
|
|
|
func (a *Actor) ID() string {
|
|
|
|
return a.id
|
|
|
|
}
|
2019-06-23 23:15:09 +00:00
|
|
|
|
|
|
|
// AddLink adds a linked Actor to an Actor. Add the linked actor by its ID.
|
|
|
|
func (a *Actor) AddLink(id string) {
|
|
|
|
// Don't add a duplicate ID to the links array.
|
|
|
|
for _, exist := range a.Links {
|
|
|
|
if exist == id {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a.Links = append(a.Links, id)
|
|
|
|
}
|