Skip to content

How to create a new Enemy

Simon edited this page Mar 29, 2020 · 5 revisions

An enemy is treated as a character from a code point of view. It is not different to the player or NPCs.

Follow these steps to create a new enemy. All code adjustments are done in the core project:

  1. Add a new enum value to the Character enum in the Character.kt file
  2. Add a new enum value to the ModelType enum in the AnimationComponent.kt file
  3. Add a new configuration for the enum value that you have created in step 1 within the loadCharacterConfigurations method of the Character.kt file. Let's say that you have added the value BAT to both enums then a new configuration might look like this:
    cfg(Character.BAT, EntityType.ENEMY, ModelType.BAT) {
        speed = 0.3f
        size(0.5f, 0.5f)
        attackRange = 0.15f
        attackCooldown = 2f
        damage = 2f
        life = 10f
        defaultState = DefaultEnemyState.IDLE
        aggroRange = 2.5f
    }
    To see all possible options for a configuration, refer to the CharacterCfg class of the Character.kt file
  4. Finally, to link your new character to the correct graphics go to the graphics_raw/gameObjects folder of the root project. Create a new folder using the same name as the enum value of the ModelType value that you have added.
  5. Add your graphics to this folder, start the graphicsTexPackProject.tpproj texture packer project located in the graphics_raw folder and repack the gameObjects atlas.
    The convention for the file names is the following: "AnimationType_INDEX" (refer to AnimationComponent.kt file).
    For example if you have a run animation with three frames then the names of the files would be: RUN_0, RUN_1, RUN_2
  6. Sometimes the graphic does not exactly match the physic boundaries. In that case you can use the collisionBodyOffset of a CharacterCfg.
    Example: To correct the physic body by moving it 3 pixels to the right we can adjust the configuration from above to this:
    cfg(Character.BAT, EntityType.ENEMY, ModelType.BAT) {
        speed = 0.3f
        size(0.5f, 0.5f)
        attackRange = 0.15f
        attackCooldown = 2f
        damage = 2f
        life = 10f
        defaultState = DefaultEnemyState.IDLE
        aggroRange = 2.5f
    +   collisionBodyOffset(3f * UNIT_SCALE, 0f)
    }
  7. Sometimes you want to play a sound effect when a certain animation is played. You can configure that in the getSound function of the AnimationSystem.