Friday, September 27, 2013

State Based AI

I want to explain a little bit about FSM based AI. The concept is simple: an AI has a set of behaviors, and acts on a behavior depending on certain conditions. The enemies in Cranky Rampage all use this method.

The Sentry is your basic cannon fodder enemy. It can be in any of three states:

private int state;
private final int MOVING_TO_PLAYER = 0;
private final int SHOOTING = 1;
private final int COOLDOWN = 2;

When in the MOVING_TO_PLAYER state, the Sentry will simply try to get closer to the player. Once it reaches a certain proximity, it will move on to the SHOOTING state. Here, it will stop moving and fire a bullet. Then it will move into the COOLDOWN state where it will wait for some time. I have to find the edges in this FSM, meaning I have to find which states can transition to which other states.

MOVING_TO_PLAYER transitions to SHOOTING, SHOOTING transitions to COOLDOWN, and COOLDOWN can either go back to MOVING_TO_PLAYER or SHOOTING. So here's our list of edges for the graph:

MOVING_TO_PLAYER --> SHOOTING : if within range
SHOOTING --> COOLDOWN
COOLDOWN --> MOVING_TO_PLAYER : if out of range
COOLDOWN --> SHOOTING : if within range

Now coding the Sentry's AI is simple.

if(state == MOVING_TO_PLAYER) {
    if(player.getx() < x) {
        left = true;
        right = false;
    }
    else if(player.getx() > x) {
        left = false;
        right = true;
    }
    if(Math.abs(player.getx() - x) < range) {
        state == SHOOTING;
    }
}
else if(state == SHOOTING) {
    left = right = false;
    GameObjectFactory.createBullet(...);
    state = COOLDOWN;
}
else if(state == COOLDOWN) {
    timer++;
    if(timer > time) {
        timer = 0;
        if(Math.abs(player.getx() - x) < range) {
            state = SHOOTING;
        }
        else {
            state = MOVING_TO_PLAYER;
        }
    }
}

This is an easy way to give your NPCs some behavior. State based AI is simple to implement and can be effective.


Sunday, September 22, 2013

Cranky Rampage

I seriously never would have thought my channel would get so many subscribers. As of this post, I'm sitting at 865. I remember back when I posted a few videos about setting up a game with only a few video views and a couple subs. I was pretty content with that. It's just been crazy now.

Unfortunately I don't have any more tutorial ideas. So this is probably the end of that run. The only thing left is to just make some games.

I recently got started on a new game: Cranky Rampage. It's about a guy who just wants to get some sleep, but the alien invasion is making too much noise, so he decides to do something about it. This game is a throwback to arcade platform shoot em ups.


I'm hoping to get a demo + source released soon.

Lol nope. I've stopped working on it. Here's the source. You can dissect it if you want.
https://dl.dropboxusercontent.com/u/59779278/games/Cranky%20Rampage.rar