Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

MouseControl.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  */
00009 #include "MouseControl.h"
00010 
00011 #include "Controls.h"
00012 #include "View.h"
00013 #include "FinderAlg.h"
00014 #include "Unit.h"
00015 #include "InputProvider.h"
00016 
00017 //-----------------------------------------------------------------
00018 MouseControl::MouseControl(Controls *controls, const View *view,
00019         FinderAlg *finder)
00020 {
00021     m_controls = controls;
00022     m_view = view;
00023     m_finder = finder;
00024 }
00025 //-----------------------------------------------------------------
00026 /**
00027  * Left button finds shortest path.
00028  * Right button moves to the cursor.
00029  * @return whether a move was made
00030  */
00031 bool
00032 MouseControl::mouseDrive(const InputProvider *input) const
00033 {
00034     bool moved = false;
00035     V2 field = m_view->getFieldPos(input->getMouseLoc());
00036     if (input->isLeftPressed()) {
00037         moved = moveTo(field);
00038     }
00039     else if (input->isRightPressed()) {
00040         moved = moveHardTo(field);
00041     }
00042     return moved;
00043 }
00044 //-----------------------------------------------------------------
00045 /**
00046  * Move along shortest path without pushing.
00047  * @param field destination field
00048  * @return whether a move was made
00049  */
00050 bool
00051 MouseControl::moveTo(const V2 &field) const
00052 {
00053     bool moved = false;
00054     const Unit *unit = m_controls->getActive();
00055     if (unit) {
00056         Dir::eDir dir = m_finder->findDir(unit, field);
00057         if (dir != Dir::DIR_NO) {
00058              moved = m_controls->makeMove(unit->myOrder(dir));
00059         }
00060     }
00061     return moved;
00062 }
00063 //-----------------------------------------------------------------
00064 /**
00065  * Move direct to the destination.
00066  * @param field destination field
00067  * @return whether a move was made
00068  */
00069 bool
00070 MouseControl::moveHardTo(const V2 &field) const
00071 {
00072     bool moved = false;
00073     const Unit *unit = m_controls->getActive();
00074     if (unit) {
00075         V2 loc = unit->getLoc();
00076         if (field.getX() < loc.getX()) {
00077             moved = m_controls->makeMove(unit->myOrder(Dir::DIR_LEFT));
00078         }
00079         else if (loc.getX() + unit->getW() <= field.getX()) {
00080             moved = m_controls->makeMove(unit->myOrder(Dir::DIR_RIGHT));
00081         }
00082         else if (field.getY() < loc.getY()) {
00083             moved = m_controls->makeMove(unit->myOrder(Dir::DIR_UP));
00084         }
00085         else if (loc.getY() + unit->getH() <= field.getY()) {
00086             moved = m_controls->makeMove(unit->myOrder(Dir::DIR_DOWN));
00087         }
00088     }
00089     return moved;
00090 }
00091 

Generated on Wed Jun 1 09:54:31 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2