Create simple behaviour tree

Started by
2 comments, last by Tom Sloper 1 year, 1 month ago

Hi,

I want to create a simple behaviour tree for an npc.

The default state is the idle state where the npc just stands and does nothing, if a certain amount of time passed and the npc didnt see the player then the npc should transition to the patrole state. If the npc sees the player while he is in idle or patrole mode he should switch to the chase state. if he is in the chase state and the distance between the npc and the player is greater than a certain value the npc should switch to idle mode. if the npc finished patroling then he should automatically switch to the idle state.

could someone please provide me with a corresponding behaviour tree implementation? Thanks very much!

Advertisement

What you need is some states, knowing when to switch between them. And a class to not allow unwanted changes:

	enum npcAI { IDLE=0, PATROL, CHASE };

class Player {
	int x_ = 0, y_ = 0;
	public:
};

class NPC_AI {
	int x = 0, y = 0, dirx = 0, diry = 0;
	int_fast8_t state = npcAI::IDLE;
	const Player *target = nullptr;
	public:
	auto operator () (const Player &player) {
//Find distance between both. (NPC's) vision is some global constant.
		if (distance > vision) { target = nullptr; return state = npcAI::PATROL; }
		state = npcAI::CHASE;
		target = &player;    //Gets its address.
//Calculate dirx, diry, the x,y direction per frame, when chasing, according
//to player's location.
		return state;
	}
};

int main () {
	NPC_AI guard {/*Beginning position*/}; Player player {/*Beginning position*/};
	guard (player);
	return EXIT_SUCCESS;
}

@átilafernandes , please don't necro. Thread locked.

-- Tom Sloper -- sloperama.com

This topic is closed to new replies.

Advertisement