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

Author Topic: Deleting and spawning object  (Read 3849 times)

0 Members and 1 Guest are viewing this topic.

zpawlo00

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Deleting and spawning object
« on: March 29, 2018, 10:58:27 am »
How can i delete object when it passes the player and spawn new in the same time?

#pragma once
#include <vector>
#include <SFML\Graphics.hpp>


class TBlocks:public sf::Drawable
{
public:
        TBlocks(float t_Y);
        TBlocks()=delete;
        ~TBlocks();

        void update_block();
        void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
        sf::Texture block_texture;
        sf::Sprite blockSprite;
        sf::Texture *bTexture = &block_texture;
        sf::FloatRect blockBound();
        sf::Sprite getSprite();
        sf::Vector2f getPosition();




private:
       

       
        std::string blocks[2];


};

 

#include "TBlocks.h"
#include "Collision.h"
#include <iostream>
#include <stdio.h>    
#include <SFML\Graphics.hpp>
#include <stdlib.h>  
#include <algorithm>
#include <random>
#include <time.h>



TBlocks::TBlocks(float t_Y)
{
        //POSITION X
        int position_of_block_X[8] = {100, 200, 300, 400, 500, 600,700,800};

        //ROTATION
        int rotation_of_block[5] = { 30,20,45,90,15 };

        //PATHS TO BLOCKS
        blocks[0] = "blocks/oracle_1.png";
        blocks[1] = "blocks/rectangle_1.png";
       

        //RANDOM BLOCKS
        std::random_device block_rseed;
        std::mt19937 block_rgen(block_rseed());
        std::uniform_int_distribution<int> block_idist(0, 2);

        int B = block_idist(block_rgen);
        //RANDOM BLOCKS END


        //RANDOM POSITION X
        std::random_device position_rseed;
        std::mt19937 position_rgen(position_rseed());
        std::uniform_int_distribution<int> position_idist(0,7);

        int X = position_idist(position_rgen);
        //RANDOM POSITION X END


        //RANDOM ROTATION
        std::random_device rotation_rseed;
        std::mt19937 rotation_rgen(rotation_rseed());
        std::uniform_int_distribution<int> rotation_idist(0, 4);

        int R = rotation_idist(rotation_rgen);
        //RANDOM ROTATION END

       
       
        Collision::CreateTextureAndBitmask(block_texture, blocks[B]);
        blockSprite.setRotation(rotation_of_block[R]);
        blockSprite.setTexture(block_texture);
        blockSprite.setPosition(position_of_block_X[X], t_Y);
        blockSprite.setOrigin(sf::Vector2f(blockSprite.getTexture()->getSize().x * 0.5, blockSprite.getTexture()->getSize().y * 0.8));
}

void TBlocks::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
        target.draw(this->blockSprite, states);
}
void TBlocks::update_block()
{
        blockSprite.move(0, 3);
}

sf::FloatRect TBlocks::blockBound()
{
        return blockSprite.getGlobalBounds();

}


sf::Sprite TBlocks::getSprite()
{
        return blockSprite;
}

sf::Vector2f TBlocks::getPosition()
{
        return blockSprite.getPosition();
}

TBlocks::~TBlocks()
{
        std::cout << "Clean";

}

#include "TGame.h"
#include "TBlocks.h"
#include "Menu.h"
#include <iostream>
#include "Collision.h"
#include <SFML\Graphics.hpp>\

#define WIDTH 900
#define HEIGTH 950


TGame::TGame(TPlayer * player_, TBlocks * block_) : player(player_), block(block_), mWindow(sf::VideoMode(WIDTH, HEIGTH), "GAME")
{
        mWindow.setFramerateLimit(60);
}


void TGame::run()
{
        sf::Clock clock;

        while (mWindow.isOpen())
        {
               
                processEvents();
                sf::Time elapsed = clock.getElapsedTime();
                render();
                update_game();
               
        }
}

void TGame::processEvents()
{
        sf::Event event;
        while (mWindow.pollEvent(event))
        {
                if (event.type == sf::Event::Closed)
                        mWindow.close();
                break;
        }
}


void TGame::render()
{
       
        mWindow.clear();

        mWindow.draw(*block);
        mWindow.draw(*player);
        block->update_block();
        renderBlocks();
        player->update();


        mWindow.display();
}

void TGame::renderBlocks()
{
       
                if (block->getPosition().y > (player->getPosition().y * (5.f/2) ))
                {
                        std::cout << "MNEIJSZE";
                        //block->~TBlocks();
                }

}

void TGame::update_game()
{
       
}


bool TGame::checkCollision()
{
        if (Collision::PixelPerfectTest(player->getSprite(), block->getSprite()))
        {
                std::cout << "Collision!";
                return true;
        }

}

 
« Last Edit: March 29, 2018, 11:05:12 am by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Deleting and spawning object
« Reply #1 on: March 29, 2018, 11:06:59 am »
You can for example use a vector, then you can push and pop objects from it. Keep in mind that creating and removing object instances is basic C++ knowledge.

Make sure you use ht [code=cpp][/code] code tags next time you post code. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/