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
|
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
|