NPC Code - UQdeco2800/2022-studio-2 GitHub Wiki

Back to NPC + Dialogue

JUMP TO:

Configs

NPCForstGameArea

NPCAnimationController

NPC Factory

This class creates the entity for each NPC and adds components. The property of NPCs will be set here, such as the position, etc. The animation for each NPC is also be created here.

// Create every single NPCs in the game map and set the configs and the animation.
public static Entity createOneLegGirl (Entity target) {
    Entity oneLegGirl = createBaseNPC();
    FemaleCitizenConfig config = configs.oneLegGirl;

    AnimationRenderComponent animator =
            new AnimationRenderComponent(ServiceLocator.getResourceService().getAsset("images/NPC/female npc/npcfemale.atlas", TextureAtlas.class));
    animator.addAnimation("femaleShake", 0.1f, Animation.PlayMode.LOOP);

    oneLegGirl
            .addComponent(new CombatStatsComponent(config.health, config.baseAttack, config.stamina, config.mana))
            .addComponent(animator)
            .addComponent(new NPCAnimationController());

    oneLegGirl.getComponent(AITaskComponent.class);
    oneLegGirl.getComponent(AnimationRenderComponent.class).scaleEntity();
    oneLegGirl.getComponent(PhysicsComponent.class).setBodyType(BodyDef.BodyType.StaticBody);
    oneLegGirl.setScale(1, 1);
    return oneLegGirl;
  }

  /**
   * Creates an atlantis child NPC entity.
   *
   * @param target entity to stand
   * @return entity
   */
  public static Entity createChild (Entity target) {
    Entity child = createBaseNPC();
    ChildConfig config = configs.child;

    AnimationRenderComponent animator =
            new AnimationRenderComponent(ServiceLocator.getResourceService().getAsset("images/NPC/child npc/npcchild.atlas", TextureAtlas.class));
    animator.addAnimation("childShake", 0.1f, Animation.PlayMode.LOOP);


    child
            .addComponent(new CombatStatsComponent(config.health, config.baseAttack, config.stamina, config.mana))
            .addComponent(animator)
            .addComponent(new NPCAnimationController());


    child.getComponent(AITaskComponent.class);
    child.getComponent(AnimationRenderComponent.class).scaleEntity();
    child.getComponent(PhysicsComponent.class).setBodyType(BodyDef.BodyType.StaticBody);
    child.setScale(1, 1);
    return child;
  }

  /**
   * Creates an atlantis guard NPC entity.
   *
   * @param target entity to stand
   * @return entity
   */
  public static Entity createGuard (Entity target) {
    Entity guard = createBaseNPC();
    GuardConfig config = configs.guard;

    AnimationRenderComponent animator =
            new AnimationRenderComponent(ServiceLocator.getResourceService().getAsset("images/NPC/guard npc/npcguard.atlas", TextureAtlas.class));
    animator.addAnimation("guardShake", 0.1f, Animation.PlayMode.LOOP);


    guard
            .addComponent(new CombatStatsComponent(config.health, config.baseAttack, config.stamina, config.mana))
            .addComponent(animator)
            .addComponent(new NPCAnimationController());

    guard.getComponent(AITaskComponent.class);
    guard.getComponent(AnimationRenderComponent.class).scaleEntity();
    guard.getComponent(PhysicsComponent.class).setBodyType(BodyDef.BodyType.StaticBody);
    guard.setScale(1, 1);
    return guard;
  }
  public static Entity createHumanGuard (Entity target) {
    Entity humanguard = createBaseNPC();
    HumanGuardConfig config = configs.humanguard;

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


    humanguard
            .addComponent(new CombatStatsComponent(config.health, config.baseAttack, config.stamina, config.mana))
            .addComponent(animator)
            .addComponent(new NPCAnimationController());

    humanguard.getComponent(AITaskComponent.class);
    humanguard.getComponent(AnimationRenderComponent.class).scaleEntity();
    humanguard.getComponent(PhysicsComponent.class).setBodyType(BodyDef.BodyType.StaticBody);
    humanguard.setScale(1, 1);
    return humanguard;
  }

  public static Entity createPlumberFriend (Entity target) {
    Entity plumberfriend = createBaseNPC();
    PlumberFriendConfig config = configs.plumberfriend;

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


    plumberfriend
            .addComponent(new CombatStatsComponent(config.health, config.baseAttack, config.stamina, config.mana))
            .addComponent(animator)
            .addComponent(new NPCAnimationController());

    plumberfriend.getComponent(AITaskComponent.class);
    plumberfriend.getComponent(AnimationRenderComponent.class).scaleEntity();
    plumberfriend.getComponent(PhysicsComponent.class).setBodyType(BodyDef.BodyType.StaticBody);
    plumberfriend.setScale(1, 1);
    return plumberfriend;
  }

  /**
   * Creates a friendly creature NPC entity.
   *
   * @param target entity to stand
   * @return entity
   */

  public static Entity createFriendlyCreature (Entity target) {
    Entity friendlycreature = createBaseNPC();
    FriendlyCreatureConfig config = configs.friendlycreature;

    AnimationRenderComponent animator =
            new AnimationRenderComponent(ServiceLocator.getResourceService().getAsset("images/NPC/friendly_creature npc/friendly_creature.atlas", TextureAtlas.class));
    animator.addAnimation("creatureShake", 0.1f, Animation.PlayMode.LOOP);


    friendlycreature
            .addComponent(new CombatStatsComponent(config.health, config.baseAttack, config.stamina, config.mana))
            .addComponent(animator)
            .addComponent(new NPCAnimationController());

    friendlycreature.getComponent(AITaskComponent.class);
    friendlycreature.getComponent(AnimationRenderComponent.class).scaleEntity();
    friendlycreature.getComponent(PhysicsComponent.class).setBodyType(BodyDef.BodyType.StaticBody);
    friendlycreature.setScale(1, 1);
    return friendlycreature;

  }

  /**
   * Creates an atlantis male NPC entity.
   *
   * @param target entity to stand
   * @return entity
   */
  public static Entity createMale_citizen (Entity target) {

    Entity male_citizen = createBaseNPC();
    Male_citizenConfig config = configs.male_citizen;
    AnimationRenderComponent animator =
            new AnimationRenderComponent(
                    ServiceLocator.getResourceService()
                            .getAsset("images/NPC/male_citizen/male-atlas.atlas", TextureAtlas.class));
    animator.addAnimation("MaleShake", 0.1f, Animation.PlayMode.LOOP);

    male_citizen
            .addComponent(new CombatStatsComponent(config.health, config.baseAttack, config.stamina, config.mana))

            .addComponent(new MaleAnimationController())
            .addComponent(animator)
            .addComponent(new NPCAnimationController());
    ;

//images/NPC/male_citizen/male-atlas.atlas

    male_citizen.getComponent(AITaskComponent.class);
    male_citizen.getComponent(AnimationRenderComponent.class).scaleEntity();
    male_citizen.getComponent(PhysicsComponent.class).setBodyType(BodyDef.BodyType.StaticBody);
    male_citizen.setScale(1, 1);
    return male_citizen;
  }

