Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm having a difficult time to understand , how to implement the behavior of an enemy in a tile based game,

  • movement
  • attack
  • behavior per enemy level ( higher level enemies should be hard to kill)
  • defend
  • other behavior

since the game is a tile game written in javascript, the behavior of the enemies could be implement as functions like

  • Chaser - hunts its opponents
  • Brave - does not start running if with low HP
  • Sensitive - when damaged might take additional damage

using the standard entity class , based on the configuration defined for the enemy, could it be extend as the behavior?

Or write like standard enemy behavior and additionally write separate scripts per enemy on how they should behave against the opponent, surroundings, etc..

share|improve this question

closed as not a real question by Philipp, Laurent Couvidou, Anko, Byte56, Nicol Bolas Jun 13 '13 at 16:35

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

There are countless ways to do this, but no single correct way. This unfortunately makes this question quite unsuitable for the Q&A format of stackexchange. – Philipp Jun 13 '13 at 8:16 is a great overview of the concepts – gareththegeek Jun 13 '13 at 9:43
up vote 2 down vote accepted

Here are my thoughts:

  • Movement: you can do with path finding,if you find it hard to implement as an algorithm start with simple logic (while move if hit wall do...), and after that try to create a simple algorithm to make it more efficient. The A* algorithm seems to be the best for path finding. If that's too hard use "zombie movements" (if target.x>me.x me.x-=speed...).

  • Attack: you can use collision detection for close range (and timer so it wont attack every tick of game loop), check distance of entities, if close shot. Or you can use raycasting for FOV.

  • Behavior per enemy level: check Pacman every ghost had a different behavior pattern. You can also use a finite state machine (i used it for patrolling enemy).

  • Defend: like attack but you should randomize so it wont block every time, or create a weak spot an enemy can't defend from (in Golden Axe it was jump attack and from behind, and also when the enemy slept, ate, etc.).

As this is javascript you should take a look at some AS3 AI (and get a good SWF decompiler for educational purpose ).

Also check out waypoints, circular movement (mostly like circle point by point drawing functions with trigonometry), and move by path (irregular paths I found it simple to learn its easy math even for me).

Check Google for tutorials and info, research is the programmer's best friend. Here are some of my findings:

Edit: about tiles you can implement non-tile behavior if you know how to convert tile coordinates to normal x,y(,z?) coordinates, e.g.:

player.x = tile_cor_x * tile_size_width / 2
// same for y (and z?)

Or maybe a loop that checks who's on 20 tiles in front of a character, if there's an enemy shooting here, take cover and switch to a "coward" behavior.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.