PageRenderTime 49ms CodeModel.GetById 13ms app.highlight 30ms RepoModel.GetById 2ms app.codeStats 0ms

/src/NPCManager.cpp

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