Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - haz

Pages: [1]
1
General / Re: Game logic help!
« on: December 05, 2015, 09:41:50 pm »
@Ungod

The 'isEntityActive' flag is just for me to know which objects to update, if the object is false, it will have been destroyed by a bullet so will no longer exist.

With the code I pasted, I am trying to get my objects to move from  left to right on the screen. I can get them to move to the left based on the position of the last element in the vector.
I am then trying to move them back once I know that the last element in the vector has hit the left side of the game border. Then do the same for the right side with the first element in the vector.

I think my logic is a mess, hence why I am having some issues!

2
General / Re: Game logic help!
« on: December 05, 2015, 08:54:39 pm »
Yeah just the first and last, because I have multiple objects in a grid in my game and they move together, I just want to check the first (top left) object and the bottom object (bottom right) and see when they hit the walls to change the direction accordingly if that makes sense?

Thanks for your feedback also, I guess I should not be iterating through the whole list, but I am only doing so because I am also updating all objects to move in the direction based on the collision above!

3
General / Game logic help!
« on: December 05, 2015, 08:43:44 pm »
Hey,

I am struggling with some game logic, it seems really simple in my mind but I am getting so stuck! I have a vector of objects.
I iterate through the the list and check if the position(X) of the last element in the vector is >= to the games border. If so I want to change the direction to move backwards. Then I want to check if the position of the first element in the vector has hit the game wall. If so reverse the direction once again.

I'm having multiple failed attempts right now, I am guessing it is because when the object is not >= to the game border it no longer needs to move.


for (m_it = m_vEnemies.begin(); m_it != m_vEnemies.end(); m_it++)
        {
                EnemyEntity* pEnemy = *m_it;

                if (pEnemy->isEntityActive() && m_vEnemies.front()->getEntityPosition().x >= 0)
                {
                        pEnemy->MoveEnemiesRight();
                }
                else
                {
                        pEnemy->MoveEnemiesLeft();
                }
        }

4
Graphics / Re: Drawing out objects in a grid shape!
« on: December 05, 2015, 12:49:37 am »
Yep so, I have a base class called 'Entity' which inherits from sf::Drawable.

I have set up 'EnemyEntity' class pretty similar to the way I set up a 'Bullet' class which are also derived from Entity however in this case the functionality is completely different.

I set up the texture of the enemies in the handler. I have a 'Texturehandler' class which I can add and get textures. In the enemies class I have function that takes in a reference to a texture and the goes through the vector and adds them accordingly to each object.

Some code below to help explain better.

I tried this code below, this works and the enemies are drawn on the screen at random positions. So I am not sure what is causing them not to be drawn from the code shown in an earlier post.

               
m_vEnemies[i]->getEntitySprite().setPosition(rand() % m_screenWidth, rand() % m_screenHeight);
 

EnemyEntity::EnemyEntity()
        :Entity()
        , m_speed(0.1)
        , m_fireRate(0.1)
{
        setEntityType(eEnemy);
        getEntitySprite().setScale(0.1f, 0.1f);
        setEntityActive(true);
}

void EnemyHandler::setEnemyTexture(sf::Texture& enemyTexture)
{
        for (m_it = m_vEnemies.begin(); m_it != m_vEnemies.end(); m_it++)
        {
                EnemyEntity* pEnemy = *m_it;
                pEnemy->setEntityTexture(enemyTexture);
        }
}

//Base class draw function I have used in all of my other derived classes which works fine.
void Entity::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
        target.draw(m_entitySprite, states);
}
 

5
Graphics / Re: Drawing out objects in a grid shape!
« on: December 05, 2015, 12:27:52 am »
Haha sorry, yes I did, and the same values are being spat out.

And yes that returns the sprites position!

Edit: is this the best way to go about drawing objects in a grid?

6
Graphics / Re: Drawing out objects in a grid shape!
« on: December 05, 2015, 12:22:06 am »
Thanks for the reply Arcade!

Yeah so, I use std::cout and I can see that the 'X' positions of the object has been incremented by 20 for each one and the 'Y' value stays the same! If I alter the 'Y' position the positions update as expected however only the first element from the vector is displayed.

EDIT: I just re-read your comment, yes I just debugged in the draw loop and the positions are exactly the same as when doing the code below!

        for (int i = 0; i < m_vEnemies.size(); i++)
        {
                m_vEnemies[i]->getEntitySprite().setPosition(20 + (i * 10), 0);
                std::cout << m_vEnemies[i]->getEntityPosition().x << std::endl;
                std::cout << m_vEnemies[i]->getEntityPosition().y << std::endl;
        }
 

