update bots handling, spawn bot on player

This commit is contained in:
RedSparr0w
2019-11-22 19:11:03 +13:00
parent 07472a9a90
commit a91dc597e9
4 changed files with 64 additions and 26 deletions
@@ -1,7 +1,10 @@
package redone.game.bots;
import redone.Constants;
import redone.game.players.Client;
import redone.game.players.Player;
import redone.game.players.PlayerHandler;
import redone.util.Misc;
import java.security.SecureRandom;
import java.util.ArrayList;
@@ -13,22 +16,45 @@ public class BotHandler
static final List<Bot> botList = new ArrayList<>(BotConstants.MAX_BOTS);
static final Random random = new SecureRandom();
public static void connectBots(int botCount)
{
public static Bot connectBot(String username, int x, int y, int z) {
Bot bot;
for (int bots = 0; bots < botCount; bots++)
{
if (PlayerHandler.playerCount >= Constants.MAX_PLAYERS)
{
System.out.println("Bot could not be connected, server is full.");
return;
}
final String botName = "bot" + random.nextInt(9999);
bot = new Bot(botName);
botList.add(bot);
if (PlayerHandler.playerCount >= Constants.MAX_PLAYERS) {
System.out.println("Bot could not be connected, server is full.");
return null;
}
bot = new Bot(username, x, y, z);
botList.add(bot);
return bot;
}
public static void playerShop(Client player){
Bot playerShop = getPlayerShop(player);
if (playerShop == null) {
String shopName = getShopName(player);
playerShop = connectBot(shopName, player.getX(), player.getY(), player.getH());
}
if (playerShop != null)
playerShop.getBotClient().getPlayerAssistant().movePlayer(player.getX(), player.getY(), player.getH());
}
private static String getShopName(Client player){
return "" + player.playerName;
}
private static Bot getPlayerShop(Client player){
String shopName = getShopName(player);
for(Bot bot : botList) {
if(bot != null && bot.getBotClient() != null) {
Client botClient = bot.getBotClient();
if(botClient.playerName.equalsIgnoreCase(shopName)) {
return bot;
}
}
}
return null;
}
}