Skip to content

Bear (Enemy NPC)

Urawee Thani edited this page Oct 23, 2024 · 9 revisions

Overview

The Bear NPC is a true force of nature, a foe seldom trifled with and for good reason. It reigns supreme as the strongest enemy NPC in the forest biome due to it's well-rounded characteristics: high health and defense, with moderate strength. Fortunately for you, the bear spawns less frequently than other enemies and boasts less than average speed, meaning it is easier to run away from.

Screenshot 2024-09-12 at 12 11 50 AM

Bear NPC Implementation

The bear enemy NPC is spawned through the EnemyFactory class, which is responsible for creating non-playable character (NPC) entities.

Create new bear enemy NPC:

Entity bear = createBaseEnemy(playerEntity, EnemyType.BEAR);

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

  public static Entity createBear(Entity target) {
    Entity bear = createBaseEnemy(target, EnemyType.BEAR);
    BaseEnemyEntityConfig config = configs.bear;

    TextureAtlas bearAtlas = ServiceLocator.getResourceService().getAsset(config.getSpritePath(), TextureAtlas.class);

    AnimationRenderComponent animator = new AnimationRenderComponent(bearAtlas);

    animator.addAnimation("chase", 0.5f, Animation.PlayMode.LOOP);
    animator.addAnimation("float", 0.5f, Animation.PlayMode.LOOP);
    animator.addAnimation("spawn", 1.0f, Animation.PlayMode.NORMAL);

    bear
            .addComponent(new CombatStatsComponent(config.getHealth() + (int)(Math.random() * 2) - 1, 0,
                    config.getBaseAttack() + (int)(Math.random() * 2), 0, 0, 0))
            .addComponent(animator)
            .addComponent(new BearAnimationController());


    bear.getComponent(AnimationRenderComponent.class).scaleEntity();
    bear.getComponent(PhysicsMovementComponent.class).changeMaxSpeed(new Vector2(config.getSpeed(), config.getSpeed()));

    return bear;
  }

The bear is also assigned animations. "Chase" for when it is chasing the player. "float" for when it is idle, and "spawn" for when it is first spawned in:

animator.addAnimation("chase", 0.5f, Animation.PlayMode.LOOP);
animator.addAnimation("float", 0.5f, Animation.PlayMode.LOOP);
animator.addAnimation("spawn", 1.0f, Animation.PlayMode.NORMAL);

The bear is also assigned components. CombatStatsCompoennt give it the correct attributes to inherit into the combat screen, as you can see there are variations of the stats to keep things unpredictable. Animator is added to give the bear animations, and animation controller is used to trigger the different animations.

bear
        .addComponent(new CombatStatsComponent(config.getHealth() + (int)(Math.random() * 2) - 1, 0,
                    config.getBaseAttack() + (int)(Math.random() * 2), 0, 0, 0, 0, false, false))
        .addComponent(animator)
        .addComponent(new BearAnimationController());

Enemy Bear 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
    }
  }

Number of Bear Spawns

The number of bear spawns is determined by the "NUM_BEARS" variable, in the entitySpawn.json file.

  "NUM_BEARS" : 4,

Sequence Diagram

sequenceDiagram
Player        -> EnemyFactory      : Player within proximity
EnemyFactory  -> Bear Entity       : createBear(target)
Bear Entity   -> AnimationRenderComponent : Add bear animations (WAIT, RUNRIGHT)
Bear Entity   -> AI Task Component : Add wander task (WanderTask)
Player        -> Bear Entity       : Within interaction range
Bear Entity   -> AnimationRenderComponent : Start RUNRIGHT animation
Bear Entity   -> Player            : Attack or avoid based on game logic
Loading
Clone this wiki locally