summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2006-09-02 12:50:40 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2006-09-02 12:50:40 +0000
commit3213f41daeed034c8aa9b8be9e86796737a38a78 (patch)
tree5441dd101209774d6e7c6612281b3ef12bd941a5 /src
parentf5fc009543a0a189bcde5acf91ff315dd9cd05e9 (diff)
downloadmana-3213f41daeed034c8aa9b8be9e86796737a38a78.tar.gz
mana-3213f41daeed034c8aa9b8be9e86796737a38a78.tar.bz2
mana-3213f41daeed034c8aa9b8be9e86796737a38a78.tar.xz
mana-3213f41daeed034c8aa9b8be9e86796737a38a78.zip
Applied patch by Bernard Lidicky, adding targetting nearest monster with either
keyboard or mouse. Also made clicking beings in general a bit easier (patch applied by Bjørn Lindeijer).
Diffstat (limited to 'src')
-rw-r--r--src/beingmanager.cpp25
-rw-r--r--src/beingmanager.h17
-rw-r--r--src/game.cpp12
-rw-r--r--src/gui/gui.cpp16
-rw-r--r--src/localplayer.h7
5 files changed, 71 insertions, 6 deletions
diff --git a/src/beingmanager.cpp b/src/beingmanager.cpp
index 25edeb24..027e08d3 100644
--- a/src/beingmanager.cpp
+++ b/src/beingmanager.cpp
@@ -159,3 +159,28 @@ void BeingManager::clear()
mBeings.push_back(player_node);
}
}
+
+Being* BeingManager::findNearestLivingBeing(Uint16 x, Uint16 y, int maxdist,
+ Being::Type type)
+{
+ Being *closestBeing = NULL;
+ int dist = 0;
+
+ for (BeingIterator i = mBeings.begin(); i != mBeings.end(); i++)
+ {
+ Being *being = (*i);
+ int d = abs(being->mX - x) + abs(being->mY - y);
+
+ if ((being->getType() == type || type == Being::UNKNOWN)
+ && (d < dist || closestBeing == NULL) // it is closer
+ && being->mAction != Being::DEAD // no dead beings
+ && being->mAction != Being::MONSTER_DEAD
+ )
+ {
+ dist = d;
+ closestBeing = being;
+ }
+ }
+
+ return (maxdist >= dist) ? closestBeing : NULL;
+}
diff --git a/src/beingmanager.h b/src/beingmanager.h
index cfaf7fbf..152fb640 100644
--- a/src/beingmanager.h
+++ b/src/beingmanager.h
@@ -49,26 +49,35 @@ class BeingManager
void setPlayer(LocalPlayer *player);
/**
- * Create a being and add it to the list of beings
+ * Create a being and add it to the list of beings.
*/
Being* createBeing(Uint32 id, Uint16 job);
/**
- * Remove a Being
+ * Remove a Being.
*/
void destroyBeing(Being *being);
/**
- * Return a specific id Being
+ * Return a specific id Being.
*/
Being* findBeing(Uint32 id);
/**
- * Return a being at specific coordinates
+ * Return a being at specific coordinates.
*/
Being* findBeing(Uint16 x, Uint16 y, Being::Type type = Being::UNKNOWN);
/**
+ * Return a being nearest to specific coordinates.
+ *
+ * \param maxdist maximal distance. If minimal distance is larger,
+ * no being is returned
+ */
+ Being* findNearestLivingBeing(Uint16 x, Uint16 y, int maxdist,
+ Being::Type type = Being::UNKNOWN);
+
+ /**
* Returns the whole list of beings
*/
Beings& getAll();
diff --git a/src/game.cpp b/src/game.cpp
index ed8ad827..7921c388 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -686,6 +686,18 @@ void Game::handleInput()
player_node->attack(target, newTarget);
}
+ // Target the nearest monster if 'a' pressed
+ if (keys[SDLK_a])
+ {
+ Being *target =
+ beingManager->findNearestLivingBeing(x, y, 20, Being::MONSTER);
+
+ if (target)
+ {
+ player_node->setTarget(target);
+ }
+ }
+
if (joystick)
{
if (joystick->buttonPressed(1))
diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp
index a0548e9c..6c46d636 100644
--- a/src/gui/gui.cpp
+++ b/src/gui/gui.cpp
@@ -280,7 +280,7 @@ Gui::mousePress(int mx, int my, int button)
FloorItem *item;
// Interact with some being
- if ((being = beingManager->findBeing(tilex, tiley)))
+ if ((being = beingManager->findNearestLivingBeing(tilex, tiley, 1)))
{
switch (being->getType())
{
@@ -317,6 +317,20 @@ Gui::mousePress(int mx, int my, int button)
}
}
}
+
+ if (button == gcn::MouseInput::MIDDLE)
+ {
+ // Find the being nearest to the clicked position
+ Being *target = beingManager->findNearestLivingBeing(
+ player_node->mX,
+ player_node->mY,
+ 20, Being::MONSTER);
+
+ if (target)
+ {
+ player_node->setTarget(target);
+ }
+ }
}
void
diff --git a/src/localplayer.h b/src/localplayer.h
index 1f8c836f..a3fe91f7 100644
--- a/src/localplayer.h
+++ b/src/localplayer.h
@@ -99,12 +99,17 @@ class LocalPlayer : public Player
* Sets the trading state of the player, i.e. whether or not he is
* currently involved into some trade.
*/
- void setTrading(bool trading) { mTrading = trading; };
+ void setTrading(bool trading) { mTrading = trading; }
void attack(Being *target=NULL, bool keep=false);
void stopAttack();
Being* getTarget() const;
+ /**
+ * Sets the target being of the player.
+ */
+ void setTarget(Being* target) { mTarget = target; }
+
void walk(unsigned char dir);
/**