让所有生物变成坐骑。
local mountMap = {}
local keyState = {}
-- ================= 配置参数 =================
local MOVE_SPEED = 0.2 -- 前进移动速度系数
local JUMP_FORCE = 0.8 -- 跳跃力度
-- ===========================================
local function Player_ClickActor(event)
local pid = event.eventobjid
local aid = event.toobjid
mountMap[pid] = aid
Creature:setAIActive(aid, false)
Player:mountActor(pid, aid, -1)
end
local function Player_DismountActor(event)
local pid = event.eventobjid
local aid = mountMap[pid]
if aid then
Creature:setAIActive(aid, true)
mountMap[pid] = nil
keyState[pid] = nil
end
end
local function Player_InputKeyDown(event)
local pid = event.eventobjid
local vkey = event.vkey
if vkey == "W" then
if not keyState[pid] then keyState[pid] = {} end
keyState[pid].W = true
end
if vkey == "SPACE" then
local aid = mountMap[pid]
if aid then
Actor:appendSpeed(aid, 0, JUMP_FORCE, 0)
end
end
end
-- 4. 按键松开:停止移动状态
local function Player_InputKeyUp(event)
local pid = event.eventobjid
if event.vkey == "W" then
if keyState[pid] then
keyState[pid].W = false
end
end
end
local function Game_Run()
for pid, aid in pairs(mountMap) do
local result, yaw = Actor:getFaceYaw(pid)
if result == 0 then
Actor:setFaceYaw(aid, yaw)
if keyState[pid] and keyState[pid].W then
local rad = math.rad(yaw)
local dx = -math.sin(rad) * MOVE_SPEED
local dz = -math.cos(rad) * MOVE_SPEED
Actor:appendSpeed(aid, dx, 0, dz)
end
end
end
end
ScriptSupportEvent:registerEvent("Player.ClickActor", Player_ClickActor)
ScriptSupportEvent:registerEvent("Game.Run", Game_Run)
ScriptSupportEvent:registerEvent("Player.DismountActor", Player_DismountActor)
ScriptSupportEvent:registerEvent("Player.InputKeyDown", Player_InputKeyDown)
ScriptSupportEvent:registerEvent("Player.InputKeyUp", Player_InputKeyUp)