Jump to content

  • Log In with Google      Sign In   
  • Create Account

Estivo

Member Since 27 Jun 2013
Offline Last Active Jan 27 2014 09:25 AM

Posts I've Made

In Topic: Shooting a bullet to the mouse position?

03 January 2014 - 05:31 PM

In Zombienation (my project)

class Bullet : public sf::RectangleShape
{
public:
    Bullet(float speed,sf::Vector2f direct,sf::Vector2f pos);
    bool update(float dt);
    double getDistance()
    {
        return distanceTraveled;
    }

private:
    float speed;
    sf::Vector2f direction;
    double distanceTraveled; // if distance is to long then delete bullet from memory
    sf::Vector2f position, startingPosition;
};
bool Weapon::shot(sf::Vector2f pos, sf::Vector2f direction)
{
    if((roundsIn > 0) && canShot)
    {
        isShooting = true;
        for(unsigned int x = 0; x < bulletCont.size(); x++)
        {
            if(bulletCont[x] == nullptr) // when one is empty fill it with new bullet
            {
                bulletCont[x] = new Bullet(1600,direction,pos);
                canShot = false; // each weapon have different fire rate 
                roundsIn--;
                return true;
                break;
            }
        }
        bulletCont.push_back(new Bullet(1600,direction,pos));
        canShot = false;
        roundsIn--;
    }
    else
    {
        isShooting = false; // weapon is empty or have to wait for new bullet
        return false;
    }
    if(roundsIn == 0)
    {
        reloadingCall = true;
    }
}

where bulletCont is a vector of Bullet's pointers. Simple.

 

And direction

void CCharacter::shot()
{
    sf::Vector2f tempDirect(aim); // aim store where mouse is
    recoil(&tempDirect);
    if(actual->shot(pos,tempDirect)) // player tried too shot
    {
        crosshair->pushSize(1.1);
    }
}
void CCharacter::updateDirection()
{
    direct.x = (aim.x-400);
    direct.y = (aim.y-300);

    double len = sqrtf(direct.x*direct.x+direct.y*direct.y);
    direct.x/=len;
    direct.y/=len;
}

If you want I could send you all files (cpp + hpp of those 3 classes)


In Topic: SFML - Is there a way to implement lights?

27 June 2013 - 02:09 PM

As well you can create your own drawable class.

 

http://www.sfml-dev.org/tutorials/2.0/graphics-vertex-array.php


PARTNERS