mafon2
Member
I'm prototyping Pac-Man clone and wrote simple chase script, but it has 2 strange quirks:
1 - o_enemy don't want to chase o_player, it chases o_levelstart for some reason, though o_levelstart has nothing to do with the script or any object in the project, it used to draw a grid, I had to stick it to o_player and it worked, but after deleting it, o_enemy don't care for o_player anymore and just wanders away (?).
it says that there's a reference somewhere to o_levelstart, but I re-read all code 10 times and didn't find it. Ctrl+F doesn't show it either. The project used to crash without it, now it works :-\. And it's not like it's full of spaghetti - quite contrary.
2 - sometimes o_enemy walks through the walls on the left.
Script itself
you can see broken "AI" in "action": https://radikal.ru/video/bqVCjdcXrcd (yellow is a "coward" and pink targets a space to the right of o_player for now)
it used to be fine, before I deleted o_levelstart -_-.
1 - o_enemy don't want to chase o_player, it chases o_levelstart for some reason, though o_levelstart has nothing to do with the script or any object in the project, it used to draw a grid, I had to stick it to o_player and it worked, but after deleting it, o_enemy don't care for o_player anymore and just wanders away (?).
it says that there's a reference somewhere to o_levelstart, but I re-read all code 10 times and didn't find it. Ctrl+F doesn't show it either. The project used to crash without it, now it works :-\. And it's not like it's full of spaghetti - quite contrary.
2 - sometimes o_enemy walks through the walls on the left.
Script itself
Code:
// move speed
cell_size = 2;
// Checking distance to the target, different enemies have different targets (on create - target = "o_player" or target = "o_something_else")
A = point_distance(x, y-cell_size, target.x, target.y);
B = point_distance(x+cell_size, y, target.x, target.y);
C = point_distance(x, y+cell_size, target.x, target.y);
D = point_distance(x-cell_size, y, target.x, target.y);
// Excluding walls
if place_meeting(x,y-cell_size, o_wall)
{
A = 1000;
}
if place_meeting(x+cell_size,y, o_wall)
{
B = 1000;
}
if place_meeting(x,y+cell_size, o_wall)
{
C = 1000;
}
if place_meeting(x-cell_size,y, o_wall)
{
D = 1000;
}
// No turning back movement
// UP
if facing = "up"
{
if A = min(A,B,D)
{
y = y - cell_size;
facing = "up";
}
if B = min(A,B,D)
{
x = x + cell_size;
facing = "right";
}
if D = min(A,B,D)
{
x = x - cell_size;
facing = "left";
}
}
// RIGHT
if facing = "right"
{
if A = min(A,B,C)
{
y = y - cell_size;
facing = "up";
}
if B = min(A,B,C)
{
x = x + cell_size;
facing = "right";
}
if C = min(A,B,C)
{
y = y + cell_size;
facing = "down";
}
}
// DOWN
if facing = "down"
{
if B = min(B,C,D)
{
x = x + cell_size;
facing = "right";
}
if C = min(B,C,D)
{
y = y + cell_size;
facing = "down";
}
if D = min(B,C,D)
{
x = x - cell_size;
facing = "left";
}
}
// LEFT
if facing = "left"
{
if A = min(A,C,D)
{
y = y - cell_size;
facing = "up";
}
if C = min(A,C,D)
{
y = y + cell_size;
facing = "down";
}
if D = min(A,C,D)
{
x = x - cell_size;
facing = "left";
}
}
it used to be fine, before I deleted o_levelstart -_-.