-
Notifications
You must be signed in to change notification settings - Fork 1
/
objects.go
142 lines (131 loc) · 3 KB
/
objects.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
*****************************************************
* © 2019 Stefano Peris <xenonlab.develop@gmail.com> *
*****************************************************
*
* Released under the GNU/GPL 3.0 license
*
* github: <https://github.com/XenonLab-Studio/GoZork>
*
*
* :'######::::'#######::'########::'#######::'########::'##:::'##:
* ##... ##::'##.... ##:..... ##::'##.... ##: ##.... ##: ##::'##::
* ##:::..::: ##:::: ##::::: ##::: ##:::: ##: ##:::: ##: ##:'##:::
* ##::'####: ##:::: ##:::: ##:::: ##:::: ##: ########:: #####::::
* ##::: ##:: ##:::: ##::: ##::::: ##:::: ##: ##.. ##::: ##. ##:::
* ##::: ##:: ##:::: ##:: ##:::::: ##:::: ##: ##::. ##:: ##:. ##::
* . ######:::. #######:: ########:. #######:: ##:::. ##: ##::. ##:
* :......:::::.......:::........:::.......:::..:::::..::..::::..::
*
* Textual adventure written in golang inspired by "Zork I"
*/
package main
import (
"errors"
"fmt"
"strings"
)
type Object struct {
name string
desc string
adjectives []string
aliases []string
verbs map[string]func(*Object, *Player)
openable bool
open bool
// whether or not this object should be mentioned below the room description
fixture bool
// if this object can be picked up by the player
carryable bool
}
// Return true if the string matches the object.
func (o *Object) RespondTo(args []string) bool {
str := strings.Join(args, " ")
// match all adjectives
for _, adj := range o.adjectives {
if i := strings.Index(str, strings.ToUpper(adj)+" "); i == 0 {
str = str[len(adj)+1:]
}
}
if str == strings.ToUpper(o.name) {
return true
}
for _, alias := range o.aliases {
if str == strings.ToUpper(alias) {
return true
}
}
return false
}
func (o *Object) GetName() string {
res := "a " + o.name
if o.openable {
if o.open {
res += " (open)"
} else {
res += " (closed)"
}
}
return res
}
func (o *Object) GetDesc() string {
res := ""
if len(o.desc) > 0 {
res = o.desc
if o.openable {
res += "\n"
}
}
if o.openable {
if o.open {
res += fmt.Sprintf("The %v is open.", o.name)
} else {
res += fmt.Sprintf("The %v is closed.", o.name)
}
}
return res
}
type ObjectContainer struct {
objects []*Object
}
func (c *ObjectContainer) AddObject(objs ...*Object) {
c.objects = append(c.objects, objs...)
}
func (c *ObjectContainer) RemoveObject(obj *Object) {
loc := -1
for i, val := range c.objects {
if val == obj {
loc = i
break
}
}
c.objects = append(c.objects[:loc], c.objects[loc+1:]...)
}
func (c *ObjectContainer) ObjectNames() (res string, err error) {
count := 0
res = ""
for i, obj := range c.objects {
if obj.fixture {
continue
}
res += obj.GetName()
count++
if i < len(c.objects)-2 {
res += ", "
} else if i == len(c.objects)-2 {
res += " and "
}
}
if count == 0 {
err = errors.New("no objects found")
}
return
}
func (c *ObjectContainer) FindObject(args []string) *Object {
for _, obj := range c.objects {
if obj.RespondTo(args) {
return obj
}
}
return nil
}