Skip to content

Chicken (Enemy NPC)

LJ edited this page Sep 12, 2024 · 13 revisions

Overview

The Chicken NPC is a swift enemy NPC that inhabits the Land Kingdom. With a speed of 2 units, it excels at rapidly closing the distance to its target, catching them off guard with its quick movements. Although its health and attack power are relatively modest, its speed and agility compensate, makes it a force to be reckoned with in the kingdom.

image

Enemy Stats

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

  • Health: 10
  • Base Attack: 1
  • Base Defense: 1
  • Speed: 2

Chicken NPC Implementation

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

Create new chicken enemy NPC:

Entity chicken = createBaseEnemy(playerEntity, EnemyType.CHICKEN);

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

public static Entity createChicken(Entity target) {
    Entity chicken = createBaseEnemy(target, EnemyType.CHICKEN);
    BaseEntityConfig config = configs.chicken;

    TextureAtlas chickenAtlas = ServiceLocator.getResourceService().getAsset("images/chicken.atlas", TextureAtlas.class);

    AnimationRenderComponent animator = new AnimationRenderComponent(chickenAtlas);

    animator.addAnimation("spawn", 1.0f, Animation.PlayMode.NORMAL);
    animator.addAnimation("walk", 0.25f, Animation.PlayMode.LOOP);

    chicken
            .addComponent(animator)
            .addComponent(new CombatStatsComponent(config.health, config.baseAttack))
            .addComponent(new ChickenAnimationController());

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

    return chicken;
  }

Enemy Chicken 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
    }
  }
Clone this wiki locally