summaryrefslogtreecommitdiff
path: root/client/data/room.lua
diff options
context:
space:
mode:
authorAlexander Pickering <alex@cogarr.net>2019-01-27 10:32:09 -0500
committerAlexander Pickering <alex@cogarr.net>2019-01-27 10:32:09 -0500
commit0aae46ecc38005236210f7e243f02cac39ab1dc3 (patch)
treee2fcc9893df4ff03a87e113b3c0ff89357c86ef7 /client/data/room.lua
downloadhome_text_adventure-0aae46ecc38005236210f7e243f02cac39ab1dc3.tar.gz
home_text_adventure-0aae46ecc38005236210f7e243f02cac39ab1dc3.tar.bz2
home_text_adventure-0aae46ecc38005236210f7e243f02cac39ab1dc3.zip
Inital commit
Diffstat (limited to 'client/data/room.lua')
-rw-r--r--client/data/room.lua68
1 files changed, 68 insertions, 0 deletions
diff --git a/client/data/room.lua b/client/data/room.lua
new file mode 100644
index 0000000..410b08e
--- /dev/null
+++ b/client/data/room.lua
@@ -0,0 +1,68 @@
+local room = {}
+local item = require("item")
+
+local room_base = {
+ add_item = function(self,itemname,itemdesc,itemlocation)
+ local newitem = item.create_new(itemname,itemdesc,itemlocation)
+ self.items[#self.items + 1] = newitem
+ end,
+ remove_item = function(self,itemname,itemdesc,itemlocation)
+ local to_remove = {}
+ for k,v in pairs(self.items) do
+ if v.name == itemname and v.desc == itemdesc and v.itemlocation == itemlocation then
+ to_remove[#to_remove + 1] = k
+ end
+ end
+ for k,v in pairs(to_remove) do
+ table.remove(self.items,v)
+ end
+ end,
+ get_items_names = function(self)
+ local ret = {}
+ for k,v in pairs(self.items) do
+ ret[#ret + 1] = v.name
+ end
+ return ret
+ end,
+ get_room_desc = function(self)
+ local sb = {}
+ sb[#sb + 1] = string.format("You are in a %s",self.name)
+ sb[#sb + 1] = string.format(self.desc)
+ for itemnum,item in pairs(self.items) do
+ sb[#sb + 1] = string.format("There is a %s %s",item.name,item.location)
+ end
+ for direction,room in pairs(self.go) do
+ if room == "exit" then
+ sb[#sb + 1] = string.format("There is an exit to the %s",direction)
+ else
+ local name = room.name
+ local sdir = string.lower(direction)
+ if sdir == "up" then
+ sb[#sb + 1] = string.format("There is a %s above you",name)
+ elseif sdir == "down" then
+ sb[#sb + 1] = string.format("There is a %s below you",name)
+ else
+ sb[#sb + 1] = string.format("There is a %s to the %s",name,direction)
+ end
+ end
+ end
+ return table.concat(sb,"\n")
+ end
+}
+
+local room_m = {__index = room_base}
+
+function room.create_new(n,loc)
+ local ret = {
+ name=n,
+ items={},
+ desc="",
+ go={},
+ location=loc
+ }
+ print("Created room:",ret)
+ setmetatable(ret,room_m)
+ return ret
+end
+room.meta = room_m
+return room