Trebuchet - UQdeco2800/2022-studio-3 GitHub Wiki

Introduction

This page introduces the function of the trebuchet. The main function of the trebuchet is to launch the bullet to attack the ship. When the bullet contacts the ship, the bullet will disappear and the ship's HP will decrease.

  • Spawn bullet at trebuchet location
  • The bullet will move towards the target
  • The bullet disappears when it touches the target

Usage

1. Create a bullet entity by listening to the trigger in the rangedAttackTask through AttackListnerComponent.

public void create() {
        super.create();
        entity.getEvents().addListener("attack", this::attack);
    }

    void attack() {
        gameArea.spawnEntity(EnemyFactory.createBullet(this.entity, target, gameArea));
    }

2. Create a BulletHitShipComponent, which can detect whether the bullet collides with the target. When the bullet collides with the target, the bullet will be destroyed and the target will be damaged.

public void create() {
        super.create();
        entity.getEvents().addListener("collisionStart", this::Hit);
    }

    private void Hit(Fixture attack, Fixture player) {
        Entity bullet = ((BodyUserData) attack.getBody().getUserData()).entity;
        Entity target = ((BodyUserData) player.getBody().getUserData()).entity;
        if(target == this.target) {
            bullet.getComponent(PhysicsComponent.class).getPhysics().addToDestroy(bullet);
            target.getComponent(CombatStatsComponent.class).decreaseHealth(20);
        }
    }

UML

img