diff options
Diffstat (limited to 'client/data/localplayer.lua')
| -rw-r--r-- | client/data/localplayer.lua | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/client/data/localplayer.lua b/client/data/localplayer.lua new file mode 100644 index 0000000..74fbf0a --- /dev/null +++ b/client/data/localplayer.lua @@ -0,0 +1,147 @@ +local localplayer = {} +local world = require("world") +local net = require("net") +local save = require("save") +print("Doing local player") + +local directions = { + "north","south","east","west","up","down" +} +local directions_index = {} +for k,v in pairs(directions) do + directions_index[v] = k +end +--print("In localplayer, world.locations are:",world.locations) +--for k,v in pairs(world.locations) do + --print(k,":",v) +--end +local lp_base = { + location = world.locations["City"], + has_settled = false, + go = function(self,dir) + dir = string.lower(dir) + if self.location.is_location then + local to + for k,v in pairs(self.location.go) do + if v[1] == dir then + to = v[2]() + break + end + end + self.location = to + cls() + out(self.location.goto_txt) + else --We're in a house/room + local toroom = self.location.go[dir] + --print("got to room:",toroom) + if toroom == "exit" then + print("special exit room") + self.location = self.in_house["in"] + self.in_house = nil + self.owns = false + cls() + out(self.location.goto_txt) + return + end + self.location = toroom + cls() + out(toroom:get_room_desc()) + end + end, + goto_house = function(self,location,house) + print("going to ",name) + print("Got location:",location,"Got name:",house) + local house = net.get_house(location,house) + self.in_house = house + local init_room_name = "1x1x1" + local init_room = house.rooms[init_room_name] + local init_room_meta = getmetatable(init_room).__index + print("init_room_meta",init_room_meta) + local owner = net.do_i_own(location,house.name,self.id) + print("Looked at do_i_own:",owner) + if owner then + self.owns = true + else + self.owns = false + end + self.location = init_room + cls() + out(init_room:get_room_desc()) + print("I'm in the inital room!",init_room) + end, + goto_location = function(self,loc) + self.in_house = nil + print("Setting location to",loc) + self.location = loc + cls() + out(self.location.goto_txt) + end, + --goto_location = function(self,name) + --assert(name ~= nil,"tried to go to a nil location") + --print("Self is",self,"name is",name) + --print("valid locations are:") + --for k,v in pairs(world.locations) do + --print(k,":",v) + --end + --assertf(world.locations[name],"The location %q does not exist.",name) + --cls() + --out(world.locations[name].goto_txt) + --localplayer.location = name + ----out(world.locations[name].desc) + --end, + get_loc = function(self) + --Check if the player is in one of the locations + print("Trying to get location:",self.location) + print("World locations:",#world.locations) + for k,v in pairs(world.locations) do + print(k,":",v) + end + local loc + if world.locations[self.location] then + loc = world.locations[self.location] + end + print("Got location:",loc) + return loc + end, +} +local lp_m = {__index = lp_base} + +local function load_save() + return require("savedata") +end +local success , data = pcall(load_save) +if success then + print("setting local player to saved data") + localplayer = data + setmetatable(localplayer,lp_m) + out(localplayer.location.desc) + if not localplayer.has_settled then + world.basic_commands[#world.basic_commands + 1] = "settle" + end +else + print("Creating new player") + out([[ +Welcome to HTA, the Home Text Adventure. The text here describes where you +are or what your are doing. Use the text box at the bottom to enter commands. +You can submit commands by pressing the button to the right of the textbox, +or by pressing the Enter key. + +Hints for what kinds of things you can type in are displayed right above the +text box. Try typing "look" in the command box. + +You wake up in the City of Beginnings +]]) + setmetatable(localplayer,lp_m) + localplayer.id = math.random() + localplayer.owns = false + localplayer.in_house = nil + world.basic_commands[#world.basic_commands + 1] = "settle" + set_hints(unpack(world.basic_commands)) + local f = io.open("../data/savedata.lua","w") + f:write(save.table_to_string(localplayer)) + f:close() +end + + + +return localplayer |
