Skip to content
Snippets Groups Projects
Commit fa81cbde authored by dg's avatar dg
Browse files

When targetting the map will scroll to follow the target cursor

git-svn-id: http://svn.net-core.org/repos/t-engine4@2214 51575b47-30f0-44d4-a5cc-537603b46e54
parent 9459850a
No related branches found
No related tags found
No related merge requests found
......@@ -685,6 +685,7 @@ end
--- Sets the current view area if x and y are out of bounds
function _M:moveViewSurround(x, y, marginx, marginy)
local omx, omy = self.mx, self.my
if self.mx + marginx >= x or self.mx + self.viewport.mwidth - marginx <= x then
self.mx = x - math.floor(self.viewport.mwidth / 2)
self.changed = true
......@@ -694,6 +695,7 @@ function _M:moveViewSurround(x, y, marginx, marginy)
self.changed = true
end
self:checkMapViewBounded()
return self.mx - omx, self.my - omy
end
--- Checks the map is bound to the screen (no "empty space" if the map is big enough)
......
......@@ -189,14 +189,14 @@ function _M:freemove(dir)
self.target.x = self.target.x + d[1]
self.target.y = self.target.y + d[2]
self.target.entity = game.level.map(self.target.x, self.target.y, engine.Map.ACTOR)
if self.on_set_target then self:on_set_target() end
if self.on_set_target then self:on_set_target("freemove") end
end
function _M:setSpot(x, y)
function _M:setSpot(x, y, how)
self.target.x = x
self.target.y = y
self.target.entity = game.level.map(self.target.x, self.target.y, engine.Map.ACTOR)
if self.on_set_target then self:on_set_target() end
if self.on_set_target then self:on_set_target(how) end
end
function _M:scan(dir, radius, sx, sy, filter)
......@@ -233,7 +233,7 @@ function _M:scan(dir, radius, sx, sy, filter)
self.target.entity = actors[1].a
self.target.x = self.target.entity.x
self.target.y = self.target.entity.y
if self.on_set_target then self:on_set_target() end
if self.on_set_target then self:on_set_target("scan") end
end
end
......
......@@ -32,6 +32,16 @@ function _M:init()
self.target.target.entity = self.player
self.old_tmx, self.old_tmy = 0, 0
self.target_style = "lock"
-- Allow scrolling when targetting
self.target.on_set_target = function(self, how)
if self.key ~= self.targetmode_key then return end
local dx, dy = game.level.map:moveViewSurround(self.target.x, self.target.y, 1, 1)
if how == "mouse" then
local cx, cy = core.mouse.get()
core.mouse.set(cx - game.level.map.tile_w * dx, cy - game.level.map.tile_h * dy)
end
end
end
--- Maintain the current target each tick
......@@ -184,7 +194,7 @@ function _M:targetMouse(button, mx, my, xrel, yrel, event)
if self.key == self.targetmode_key then
-- Target with mouse
if button == "none" and xrel and yrel and event == "motion" then
self.target:setSpot(tmx, tmy)
self.target:setSpot(tmx, tmy, "mouse")
-- Accept target
elseif button == "left" and not xrel and not yrel and event == "button" then
self:targetMode(false, false)
......
......@@ -344,9 +344,6 @@ end
function _M:initTargeting()
engine.interface.GameTargeting.init(self)
self.target.on_set_target = function(self)
-- if game.key == game.targetmode_key then game.level.map:moveViewSurround(self.target.x, self.target.y, 8, 8) end
end
end
......
......@@ -52,6 +52,13 @@ static int lua_get_mouse(lua_State *L)
return 2;
}
static int lua_set_mouse(lua_State *L)
{
int x = luaL_checknumber(L, 1);
int y = luaL_checknumber(L, 2);
SDL_WarpMouse(x, y);
return 0;
}
extern int current_mousehandler;
static int lua_set_current_mousehandler(lua_State *L)
{
......@@ -68,6 +75,7 @@ static int lua_set_current_mousehandler(lua_State *L)
static const struct luaL_reg mouselib[] =
{
{"get", lua_get_mouse},
{"set", lua_set_mouse},
{"set_current_handler", lua_set_current_mousehandler},
{NULL, NULL},
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment