Frog (Enemy NPC) - UQcsse3200/2024-studio-2 GitHub Wiki

Overview

The Frog NPC is a slower enemy NPC that inhabits the Land Kingdom, that is easier to run away from and avoid - suitable for newer players who are getting used to the game and surviving enemy NPCs. With a speed of 0.5 units, it moves at a pace, making it one of the slower enemies in the kingdom.

image

Enemy Stats

Enemy frog NPC stats are defined in an NPC.json file which are loaded into the game using the FileLoader class.

  • Health : 15
  • Base Attack : 2
  • Base Defense : 1
  • Speed : 0.5

Frog Enemy NPC Implementation

Like the other enemy NPCs, the frog enemy NPC is spawned through the EnemyFactory class, which is responsible for creating non-playable character (NPC) entities.

Create new frog enemy NPC:

Entity frog = createBaseEnemy(playerEntity, EnemyType.FROG);

Creation of frog enemy NPC, along with texture and animation:

 public static Entity createFrog(Entity target) {
    Entity frog = createBaseEnemy(target, EnemyType.FROG);
    BaseEntityConfig config = configs.frog;

    AnimationRenderComponent animator =
            new AnimationRenderComponent(
                    ServiceLocator.getResourceService().getAsset("images/frog.atlas", TextureAtlas.class));
    animator.addAnimation("angry_float", 0.1f, Animation.PlayMode.LOOP);
    animator.addAnimation("float", 0.1f, Animation.PlayMode.LOOP);

    frog
            .addComponent(new CombatStatsComponent(config.health, config.baseAttack))
            .addComponent(animator)
            .addComponent(new FrogAnimationController());

    frog.getComponent(AnimationRenderComponent.class).scaleEntity();
    frog.getComponent(PhysicsMovementComponent.class).changeMaxSpeed(new Vector2(config.speed, config.speed));

    return frog;
  }

Enemy frog NPCs are spawned in ForestGameTerrain class through randomisation based on the player's position:

 private void spawnRandomEnemy(Supplier<Entity> creator, int numItems, double proximityRange) {
    GridPoint2 minPos = new GridPoint2(PLAYER_SPAWN.x - 20, PLAYER_SPAWN.y - 20);
    GridPoint2 maxPos = new GridPoint2(PLAYER_SPAWN.x + 20, PLAYER_SPAWN.y + 20);

    for (int i = 0; i < numItems; i++) {
      GridPoint2 randomPos = RandomUtils.random(minPos, maxPos);
      Entity enemy = creator.get();
      spawnEntityAt(enemy, randomPos, true, false);
      enemies.add(enemy);
      enemy.addComponent(new ProximityComponent(player, proximityRange)); // Add ProximityComponent
    }
  }

Sequence Diagram

sequenceDiagram
Player        -> EnemyFactory      : Player within proximity
EnemyFactory  -> Frog Entity       : createFrog(target)
Frog Entity   -> AnimationRenderComponent : Add frog animations (WAIT, RUNRIGHT)
Frog Entity   -> AI Task Component : Add chase task (ChaseTask)
Player        -> Frog Entity       : Within chase range
Frog Entity   -> AnimationRenderComponent : Start RUNRIGHT animation
Frog Entity   -> Player            : Attack or chase player
Loading
⚠️ **GitHub.com Fallback** ⚠️