7
Graphics / Drawing out objects in a grid shape!
« on: December 04, 2015, 10:52:17 pm »
EDIT: Sorry if this was not the best place to post it! If someone could move it to a more appropriate area, that would be great!

Hey!

Could do with some help/guidance. I am trying to draw some objects that I have stored in a vector in a grid format.

I push back a number of objects in to a vector with no default position. Each object has a sprite & texture.

I then call a function which I want to go through the vector and put the objects in a grid like shape. But for some reason, it only displays the first object however when I look at the individual objects in the vector they're positions are different.

Some code below to help explain what I am doing.

Thanks for any help/guidance!

//Called at initialisation
        for (int i = 0; i < m_maxEnemies; i++)
        {
                m_vEnemies.push_back(new EnemyEntity());
        }

//Then I call the function below to try and manipulate their positions to where I want them to be.
        for (int i = 0; i < m_maxEnemies; i++)
        {
                m_vEnemies[i]->getEntitySprite().setPosition(10 + (i * 10), 0);
        }
//The above code seems to work when I look at the debug information however only the first enemy is displayed, a bit stuck here!

//I draw the objects from my main.cpp, each object has a flag to check whether it is active, by default when creating one instance of type 'EnemyEntity()' it is set to true so they should be all being drawn.
       
for (auto &enemy : enemyHandler->getVectorOfEnemies())
                {
                        if (enemy->isEntityActive())
                        {
                                m_renderWindow.draw(*enemy);
                        }
                }
 

8
General / SFML shooting help!
« on: November 24, 2015, 05:47:22 pm »
Hi all, I am currently learning C++ with SFML.

For the past few hours I have been stuck on trying to create bullets in my game. I understand what I need to do but I am bit stuck on how to get my objects to draw based on certain conditions. I am trying to do this in an OOP way so I'll do my best to explain and show you what I have done so far.

So I have a weapon class. A weapon has a vector of bullets. I have reserved space at initialization and I load the vector with 100 bullets.
The bullets should only be drawn when they're active. By default they are not and only become active once the player fires using the 'space key'.

Once they are active I also allowed them to be updated so that they can move on the Y axis, I will be implementing enemies and then checking if they collide with anything or go off the screen, if they do so I will set them back to inactive so they can be reused.

I am inheriting from sf::Drawable in the bullet class but I am not sure how to pass through the vector to it so it can check which bullets are to be drawn based on the Boolean. Below is some code in case I didn't make sense!

I also have a texture handler, where I can load in and set textures. However when trying to do this with my Bullet, it only does the first instance and the rest are null. So for now I have created a texture within the bullet class just until I can get it working.

Any guidance/help will be much appreciated, and thanks for taking the time to read my post!

class Bullet : public sf::Drawable , public sf::Transformable

Bullet::Bullet()
        : m_Speed(0.1)
        , m_isActive(false)
{
        m_bulletTexture.loadFromFile("normalBullet.jpg");
        m_bulletSprite.setTexture(m_bulletTexture);
        m_bulletSprite.setScale(0.1f, 0.1f);
}

void Bullet::Update()
{
        if (m_isActive)
        {
                m_bulletSprite.move(0, 0.1);
        }
}

void Bullet::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
        if (m_isActive)
        {
                states.transform *= getTransform();
                target.draw(m_bulletSprite, states);
        }
               
}

class Weapon
{
private:
        std::vector <Bullet> m_vBullets;

void loadWeapon();

public:

void fireWeapon(); //This is called from the player class when the space bar is hit.

void Weapon::loadWeapon()
{
        for (int i = 0; i < 100; i++)
        {
                m_vBullets.push_back(Bullet());
        }
}

void Weapon::fireWeapon() //When the player fires, I want to set the position relative to the players, at the moment for testing purposes I do 50, 50. I then set the bullet to active, as it goes through the vector we keep going until we find a bullet that is not active.
{
        for (int i = 0; i < 100; i++)
        {
                m_vBullets[i].setBulletActive(true);
                m_vBullets[i].setBulletPosition(50, 50);
               
                if (m_vBullets[i].isBulletActive() == true)
                {
                        return;
                }
       
        }
}

 

Pages: [1]