/* * 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 <iostream> #include <vector> #include <algorithm> #include "Entity.h" #include "Npc.h" #include "Player.h" #include "Object.h" int main() { Player* player = new Player(); std::vector<Entity*> objs; std::vector<Entity*> npcs; std::vector<Entity*> 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 <iostream> 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"; } // Overloading < operator for std::sort in main: draw bool operator<(const Entity& obj) const { return worldY < obj.worldY; } }; ----------------------------------------------- Npc.h: #pragma once #include "Entity.h" class NPC : public Entity { public: NPC(); void update() override; void draw() override; }; ----------------------------------------------- Npc.cpp: #include "Npc.h" #include <iostream> NPC::NPC() { name = "npc"; } void NPC::update() { std::cout << name << std::endl; } void NPC::draw() { std::cout << "npc y: " << worldY << std::endl; } ----------------------------------------- Player.h: #pragma once #include "Entity.h" class Player : public Entity { public: Player(); void update() override; void draw() override; }; ------------------------------------------- Player.cpp: #include "Player.h" #include <iostream> Player::Player() { name = "player"; } void Player::update() { std::cout << name << std::endl; } void Player::draw() { std::cout << "player y: " << worldY << std::endl; } ----------------------------------------------- Object.h: #pragma once #include "Entity.h" class OBJ_o : public Entity { public: OBJ_o(); void update() override; void draw() override; }; ------------------------------------------------------------------- Object.cpp: #include "Object.h" #include <iostream> OBJ_o::OBJ_o() { name = "object"; } void OBJ_o::update() { std::cout << name << std::endl; } void OBJ_o::draw() { std::cout << "object y: " << worldY << std::endl; }
// sort entities std::sort(entities.begin(), entities.end(), [](Entity *a, Entity *b)->bool { return a->worldY < b->worldY; });
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)