Enemy movement

Hi, I’ve a tilemap game… I’ve writed an enemy class with a move method like this:


public void move()
    {
        switch((int)(4 * Math.random()))
        {
            case 0: setDx(getMaxSpeed());
                    setDy(0);
                    break;
            case 1: setDx(0);
                    setDy(getMaxSpeed());
                    break;
            case 2: setDx(-getMaxSpeed());
                    setDy(0);
                    break;
            case 3: setDx(0);
                    setDy(-getMaxSpeed());
                    break;
            default: setDx(getMaxSpeed());
                     setDy(0);
        }
    }

The enemy can run around randomly and at each intersection, they have to look all four ways.
I’ve to put this scannig on the collision detection method? and what kind of scanning?
I’m a little confused… I hope you can help me!

could you clarify everything? The 2 questions don’t make sense to me, sorry.

I’m sorry… english is not my mother language ;D

So, I’ve a maze with tilemap… the enemy can move on left - right - up & down…
| |
| |_____
___ o ________
| |

If we assume that o is the sprite, at each intersection it have to call

public void move

?
where I’ve to implement the intersection control?

I hope you understand me ???

I didn’t write a single tile game, but I would put the control in enemy’s move(). First part of move() would call deceideDirection() where you would pass known parametars and deceide on next direction, and second part of move() would actually move to that direction.
I hope this is what you were asking… I try to think OO way, like, the enemy is the one who has to deceide where to move and then move, so that’s why I would put those methods in enemy class.

Edit: oh I’ve figured it out what you’re asking now, … no, you don’t have to put it into collision detection. Usually you first move something and then test if it colided or whatever (and change direction in collision method if it collided so next update it will move different way).

I’ve put a

enemyupdate

method in a manager class that update the movement of the enemy sprite…

        x

o --> x
x

so, let assume that o is my sprite, it have a movement to the right and x are my possible directions, I’ve to check everytime if a tile is blocked or not with a code like this


if (dx > 0)
   if(map.canMove(x,y+1) == false && map.canMove(x,y-1) == false && map.canMove(x+1,y) == false)
      move to left
   else
    if(map.canMove(x,y+1) == true && map.canMove(x,y-1) == false && map.canMove(x+1,y) == false)
      move or left or up
    else
    if(map.canMove(x,y+1) == true && map.canMove(x,y-1) == true && map.canMove(x+1,y) == false)
      move or left or up or down
.....
.....
....

??

I don’t like this kind of control! :-X

as I said I didn’t write a single tile game so I don’t feel comfortable trying to help you anymore… there’s a lot of people who wrote and know about tile games, it’s weird nobody replyed yet (c’mon guys!)… try searching the forum, as I said, after initial space invaders tile games are most popular newbie work.

Edit: I couldn’t help myself, I’ll say a few things :slight_smile:
of course you don’t like the code above, you don’t have to check every option. As I see it, you only need to check to what sides can you move and randomly choose one you can. This is my first time I think about movement in tile games and within 5 mins, so take this with some reserve.

They have to ``look all four ways’’? I assume you want them to move in any of those directions.

Yes, put it in the move method. I don’t understand very much of what you want, so that’s probably why there are not a lot of replies.

My game is like pacman but I use a TileMap…

So, “ghost” have to deceide the next directions at any intersection of the path
is this the correct way to check the path?


if (dx > 0)
   if(map.canMove(x,y+1) == false && map.canMove(x,y-1) == false && map.canMove(x+1,y) == false)
      move to left
   else
    if(map.canMove(x,y+1) == true && map.canMove(x,y-1) == false && map.canMove(x+1,y) == false)
      move or left or up
    else
    if(map.canMove(x,y+1) == true && map.canMove(x,y-1) == true && map.canMove(x+1,y) == false)
      move or left or up or down

I put this code on the enemyupdate method of the manager class.

hmm, you’re gonna need to add little ai to your ghosts, like that they don’t turn in way they came from and that they mostly move towards player (or if they see him in the distance). But first I suggest you try just random movement at intersection, equal chance of going all ways.

For all random, I would do it something like this, pseudo:


up = 0; down = 1; left = 2; right = 3;
can_move[up] = can he move up? true : false;
can_move[down] = can he move up? true : false;
can_move[left] = can he move up? true : false;
can_move[right] = can he move up? true : false;
deceided = false
while(!deceided) {
    direction = (int) math.random() * 4;
    if (can_move[direction])
        deceided = true;
}
move(direction);

Ok, but if the enemy is moving, for example, to the right, on the next step it can move:

  • only right
  • only up
  • only down
  • only up or right
  • only up or down
  • only right or down
  • only up or right or down

What is the best way to check and then deceide the direction?

what do you mean? The code I gave works in all cases but with same chance of going towards avaliable positions.
So he moves right and next time it’s clear it isn’t good he moves left… so you need to return false in your:


can_move[left] = can he move left? true : false;

like:


public boolean canHeMoveLeft() {
    if (something is blocking the way) return false;
    // if last direction was right, he can't move left:
    if (direction == right) return false; 
    return true;
}

you’ll only need to add few lines of code in move() to test if it’s a dead end steet becouse like this it would reach the end and get stuck or go into wall.

[quote]What is the best way to check and then deceide the direction?
[/quote]
You had a suggestion already in the first post. That’s why it is difficult to understand your question. You are looking for a solution, but you already have a solution. In which way does your current solution not satisfy your needs?

Let’s go through the update routine. The player movement is updated and then the ghosts. Presumably the updating of a ghost involves the move() method. In this method you could check whether the ghost should go up, down, left or right by invoking the code you quoted in the beginning (you just have to ask whether there is free space in the particular directions).

I see no problem.

Bad bad code.

If he cannot move in any direction, your game locks up in an infinite loop.

I mentioned this is short, illustrative algorithm… also (as mentioned) dosen’t work for dead end ways. He’ll need to adjust it of course.

So, why anybody don’t post others code’s examples?

I’ve a class TileMap that contains the data for a tile-based map, including Sprites…
with methods that gets width and height of the map,
getTile, setTile, addSprite, getSprite and
public boolean canMove(int x, int y) where I check if there is a blocked tile or not.

In the class ResourceManager I’ve the
void updateCreature(Creature creature, long elapsedTime)
where I calculate new value of x and y position

In the class Enemy I’ve the move method.

So, the control if a tile is blocked or not (with public boolean canMove(int x, int y) method)
it must have done in the enemy class or in the ResourceManager class?

I would love to help you out as I have made a tilemapped pacman game myself but maybe my brain is a little slow at the moment. I would suggest that you read Kevs tutorial at http://www.cokeandcode.com/collisiontilemaps and see if that helps you out . That tutorial was the basis of all movement and collision detection in my game.

I know how move the player
I’m interested on the movement of enemy :frowning:
I want to move it on all possible directions.

nobody has done a maze game with tilemap??

I remember some topic on pacman and enemy movement, it was about optimizing where in what direction should enemy look at intersection to try to see the player in distance… try to find that topic.

Can’t you do it yourself? Movement isn’t really rocket scienece… you just need to test in what direction enemy can move and randomly pick one of them. My algoritham is not good as we concluded but I’m sure you can write something similar that works… like put all directions he can move to in an array, shuffle the array and pick first element in it as direction… before that you’ll only need to test if he can move at all (other enemys blocking his sides) and set speed to 0 if he can’t.

I have done what you ask but I will have to look at my code and see if there is anything useful there. will try to post something later

ok, thanks