rolegame.hpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #ifndef BOOST_POLY_COLLECTION_EXAMPLE_ROLEGAME_HPP
  9. #define BOOST_POLY_COLLECTION_EXAMPLE_ROLEGAME_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. /* entities of a purported role game used in the examples */
  14. #include <iostream>
  15. #include <string>
  16. #include <utility>
  17. //[rolegame_1
  18. struct sprite
  19. {
  20. sprite(int id):id{id}{}
  21. virtual ~sprite()=default;
  22. virtual void render(std::ostream& os)const=0;
  23. int id;
  24. };
  25. //]
  26. //[rolegame_2
  27. struct warrior:sprite
  28. {
  29. using sprite::sprite;
  30. warrior(std::string rank,int id):sprite{id},rank{std::move(rank)}{}
  31. void render(std::ostream& os)const override{os<<rank<<" "<<id;}
  32. std::string rank="warrior";
  33. };
  34. struct juggernaut:warrior
  35. {
  36. juggernaut(int id):warrior{"juggernaut",id}{}
  37. };
  38. struct goblin:sprite
  39. {
  40. using sprite::sprite;
  41. void render(std::ostream& os)const override{os<<"goblin "<<id;}
  42. };
  43. //]
  44. //[rolegame_3
  45. struct window
  46. {
  47. window(std::string caption):caption{std::move(caption)}{}
  48. void display(std::ostream& os)const{os<<"["<<caption<<"]";}
  49. std::string caption;
  50. };
  51. //]
  52. //[rolegame_4
  53. struct elf:sprite
  54. {
  55. using sprite::sprite;
  56. elf(const elf&)=delete; // not copyable
  57. elf(elf&&)=default; // but moveable
  58. void render(std::ostream& os)const override{os<<"elf "<<id;}
  59. };
  60. //]
  61. #endif