Structure Factory - UQdeco2800/2022-studio-1 GitHub Wiki

Introduction

The Structure Factory allows the creation of different structure entities. This feature is critical to the game as it is the Design Pattern which allows the player to create and access all buildable structures in the game, and therefore allows them to defend themselves and the crystal.

Factory Design

createBaseStructure method: First creates a new Entity and adds various components which will be inherited by all objects building off of the method.


createWall: Creates a new BaseStructure object and extends it by adding a health bar component. For more information see Wall.

/**
   * Creates a wall entity.
   *
   * @return specialised Wall entity
   */
  public static Entity createWall() {
    Entity wall = createBaseStructure("images/Wall-right.png");
    BaseStructureConfig config = configs.wall;

    wall.addComponent(new CombatStatsComponent(config.health, config.baseAttack))
        .addComponent(new ResourceCostComponent(config.gold));
    return wall;
  }

createTower: The method of creating a defensive tower in the StructureFactory followed the general structure shown below, where a base structure is created, and assigned various components and attributes depending on the tower, and the level passed as a parameter.

/**
   * Creates a tower1 entity.
   * @param level of the tower to create
   * @return entity
   */
  public static Entity createTower1(int level) {
    //TODO Change string constant 
    String TOWER1I = "images/mini_tower.png";
    String TOWER1II = "images/mini_tower.png";
    Entity tower1;
    BaseStructureConfig config;

    tower1 = createBaseStructure("images/mini_tower.png");
    switch (level) {
      case 1: // Represents the base level structure
        tower1 = createBaseStructure("images/mini_tower.png");
        config = configs.tower1;

        tower1.addComponent(new CombatStatsComponent(config.health, config.baseAttack, 1))
            .addComponent(new RangeAttackComponent(PhysicsLayer.NPC, 10f, 100f))
            .addComponent(new ResourceCostComponent(config.gold));
        return tower1;

      case 2: // Represents the first upgraded version of the tower
        tower1 = createBaseStructure(TOWER1I);
        config = configs.tower1I;
        tower1.addComponent(new CombatStatsComponent(config.health, config.baseAttack, 2))
            .addComponent(new RangeAttackComponent(PhysicsLayer.NPC, 10f, 100f))
            .addComponent(new ResourceCostComponent(config.gold));
        return tower1;

      case 3: // Represents the second upgraded version of the tower
        tower1 = createBaseStructure(TOWER1II);
        config = configs.tower1II;
        tower1.addComponent(new CombatStatsComponent(config.health, config.baseAttack, 3))
            .addComponent(new RangeAttackComponent(PhysicsLayer.NPC, 10f, 100f))
            .addComponent(new ResourceCostComponent(config.gold, config.stone));
        return tower1;
    }
    // should never run
    return tower1;
  }

createTrap: Creates a base structure and assigns it various components, including the trap component, to create a trap structure.

/**
   * Creates a trap entity
   * 
   * @return entity
   */
  public static Entity createTrap() {
    // TODO change trap texture
    Entity trap = createBaseStructure("images/wall-right.png");
    BaseStructureConfig config = configs.trap;

    trap.addComponent(new CombatStatsComponent(config.health, config.baseAttack))
        .addComponent(new TrapComponent(PhysicsLayer.NPC, 1.5f))
        .addComponent(new HealthBarComponent(75, 10))
        .addComponent(new TrapComponent(PhysicsLayer.NPC, 1.5f))
        .addComponent(new ResourceCostComponent(config.gold));
    return trap;
  }

Unit Testing

Unit tests were performed to validate the dynamic loading of structure attributes and parameters upon spawning structure entities. These unit tests can be found in StructureFactoryTest.java. Data is loaded from a JSON file, structure.json, to keep all structures and their attributes accounted for in one easy to edit file.

Future Expansion

The factory by design can be easily extended using the createBaseStructure method. This allows developers to create new structure types by extending upon the base structure entity. In the future it is intended that many more structure entities / types will be created to expand upon the gameplay experience.

To do so, further research/investigation will need to be carried out with users to understand what additional types and extensions of structures could be added to further enhance the gameplay experience.

UML Diagram

This UML diagram was based off of the Factory Design Pattern, and shows its utilisation by the StructureFactory. image