How to structure a decent enemy ai

yes more clarification because my tism brain doesn’t work that well with just words (I.e text based directions)

Imagine your boss enemy is wandering to the right. The player is to the left of the enemy and is approaching the boss. The boss notices the player and switches to attack mode. It sets the sprite’s direction, but because the boss was traveling to the right, the attack animation plays facing to the right.

So, to fix that, you would check what side of the boss is the player on? If it’s the left side, make the boss face left. If it’s not on the left, the player must be on the right, so make the boss face right.

if enemy.global_position.x < player.global_position:
    enemy.animations.flip_horizontal = false
else:
    enemy.animations.flip_horizontal = true

In the code above, I’m comparing the boss’s position (specifically the left-right component of it) to the player’s position. If the player’s position is to the right of the boss’s, make the boss face right. Otherwise, the player must be to the left of the boss, so make the boss face left.

EDIT: easier way to do it to set the flip property directly

would it be better to remake the ai from scratch and if so is there a good guide for making one that is very adaptable/configurable for a 2d platformer?

No no! You have a very good AI, but it just needs some visual tweaks. I am not an expert on making enemy AIs (in fact, I’m having trouble with that right now), but I think yours is great.

If you ever do want to remake something from scratch, don’t delete the files. Keep them as something to learn from.

tbh i’ve just been splicing tutorials and just wing some of it (parts that I cant find in tutorials)

I understand most of it but for me at least its very spaghetti coded even with component based structure and script based state machine

like for the enemy there’s three ways I get the direction and it feels off

I have OCD (unconfirmed… yet) so what I would do is create an entirely different bunch of scripts and nodes and then copy over one function at a time whilst closely examining each one for things I could do better. But I’m not you lol. Do whatever you feel is best. One thing I would recommend, though, is to not mess with the scale of nodes. I haven’t found a way to set the point around which they scale yet. You might end up with a desync between the visual (sprite) and physical (collision)

it’s on the player too

I did it this way so the stuff on the player will all flip together.

As I’m typing this a question came up, would the all flip if I put it under the animation player?

Actually, flipping the Body node’s scale.x is what you should do. You need to move the hit boxes with the orientation of the enemy. They would all flip if you put them under the AnimationPlayer, as long as you’re setting scale.x