/** * Check on hit on object * * @param helpCircle - help figure for detect on hit * @return - object or null */ private SuperFigure checkOnTouch(Circle helpCircle) { for (Mob mob : mobs) { if (mob.getFigure().overlaps(helpCircle)) { return mob; } } for (Planet planet : planets) { if (planet.getFigure().overlaps(helpCircle)) { return planet; } } return null; }
/** * If near planet only one type enemy return his name * * @param planet - our planet * @return - enemy name */ private String whoIsInvader(Planet planet) { String invader = ""; for (Mob mob : mobs) { if (mob.getFigure() .overlaps( new Circle( planet.getFigure().x, planet.getFigure().y, 2 * planet.getFigure().radius))) { if (invader.equals("")) { invader = mob.getHostName(); } else if (!invader.equals(mob.getHostName())) { return ""; } } } return invader; }
/** * Check if player's mobs near planet * * @param planet - planet * @return - true, if only enemies */ private Boolean onlyEnemiesNearPlanet(Planet planet) { int count = 0; for (Mob mob : mobs) { if (mob.getFigure() .overlaps( new Circle( planet.getFigure().x, planet.getFigure().y, 2 * planet.getFigure().radius)) && mob.getHostName().equals(planet.getHostName())) { return false; } else if (mob.getFigure() .overlaps( new Circle( planet.getFigure().x, planet.getFigure().y, 2 * planet.getFigure().radius)) && !mob.getHostName().equals(planet.getHostName())) { count++; } } if (count > 0) { return true; } return false; }
/** * Click on game field * * @param screenX - screen position click X * @param screenY - screen position click Y */ public void clickOnWorld(int screenX, int screenY) { screenY = (int) (game.getScreenHeight() - screenY); Circle helpCircle = new Circle(screenX, screenY, 1); if (checkOnTouch(helpCircle) == null && isSelected) { moveToPoint(screenX, screenY, null); } else if (checkOnTouch(helpCircle) instanceof Planet) { Planet planet = (Planet) checkOnTouch(helpCircle); if (planet.getHostName().equals(playerName)) { isSelected = true; planet.setIsSelected(true); } else { moveToPoint(planet.getFigure().x, planet.getFigure().y, planet); } } else if (checkOnTouch(helpCircle) instanceof Mob) { Mob mob = (Mob) checkOnTouch(helpCircle); if (mob.getHostName().equals(playerName)) { isSelected = true; mob.setIsSelected(true); } else { moveToPoint(mob.getFigure().x, mob.getFigure().y, mob); } } }
/** * Move object to free point or follow for target * * @param newX - target position x * @param newY - target position y * @param target - target */ private void moveToPoint(float newX, float newY, SuperFigure target) { String command; if (target == null) { command = "MovM"; } else { command = "FolM"; } messageText = "From" + 1 + command + (int) newX + "-" + (int) newY + ":"; Boolean isFirst = true; for (Mob mob : mobs) { if (mob.getIsSelected()) { if (isFirst) { messageText += mob.getID(); isFirst = false; } else { messageText += "," + mob.getID(); } float k = (newX - mob.getFigure().x) / Math.abs(newY - mob.getFigure().y); mob.setStepX(k); mob.setStepY(newY - mob.getFigure().y > 0 ? 1 : -1); mob.setIsSelected(false); mob.setNewX(newX); mob.setNewY(newY); mob.setTarget(target); mob.setIsMove(true); } } if (target == null) { command = "MovP"; } else { command = "FolP"; } messageText = "From" + 1 + command + (int) newX + "-" + (int) newY + ":"; for (Planet mob : planets) { if (mob.getIsSelected()) { if (isFirst) { messageText += mob.getID(); isFirst = false; } else { messageText += "," + mob.getID(); } float k = (newX - mob.getFigure().x) / Math.abs(newY - mob.getFigure().y); mob.setStepX(k); mob.setStepY(newY - mob.getFigure().y > 0 ? 1 : -1); mob.setIsSelected(false); mob.setNewX(newX); mob.setNewY(newY); mob.setTarget(target); mob.setIsMove(true); } } System.out.println(messageText); }