diff --git a/item.lua b/item.lua index 770e29a..b96249f 100644 --- a/item.lua +++ b/item.lua @@ -27,7 +27,6 @@ function m:itemMode() self.mousepressed = window.mousepressed(function (self, x, y, button) log:info(self) inventory.selected_item = self - log:format("selected idx : %s", inventory.selected_item.idx) end) end diff --git a/main.lua b/main.lua index 784951a..61a4f91 100644 --- a/main.lua +++ b/main.lua @@ -17,15 +17,17 @@ end --load room list local rooms = require("rooms") -current_room = rooms['default'] +current_room = rooms['Alley'] function set_room_raw(r) + current_room:onexit() current_room = r + current_room:onenter() end function set_room(r) - current_room = rooms[r] - assert(current_room) + assert(rooms[r]) + set_room_raw(rooms[r]) end screen = { @@ -64,7 +66,7 @@ end) inventory.draw = window.draw(function (self) for i, item in pairs(self.items) do - if self.selected_item and self.selected_item.idx == i then + if self.selected_item and self.selected_item == item then love.graphics.circle('line', 60*i - 30, 30, 25) end @@ -83,7 +85,7 @@ end function inventory:pickup(item) util.add_entity(self.items, item) - self:set_pos(item.idx, item) + self:set_pos(item.idx[self.items], item) end function inventory:drop(item) diff --git a/obj/door.lua b/obj/door.lua index d0c1d73..a820d26 100644 --- a/obj/door.lua +++ b/obj/door.lua @@ -27,37 +27,41 @@ function m:draw() end end -m.keyMode = window.mousepressed(function (self) - if self.locked then - local i = inventory.selected_item - if i and i.code then - if i.code == self.code then - self.locked = false - inventory:use() - - log:log("Door opened.") +function m:keyMode() + self.mousepressed = window.mousepressed(function (self) + if self.locked then + local i = inventory.selected_item + if i and i.code then + if i.code == self.code then + self.locked = false + inventory:use() + + log:log("Door opened.") + else + log:log("This seems not a matching key.") + inventory.selected_item = nil + end else - log:log("This seems not a matching key.") - inventory.selected_item = nil + log:log("It's locked.") end else - log:log("It's locked.") + log:format("You enter %s", self.destination) + set_room(self.destination) end - else - log:format("You enter %s", self.destination) - set_room(self.destination) - end -end) + end) +end -m.keypadMode = window.mousepressed(function (self) - if self.locked then - log:log("It's locked.") - else - log:format("You enter %s", self.destination) - set_room(self.destination) - end -end) +function m:keypadMode() + self.mousepressed = window.mousepressed(function (self) + if self.locked then + log:log("It's locked.") + else + log:format("You enter %s", self.destination) + set_room(self.destination) + end + end) +end -m.mousepressed = m.keyMode +m:keyMode() return m diff --git a/obj/keypad.lua b/obj/keypad.lua index 08461a3..165a3c4 100644 --- a/obj/keypad.lua +++ b/obj/keypad.lua @@ -3,9 +3,13 @@ local entity = require('entity') local keypad = require('room/keypad') local m = entity:from{ - width = 50, - height = 50, - lines = {'fill', {0, 0, 0, 50, 50, 50, 50, 0}}, + width = 30, + height = 40, + lines = { + {'fill', {0, 0, 0, 40, 30, 40, 30, 0}}, + {'fill', {5, 5, 25, 5, 25, 10, 5, 10}}, + {'fill', {5, 15, 25, 15, 25, 35, 5, 35}}, + }, code = 45100, } diff --git a/room.lua b/room.lua index 45717a2..c4156be 100644 --- a/room.lua +++ b/room.lua @@ -26,14 +26,6 @@ function room:from(t) return r end -function room:insert(obj) - util.add_entity(self.objects, obj) -end - -function room:remove(obj) - util.del_entity(self.objects, obj) -end - room.mousepressed = window.propagate{'edges', 'objects'} function room:draw() @@ -48,6 +40,27 @@ function room:draw() end end +function room:insert(obj) + util.add_entity(self.objects, obj) +end + +function room:remove(obj) + util.del_entity(self.objects, obj) +end + + +function room:onenter() + if self.update then + register(self) + end +end + +function room:onexit() + if self.update then + unregister(self) + end +end + function room.calc_align(width, height) local sx = (320 - width)/2 local sy = (240 - height)/2 diff --git a/room/alley.lua b/room/alley.lua index 65bcae2..38ee45f 100644 --- a/room/alley.lua +++ b/room/alley.lua @@ -3,7 +3,7 @@ local edge = require("edge") local m = room:new() -edge.set(m, 'down', "sundial_alley") +edge.set(m, 'down', "Sundial alley") --edge.set(m, 'left', "alley2") return m diff --git a/room/keypad.lua b/room/keypad.lua index 509998c..5f78f03 100644 --- a/room/keypad.lua +++ b/room/keypad.lua @@ -55,8 +55,6 @@ function m:from(t) edge.set(kp, "down", kp.room) - register(kp) - return kp end diff --git a/room/sundial_alley.lua b/room/sundial_alley.lua index 8a3d506..c1cf425 100644 --- a/room/sundial_alley.lua +++ b/room/sundial_alley.lua @@ -22,8 +22,8 @@ keypad:from{ oncorrect = function (self) d.locked = false end, -}:install(m, 180, 100) +}:install(m, 160, 90) -edge.set(m, 'up', "alley") +edge.set(m, 'up', "Alley") return m diff --git a/rooms.lua b/rooms.lua index 53199f0..0e8c044 100644 --- a/rooms.lua +++ b/rooms.lua @@ -1,8 +1,8 @@ local m = { ['default'] = require('room/default'), ['doortest'] = require('room/doortest'), - ['alley'] = require("room/alley"), - ['sundial_alley'] = require("room/sundial_alley"), + ['Alley'] = require("room/alley"), + ['Sundial alley'] = require("room/sundial_alley"), } return m diff --git a/util.lua b/util.lua index fd49ede..72d9744 100644 --- a/util.lua +++ b/util.lua @@ -23,14 +23,17 @@ end function m.add_entity(a, x) table.insert(a, x) - x.idx = #a + if not x.idx then + x.idx = {} + end + x.idx[a] = #a end function m.del_entity(a, x) - table.remove(a, x.idx) + table.remove(a, x.idx[a]) for i, v in ipairs(a) do - v.idx = i + v.idx[a] = i end end