Keyring + Smart Locks
Posted: Sat Oct 10, 2015 12:55 am
The keyring works just like a sack/wooden_box, but only accepts items with the "key" trait.
It also remembers the inventory position where it was last stored.
The keyring only works in combination with smart locks.
Pick up the key ring and click it on a smart lock => the lock searches for a matching key inside the keyring.
If the lock finds a matching key, then the key becomes the mouse cursor, and the keyring (minus the matching key) is returned to its last position in a champion's inventory.
Add this code to your mod's "items.lua":
This to "objects.lua":
And this to a script entity named "script_entity_give_mouse_item":
It also remembers the inventory position where it was last stored.
The keyring only works in combination with smart locks.
Pick up the key ring and click it on a smart lock => the lock searches for a matching key inside the keyring.
If the lock finds a matching key, then the key becomes the mouse cursor, and the keyring (minus the matching key) is returned to its last position in a champion's inventory.
Add this code to your mod's "items.lua":
Code: Select all
defineObject{
name = "keyring",
baseObject = "base_item",
components = {
{
class = "Model",
model = "assets/models/items/lockpicks.fbx",
},
{
class = "Item",
uiName = "Keyring",
gfxIndex = 356,
gfxIndexContainer = 356,
weight = 0.2,
onEquipItem = function(self, champion, slot)
self.go.data:set("champion", champion:getOrdinal())
self.go.data:set("slot", slot)
end,
},
{
class = "ContainerItem",
containerType = "chest",
openSound = "container_box_open",
closeSound = "container_box_close",
onInsertItem = function(self, item, slot)
if (item:hasTrait("key")) then
-- accept keys
else
hudPrint("The keyring can only store keys!")
local altar_local = spawn("altar", party.level, party.x, party.y, party.facing, party.elevation)
altar_local.surface:addItem(item)
self:removeItemFromSlot(slot)
delayedCall("script_entity_give_mouse_item", 0, "giveMouseItem", item.go.id, altar_local.id)
if (getMouseItem() ~= nil) then
if (getMouseItem().go.id ~= item.go.id) then
self:insertItem(slot, getMouseItem())
end
end
end
end,
onRemoveItem = function(self, item)
-- todo
end,
},
{
class = "Script",
name = "data",
source = [[
data = { ["champion"] = 0, ["slot"] = 0 }
get = function(self, name)
return self.data[name]
end
set = function(self, name, value)
self.data[name] = value
end
]],
},
}
}Code: Select all
defineObject{
name = "lock_gold_smart",
baseObject = "lock",
components = {
{
class = "Model",
model = "assets/models/env/wall_lock_golden.fbx",
offset = vec(0, 1.375, 0),
staticShadow = true,
},
{
class = "Clickable",
offset = vec(0, 1.375, 0),
size = vec(0.4, 0.4, 0.4),
onClick = function(self)
local mouse_item = getMouseItem()
if (mouse_item ~= nil) and (mouse_item.go.name == "keyring") then
-- go through all items in the conainer
local key_slot = 0
local slot_item
for i = 1, mouse_item.go.containeritem:getCapacity() do
slot_item = mouse_item.go.containeritem:getItem(i)
if (slot_item ~= nil) and (slot_item.go.item:hasTrait("key")) and (self.go.lock:getOpenedBy() == slot_item.go.name) then
key_slot = i
break
end
end
local data_champion = mouse_item.go.data:get("champion")
local data_slot = mouse_item.go.data:get("slot")
if (key_slot > 0) then
-- a matching key was found
if (data_champion == 0) or (data_slot == 0) then
-- inventory position not set
print("Can't return keyring to inventory - not set")
elseif (party.party:getChampionByOrdinal(data_champion):getItem(data_slot) ~= nil) then
-- inventory position set, but occupied
print("Can't return keyring to inventory - occupied")
else
-- inventory position set and empty
local altar_0 = spawn("altar", party.level, party.x, party.y, party.facing, party.elevation)
altar_0.surface:addItem(slot_item)
mouse_item.go.containeritem:removeItemFromSlot(key_slot)
party.party:getChampionByOrdinal(data_champion):insertItem(mouse_item.go.data:get("slot"), mouse_item)
delayedCall("script_entity_give_mouse_item", 0, "giveMouseItem", slot_item.go.id, altar_0.id)
end
else
-- no matching key was found
if (data_champion == 0) or (data_slot == 0) then
-- inventory position not set
print("Can't return keyring to inventory - not set")
elseif (party.party:getChampionByOrdinal(data_champion):getItem(data_slot) ~= nil) then
-- inventory position set, but occupied
print("Can't return keyring to inventory - occupied")
else
-- inventory position set and empty
party.party:getChampionByOrdinal(data_champion):insertItem(mouse_item.go.data:get("slot"), mouse_item)
setMouseItem(nil)
end
end
end
end
},
},
}Code: Select all
function giveMouseItem(item_id, altar_id)
local item_local = findEntity(item_id)
local altar_local = findEntity(altar_id)
if ((item_local ~= nil) and (altar_local ~= nil)) then
setMouseItem(item_local.item)
altar_local:destroy()
end
end