Skip to content

Commit

Permalink
Refactored ProjectileFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
JSLLW committed Sep 7, 2023
1 parent 6f7acb1 commit 18a01da
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@
import com.csse3200.game.files.FileLoader;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.rendering.AnimationRenderComponent;
import com.csse3200.game.rendering.TextureRenderComponent;
import com.csse3200.game.services.ServiceLocator;
import com.csse3200.game.physics.PhysicsLayer;
import com.csse3200.game.physics.PhysicsUtils;
import com.csse3200.game.physics.components.ColliderComponent;
import com.csse3200.game.physics.components.HitboxComponent;
import com.csse3200.game.physics.components.PhysicsComponent;
import com.csse3200.game.physics.components.PhysicsMovementComponent;
import com.csse3200.game.components.SelfDestructOnHitComponent;
import com.badlogic.gdx.math.Vector2;
import com.csse3200.game.components.projectile.ProjectileAnimationController;
import com.csse3200.game.services.ServiceLocator;

/**
* Responsible for creating projectiles within the game.
Expand All @@ -51,9 +48,7 @@ public class ProjectileFactory {
* @param effect Specified effect from the ProjectileEffects enums
* @return Returns a new single-target projectile entity
*/
public static Entity createEffectProjectile(short targetLayer, Vector2 destination, Vector2 speed,
ProjectileEffects effect, boolean aoe) {
BaseEntityConfig config = configs.fireBall;
public static Entity createEffectProjectile(short targetLayer, Vector2 destination, Vector2 speed, ProjectileEffects effect, boolean aoe) {
Entity projectile = createFireBall(targetLayer, destination, speed);

switch(effect) {
Expand Down Expand Up @@ -104,9 +99,7 @@ public static Entity createRicochetFireball(short targetLayer, Vector2 destinati
* @return Returns a new fireball projectile entity.
*/
public static Entity createFireBall(short targetLayer, Vector2 destination, Vector2 speed) {
BaseEntityConfig config = configs.fireBall;

Entity projectile = createBaseProjectile(destination);
Entity projectile = createBaseProjectile(targetLayer, destination, speed);

AnimationRenderComponent animator =
new AnimationRenderComponent(
Expand All @@ -116,37 +109,26 @@ public static Entity createFireBall(short targetLayer, Vector2 destination, Vect
animator.addAnimation(FINAL_ANIM, FINAL_SPEED, Animation.PlayMode.NORMAL);

projectile
.addComponent(new ProjectileAnimationController())
.addComponent(new ColliderComponent().setSensor(true))
.addComponent(animator)

// This is the component that allows the projectile to damage a specified target.
.addComponent(new TouchAttackComponent(PhysicsLayer.NPC, 1.5f, true))
.addComponent(new CombatStatsComponent(config.health, config.baseAttack));
.addComponent(animator)
.addComponent(new ProjectileAnimationController());
// .addComponent(new SelfDestructOnHitComponent(PhysicsLayer.OBSTACLE));

// projectile
// .getComponent(TextureRenderComponent.class).scaleEntity();

projectile
.getComponent(PhysicsMovementComponent.class).setSpeed(speed);


return projectile;
}

/**
* Creates a projectile specifically for mobs to shoot
*
* @param targetLayer The enemy entities that the projectile collides with.
* @param targetLayer The enemy layer that the projectile collides with.
* @param destination The destination the projectile heads towards.
* @param speed The speed of the projectile.
* @return Returns a new fireball projectile entity.
*/
public static Entity createMobBall(short targetLayer, Vector2 destination, Vector2 speed) {
BaseEntityConfig config = configs.fireBall;

Entity projectile = createBaseProjectile(destination);
Entity projectile = createBaseProjectile(targetLayer, destination, speed);

AnimationRenderComponent animator =
new AnimationRenderComponent(
Expand All @@ -156,30 +138,27 @@ public static Entity createMobBall(short targetLayer, Vector2 destination, Vecto
animator.addAnimation("rotate", 0.15f, Animation.PlayMode.LOOP);

projectile
.addComponent(new ColliderComponent().setSensor(true))

// This is the component that allows the projectile to damage a specified target.
.addComponent(new TouchAttackComponent(PhysicsLayer.PLAYER, 1.5f, true))
.addComponent(new CombatStatsComponent(config.health, config.baseAttack))
.addComponent(animator)
.addComponent(new MobProjectileAnimationController());

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

projectile
.getComponent(PhysicsMovementComponent.class).setSpeed(speed);

return projectile;
}

/**
* Creates a generic projectile entity that can be used for multiple types of * projectiles.
*
* @param targetLayer The enemy layer that the projectile collides with.
* @param destination The destination the projectile heads towards.
* @param speed The speed of the projectile.
* @return Returns a generic projectile entity.
*/
public static Entity createBaseProjectile(Vector2 destination) {
public static Entity createBaseProjectile(short targetLayer, Vector2
destination, Vector2 speed) {
BaseEntityConfig config = configs.fireBall;

AITaskComponent aiComponent =
new AITaskComponent()
.addTask(new TrajectTask(destination));
Expand All @@ -188,7 +167,16 @@ public static Entity createBaseProjectile(Vector2 destination) {
.addComponent(new PhysicsComponent())
.addComponent(new PhysicsMovementComponent())
.addComponent(new HitboxComponent().setLayer(PhysicsLayer.PROJECTILE))
.addComponent(aiComponent);
.addComponent(aiComponent)
.addComponent(new ColliderComponent().setSensor(true))
// This is the component that allows the projectile to damage a
// specified target.
// Original knockback value: 1.5f
.addComponent(new TouchAttackComponent(targetLayer, 1.5f, true))
.addComponent(new CombatStatsComponent(config.health, config.baseAttack));

projectile
.getComponent(PhysicsMovementComponent.class).setSpeed(speed);

return projectile;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.csse3200.game.components.TouchAttackComponent;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.extensions.GameExtension;
import com.csse3200.game.physics.PhysicsLayer;
import com.csse3200.game.physics.PhysicsService;
import com.csse3200.game.physics.components.HitboxComponent;
import com.csse3200.game.physics.components.PhysicsComponent;
Expand Down Expand Up @@ -55,8 +56,9 @@ public void setUp() {
// ServiceLocator.getResourceService()
// .getAsset("images/projectiles/basic_projectile.atlas", TextureAtlas.class);
Vector2 destination = new Vector2(0.1f, 0.1f);

projectile = ProjectileFactory.createBaseProjectile(destination);
short targetLayer = PhysicsLayer.PLAYER;
Vector2 speed = new Vector2(2f, 2f);
projectile = ProjectileFactory.createBaseProjectile(targetLayer, destination, speed);
}

@Test
Expand Down

0 comments on commit 18a01da

Please sign in to comment.