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

Author Topic: Game logic help!  (Read 3141 times)

0 Members and 1 Guest are viewing this topic.

haz

  • Newbie
  • *
  • Posts: 8
    • View Profile
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();
                }
        }

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Game logic help!
« Reply #1 on: December 05, 2015, 08:49:27 pm »
To get/access the very first entity, you could just use:

*m_vEnemies->begin()

For the last entity:

*m_vEnemies->rbegin()

But besides that, I think you've got your logic wrong in some way. You only want to check the first or last entity, but you're iterating over all of them?

haz

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Game logic help!
« Reply #2 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!

Ungod

  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: Game logic help!
« Reply #3 on: December 05, 2015, 09:19:31 pm »
Code: [Select]
if (pEnemy->isEntityActive() && m_vEnemies.front()->getEntityPosition().x >= 0)
        {
            pEnemy->MoveEnemiesRight();
        }
        else
        {
            pEnemy->MoveEnemiesLeft();
        }

Additional to the problem already mentioned: If I get your example right, this if doesnt work as intended since left-movement in the else block is called even if the entity isnt active. Is that indended?

haz

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Game logic help!
« Reply #4 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!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Game logic help!
« Reply #5 on: December 05, 2015, 11:07:04 pm »
To get/access the very first entity, you could just use:
*m_vEnemies->begin()
For the last entity:
*m_vEnemies->rbegin()
These (begin and rbegin) are for iterating (they're iterators!). Moreover, rbegin is for reverse iterating.

m_vEnemies.front() and m_vEnemies.back() to access the first and last elements directly makes much more sense.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: Game logic help!
« Reply #6 on: December 17, 2015, 08:54:51 pm »
Hello Haz

I don't use C++, but concerning to your game's logic, i tell you what i generally do (you can see it in my poor Sonic game)

You have a group of enemies (or entities); i suggest Enemy (or Entity) should be a class of objects; then EntityGroup should be another class. In the 1st you could define everything related to the Enemies: moves, collisions, attacks, explosions, and so; but you define that just once in the class code. In the 2nd, you could define how many Enemy objects there are, and make it draw (and execute their logic) all the Enemies in the EnemyGroup that are Alive.

the following is in C#,  i don't know C++, please could some member translate for Haz? (Thanks)
class Enemy
{
    private int x, y, sense, life;    
    public Enemy(int x, int y) {this.x = x; this.y = y; this.sense = 1; this.life = 1; }    
    private void Movement()
    {
        this.x = this.x + 4 * this.sense;  \\ or this.x += 4 * this.sense;
        if (this.x >= screenWidth || this.x <= 0)        
            this.sense = -this.sense;  \\ or this.sense *= -1; change the sense right/left when reachs the borders
        if (this.y < screenHeight / 2)
            this.y = this.y + 2;   \\ or this.y += 2; suppose the Enemy goes forward up to the middle of the screen
        \\ maybe then, they could go backwards to the screen top
    }
    public Boolean IsAlive()
    {
        if (this.life > 0)
            return true;
        return false;
    }
    private void Attack() {
        \\ throw bullets, each a randomized number of seconds      
    }
    private void TestDestroyed() {
        \\ see if some bullet hit the Enemy
    }
    public void Draw()
    {
        this.Movement();
        \\ add Attack and/or Being Destroyed stuff here
        \\ AND draw the Sprite
    }
}
class EnemyGroup
{
    private Enemy[] vector;
    public EnemyGroup(int numberOfEnemies)
    {
        this.vector = new Enemy[numberOfEnemies];
        for (int a = 0; a < numberOfEnemies; a++)
            this.vector[a] = new Enemy((a * 40) % screenWidth, (a * 40) / screenWidth);
            \\ set the Enemies together spaced by 40 px (to be changed), from one's to another's begginig,
            \\ and continue in the following line when reach the right screen border
    }
    public DrawAllEnemies()
    {
        for (int a = 0; a < this.vector.Length; a++)
            if (this.vector[a].IsAlive())
                this.vector[a].Draw();
    }
}
 

Also you can write the player's class' code but that you move and shoot by the keyboard
Hope this helps.

Pablo


 

anything