-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
68 lines (66 loc) · 2.07 KB
/
commands.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
package main
import (
"os"
"strings"
)
func ProcessCommands(player *Character, input string) {
Output("yellow", "======================================================================")
tokens := strings.Fields(input)
if len(tokens) == 0 {
Output("red", "No command received.")
return
}
command := strings.ToLower(tokens[0])
itemName := ""
if len(tokens) > 1 {
itemName = tokens[1]
}
loc := LocationMap[player.CurrentLocation]
switch command {
case "go":
fallthrough
case "goto":
if loc.CanGoTo(strings.ToLower(itemName)) {
locName, err := FindLocationName(strings.ToLower(itemName))
if err != nil {
Output("red", "Can't go to "+itemName+" from here.")
} else {
player.CurrentLocation = locName
}
} else {
Output("red", "Can't go to "+itemName+" from here.")
}
case "get":
err, index, itm := FindItemByName(itemName)
//Make sure we do not pick it up twice
if err == nil && itm.ItemInRoom(loc) && !itm.ItemOnPlayer(player) {
player.Items = append(player.Items, index) // Add Item to Player's bag
itm.RemoveItemFromRoom(loc)
} else {
Output("Could not get " + itemName)
}
case "open":
OpenItem(player, itemName)
case "inv":
Output("yellow", "Your Inventory: ")
for _, itm := range player.Items {
Output("yellow", "\t"+Items[itm].Name)
}
case "help":
Output("yellow", "Commands:")
Output("yellow", "\tgo <Location Name> - Move to the new location")
Output("yellow", "\tattack - Attacks opponent(s)")
Output("yellow", "\tblock - Block incoming attack")
Output("yellow", "\trun - Escape attack")
Output("yellow", "\tget <Item Name> - Pick up item")
Output("yellow", "\topen <Item Name> - Open an iten if it can be opened")
Output("yellow", "\tinv - Show what you are carrying")
Output("yellow", "======================================================================\n\n")
case "quit":
Output("green", "Goodbye...")
os.Exit(0)
default:
Output("yellow", "\nI'm sorry, I don't understand.\nPlease try again.")
Output("yellow", "======================================================================\n\n")
}
}