I am developing a little 2D sidescroll shooter game. Currently I am working on the basic enemies.

For that purpose I developed a component which will spawn shots directed torwards the enemy. The component-system and the engine I developed myself.

This is my UfoAttackController:

#ifndef GUARD_UfoAttackController_H
#define GUARD_UfoAttackController_H

#include "Engine\component.h"
#include "Engine\actor.h"

#include "Engine\engine.h"

#include "shotcollisionevent.h"
#include "componentget.h"
#include "ufoshotcleaner.h"

class UfoAttackController : public ActorComponent //ActorComponent is a base component all components have to inherit from
    UfoAttackController(Actor* target) //A pointer to the player will be passed to the component to give it a target
        : ActorComponent("UfoAttackController"), m_Target(target), m_Interval(1.0f), m_TimeLastShot(5.0f)

    virtual void Update()
        if ((double)Util::GetTotalTime() - m_TimeLastShot > m_Interval)
            Vector2 targetPosition = m_Target->GetPosition();
            Vector2 targetDir      = targetPosition - GetOwner()->GetPosition(); //GetOwner returns the component owner, which is the actor it's attached to

            targetDir.Y -= 100;

            getEngine().RunCreationCall(mango::bind<void>(BuildUfoShot, getEngine().GetFactory(), targetDir, GetOwner())); //tells the engine to create an Ufo-Shot

            m_TimeLastShot = (double)Util::GetTotalTime(); //contains the global game timer

    virtual void Move(Vector2 motion) //implemented due to base-component


    virtual void SetPosition(Vector2 pos) //implemented due to base-component


    static void SetShotPhysic(Actor* actor, Vector2 dir)
        PhysicProperties* physic = getActorPhysic(actor);

        physic->SetElasticity(0.001f); //The elasticy determins how far an actor bounces of if it collides with the holder of the containing physic component

        if (dir.X > 100 || dir.X < -100)
            physic->SetXVelocity(dir.X / 50.0f);
        if (dir.Y > 100 || dir.Y < -100)
            physic->SetYVelocity(dir.Y / 50.0f);


        actor->SetCollisionTrigger(triggerShotCollision); //sets the collision function which shall be called on collision

    static void BuildUfoShot(ActorFactory* factory, Vector2 direction, Actor* actor)
        static mango::int32 id = 0; //generic ID to differenciate between the shots. The engine won't allow actor with similar names.

        ActorFactory::StaticAssetLayout asset; //A layout which specifies the data of an asset.

        asset.name = String32("ufoshot") + mango::to_string32(id);

        asset.position = actor->GetPosition() + Vector2(0, 100);

        asset.size = Vector2(35, 35);
        asset.spriteName = "Effects\\laserShot0"; //sprite path and name
        asset.layer = 1; //layer in which the asset shall be rendered. 0 is the highest layer.

        Actor* shot = factory->CreateStaticProjectile(asset, false, true);

        shot->AddComponent(new UfoShotCleaner()); //class which will clean ufo shots with speed between 1 and -1

        SetShotPhysic(shot, direction);


    Actor*  m_Target;
    double  m_Interval;
    double  m_TimeLastShot;


Inside of the Update function I calculate the direction in which the shot shall be fired over the players position. Basically this works but I feel like there could be a better way of handling this. Does anyone have a better idea how to determin the direction where the ufo should shoot?

I won't explain every setting you can set on your actor since that will only confuse people, I am mainly interested in a better way to determin the direction vector for the player.

Ingame it looks like this:

enter image description here

  • 1
    \$\begingroup\$ Fundamentally, when shooting at something, you either shoot at where it is at right now (more-or-less your current implementation), or you try to predict where the target is going to be by the time the shot gets there. There are several ways to do that, generally looking at either the target's current velocity and/or the target's previous locations to determine a trend in the movement. \$\endgroup\$ Apr 9 '18 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.