summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/beingmanager.cpp27
-rw-r--r--src/beingmanager.h9
-rw-r--r--src/game.cpp13
-rw-r--r--src/keyboardconfig.cpp1
-rw-r--r--src/keyboardconfig.h1
5 files changed, 51 insertions, 0 deletions
diff --git a/src/beingmanager.cpp b/src/beingmanager.cpp
index f35a305d..daceca5e 100644
--- a/src/beingmanager.cpp
+++ b/src/beingmanager.cpp
@@ -188,3 +188,30 @@ Being* BeingManager::findNearestLivingBeing(Uint16 x, Uint16 y, int maxdist,
return (maxdist >= dist) ? closestBeing : NULL;
}
+
+Being* BeingManager::findNearestLivingBeing(Being *aroundBeing, int maxdist,
+ Being::Type type)
+{
+ Being *closestBeing = NULL;
+ int dist = 0;
+ int x = aroundBeing->mX;
+ int y = aroundBeing->mY;
+
+ 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 != aroundBeing
+ )
+ {
+ dist = d;
+ closestBeing = being;
+ }
+ }
+
+ return (maxdist >= dist) ? closestBeing : NULL;
+}
diff --git a/src/beingmanager.h b/src/beingmanager.h
index 152fb640..a9d0db35 100644
--- a/src/beingmanager.h
+++ b/src/beingmanager.h
@@ -78,6 +78,15 @@ class BeingManager
Being::Type type = Being::UNKNOWN);
/**
+ * Return a being nearest to another being.
+ *
+ * \param maxdist maximal distance. If minimal distance is larger,
+ * no being is returned
+ */
+ Being* findNearestLivingBeing(Being *aroundBeing, 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 b3fa6f07..52d70bc0 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -770,6 +770,19 @@ void Game::handleInput()
player_node->attack(target, newTarget);
}
+ // Target the nearest player if 'q' is pressed
+ if ( keyboard.isKeyActive(keyboard.KEY_TARGET_PLAYER) )
+ //if (keys[SDLK_q])
+ {
+ Being *target =
+ beingManager->findNearestLivingBeing(player_node, 20, Being::PLAYER);
+
+ if (target)
+ {
+ player_node->setTarget(target);
+ }
+ }
+
// Target the nearest monster if 'a' pressed
if ( keyboard.isKeyActive(keyboard.KEY_TARGET_CLOSEST) )
//if (keys[SDLK_a])
diff --git a/src/keyboardconfig.cpp b/src/keyboardconfig.cpp
index 271961c8..a959e244 100644
--- a/src/keyboardconfig.cpp
+++ b/src/keyboardconfig.cpp
@@ -45,6 +45,7 @@ static KeyData const keyData[KeyboardConfig::KEY_TOTAL] = {
{"keyAttack", SDLK_LCTRL, "Attack"},
{"keyTarget", SDLK_LSHIFT, "Target"},
{"keyTargetClosest", SDLK_a, "Target Closest"},
+ {"keyTargetPlayer", SDLK_q, "Target Player"},
{"keyPickup", SDLK_z, "Pickup"},
{"keyHideWindows", SDLK_h, "Hide Windows"},
{"keyBeingSit", SDLK_s, "Sit"},
diff --git a/src/keyboardconfig.h b/src/keyboardconfig.h
index 46394fa5..53a5c96d 100644
--- a/src/keyboardconfig.h
+++ b/src/keyboardconfig.h
@@ -153,6 +153,7 @@ class KeyboardConfig
KEY_ATTACK,
KEY_TARGET,
KEY_TARGET_CLOSEST,
+ KEY_TARGET_PLAYER,
KEY_PICKUP,
KEY_HIDE_WINDOWS,
KEY_SIT,