initial commit, lordsawar source, slightly modified
[lordsawar] / src / heroesmap.cpp
1 //  Copyright (C) 2007, 2008, 2009 Ben Asselstine
2 //
3 //  This program is free software; you can redistribute it and/or modify
4 //  it under the terms of the GNU General Public License as published by
5 //  the Free Software Foundation; either version 3 of the License, or
6 //  (at your option) any later version.
7 //
8 //  This program is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //  GNU Library General Public License for more details.
12 //
13 //  You should have received a copy of the GNU General Public License
14 //  along with this program; if not, write to the Free Software
15 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
16 //  02110-1301, USA.
17
18 #include "heroesmap.h"
19
20 #include "city.h"
21 #include "citylist.h"
22 #include "playerlist.h"
23 #include "GraphicsCache.h"
24 #include "player.h"
25 #include "stacklist.h"
26 #include "hero.h"
27 #include "gui/image-helpers.h"
28
29 HeroesMap::HeroesMap(std::list<Hero*> h)
30 {
31   heroes = h;
32   active_hero = *(heroes.begin());
33 }
34
35 void HeroesMap::draw_hero(Hero *hero, bool active)
36 {
37   Player *player = Playerlist::getActiveplayer();
38   Vector<int> start = player->getStacklist()->getPosition(hero->getId());
39
40   start = mapToSurface(start);
41
42   start += Vector<int>(int(pixels_per_tile/2), int(pixels_per_tile/2));
43
44   PixMask *heropic = 
45     GraphicsCache::getInstance()->getSmallHeroPic(active);
46  
47   heropic->blit_centered(surface, start);
48 }
49
50 void HeroesMap::after_draw()
51 {
52   OverviewMap::after_draw();
53   draw_cities(false);
54   for (std::list<Hero*>::iterator it = heroes.begin(); it != heroes.end();
55        it++)
56     {
57       if (*it == active_hero)
58         draw_hero(*it, true);
59       else
60         draw_hero(*it, false);
61     }
62
63   map_changed.emit(surface);
64 }
65
66 void HeroesMap::mouse_button_event(MouseButtonEvent e)
67 {
68   Player *active = Playerlist::getActiveplayer();
69   if (e.button == MouseButtonEvent::LEFT_BUTTON && 
70       e.state == MouseButtonEvent::PRESSED)
71     {
72       Vector<int> dest;
73       dest = mapFromScreen(e.pos);
74
75       //is dest close to one of our heroes?
76       Hero *hero = active->getStacklist()->getNearestHero(dest, 4);
77       if (hero)
78         {
79           active_hero = hero;
80           hero_selected.emit(hero);
81         }
82     }
83
84 }