-
Notifications
You must be signed in to change notification settings - Fork 0
/
archetype.go
95 lines (89 loc) · 2.08 KB
/
archetype.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package game
import (
"encoding/json"
"fmt"
"path"
"strings"
"github.com/kettek/morogue/id"
)
// Archetype is an interface that all archetypes must implement.
type Archetype interface {
GetID() id.UUID
Type() string
}
type archetypeWrapper struct {
ID string `json:"id"`
}
func DecodeArchetype(bytes []byte, rootPath string) (Archetype, error) {
var w archetypeWrapper
err := json.Unmarshal(bytes, &w)
if err != nil {
return nil, err
}
parts := strings.SplitAfterN(w.ID, ":", 3)
if len(parts) < 3 {
return nil, fmt.Errorf("invalid archetype id: %s", w.ID)
}
key := parts[0] + parts[1][:len(parts[1])-1]
switch key {
case id.KeyTile:
var a TileArchetype
if err = json.Unmarshal(bytes, &a); err != nil {
return nil, err
}
a.Image = path.Join(rootPath, a.Image)
return a, nil
case id.KeyCharacter:
var a CharacterArchetype
if err = json.Unmarshal(bytes, &a); err != nil {
return nil, err
}
a.Image = path.Join(rootPath, a.Image)
a.PlayerOnly = true
return a, nil
case id.KeyDoor:
var a DoorArchetype
if err = json.Unmarshal(bytes, &a); err != nil {
return nil, err
}
a.Image = path.Join(rootPath, a.Image)
return a, nil
case id.KeyMob:
var a CharacterArchetype
if err = json.Unmarshal(bytes, &a); err != nil {
return nil, err
}
a.Image = path.Join(rootPath, a.Image)
return a, nil
case id.KeyItem:
var a ItemArchetype
if err = json.Unmarshal(bytes, &a); err != nil {
return nil, err
}
a.Image = path.Join(rootPath, a.Image)
return a, nil
case id.KeyWeapon:
var a WeaponArchetype
if err = json.Unmarshal(bytes, &a); err != nil {
return nil, err
}
a.Image = path.Join(rootPath, a.Image)
return a, nil
case id.KeyArmor:
var a ArmorArchetype
if err = json.Unmarshal(bytes, &a); err != nil {
return nil, err
}
a.Image = path.Join(rootPath, a.Image)
return a, nil
case id.KeyFood:
var a FoodArchetype
if err = json.Unmarshal(bytes, &a); err != nil {
return nil, err
}
a.Image = path.Join(rootPath, a.Image)
return a, nil
default:
return nil, fmt.Errorf("invalid archetype type: %s", key)
}
}