Here, I'll give a basic example of how it'd work in a 2D game, since that's how ZC is. But it's not ZC's scripting syntax. I even put comments for you guys =).
The current object calling this code every frame would be the fish object. Player is the player object.
var; xtoplayer = Player.x - x; //Store the x pixels distance from the player.
var; ytoplayer = Player.y - y; //Store the y pixels distance from the player.
/*
Use the distance formula sqrt(x^2 + y^2) to calculate the distance in pixels
from the player, and round it down using the floor function.
*/
var; distancetoplayer = floor(sqrt((xtoplayer * xtoplayer) + (ytoplayer * ytoplayer)));
/*
If the player comes within 32 pixels of this object, make it
move out of the way.
*/
if (distancetoplayer <= 32 )
{
/*
Change the direction of motion to be that in the direction away
from the player.
*/
direction=point_direction(Player.x, Player.y, x, y);
/*
Give it a speed of 4 pixels per frame, as to give it a sort of
hurried appeal.
*/
speed=4;
/*
In order to use the trig functions, the argument needs to be in
radians. So multiply the direction by pi/180 and store it in
a temporary variable.
*/
var; trigdirection=direction *pi / 180;
/*
Check for a collission in the direction of motion with a loop.
We want the fish to keep turning until it can go in a path such that
it doesn't run into a wall.
*/
while (!place_free(x + (speed * cos(trigdirection)), y + (speed * sin(trigdirection)))
{
direction+=10; //Turn 10 degrees counterclockwise.
}
}
Notes
-Trig is used since direction would be a value between 0 and 360, as it is a degrees measurement. Degrees can be easily converted into radians.
-If you want to make sure the fish doesn't collide with other fish or objects as well, you'd add that to the condition in the loop.
There, that should appease you peeps. But I've got more important things to code than some fish right now XD. Though I'll stash it and tweak it for later.
Edited by Koh, 30 May 2013 - 05:59 PM.

