GdxAI是libgdx的一个AI库,里面有各种AI系统和算法,包括有限状态机,状态树,行为算法(追逐,躲避...群聚),路径算法。基于Message和Scheduler两种事件驱动机制。这绝对是个好东西,但要讲这些知识的话,就不是局限于libgdx这个平台了,AI算法是游戏开发通用的,所以以后随着我的学习可能会有这个专题。这里就先贴上状态机的demo。
状态机逻辑:
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ai.fsm.State;
import com.badlogic.gdx.ai.msg.MessageManager;
import com.badlogic.gdx.ai.msg.Telegram;
/** @author davebaol */
public enum BobState implements State<Bob> {
ENTER_MINE_AND_DIG_FOR_NUGGET() {
@Override
public void enter (Bob bob) {
// if the miner is not already located at the goldmine, he must
// change location to the gold mine
if (bob.getLocation() != Location.GOLD_MINE) {
talk(bob, "Walkin' to the goldmine");
bob.setLocation(Location.GOLD_MINE);
}
}
@Override
public void update (Bob bob) {
// Now bob is at the goldmine he digs for gold until he
// is carrying in excess of MAX_NUGGETS. If he gets thirsty during
// his digging he packs up work for a while and changes state to
// go to the saloon for a whiskey.
bob.addToGoldCarried(1);
bob.increaseFatigue();
talk(bob, "Pickin' up a nugget");
// if enough gold mined, go and put it in the bank
if (bob.isPocketsFull()) {
bob.getStateMachine().changeState(VISIT_BANK_AND_DEPOSIT_GOLD);
}
if (bob.isThirsty()) {
bob.getStateMachine().changeState(QUENCH_THIRST);
}
}
@Override
public void exit (Bob bob) {
talk(bob, "Ah'm leavin' the goldmine with mah pockets full o' sweet gold");
}
},
GO_HOM