| 1 |
/* |
| 2 |
* Copyright (C) 2004 Ivo Danihelka ([email protected]) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
#include "MouseControl.h" |
| 10 |
|
| 11 |
#include "Controls.h" |
| 12 |
#include "View.h" |
| 13 |
#include "FinderAlg.h" |
| 14 |
#include "Unit.h" |
| 15 |
#include "InputProvider.h" |
| 16 |
|
| 17 |
//----------------------------------------------------------------- |
| 18 |
MouseControl::MouseControl(Controls *controls, const View *view, |
| 19 |
FinderAlg *finder) |
| 20 |
{ |
| 21 |
m_controls = controls; |
| 22 |
m_view = view; |
| 23 |
m_finder = finder; |
| 24 |
} |
| 25 |
//----------------------------------------------------------------- |
| 26 |
/** |
| 27 |
* Left button finds shortest path. |
| 28 |
* Right button moves to the cursor. |
| 29 |
* @return whether a move was made |
| 30 |
*/ |
| 31 |
bool |
| 32 |
MouseControl::mouseDrive(const InputProvider *input) const |
| 33 |
{ |
| 34 |
bool moved = false; |
| 35 |
V2 field = m_view->getFieldPos(input->getMouseLoc()); |
| 36 |
if (input->isLeftPressed()) { |
| 37 |
moved = moveTo(field); |
| 38 |
} |
| 39 |
else if (input->isRightPressed()) { |
| 40 |
moved = moveHardTo(field); |
| 41 |
} |
| 42 |
return moved; |
| 43 |
} |
| 44 |
//----------------------------------------------------------------- |
| 45 |
/** |
| 46 |
* Move along shortest path without pushing. |
| 47 |
* @param field destination field |
| 48 |
* @return whether a move was made |
| 49 |
*/ |
| 50 |
bool |
| 51 |
MouseControl::moveTo(const V2 &field) const |
| 52 |
{ |
| 53 |
bool moved = false; |
| 54 |
const Unit *unit = m_controls->getActive(); |
| 55 |
if (unit) { |
| 56 |
Dir::eDir dir = m_finder->findDir(unit, field); |
| 57 |
if (dir != Dir::DIR_NO) { |
| 58 |
moved = m_controls->makeMove(unit->myOrder(dir)); |
| 59 |
} |
| 60 |
} |
| 61 |
return moved; |
| 62 |
} |
| 63 |
//----------------------------------------------------------------- |
| 64 |
/** |
| 65 |
* Move direct to the destination. |
| 66 |
* @param field destination field |
| 67 |
* @return whether a move was made |
| 68 |
*/ |
| 69 |
bool |
| 70 |
MouseControl::moveHardTo(const V2 &field) const |
| 71 |
{ |
| 72 |
bool moved = false; |
| 73 |
const Unit *unit = m_controls->getActive(); |
| 74 |
if (unit) { |
| 75 |
V2 loc = unit->getLoc(); |
| 76 |
if (field.getX() < loc.getX()) { |
| 77 |
moved = m_controls->makeMove(unit->myOrder(Dir::DIR_LEFT)); |
| 78 |
} |
| 79 |
else if (loc.getX() + unit->getW() <= field.getX()) { |
| 80 |
moved = m_controls->makeMove(unit->myOrder(Dir::DIR_RIGHT)); |
| 81 |
} |
| 82 |
else if (field.getY() < loc.getY()) { |
| 83 |
moved = m_controls->makeMove(unit->myOrder(Dir::DIR_UP)); |
| 84 |
} |
| 85 |
else if (loc.getY() + unit->getH() <= field.getY()) { |
| 86 |
moved = m_controls->makeMove(unit->myOrder(Dir::DIR_DOWN)); |
| 87 |
} |
| 88 |
} |
| 89 |
return moved; |
| 90 |
} |
| 91 |
|