Configs

Atlantis female NPC config

The config file for the female NPC

  public class OneLegGirlConfig extends BaseEntityConfig{
    public int baseAttack = 0;
}

Atlantis child NPC config

The config file for the child NPC

  public class ChildConfig extends BaseEntityConfig{
    public int baseAttack = 0;
}

Atlantis guard NPC config

The config file for Guard NPC.

  public class GuardConfig extends BaseEntityConfig{
    public int baseAttack = 0;
}

Atlantis male NPC config

The config file for Male NPC

  public class Male_citizenConfig extends BaseEntityConfig{
    public int baseAttack = 0;
}

NPC ForestGameArea

Adding the NPC to the main game area, the position will be set here. The NPC will spawn at a specific position, and can not move by the player. The coder can use the GridPointToVector(GridPoint2 position) to convert GridPoint2 type to Vector type.

//The updated forestGamaArea code will not spawn the NPCs in a random position. The NPCs will spawn at a specific position, so that the player //do not need to look around for NPCs
  /**
   * Spawn female NPC in random position. - Team 7 all-mid-npc
   */
  private void spawnOneLegGirl() {

    oneLegGirlPosition = new GridPoint2(20, 20);

    Entity oneLegGirl = NPCFactory.createOneLegGirl(player);
    spawnEntityAt(oneLegGirl, oneLegGirlPosition, true, true);


    Entity dialogue = DialogueFactory.createDialogue();
    spawnEntityAt(dialogue, oneLegGirlPosition, true, true);
  }
  public static GridPoint2 getOneLegGirlPosition() {
    return oneLegGirlPosition;
  }

  public static Vector2 GridPointToVector(GridPoint2 position) {
    int playerX = (int) position.x;
    int playerY = (int) position.y;
    Vector2 new_position = new Vector2(playerX, playerY);
    return new_position;
  }

  /**
   * Spawn child NPC in random position. - Team 7 all-mid-npc
   */
  private void spawnChild() {
    childPosition = new GridPoint2(7, 7);

    Entity child = NPCFactory.createChild(player);
    spawnEntityAt(child, childPosition, true, true);

    Entity dialogue = DialogueFactory.createDialogue();
    spawnEntityAt(dialogue, childPosition, true, true);
  }

  public static GridPoint2 getChildPosition() {
    return childPosition;
  }


  /**
   * Spawn guard NPC in random position. - Team 7 all-mid-npc
   */
  private void spawnGuard() {
    GuardPosition = new GridPoint2(4, 8);

    Entity guard = NPCFactory.createGuard(player);
    spawnEntityAt(guard, GuardPosition, true, true);

    Entity dialogue = DialogueFactory.createDialogue();
    spawnEntityAt(dialogue, GuardPosition, true, true);
  }
  public static GridPoint2 getGuardPosition() {
    return GuardPosition;
  }

  private void spawnHumanGuard() {
    HumanGuardPosition = new GridPoint2(7, 10);

    Entity humanguard = NPCFactory.createHumanGuard(player);
    spawnEntityAt(humanguard, HumanGuardPosition, true, true);

    Entity dialogue = DialogueFactory.createDialogue();
    spawnEntityAt(dialogue, HumanGuardPosition, true, true);
  }

  private void spawnPlumberFriend() {
    PlumberFriendPosition = new GridPoint2(7, 20);

    Entity plumberfriend = NPCFactory.createPlumberFriend(player);
    spawnEntityAt(plumberfriend, PlumberFriendPosition, true, true);

    Entity dialogue = DialogueFactory.createDialogue();
    spawnEntityAt(dialogue, PlumberFriendPosition, true, true);
  }

  /**
   * Spawn male NPC in random position. - Team 7 all-mid-npc
   */
  private void spawnMaleCitizen() {
    maleCitizenPosition = new GridPoint2(3, 8);

    Entity male_citizen = NPCFactory.createMale_citizen(player);
    spawnEntityAt(male_citizen, maleCitizenPosition, true, true);

    Entity dialogue = DialogueFactory.createDialogue();
    spawnEntityAt(dialogue, maleCitizenPosition, true, true);
  }
  public static GridPoint2 getMaleCitizenPosition() {
    return maleCitizenPosition;
  }

  /**
   * Spawn friendly creature NPC in random position. - Team 7 all-mid-npc
   */

  private void spawnfriendlycreature() {
    friendlycreaturePosition = new GridPoint2(5, 10);

    Entity friendlycreature = NPCFactory.createFriendlyCreature(player);
    spawnEntityAt(friendlycreature, friendlycreaturePosition, true, true);

    Entity dialogue = DialogueFactory.createDialogue();
    spawnEntityAt(dialogue, friendlycreaturePosition, true, true);
  }

