ID:1396054
 
(See the best response by Ter13.)
Hi. I'm just wandering if there is anything that will enable me to move my mouse and have something follow it (e.g An Object). Specifically a demo or just a really good explanation.

And Forgive me for asking of two things in one post, though I'm wondering whether there are any good Tile to Tile smooth movement libraries or demos. I liked using step_size (changing walk speed is essential!) but it was rather tedious since my game is built for tile to tile movement and attacks. I also checked out a few delay demos; but they are awfully glitchy - my character warps steps when I incorporate it into my game.

Best response
In order to track the mouse, you are going to need to track some mouse coordinates from the client:

#define TILE_WIDTH 32
#define TILE_HEIGHT 32
#define DEFAULT_MAP "default.map1"

client
var/tmp
mouse_x
mouse_y
mouse_screen_loc
proc
MousePosition(params)
var/s = params2list(params)["screen-loc"]
var/x = 0
var/y = 0

var/s1 = copytext(s,1,findtext(s,",",1,0))
var/s2 = copytext(s,length(s1)+2,0)

var/colon1 = findtext(s1,":",1,0)
var/colon2 = findtext(s1,":",colon1+1,0)

if(colon2)
x = (text2num(copytext(s1,colon1+1,colon2))-1) *TILE_WIDTH
x += text2num(copytext(s1,colon2+1,0))-1

else
x = (text2num(copytext(s1,1,colon1))-1) * TILE_WIDTH
x += text2num(copytext(s1,colon1+1,0))-1

colon2 = findtext(s2,":",1,0)
y = (text2num(copytext(s2,1,colon2))-1) * TILE_HEIGHT
y += text2num(copytext(s2,colon2+1,0))-1

mouse_screen_loc = s
mouse_x = x
mouse_y = y
MouseMove(object,location,control,params)
if(control==DEFAULT_MAP)
MousePosition(params)


This will give you the mouse position in reference to the screen.

If you need mouse positions in reference to a particular object, I highly recommend you download my DatumPoint library, because it makes it quite easy:

client
var/tmp
point/mouse_world_loc = new()
MouseMove(var/atom/object,var/atom/location,var/control,var/params)
if(control==DEFAULT_MAP)
MousePosition(params)
var/list/l = params2list(params)
mouse_world_loc.Set(object)
mouse_world_loc.Add(object.pixel_x+text2num(l["icon-x"]),object.pixel_y+text2num(l["icon-y"]))


Making an object follow this, is as simple as getting the location from the point datum, and telling the object to move toward this point:

var/turf/t = client.mouse_world_loc.getLoc()
step(obj,get_dir(obj,t))


Now, this won't be perfect, because you need to account for pixel_step, but I'll leave that to you to figure out.

I left a link to a matrixstep proc I had written on one of Kaio's feature requests, specifically regarding a request to stop rounding off step values. If you want perfect pixel movement towards wherever your mouse is pointing, I'd take a look at that.

Also, a link to the DatumPoint library:

http://www.byond.com/developer/Ter13/DatumPoint
Oh, and in case you were wondering why you should download datumpoint? Because this is what you would have to do otherwise:

client
var/tmp
mouse_world_x
mouse_world_y
mouse_world_z
MouseMove(var/atom/object,var/atom/location,var/control,var/params)
if(control==DEFAULT_MAP)
MousePosition(params)
var/list/l = params2list(params)
mouse_world_x = (object.x-1)*TILE_WIDTH + object.pixel_x + text2num(l["icon-x"])
mouse_world_y = (object.y-1)*TILE_HEIGHT + object.pixel_y + text2num(l["icon-y"])
mouse_world_z = object.z
if(istype(object,/atom/movable))
var/atom/movable/m = object
mouse_world_x += m.step_x
mouse_world_y += m.step_y


var/turf/t = locate(round(client.mouse_world_x/TILE_WIDTH)+1,round(client.mouse_world_y/TILE_HEIGHT)+1,client.mouse_world_z)


As for a few other things, I should mention, I'm not sure what will happen when the object you hover over is a screen object. So... There's that, and if you hover your mouse over something that is overlaying the edge of the map, you are going to get all kinds of problems.

Admittedly, it would probably be easier to do something along these lines:

var/get_dir_to_point(var/atom/o,var/point/dp)
var/point/p = new/point(obj)
p.Sub(dp)
. = 0
if(p.x>0)
. |= WEST
else if(p.x<0)
. |= EAST
if(p.y>0)
. |= SOUTH
else if(p.y>0)
. |= NORTH


That should make it a bit more reliable.
Uhh...Wow.

I'll try to understand all of that xD! I'm still on my way to become awesome. Thanks for taking the time to write that again, Ter.
I'd actually taken the time to write most of it in another thread, I just repurposed it for your needs.
Even So~