| 1 | /* Copyright 2016-2017 Joaquin M Lopez Munoz. |
|---|---|
| 2 | * Distributed under the Boost Software License, Version 1.0. |
| 3 | * (See accompanying file LICENSE_1_0.txt or copy at |
| 4 | * http://www.boost.org/LICENSE_1_0.txt) |
| 5 | * |
| 6 | * See http://www.boost.org/libs/poly_collection for library home page. |
| 7 | */ |
| 8 | |
| 9 | /* basic usage of boost::base_collection */ |
| 10 | |
| 11 | #include <algorithm> |
| 12 | #include <boost/poly_collection/base_collection.hpp> |
| 13 | #include <random> |
| 14 | #include "rolegame.hpp" |
| 15 | |
| 16 | int main() |
| 17 | { |
| 18 | //[basic_base_1 |
| 19 | //= #include <boost/poly_collection/base_collection.hpp> |
| 20 | //= ... |
| 21 | //= |
| 22 | boost::base_collection<sprite> c; |
| 23 | |
| 24 | std::mt19937 gen{92748}; // some arbitrary random seed |
| 25 | std::discrete_distribution<> rnd{{1,1,1}}; |
| 26 | for(int i=0;i<8;++i){ // assign each type with 1/3 probability |
| 27 | switch(rnd(gen)){ |
| 28 | case 0: c.insert(x: warrior{i});break; |
| 29 | case 1: c.insert(x: juggernaut{i});break; |
| 30 | case 2: c.insert(x: goblin{i});break; |
| 31 | } |
| 32 | } |
| 33 | //] |
| 34 | |
| 35 | auto render=[&](){ |
| 36 | //[basic_base_2 |
| 37 | const char* comma=""; |
| 38 | for(const sprite& s:c){ |
| 39 | std::cout<<comma; |
| 40 | s.render(os&: std::cout); |
| 41 | comma=","; |
| 42 | } |
| 43 | std::cout<<"\n"; |
| 44 | //] |
| 45 | }; |
| 46 | render(); |
| 47 | |
| 48 | //[basic_base_3 |
| 49 | c.insert(x: goblin{8}); |
| 50 | //] |
| 51 | render(); |
| 52 | |
| 53 | //[basic_base_4 |
| 54 | // find element with id==7 and remove it |
| 55 | auto it=std::find_if(first: c.begin(),last: c.end(),pred: [](const sprite& s){return s.id==7;}); |
| 56 | c.erase(pos: it); |
| 57 | //] |
| 58 | render(); |
| 59 | } |
| 60 |
