Sign in to follow this  
ollyb342

Simple Platform Game AI

Recommended Posts

Hey guys. I'm sure that this question has been asked a million times before (I did do a search through this section of the forum but had no luck) so I'm sorry if I'm showing my ignorance here. I'd like to create some extremely simple AI for a platform game that I'm writing; I'd like the enemies to be in the style of the original Mario games enemies (wander left until collision, wander right until collision etc.) but I have no idea how to implement this. If anyone has any ideas on this then let me know :D Another point that I might aswell mention while I'm here is that I don't really know how's best to actually initialise the enemies, should I have them in a vector and read their locations from a file or how is this normally done? Cheers guys Heh.

Share this post


Link to post
Share on other sites
Breaking it down, you are looking at three things:

1) A way to find out if something interesting happened. This means either polling the game world (each object asks amIColliding() each frame) or getting notified about an event (CollisionResolver checks for collisions and creates an event for each collision, which is somehow dispatched, either right then or later, to the AI. Observer pattern can help here - make your entities register themselves as listeners for the events they want to receive)

2) A way to decide what to do each frame, and how to react to "interesting" events. I think one of the most straightforward ways of implementing that is a finite state machine. This FSM stores the current state, and executes it when you call update(). The state can then react to interesting events, determine what action should be taken right now, or transition to another state (for instance, if the "attack" state notices that hp<20%, it might transition to the "flee" state).

3) A way to turn your AI's decisions into concrete actions. One way you might do this is to store the currently requested action, let's say in your case GoLeft, and then turn it into output when processing the entity (something like currentAction.apply(myEntity), which does myEntity.move(-5,0)

About initializing the enemies: Normally you'd have initial level data in a file somewhere, although nothing stops you from hard coding it. I usually use XML for all level data, but that's just a personal preference - you can store it however is most convenient for you for both editing it and loading it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this