Skip to content

Turret (Game Entity)

Rowan Gray edited this page Sep 14, 2023 · 10 revisions

Overview

The Turret class is a placeable entity within the game responsible for creating and managing turret entities. Turrets are structures that can automatically target and fire at enemy entities within their range, providing a defensive mechanism for players. This class encompasses the behavior, properties, and interactions of turrets, including ammunition management.

Usage

Placing

Players can place a turret by selecting it via the StructureToolPicker (press 3 to select the hammer, press T to open the ToolPicker menu) and clicking at an available location on the map. Placing a turret via the StructureToolPicker consumes resources based on the defined cost in the structure_tools.json config file.

Firing

Turrets automatically target and fire at nearby enemy entities within their line of sight. The firing rate is rate-limited to prevent excessive damage. Each time a turret fires, its ammunition is reduced by one. Once its ammunition runs out, it will no longer be able to fire.

Ammunition Refill

Players can interact with turrets to refill their ammunition using player resources. This action deducts the required resources from the player's inventory and replenishes the turret's ammunition.

Destroying

Players can destroy a turret by right clicking on it whilst the hammer is selected (press 3 to select the hammer), which immediately removes the turret from the game world.

Class

The Turret class inherits from the PlaceableEntity class and utilizes several key components to define its behavior:

  • PhysicsComponent: Provides the necessary physics properties to handle collision and placement.
  • ColliderComponent: Defines the collision layer for turrets, ensuring they interact correctly with other game entities.
  • HitboxComponent: Specifies the hitbox layer, ensuring precise collision detection.
  • CombatStatsComponent: Manages the health, damage, and other combat-related properties of the turret.
  • HealthBarComponent: Displays a health bar above the turret for visual feedback.
  • TextureRenderComponent: Handles the rendering of the turret's visual representation.
  • FOVComponent: Defines the turret's field of view, allowing it to detect and target enemy entities within its range. Learn more here.
  • StructureDestroyComponent: Enables the destruction of turrets.

High-Level UML

The following UML diagram illustrates the relationships between the Turret class and the components it utilizes:

classDiagram
    class Turret {
        <<extends>>
        - start : long
        - turretConfigs : TurretConfigs
        - type : TurretType
        + currentAmmo : int
        - maxAmmo : int
        - damage : int
        + Turret() Turret
        + refillAmmo()
        + canFire() boolean
        + refillAmmo(amount : int)
        + interact(player : Entity)
        + startDamage(focus : Entity)
        + stopDamage(focus : Entity)
        + giveDamage(focus : Entity)
        + rotateTurret(focus : Entity)

    }

    PlaceableEntity <|-- Turret
    Turret --o PhysicsComponent
    Turret --o CollidorComponent
    CollidorComponent --o PhysicsComponent
    Turret --o TurretConfigs
    TurretConfigs --o TurretConfig

    class TurretConfig {
        <<extends>>
    }

    TurretConfig --|> BaseEntityConfig
    Turret --o CombatStatsComponent
    Turret --o HealthBarComponent
    HealthBarComponent --o CombatStatsComponent

    class FOVComponent {
        <<extends>>
    }
    Turret --o TurretType
    Turret --o TextureRendererComponent
    Turret --o StructureDestroyComponent
    Turret --o FOVComponent

    FOVComponent --|> ProximityActivationComponent
    FOVComponent --o TurretTargetableComponent
Loading

Importing Turret Class

com.csse3200.game.entities.buildables.Turret

Purpose

The Turret class serves the purpose of creating and managing individual turret entities within the game world. Turrets are essential for defending against enemy entities and provide a strategic element to the game.

Usage

var TurretConfigs turretConfigs = FileLoader.readClass(TurretConfigs.class, "configs/turrets.json");

var turret = new Turret(turretConfigs.getTurretConfig(type));

ServiceLocator.getStructurePlacementService().placeStructureAt(turret, new GridPoint2(0,0));

TurretConfig

there are two different levels of turrets:

  • levelOne with max health of 200, max ammunition of 50 and damage level 2.
  • levelTwo with max health of 500, max ammunition of 100 and damage level 5.

they can be swapped around by calling TurretConfig.

You can modify these values within assets/configs/turrets.json.

Testing

Testing of the Turret class involves verifying its functionality and interaction with other game entities. This includes placing turrets, ensuring they target and fire at enemies correctly, and testing their collision behavior.

Clone this wiki locally