Skip to content

Projectiles

pchsa edited this page Oct 3, 2023 · 8 revisions

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.

Functionality

  • 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.

Usage

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);
    }

System Overview

Projectiles are created using the ProjectileFactory. A standard projectile should have the following components. image

Clone this wiki locally