0
\$\begingroup\$

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..

\$\endgroup\$
2

1 Answer 1

3
\$\begingroup\$

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.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged .