action()
if(dead)
slow_down()
return
move(dir)
if(at_edge())
turn_around()
if(!target)
get_target()
if(target)
if(shooting || target:dead) return
if(bounds_dist(src,target) > 90)
target = null
return
var/turn_to = get_dir(target, src)
if(dir == turn_to)
turn_around()
shoot(dir)
..()
get_target()
for(var/mob/m in oview(5, src))
if(m.dead)
return
src.target = m
Problem description: How would I set about improving this piece of code? The turn_to variable I couldn't figure out another way for the NPC to face the player.
Bounds_dist is the only way I could figure to clear the target and stop the NPC from shooting.
Please note I'm using the forum_account sidescroller library, the code works I just want to learn to improve. A bit of this I just made from the reference.
Also you'd have probably laughed when you seen my first attempt at this, it used to 3 bullets rapidly then a copy of my character in a random direction.
Is target defined as a mob var? If so, then you shouldn't need the : operator; you should be using the . operator instead which can check var safety at compile time. If you've only defined the dead var under a subtype such as /mob/player, then this would point to a design flaw.
This does not look right. If you want to face the target, then the call should be get_dir(src, target), and you want to change directions if dir does not match turn_to. At the moment, it appears your mob will only turn around if they're facing exactly the other way.
You want continue, not return. Return will bail out of this loop as soon as it finds a dead mob, instead of skipping on to the next one.