Im Currently develop a little Dungeon Crawler. The Singeplayer works fine, but it also should have multiplayer so i implemented it. But there's one problem. I have no idea how to make the AI chase the player and the other players. In singleplayer it works like this :

if(distanceToPlayer <= 400f){



Heres my full chasing code :

// in enemy update method i put all multiplayers into a map with their login number..

 // Pick a random Multiplayer and chase him

        boolean isAvaible = false;

 //Convert Map to Object array
        Object[] values = ((EnemyEntity)arg0).mpSortedPos.values().toArray();

        int rdm = 0;

        float randomValue = 0;

        // wenn map isnt empty
        if(((EnemyEntity)arg0).mpSortedPos.size() > 0){

            rdm = generator.nextInt(values.length);

            randomValue = (float)values[rdm];

            isAvaible = true;


        //selecting the key     
        // +2 because the first joined player is the second Player in the game so it begins with connection 2, the second with 3 and so on.
        int key = rdm + 2;

        // Only printing stuff
        System.out.println("Found a Map : "+((EnemyEntity)arg0).mpSortedPos + "Size of da HashMap : "+ ((EnemyEntity)arg0).mpSortedPos.size());
        System.out.println("Found a Random Value : "+rdm + " "+randomValue );
        System.out.println("Found a Key: "+key + " "+((EnemyEntity)arg0).mpSortedPos.get(key) );

        // When the multiplayer is near the enemy than
        if (/*((EnemyEntity)arg0).distanceToNextPlayer < 400 && arg0.isAlive() == true */ isAvaible && ((EnemyEntity)arg0).mpSortedPos.get(rdm) != null && ((EnemyEntity)arg0).mpSortedPos.get(rdm) < 400) {

            ((EnemyEntity)arg0).activated = true; 



This is my code for multiplayer chasing but it doesnt work ... any ideas ? What could i improve ?

But how should i do that with multiple players ? Any ideas ? :)

  • 1
    \$\begingroup\$ How much do you know about route finding algorithms like A* or Dijkstra's algorithm? \$\endgroup\$ – Philipp Sep 29 '16 at 11:18

Just the same way. You just have to pick an algorithm to select a player to chase from available players. E.g. by factoring distance to the player, remaining hitpoints, random, etc.

  • \$\begingroup\$ That sounds logical, updated my question, maybe you could add some simple code or help me to solve mine ? :/ \$\endgroup\$ – genaray Sep 30 '16 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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