-
Notifications
You must be signed in to change notification settings - Fork 7
Projectiles
To make combat in the game more interesting, projectiles were added, in which hostiles can shoot at the player, and the player can shoot at hostiles.
Projectiles main functionality is provided in ProjectileComponent
. This component handles disposing projectile after x seconds, settting speed of projectile, moving projectile in target direction, and impact behaviour.
- Set time (seconds) until projectile expired in constructor
-
setTargetDirection
start projectile movement towards given target -
setConstantVelocity
set if projectile has constant velocity- -
setSpeed
set speed of projectile -
destroyProjectile
handle disposing of projectile entity -
impact
handle impact event, stops projectile movement.
To code below outlines how to add an oxygen eater projectile.
Add projectile in ProjectileFactory with necessary components and setters. This projectile will have a speed of 4, is destroyed on impact, and has a constant velocity.
public static Entity createOxygenEaterProjectile() {
Entity projectile = createBaseProjectile();
...
projectile
.addComponent(new ProjectileComponent(2f))
.addComponent(new TouchAttackComponent(PhysicsLayer.PLAYER, 10f))
.addComponent(animator);
projectile.getComponent(ProjectileComponent.class).setSpeed(new Vector2(4f, 4f));
projectile.getComponent(ProjectileComponent.class).setDestroyOnImpact(true);
projectile.getComponent(ProjectileComponent.class).setConstantVelocity(true);
...
return projectile
The projectile is spawned in the oxygen eater's shoot()
function towards a target position
private void shoot(Vector2 position) {
Entity projectile = projectileSupplier.get(); // this is ProjectileFactory::createOxygenEaterProjectile
projectile.setCenterPosition(entity.getCenterPosition());
ServiceLocator.getGameArea().spawnEntity(projectile);
ProjectileComponent projectileComponent = projectile.getComponent(ProjectileComponent.class);
projectileComponent.setTargetDirection(position);
}
Projectiles are created using the ProjectileFactory
. A standard projectile should have the following components.
When a projectile is directed, it can be sent towards position using setTargetDirection
. Once it has collided with a target entity of the set PhysicsLayer
, the TouchAttackComponent
will damage CombatStatsComponent
and apply knockback to its PhysicsComponent
. It will also start an impact event. This event will start the impact animation in ProjectileAnimationController
. It will also cause ProjectileComponent
to stop movement, and if set as true, start checking to destroy the ProjectileEntity. Once the impact animation has finished in AnimationRenderComponent
, the ProjectileComponent
will handle disposing of the entity.