NPC Animation controller

The NPCAnimationController contains all of the animations of the NPCs and adds the event listener and triggers for each NPCs


public class NPCAnimationController extends Component {
    AnimationRenderComponent animator;

    @Override
    public void create() {
        super.create();
        animator = this.entity.getComponent(AnimationRenderComponent.class);
        entity.getEvents().addListener("MaleShake", this::MaleShakeStart);
        entity.getEvents().trigger("MaleShake");
        entity.getEvents().addListener("femaleShake", this::femaleShakeStart);
        entity.getEvents().trigger("femaleShake");
        entity.getEvents().addListener("childShake", this::childShakeStart);
        entity.getEvents().trigger("childShake");
        entity.getEvents().addListener("guardShake", this::guardShakeStart);
        entity.getEvents().trigger("guardShake");
        entity.getEvents().addListener("humanguardShake", this::humanguardShakeStart);
        entity.getEvents().trigger("humanguardShake");
        entity.getEvents().addListener("plumberfriendShake", this::plumberfriendShakeStart);
        entity.getEvents().trigger("plumberfriendShake");
        entity.getEvents().addListener("creatureShake", this::creatureShakeStart);
        entity.getEvents().trigger("creatureShake");
    }

    void MaleShakeStart() {
        animator.startAnimation("MaleShake");
    }
    void femaleShakeStart() {
        animator.startAnimation("femaleShake");
    }
    void childShakeStart() {
        animator.startAnimation("childShake");
    }
    void guardShakeStart() {
        animator.startAnimation("guardShake");
    }
    void humanguardShakeStart() {
        animator.startAnimation("humanguardShake");
    }
    void plumberfriendShakeStart() {
        animator.startAnimation("plumberfriendShake");
    }
    void creatureShakeStart() {
        animator.startAnimation("creatureShake");
    }

}