Refactor Item type; add get abilities

This commit is contained in:
Logen Kain 2018-02-03 10:16:34 -07:00
parent 6d8d64ed58
commit 851dae2107

51
main.go
View File

@ -9,7 +9,7 @@ import (
func main() {
type Item struct {
name string
name, description string
moveable bool
openable bool
}
@ -21,20 +21,20 @@ func main() {
type Player struct {
loc *Room
items map[string]string
items map[string]*Item
}
inventory := map[string]string{}
inventory := map[string]*Item{}
entryItems := map[string]*Item{}
chestItems := map[string]*Item{
"chest": {"chest",
"chest": {"chest", "none",
false,
true,
},
}
caveItems := map[string]*Item{
"House Key": {"House Key",
"key": {"House Key", "It resembles a house key.",
true,
false},
}
@ -58,7 +58,7 @@ func main() {
player := Player{rooms["entry"], inventory}
player.items["syringe"] = "A syringe with a missing needle is in your pocket."
player.items["syringe"] = &Item{"Syringe", "A syringe with a missing needle is in your pocket.", false, false}
var command [2]string
var quit bool = false
@ -151,10 +151,39 @@ func main() {
println("")
println("Inventory:\n")
//Listing items, and looking at them, should be different.
for k, v := range player.items {
println(k, " ", v)
for _, v := range player.items {
println(v.name, " ", v.description)
}
println("")
case "get":
if command[1] == "" {
println("")
println("Get what? ")
break
}
if _, ok := player.loc.items[command[1]]; ok &&
player.loc.items[command[1]].moveable == true {
println("You grab the", command[1],
"and place it in your pocket.")
player.items[command[1]] = player.loc.items[command[1]]
delete(player.loc.items, command[1])
if player.loc.name == "Chest" {
player.loc.description = "" +
"\nYou see an open chest freezer in front of you.\n" +
"The freezer is empty\n" +
"There is an empty field to the (w)est.\n"
break
}
if player.loc.name == "Cave" {
player.loc.description = "You are in a dark cave.\n" +
"There is an empty field to the (e)ast."
break
}
}
println("\nI don't see any", command[1], "to get!")
case "open":
if command[1] == "" {
println("")
@ -168,7 +197,8 @@ func main() {
if player.loc.name == "Chest" {
println("\nYou open the chest")
player.loc.items["chest"].openable = false
player.loc.items["steak"] = &Item{"steak", true, false}
player.loc.items["steak"] = &Item{"steak", "A rotting disgusting steak. " +
"\n You have no idea why you put it in your pocket.", true, false}
player.loc.description = "" +
"\nYou see an open chest freezer in front of you.\n" +
"There is a putrid steak in the freezer\n" +
@ -177,7 +207,7 @@ func main() {
}
}
println("I don't know how to open a", command[1])
println("\nI don't know how to open a", command[1]+"!")
default:
println("\nI don't know how to '", command[0], "'\n")
@ -200,6 +230,7 @@ func main() {
"There is a key on the floor.\n" +
"There is an empty field to the (e)ast."
steak_happened = true
delete(player.items, "steak")
}
if steak_happened == false {
println("Well, it looks like you died! Good luck next time!")