tibia-client/modules/game_minimap/minimap.lua

184 lines
4.7 KiB
Lua
Raw Normal View History

DEFAULT_ZOOM = 60
MAX_FLOOR_UP = 0
MAX_FLOOR_DOWN = 15
navigating = false
minimapWidget = nil
minimapButton = nil
minimapWindow = nil
--[[
2012-07-12 21:16:23 +02:00
Known Issue (TODO):
If you move the minimap compass directions and
you change floor it will not update the minimap.
]]
function init()
2012-08-18 12:34:15 +02:00
connect(g_game, {
onGameStart = online,
onGameEnd = offline,
})
2012-08-18 12:48:50 +02:00
connect(LocalPlayer, { onPositionChange = center })
2012-07-12 21:16:23 +02:00
g_keyboard.bindKeyDown('Ctrl+M', toggle)
minimapButton = TopMenu.addRightGameToggleButton('minimapButton', tr('Minimap') .. ' (Ctrl+M)', 'minimap.png', toggle)
minimapButton:setOn(true)
minimapWindow = g_ui.loadUI('minimap.otui', modules.game_interface.getRightPanel())
2012-06-22 07:26:22 +02:00
minimapWidget = minimapWindow:recursiveGetChildById('minimap')
g_mouse.bindAutoPress(minimapWidget, compassClick, nil, MouseRightButton)
g_mouse.bindAutoPress(minimapWidget, compassClick, nil, MouseLeftButton)
2012-06-22 07:26:22 +02:00
minimapWidget:setAutoViewMode(false)
minimapWidget:setViewMode(1) -- mid view
minimapWidget:setDrawMinimapColors(true)
minimapWidget:setMultifloor(false)
minimapWidget:setKeepAspectRatio(false)
minimapWidget.onMouseRelease = onMinimapMouseRelease
minimapWidget.onMouseWheel = onMinimapMouseWheel
reset()
end
function terminate()
2012-08-18 12:34:15 +02:00
disconnect(g_game, {
onGameStart = online,
onGameEnd = offline,
})
2012-08-18 12:48:50 +02:00
disconnect(LocalPlayer, { onPositionChange = center })
2012-08-18 12:34:15 +02:00
if g_game.isOnline() then
saveMap()
end
2012-07-12 21:16:23 +02:00
2012-06-26 00:13:30 +02:00
g_keyboard.unbindKeyDown('Ctrl+M')
minimapButton:destroy()
2012-06-22 07:26:22 +02:00
minimapWindow:destroy()
end
2012-08-18 12:34:15 +02:00
function online()
reset()
loadMap()
end
function offline()
saveMap()
end
function loadMap()
local clientVersion = g_game.getClientVersion()
local minimapFile = '/minimap_' .. clientVersion .. '.otcm'
if g_resources.fileExists(minimapFile) then
g_map.clean()
g_map.loadOtcm(minimapFile)
end
2012-08-18 12:34:15 +02:00
end
function saveMap()
local clientVersion = g_game.getClientVersion()
local minimapFile = '/minimap_' .. clientVersion .. '.otcm'
g_map.saveOtcm(minimapFile)
2012-08-18 12:34:15 +02:00
end
function toggle()
2012-06-24 14:41:39 +02:00
if minimapButton:isOn() then
2012-06-22 07:26:22 +02:00
minimapWindow:close()
minimapButton:setOn(false)
2012-06-24 14:41:39 +02:00
else
minimapWindow:open()
minimapButton:setOn(true)
2012-06-22 07:26:22 +02:00
end
end
function isClickInRange(position, fromPosition, toPosition)
return (position.x >= fromPosition.x and position.y >= fromPosition.y and position.x <= toPosition.x and position.y <= toPosition.y)
2012-06-24 14:41:39 +02:00
end
function reset()
local player = g_game.getLocalPlayer()
if not player then return end
minimapWidget:followCreature(player)
minimapWidget:setZoom(DEFAULT_ZOOM)
2012-06-26 07:54:55 +02:00
end
2012-07-12 21:16:23 +02:00
function center()
local player = g_game.getLocalPlayer()
if not player then return end
minimapWidget:followCreature(player)
end
function compassClick(self, mousePos, mouseButton, elapsed)
if elapsed < 300 then return end
navigating = true
2012-07-12 21:16:23 +02:00
local px = mousePos.x - self:getX()
local py = mousePos.y - self:getY()
local dx = px - self:getWidth()/2
local dy = -(py - self:getHeight()/2)
local radius = math.sqrt(dx*dx+dy*dy)
local movex = 0
local movey = 0
2012-07-12 21:16:23 +02:00
dx = dx/radius
dy = dy/radius
if dx > 0.5 then movex = 1 end
if dx < -0.5 then movex = -1 end
if dy > 0.5 then movey = -1 end
if dy < -0.5 then movey = 1 end
local cameraPos = minimapWidget:getCameraPosition()
2012-07-12 21:16:23 +02:00
local pos = {x = cameraPos.x + movex, y = cameraPos.y + movey, z = cameraPos.z}
minimapWidget:setCameraPosition(pos)
2012-06-26 07:54:55 +02:00
end
function onButtonClick(id)
2012-06-26 07:54:55 +02:00
if id == "zoomIn" then
minimapWidget:setZoom(math.max(minimapWidget:getMaxZoomIn(), minimapWidget:getZoom()-15))
elseif id == "zoomOut" then
minimapWidget:setZoom(math.min(minimapWidget:getMaxZoomOut(), minimapWidget:getZoom()+15))
2012-07-12 19:29:44 +02:00
elseif id == "floorUp" then
2012-06-26 07:54:55 +02:00
local pos = minimapWidget:getCameraPosition()
pos.z = pos.z - 1
if pos.z > MAX_FLOOR_UP then
minimapWidget:setCameraPosition(pos)
end
2012-07-12 19:29:44 +02:00
elseif id == "floorDown" then
2012-06-26 07:54:55 +02:00
local pos = minimapWidget:getCameraPosition()
pos.z = pos.z + 1
if pos.z < MAX_FLOOR_DOWN then
minimapWidget:setCameraPosition(pos)
end
2012-06-26 07:54:55 +02:00
end
end
function onMinimapMouseRelease(self, mousePosition, mouseButton)
if navigating then
navigating = false
return
end
local tile = self:getTile(mousePosition)
if tile and mouseButton == MouseLeftButton and self:isPressed() then
local dirs = g_map.findPath(g_game.getLocalPlayer():getPosition(), tile:getPosition(), 127)
if #dirs == 0 then
modules.game_textmessage.displayStatusMessage(tr('There is no way.'))
return true
end
g_game.autoWalk(dirs)
return true
end
return false
end
function onMinimapMouseWheel(self, mousePos, direction)
if direction == MouseWheelUp then
self:zoomIn()
else
self:zoomOut()
end
end
function onMiniWindowClose()
minimapButton:setOn(false)
end