Skip to content

Projectile Factory

Isaac Arli edited this page Sep 15, 2023 · 14 revisions

Introduction

The projectile entity is created to ensure that other entities such as mobs and towers are able to defend/attack each other. So, the 'ProjectileFactory' class was implemented. Within this class, the animations as well as speed and effect are integrated to create the different projectiles.

Base Projectile function createBaseProjectile()

The function createBaseProjectile() is responsible for assigning the base components for all projectiles within game. The components added for base projectiles are PhysicsComponent(), PhysicsMovementComponent(), hitBoxComponent() and an AI component TrajectTask() which focuses on the direction of the projectile. image

BaseProjectile UML

Creating a Fireball Projectile createFireBall()

This function will build upon the createBaseProjectile() and adds more specific components for the fireball. It will add TouchAttackComponent() and CombatStatsComponent(). Then it will also set the speed depending on the parameter by accessing PhysicsMovementComponent() and the .setSpeed() function. Within the TouchAttackComponent(), there is an optional knockback parameter which accepts a float and determines how far enemies will be knocked back. Another parameter is the disposeOnHit, which accepts a boolean that determines whether the projectile will disappear on contact. image

Image for the Fireball: new_projectile_4

Creating an Effect Projectile createEffectProjectile()

This function builds upon createBaseProjectile() and creates a projectile that applies an effect from the Effects Component class to the target entity and creates animations for the projectile.

image

Image for Stun effect: projectilestun4

Image for Burn effect: burn4

Image for Slow effect: snow_ball4

Creating a Mob Ball Projectile createMobBall()

This function builds upon createBaseProjectile() and creates a projectile with a green design and rolling animation.

public static Entity createMobBall(short targetLayer, Vector2 destination, Vector2 speed) {
    Entity projectile = createBaseProjectile(targetLayer, destination, speed);

    AnimationRenderComponent animator = 
      new AnimationRenderComponent(
        ServiceLocator.getResourceService()
          .getAsset("images/projectiles/mobProjectile.atlas", TextureAtlas.class));

      animator.addAnimation("rotate", 0.15f, Animation.PlayMode.LOOP);

    projectile
        .addComponent(animator)
        .addComponent(new MobProjectileAnimationController());

    projectile
        .getComponent(AnimationRenderComponent.class).scaleEntity();

    return projectile;
  }

Image used within game enimiesBall2

Creating MobKing Ball Projectile 'createMobKingBall()'

MobKing fireball builds upon createBaseProjectile() and creates a fireball that does damage to its target but won't self destruct on impact.

  public static Entity createMobKingBall(short targetLayer, Vector2 destination, Vector2 speed) {
    Entity projectile = createBaseProjectile(targetLayer, destination, speed);

    AnimationRenderComponent animator =
            new AnimationRenderComponent(
                    ServiceLocator.getResourceService()
                            .getAsset("images/projectiles/mobKing_projectile.atlas", TextureAtlas.class));
    animator.addAnimation("mob_boss", 0.17f, Animation.PlayMode.NORMAL);
    animator.addAnimation("mob_bossFinal", 0.17f, Animation.PlayMode.NORMAL);


    projectile
            .addComponent(animator)
            .addComponent(new MobKingProjectAnimController());

    projectile
            .getComponent(AnimationRenderComponent.class).scaleEntity();

    return projectile;
  }

Image for MobKing ball: mob_boss7

Creating a Pierce Fireball Projectile createPierceFireBall()

Pierce fireball builds upon createBaseProjectile() and creates a fireball that does damage to its target but won't self destruct on impact.

public static Entity createPierceFireBall(short targetLayer, Vector2 destination, Vector2 speed) {
    Entity fireBall = createPierceBallAnim(targetLayer, destination, speed);
    fireBall.getComponent(TouchAttackComponent.class).setDisposeOnHit(false);
    fireBall.getComponent(TouchAttackComponent.class).setKnockBack(0f);

    return fireBall;
}

public static Entity createPierceBallAnim(short targetLayer, Vector2 destination, Vector2 speed) {
    Entity projectile = createBaseProjectile(targetLayer, destination, speed);

    AnimationRenderComponent animator = new AnimationRenderComponent(ServiceLocator.getResourceService()
                                        .getAsset("images/projectiles/pierce_anim.atlas", TextureAtlas.class));
    animator.addAnimation(START_ANIM, 0.05f, Animation.PlayMode.LOOP);
    projectile
            .addComponent(animator)
            .addComponent(new PierceProjectileAnimationController());

    return projectile;
}

Image used for Pierce Fireball: pierce2

Creating Ricochet Fireball Projectile 'createRicochetFireball'

Ricochet fireball builds upon createBaseProjectile() and creates a fireball that does damage to its target as well as changing its direction.

public static Entity createRicochetFireball(short targetLayer, Vector2 destination, Vector2 speed, int bounceCount) {
    Entity fireBall = createFireBall(targetLayer, destination, speed);
    fireBall
            .addComponent(new RicochetComponent(targetLayer, bounceCount));

    setColliderSize(fireBall, (float) 0.1, (float) 0.1);

    return fireBall;
  }

Image used for Ricochet fireball: new_projectile_4

Create Split Fireworks Projectile 'createSplitFireWorksFireball'

Split Fireworks fireball builds upon createBaseProjectile() and creates multiple fireballs that does damage to its target after a collision.

public static Entity createSplitFireWorksFireball(short targetLayer, Vector2 destination, Vector2 speed, int amount) {
    Entity fireBall = createFireBall(targetLayer, destination, speed);
    fireBall
            .addComponent(new SplitFireworksComponent(targetLayer, amount));

    return fireBall;
}

public static Entity createFireworks(short targetLayer, Vector2 destination, Vector2 speed) {
    Entity projectile = createBaseProjectile(targetLayer, destination, speed);

    AnimationRenderComponent animator =
            new AnimationRenderComponent(
                    ServiceLocator.getResourceService()
                            .getAsset("images/projectiles/firework_anim.atlas", TextureAtlas.class));
    animator.addAnimation(START_ANIM, 0.2f, Animation.PlayMode.LOOP);
    projectile
            .addComponent(animator)
            .addComponent(new FireworkAnimationController());

    return projectile;
}

Image used for Fireworks: firework4

Clone this wiki locally