-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter.go
105 lines (87 loc) · 2.36 KB
/
character.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
96
97
98
99
100
101
102
103
104
105
package main
import (
"log"
"os"
ui "github.com/gizak/termui/v3"
)
// A character contains data for the player's character.
type character struct {
name, moxie, smarts, wiggles, friends, pockets, gumption, skill, style string
}
// render renders the character's stats to the stat block and triggers
// a re-render.
func (c *character) render() {
statBlock.Text = "Name: " + c.name + "\nSkill: " + c.skill + "\nStyle: " + c.style + "\nMoxie: " + c.moxie + "\nSmarts: " + c.smarts + "\nWiggles: " + c.wiggles + "\nFriends: " + c.friends + "\nPockets: " + c.pockets + "\nGumption: " + c.gumption
ui.Render(statBlock)
}
// save saves the character data to the character log file.
func (c *character) save() {
f, err := os.Create("./logs/character")
if err != nil {
log.Fatal(err)
}
defer f.Close()
_, err2 := f.WriteString(c.name + "\n" + c.skill + "\n" + c.style + "\n" + c.moxie + "\n" + c.smarts + "\n" + c.wiggles + "\n" + c.friends + "\n" + c.pockets + "\n" + c.gumption)
if err2 != nil {
log.Fatal(err2)
}
}
// load loads the character from the character log file.
func (c *character) load(f string) {
stats, err := readNameFile(f)
if err != nil {
log.Fatalf("readLines: %s", err)
}
if len(stats) < 9 {
start := len(stats) - 1
for i := start; i < 9; i++ {
stats = append(stats, "")
}
}
c.name = stats[0]
c.skill = stats[1]
c.style = stats[2]
c.moxie = stats[3]
c.smarts = stats[4]
c.wiggles = stats[5]
c.friends = stats[6]
c.pockets = stats[7]
c.gumption = stats[8]
}
// setAttribute is used to modify a given character stat field.
// When updated, it saves and renders as well.
func (c *character) setAttribute(field string, data string) {
switch field {
case "name":
c.name = data
case "skill":
c.skill = data
case "style":
c.style = data
case "moxie":
c.moxie = data
case "smarts":
c.smarts = data
case "wiggles":
c.wiggles = data
case "friends":
c.friends = data
case "pockets":
c.pockets = data
case "gumption":
c.gumption = data
}
c.save()
c.render()
}
func generateSkill() string {
prefixes, err := readNameFile("./data/pc/skillprefix.names")
if err != nil {
log.Fatalf("readLines: %s", err)
}
skills, err := readNameFile("./data/pc/skill.names")
if err != nil {
log.Fatalf("readLines: %s", err)
}
return prefixes[generateNumber(0, len(prefixes)-1)] + " " + skills[generateNumber(0, len(skills)-1)]
}