Skip to content

Instantly share code, notes, and snippets.

@vhodges
Last active August 29, 2015 14:09
Show Gist options
  • Save vhodges/7d4ab1f056e55e3e9390 to your computer and use it in GitHub Desktop.
Save vhodges/7d4ab1f056e55e3e9390 to your computer and use it in GitHub Desktop.
...
// Have your various container objects implement this method
// and move the type specific code to it.
type GameObject interface {
AfterLoad() err
Id() string // It may be possible to replace AfterLoad() with Id - I didn't really look to see what the different blocks were doing.
}
// Now you can have a single function to load all kinds of game object - I think the map will work declared this way
func RegisterGameObjects(dirloc string, gameobject *GameObject, container map[string]interface{}) {
names := getDirNames(dirloc)
for _, element := range names {
if isDir, _ := IsDirectory(dirloc + "/" + element); isDir {
if isLink, _ := IsSymlink(dirloc + "/" + element); isLink {
fmt.Println("Ignoring link: " + dirloc + "/" + element)
return
} else {
RegisterGameObjects(dirloc + "/" + element, gameobject)
}
} else {
input, err2 := ioutil.ReadFile(dirloc + "/" + element)
if err2 != nil {
fmt.Println("Could not read " + element)
} else {
json.Unmarshal(input, gameobject)
err := gameobjectcontainer.AfterLoad()
if err != nil {
// Handle/report the error
}else {
container[gameobject.Id()] = gameobject // See my comment on the type declaration
}
}
}
}
}
// And load becomes:
func (p AdventureBot) Load() {
fmt.Println("Registering Requirements")
RegisterGameObjects("../src/github.com/cptspacetoaster/adventurebot/requirements", &Requirement{}, Requirments)
fmt.Println("Registering Actions")
RegisterGameObjects("../src/github.com/cptspacetoaster/adventurebot/actions", &Action{}, Actions)
//... and so on.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment