/** * Creates random circles on a map. Currently the only map generation algorithm. * * @param f Used field * @param n Number of circles * @param maxr Maximal radius of circles */ public static void randomCircles(Field f, int n, double maxr) { for (int i = 0; i < n; i++) { f.createCircle( new Vector2D(rand.nextDouble() * f.getTilesX(), rand.nextDouble() * f.getTilesY()), rand.nextDouble() * maxr); } }
/** * Main method * * @param args Command line arguments */ public static void main(String[] args) { int tileSize = 20; int fieldWidth = 100; int fieldHeight = 100; Dimension windowSize = new Dimension(1000, 800); Field f = new Field(fieldWidth, fieldHeight, "random"); // randomCircles(f, fieldWidth*fieldHeight/50, 3); f.setTile(new Vector2D(40.5, 40.5), 4); f.setTile(new Vector2D(40.5, 41.5), 4); Vector2D start = new Vector2D(35, 41); // Vector2D start = findStartPos(f); byte[] fi = null; try { fi = Util.readFile("savedmap"); } catch (IOException e) { System.out.println("I/O error."); } f = MapCodec.decode(fi); /*byte[] m = MapCodec.encode(f); try { Util.writeFile("testmap", m); } catch (IOException e) { System.out.println("I/O error."); }*/ ArrayList<Team> teams = new ArrayList<Team>(); teams.add(new Team("SWAR", 0)); teams.add(new Team("sWARm", 1)); Session game = (Session) new GameSession(start, teams, 0); Session editor = (Session) new EditorSession(); BFrame frame = new BFrame(windowSize); Dimension paneSize = frame.getContentPane().getSize(); OpenGLRenderer renderer = new OpenGLRenderer(editor, tileSize, frame); Core.field = f; Core.input = new Input(frame); Core.view = new View(paneSize, tileSize); Core.initialize((Session) editor, (Session) game); // game.entitySystem.addAll(randomTowers(f, 30, game,teams)); MainLoop.startLoop(renderer, frame); }
/** * Finds valid start position for the Unit * * @param f Used field * @return Valid position */ public static Vector2D findStartPos(Field f) { Point start = new Point(rand.nextInt(f.getTilesX()), rand.nextInt(f.getTilesY())); Tile[][] tiles = f.getTiles(); while (tiles[start.x][start.y].getValue() == 1) start = new Point(rand.nextInt(f.getTilesX()), rand.nextInt(f.getTilesY())); return f.getWorldPos(start); }
/** * Generates randomly spread towers on a map * * @param f Used field * @param n Number of towers * @param g Used game * @return List of towers */ public static ArrayList<Entity> randomTowers(Field f, int n, Core g, ArrayList<Team> teams) { ArrayList<Entity> list = new ArrayList<Entity>(); Tile[][] tiles = f.getTiles(); for (int i = 0; i < n; i++) { Point tower = new Point(rand.nextInt(f.getTilesX()), rand.nextInt(f.getTilesY())); while (tiles[tower.x][tower.y].getValue() == 1) tower = new Point(rand.nextInt(f.getTilesX()), rand.nextInt(f.getTilesY())); int team = rand.nextInt(teams.size()); list.add(new Tower(f.getWorldPos(tower), teams.get(team))); } return list; }