• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Fleeing enemy and collision

EDIT: Hey everyone! Help would be appreciated :) So I fixed the collision, however, during the flee state, and not the chase state, the bat will flee and then turn around and chase the player, and then spaz out

Code:
// Swoop code
// This code assumes that obj_player and obj_bat both have centered origins

vdir = 1; // Bat starts by going down, assuming it will generally be perched high
if (obj_bat.y < obj_hero.y - max_swoop) {vdir = 1;} // If bat is too high, start flying down
if (obj_bat.y > obj_hero.y + max_swoop) {vdir = -1;} // If bat is too low, start flying up
if (obj_bat.y = obj_hero.y) {vdir = 0;}

x += hdir;
y += vdir;

if ((abs(obj_hero.x - obj_bat.x) > turn_distance) && (sign(obj_hero.x - obj_bat.x) == -hdir)) // If bat is further than turn_distance and facing away from player
{
 hdir = -hdir; // Reverse direction
}
// No need for a separate turn state, since I'm just abruptly turning

var right_taken = place_meeting(x-1, y, obj_wall);
var left_taken = place_meeting(x+1, y, obj_wall);
var up_taken = place_meeting(x, y+1, obj_wall);
var down_taken = place_meeting(x, y-1, obj_wall);

if right_taken {
    x += 1;
}
if left_taken {
    x -= 1;
}
if up_taken {
    y -= 1;
}
if down_taken {
    y += 1;
}

Code:
// Flee code
// Similar to swoop, but make it move away from the player

vdir = 3; // Bat starts by going down, assuming it will generally be perched high
if (obj_bat.y < obj_hero.y - max_swoop) {vdir = 1;} // If bat is too high, start flying down
if (obj_bat.y > obj_hero.y + max_swoop) {vdir = -1;} // If bat is too low, start flying up
if (obj_bat.y = obj_hero.y) {vdir = 0;}

x += -hdir;
y += -vdir;

if ((abs(obj_hero.x - obj_bat.x) > turn_distance) && (sign(obj_hero.x - obj_bat.x) == -hdir)) // If bat is further than turn_distance and facing away from player
{
 hdir = -hdir; // Reverse direction
}
// No need for a separate turn state, since I'm just abruptly turning

if (distance_to_object(obj_hero) > safe_distance*2) {state = state.idle;}

var right_taken = place_meeting(x-1, y, obj_wall);
var left_taken = place_meeting(x+1, y, obj_wall);
var up_taken = place_meeting(x, y+1, obj_wall);
var down_taken = place_meeting(x, y-1, obj_wall);

if right_taken {
    x += 1;
}
if left_taken {
    x -= 1;
}
if up_taken {
    y -= 1;
}
if down_taken {
    y += 1;
}

I'm probably doing something very obviously wrong, but I need help. Flying enemies in platformers seem hard to code if you've never done it before :confused:
 
Last edited:

jo-thijs

Member
My guess would be it's got something to do with this line:
Code:
if (distance_to_object(obj_hero) > safe_distance*2) {state = state.idle;}
What code is executed when state == state.idle (that's actualy legit code?) and what code sets the bat to the fleeing state?
 

Roderick

Member
My guess would be it's got something to do with this line:
Code:
if (distance_to_object(obj_hero) > safe_distance*2) {state = state.idle;}
What code is executed when state == state.idle (that's actualy legit code?) and what code sets the bat to the fleeing state?
He's using some code that I wrote up the other day. state.idle is an enum set elsewhere.
 

jo-thijs

Member
I figured state would be an enum, but I didn't think state could then still be an instance variable as well.
 
@Roderick @jo-thijs I think the problem was that the bat would flee if the hero was carrying a torch until it got to a certain radius away from the hero. The problem was that it would get to that safe distance and then immediately trigger the aggro state because it was in its idle state and within the aggro range. It would move 1 pixel towards the player, entering the fleeing radius again and then move away 1 pixel before entering idle and then chase again and again, causing the bat to spaz out and look like its shaking from side to side. But I figured that out! :)
The only issue now is that the bat is still not colliding with corners. It'll stop in a corner but if the player gets too close, it tries to fly away and then goes through the corner of the walls. If either of you have any ideas, that'd be super helpful! I'm guessing that it has to do with the fact that the walls don't overlap on the corners, but rather I have another wall object positioned between the two walls that form the corner. But if I stretched the object in the room so that it overlaps, the artwork gets all messed up
 

jo-thijs

Member
Well, could you give us your collision code?
The code we've currently got barely deals with collisions at all.
 

jo-thijs

Member
The reason might be because of this:
Code:
vdir = 3; // Bat starts by going down, assuming it will generally be perched high
Moving your bat object 3 pixels vertically, while your collision code only works for moving 1 pixel at the time.

A solution would be to perform collision checks before you execute code like this:
Code:
x += -hdir;
y += -vdir;
instead of after, preventing overlap, not solving it.
(although technically, your current code also still kind of prevents it, but yeah)
 
Top