|
|
分享一个挖宝玩法脚本,适合藏宝图、活动地图、土城娱乐玩法。
功能点:
1. 使用藏宝图后生成目标地图和坐标。
2. 玩家到达坐标附近后可以挖宝。
3. 挖宝可能出奖励、怪物、空宝、陷阱。
4. 支持稀有奖励公告。
实际使用时,把 Treasure.createMap(player) 绑定到使用藏宝图,把 Treasure.dig(player) 绑定到挖宝按钮。
-- TreasureDig.lua
-- 挖宝系统:藏宝图坐标、随机事件、稀有奖励公告
local VvApi = {}
function VvApi.getPlayerName(player)
return player.name
end
function VvApi.getMapId(player)
return player.mapId or "3"
end
function VvApi.getPos(player)
return player.x or 0, player.y or 0
end
function VvApi.getVar(key, default)
_G.__vv_vars = _G.__vv_vars or {}
local v = _G.__vv_vars[key]
if v == nil then return default end
return v
end
function VvApi.setVar(key, value)
_G.__vv_vars = _G.__vv_vars or {}
_G.__vv_vars[key] = value
end
function VvApi.removeItemByName(player, itemName, count)
-- TODO: 替换为扣除藏宝图接口
print("remove item", VvApi.getPlayerName(player), itemName, count)
return true
end
function VvApi.addItem(player, itemName, count)
print("add item", VvApi.getPlayerName(player), itemName, count)
end
function VvApi.spawnMonster(mapId, x, y, monsterName, count)
-- TODO: 替换为刷怪接口
print("spawn", monsterName, count, mapId, x, y)
end
function VvApi.sendMsg(player, msg)
print(msg)
end
function VvApi.broadcast(msg)
print("[broadcast]", msg)
end
local Treasure = {}
Treasure.config = {
mapItem = "藏宝图",
digDistance = 5,
points = {
{ mapId = "3", name = "土城", x = 330, y = 330 },
{ mapId = "3", name = "土城", x = 360, y = 285 },
{ mapId = "3", name = "土城", x = 300, y = 360 },
{ mapId = "0150", name = "盟重郊外", x = 120, y = 190 },
},
rewards = {
common = {
{ item = "金币袋", count = 1 },
{ item = "强化石", count = 2 },
{ item = "经验卷轴", count = 1 },
},
rare = {
{ item = "高级藏宝箱", count = 1 },
{ item = "稀有宝石", count = 1 },
},
},
}
local function key(name, field)
return "treasure:" .. field .. ":" .. name
end
local function distance(x1, y1, x2, y2)
local dx = x1 - x2
local dy = y1 - y2
return math.sqrt(dx * dx + dy * dy)
end
local function pick(list)
return list[math.random(1, #list)]
end
function Treasure.createMap(player)
local name = VvApi.getPlayerName(player)
if not VvApi.removeItemByName(player, Treasure.config.mapItem, 1) then
VvApi.sendMsg(player, "需要一张藏宝图。")
return false
end
local point = pick(Treasure.config.points)
VvApi.setVar(key(name, "active"), 1)
VvApi.setVar(key(name, "mapid"), point.mapId)
VvApi.setVar(key(name, "x"), point.x)
VvApi.setVar(key(name, "y"), point.y)
VvApi.sendMsg(player, "藏宝图显示:宝藏位于 " .. point.name .. " 附近,坐标 " .. point.x .. "," .. point.y .. "。")
return true
end
function Treasure.dig(player)
local name = VvApi.getPlayerName(player)
if VvApi.getVar(key(name, "active"), 0) ~= 1 then
VvApi.sendMsg(player, "你当前没有可挖掘的藏宝点。")
return false
end
local targetMap = VvApi.getVar(key(name, "mapid"), "")
local targetX = VvApi.getVar(key(name, "x"), 0)
local targetY = VvApi.getVar(key(name, "y"), 0)
local x, y = VvApi.getPos(player)
if VvApi.getMapId(player) ~= targetMap or distance(x, y, targetX, targetY) > Treasure.config.digDistance then
VvApi.sendMsg(player, "藏宝点不在这里,请到藏宝图提示坐标附近再挖。")
return false
end
VvApi.setVar(key(name, "active"), 0)
local eventRoll = math.random(1, 100)
if eventRoll <= 55 then
local reward = pick(Treasure.config.rewards.common)
VvApi.addItem(player, reward.item, reward.count)
VvApi.sendMsg(player, "挖宝成功,获得:" .. reward.item)
elseif eventRoll <= 75 then
VvApi.spawnMonster(targetMap, x, y, "藏宝守卫", 2)
VvApi.sendMsg(player, "你挖出了藏宝守卫!")
elseif eventRoll <= 90 then
VvApi.sendMsg(player, "这里被别人挖过了,什么也没有。")
elseif eventRoll <= 97 then
VvApi.sendMsg(player, "触发机关,脚下一震,宝藏消失了。")
else
local reward = pick(Treasure.config.rewards.rare)
VvApi.addItem(player, reward.item, reward.count)
VvApi.broadcast("玩家 " .. name .. " 挖宝时发现稀有宝藏,获得 " .. reward.item .. "!")
end
return true
end
return Treasure
|
|