Buildings Spawning - UQdeco2800/2022-studio-3 GitHub Wiki

A Building entity is created Create Building

Within AtalantisGameArea.java

Spawning of the TownHall

Gets the position of the centre of the city from the map generator and spawns TownHall using building factory

private void spawnTownHall() {
    MapGenerator mg = terrainFactory.getMapGenerator();
    Coordinate centre = mg.getCityDetails().get("Centre");
    // Get GridPoint for the city centre
    GridPoint2 spawn = new GridPoint2(centre.getX(), mg.getHeight() - centre.getY());
    Entity townHall = BuildingFactory.createTownHall();
    spawnEntityAt(townHall, spawn.add(0, 2), true, true);
}

Spawning Barracks

The same approach is used for the two barracks, spawning it at an offset in the x direction from the centre where the TownHall is located

private void spawnBarracks() {
    // Position offset from centre of city
    int offset = 10;
    MapGenerator mg = terrainFactory.getMapGenerator();
    Coordinate centre = mg.getCityDetails().get("Centre");
    // Two spawn-points for the barracks next ot TownHall located in the centre
    GridPoint2 spawn1 = new GridPoint2(centre.getX(), mg.getHeight() - centre.getY()).add(offset, 0);
    GridPoint2 spawn2 = new GridPoint2(centre.getX(), mg.getHeight() - centre.getY()).sub(offset, 0);

    spawnEntityAt(BuildingFactory.createBarracks(), spawn1, true, true);
    spawnEntityAt(BuildingFactory.createBarracks(), spawn2, true, true);
}
TownHall Barracks and TownHall
Screenshot 2022-09-08 145354 Screenshot 2022-09-08 145651
Walls
Screenshot 2022-09-08 150240

Spawning Walls

Below is the entire spawnWalls function. What it basically does is get the position of the corner from the map generator and spawns walls in the x and y direction. (as seen in the image above)

private void spawnWalls() {
    MapGenerator mg = terrainFactory.getMapGenerator();
    Coordinate corner;
    GridPoint2 position;
    int yLength = 10; // Amount of walls to spawn in x direction
    int xLength = 20; // Amount of walls to spawn in y direction
    String[] cityCorners = {"NW", "NE", "SW", "SE"}; // Four corner locations to spawn walls in
    int direction = 1; // Spawning direction

    // Spawns walls in positive x direction from 2 left corners, and negative x direction from 2 right corners
    for (int n = 0; n < 4; n++) {
        corner = mg.getCityDetails().get(cityCorners[n]); // nth corner
        position = new GridPoint2(corner.getX(), mg.getHeight() - corner.getY() - 1); // position of nth corner

        // Absolute corner walls will have default wall texture (doesn't point in any direction)
        Entity wall = BuildingFactory.createWall();
        spawnEntityAt(wall, position, true, true);

        for (int i = 0; i < xLength; i++) {
            wall = BuildingFactory.createWall();
            // Sets wall texture which points in positive x direction
            wall.getComponent(TextureRenderComponent.class).setTexture(ServiceLocator.getResourceService()
                    .getAsset("images/stone_wall_2_.png", Texture.class));
            spawnEntityAt(wall, position.add(direction, 0), true, true);
        }
        direction *= -1;
    }
    // Spawns walls in positive y direction from 2 bottom corners, and negative y direction from 2 top corners
    for (int n = 0; n < 4; n++) {
        direction = n<2 ? -1 : 1;
        corner = mg.getCityDetails().get(cityCorners[n]);
        position = new GridPoint2(corner.getX(), mg.getHeight() - corner.getY() - 1);

        for (int i = 0; i < yLength; i++) {
            Entity wall = BuildingFactory.createWall();
            spawnEntityAt(wall, position.add(0, direction), true, true);
            // Sets wall texture which points in negative y direction
            wall.getComponent(TextureRenderComponent.class).setTexture(ServiceLocator.getResourceService()
                    .getAsset("images/stone_wall_3.png", Texture.class));
        }

    }
}