/src/NPCManager.cpp

http://github.com/clintbellanger/flare · C++ · 150 lines · 86 code · 34 blank · 30 comment · 13 complexity · d22b9cb9e6df96caa743555d3c936274 MD5 · raw file

  1. /*
  2. Copyright 2011 Clint Bellanger
  3. This file is part of FLARE.
  4. FLARE is free software: you can redistribute it and/or modify it under the terms
  5. of the GNU General Public License as published by the Free Software Foundation,
  6. either version 3 of the License, or (at your option) any later version.
  7. FLARE is distributed in the hope that it will be useful, but WITHOUT ANY
  8. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  9. PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License along with
  11. FLARE. If not, see http://www.gnu.org/licenses/
  12. */
  13. /**
  14. * class NPCManager
  15. *
  16. * NPCs which are not combatative enemies are handled by this Manager.
  17. * Most commonly this involves vendor and conversation townspeople.
  18. */
  19. #include "NPCManager.h"
  20. using namespace std;
  21. NPCManager::NPCManager(MapIso *_map, LootManager *_loot, ItemManager *_items) {
  22. map = _map;
  23. loot = _loot;
  24. items = _items;
  25. tip = new WidgetTooltip();
  26. npc_count = 0;
  27. for (int i=0; i<MAX_NPC_COUNT; i++) {
  28. npcs[i] = NULL;
  29. }
  30. tooltip_margin = 64;
  31. }
  32. void NPCManager::handleNewMap() {
  33. Map_NPC mn;
  34. ItemStack item_roll;
  35. // remove existing NPCs
  36. for (int i=0; i<npc_count; i++) {
  37. delete(npcs[i]);
  38. npcs[i] = NULL;
  39. }
  40. npc_count = 0;
  41. // read the queued NPCs in the map file
  42. while (!map->npcs.empty()) {
  43. mn = map->npcs.front();
  44. map->npcs.pop();
  45. npcs[npc_count] = new NPC(map, items);
  46. npcs[npc_count]->load(mn.id);
  47. npcs[npc_count]->pos.x = mn.pos.x;
  48. npcs[npc_count]->pos.y = mn.pos.y;
  49. // if this NPC needs randomized items
  50. while (npcs[npc_count]->random_stock > 0 && npcs[npc_count]->stock_count < NPC_VENDOR_MAX_STOCK) {
  51. item_roll.item = loot->randomItem(npcs[npc_count]->level);
  52. item_roll.quantity = rand() % items->items[item_roll.item].rand_vendor + 1;
  53. npcs[npc_count]->stock.add( item_roll);
  54. npcs[npc_count]->random_stock--;
  55. }
  56. npcs[npc_count]->stock.sort();
  57. npc_count++;
  58. }
  59. }
  60. void NPCManager::logic() {
  61. for (int i=0; i<npc_count; i++) {
  62. npcs[i]->logic();
  63. }
  64. }
  65. int NPCManager::checkNPCClick(Point mouse, Point cam) {
  66. Point p;
  67. SDL_Rect r;
  68. for(int i=0; i<npc_count; i++) {
  69. p = map_to_screen(npcs[i]->pos.x, npcs[i]->pos.y, cam.x, cam.y);
  70. r.w = npcs[i]->render_size.x;
  71. r.h = npcs[i]->render_size.y;
  72. r.x = p.x - npcs[i]->render_offset.x;
  73. r.y = p.y - npcs[i]->render_offset.y;
  74. if (isWithin(r, mouse)) {
  75. return i;
  76. }
  77. }
  78. return -1;
  79. }
  80. /**
  81. * On mouseover, display NPC's name
  82. */
  83. void NPCManager::renderTooltips(Point cam, Point mouse) {
  84. Point p;
  85. SDL_Rect r;
  86. for(int i=0; i<npc_count; i++) {
  87. p = map_to_screen(npcs[i]->pos.x, npcs[i]->pos.y, cam.x, cam.y);
  88. r.w = npcs[i]->render_size.x;
  89. r.h = npcs[i]->render_size.y;
  90. r.x = p.x - npcs[i]->render_offset.x;
  91. r.y = p.y - npcs[i]->render_offset.y;
  92. if (isWithin(r, mouse)) {
  93. // adjust dest.y so that the tooltip floats above the item
  94. p.y -= tooltip_margin;
  95. // use current tip or make a new one?
  96. if (tip_buf.lines[0] != npcs[i]->name) {
  97. tip->clear(tip_buf);
  98. tip_buf.num_lines = 1;
  99. tip_buf.lines[0] = npcs[i]->name;
  100. }
  101. tip->render(tip_buf, p, STYLE_TOPLABEL);
  102. break; // display only one NPC tooltip at a time
  103. }
  104. }
  105. }
  106. NPCManager::~NPCManager() {
  107. for (int i=0; i<npc_count; i++) {
  108. delete npcs[i];
  109. }
  110. tip->clear(tip_buf);
  111. delete tip;
  112. }