Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
U

User 11251399

@User 11251399
About
Posts
2
Topics
1
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • How to sort the elements of a std::vector of pointers to instances of derived classes
    U User 11251399

    OK

    The Lounge c++ tutorial graphics game-dev data-structures

  • How to sort the elements of a std::vector of pointers to instances of derived classes
    U User 11251399

    I'm programming a 2D RPG game that uses an array of derived class instance pointers. For clarity I provide a code example in C++. I would like to sort the elements of the entities array based on the worldY value of each element.

    main.cpp:

    /*
    * result:
    * player
    * object
    * object
    * npc
    * npc
    * 2 npc y: 30
    * 1 npc y: 6
    * 1 object y: 20
    * 2 object y: 10
    * player y: 0
    */

    #include #include #include #include "Entity.h"
    #include "Npc.h"
    #include "Player.h"
    #include "Object.h"

    int main()
    {
    Player* player = new Player();

    std::vector objs;
    std::vector npcs;
    std::vector entities;
    
    Entity\* tmp = new OBJ\_o();
    objs.push\_back(tmp);
    objs.at(0)->worldY = 20;
    objs.at(0)->ID = "1";
    
    tmp = new OBJ\_o();
    objs.push\_back(tmp);
    objs.at(1)->worldY = 10;
    objs.at(1)->ID = "2";
    
    tmp = new NPC();
    npcs.push\_back(tmp);
    npcs.at(0)->worldY = 6;
    npcs.at(0)->ID = "1";
    
    tmp = new NPC();
    npcs.push\_back(tmp);
    npcs.at(1)->worldY = 30;
    npcs.at(1)->ID = "2";
    
    //--------------- update -------------------
    player->update();
    
    for (auto iter\_objs = objs.begin(); iter\_objs != objs.end(); ++iter\_objs)
    {
        (\*iter\_objs)->update();
    }
    
    for (auto iter\_npcs = npcs.begin(); iter\_npcs != npcs.end(); ++iter\_npcs)
    {
        (\*iter\_npcs)->update();
    }
    
    //-------------- entities --------------------
    entities.push\_back(player);
    
    for (int i = 0; i < objs.size(); i++)
    {
        if (!objs.at(i)->name.empty())
        {
            entities.push\_back(objs.at(i));
        }
    }
    
    for (int i = 0; i < npcs.size(); i++)
    {
        if (!npcs.at(i)->name.empty())
        {
            entities.push\_back(npcs.at(i));
        }
    }
    
    // sort entities
    std::sort(entities.begin(), entities.end());
    
    //---------------- draw entities ------------------
    for (auto iter = entities.begin(); iter != entities.end(); ++iter)
    {
        std::cout << (\*iter)->ID << " ";
        (\*iter)->draw();
    }
    
    return 0;
    

    }

    Entity.h

    #pragma once
    #include

    class Entity
    {
    public:
    int worldY = 0;
    std::string name;
    std::string ID;

    public:
    virtual void update() { std::cout << "base" << std::endl; }
    virtual void draw() { std::cout << "base y: -\n"; }

    // Overload
    
    The Lounge c++ tutorial graphics game-dev data-structures
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups