ID:145767
 
My goal was to create a system where a mob could walk a bit with the screen centered then it would lock until they go to the edge and then it would reenter for a bit etc. A system like this was sort of used in A Link to the Past.

Here is what I came up with, but it doesn't always work. I know the code is sloppy, but I am trying. I couldn't find anything on this topic so I decided to take a shot at it. The problem is that sometimes the screen goes black for a bit, I think it has to do with where the eye is being set at. I tried to use lazy_eye instead of setting the eye, but this seemed to be more flexible. Spare no expense in criticizing my code, (as long as you help it a bit), I want this to be as rugged as possible. Also if possible I would like to see if I could include the diagonal directions; I tried by treating them as two directions but that didn't work. Thank you for your help.

Code:
client
var/tmp
movex = 0 //How many step they have taken since the last recenter
movey = 0
ckx //Check to see if the screen is locked
cky
ClVw //Client View Size
xt //Vars for locking the eye
yt
Move(src,dir)
if(dir == NORTHWEST) return
if(dir == SOUTHEAST) return
if(dir == NORTHEAST) return
if(dir == SOUTHWEST) return
. = ..()
if(dir == NORTH && .) movey += 1
if(dir == SOUTH && .) movey -= 1
if(dir == EAST && .) movex += 1
if(dir == WEST && .) movex -= 1

if((movey > ClVw || movey < -ClVw) && !cky) //North and South
yt = mob.y //Store the y value for use in changing the eye
eye = locate(mob.x,yt,mob.z) //Not set to just mob because this locks their view
cky = 1 //So it wont wont keep changing the eye
if((movey > 2*ClVw || movey < -2*ClVw) && cky)
eye = mob //center the eye on the mob
movey = 0 //reset how far they have walked
cky = 0 //restart the process

if((movex > ClVw || movex < -ClVw) && !ckx) //East and West
xt = mob.x
eye = locate(xt,mob.y,mob.z)
ckx = 1
if((movex > 2*ClVw || movex < -2*ClVw) && ckx)
eye = mob
movex = 0
ckx = 0
if(ckx || cky) //So if only one direction is locked it will still center the other way
if(ckx && !cky) eye = locate(xt,mob.y,mob.z)
if(!ckx && cky) eye = locate(mob.x,yt,mob.z)
else eye = locate(xt,yt,mob.z)
return ..()

client
view = 10

mob
icon = 'mob.dmi'
New()
spawn(..())
src.client.xt = src.x
src.client.yt = src.y
src.client.ClVw = src.client.view


Problem description:

Check if that locate is returning null.

#define nlocate(x,y,z) locate(x,y,z)?locate(x,y,z):eye


Then use nlocate(), not locate. (For that piece of code only)

In response to Jp
I can see how that would help the problem, but I tried it and it still does the same thing.