summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/beingmanager.cpp21
-rw-r--r--src/beingmanager.h1
-rw-r--r--src/gui/viewport.cpp21
-rw-r--r--src/localplayer.cpp33
-rw-r--r--src/localplayer.h11
5 files changed, 84 insertions, 3 deletions
diff --git a/src/beingmanager.cpp b/src/beingmanager.cpp
index daceca5e..4e3ab0fa 100644
--- a/src/beingmanager.cpp
+++ b/src/beingmanager.cpp
@@ -124,6 +124,27 @@ Being* BeingManager::findBeing(Uint16 x, Uint16 y, Being::Type type)
return (i == mBeings.end()) ? NULL : *i;
}
+Being* BeingManager::findBeingByPixel(Uint16 x, Uint16 y)
+{
+ BeingIterator itr = mBeings.begin();
+ BeingIterator itr_end = mBeings.end();
+
+ for (; itr != itr_end; ++itr)
+ {
+ Being *being = (*itr);
+ if ((being->mAction != Being::DEAD) &&
+ (being->getPixelX() <= x) &&
+ (being->getPixelX() + being->getWidth() >= x) &&
+ (being->getPixelY() <= y) &&
+ (being->getPixelY() + being->getHeight() >= y))
+ {
+ return being;
+ }
+ }
+
+ return NULL;
+}
+
Beings& BeingManager::getAll()
{
return mBeings;
diff --git a/src/beingmanager.h b/src/beingmanager.h
index a9d0db35..d001c377 100644
--- a/src/beingmanager.h
+++ b/src/beingmanager.h
@@ -67,6 +67,7 @@ class BeingManager
* Return a being at specific coordinates.
*/
Being* findBeing(Uint16 x, Uint16 y, Being::Type type = Being::UNKNOWN);
+ Being* findBeingByPixel(Uint16 x, Uint16 y);
/**
* Return a being nearest to specific coordinates.
diff --git a/src/gui/viewport.cpp b/src/gui/viewport.cpp
index ecd70d69..d53204b6 100644
--- a/src/gui/viewport.cpp
+++ b/src/gui/viewport.cpp
@@ -398,7 +398,10 @@ Viewport::mousePressed(gcn::MouseEvent &event)
FloorItem *item;
// Interact with some being
- if ((being = beingManager->findBeing(tilex, tiley)))
+// if ((being = beingManager->findBeing(tilex, tiley))
+ int x = event.getX() + mPixelViewX;
+ int y = event.getY() + mPixelViewY;
+ if ((being = beingManager->findBeingByPixel(x, y)))
{
switch (being->getType())
{
@@ -410,8 +413,22 @@ Viewport::mousePressed(gcn::MouseEvent &event)
case Being::PLAYER:
if (being->mAction == Being::DEAD)
break;
+ if (being == player_node)
+ break;
- player_node->attack(being, true);
+ if (player_node->withinAttackRange(being))
+ {
+ player_node->attack(being, true);
+ }
+ else
+ {
+ Uint8 *keys = SDL_GetKeyState(NULL);
+ if (!(keys[SDLK_LSHIFT] || keys[SDLK_RSHIFT]))
+ {
+ player_node->stopAttack();
+ player_node->setGotoTarget(being);
+ }
+ }
break;
default:
diff --git a/src/localplayer.cpp b/src/localplayer.cpp
index 0ed23892..2aae199e 100644
--- a/src/localplayer.cpp
+++ b/src/localplayer.cpp
@@ -48,7 +48,8 @@ LocalPlayer::LocalPlayer(Uint32 id, Uint16 job, Map *map):
mInventory(new Inventory),
mXp(0), mNetwork(0),
mTarget(NULL), mPickUpTarget(NULL),
- mTrading(false), mLastAction(-1),
+ mTrading(false), mGoingToTarget(false),
+ mLastAction(-1),
mWalkingDir(0), mDestX(0), mDestY(0)
{
}
@@ -104,6 +105,16 @@ void LocalPlayer::nextStep()
{
walk(mWalkingDir);
}
+
+ }
+
+ if (mGoingToTarget && mTarget && withinAttackRange(mTarget))
+ {
+ mAction = Being::STAND;
+ attack(mTarget, true);
+ mGoingToTarget = false;
+ mPath.clear();
+ return;
}
Player::nextStep();
@@ -453,3 +464,23 @@ void LocalPlayer::setXp(int xp)
}
mXp = xp;
}
+
+bool LocalPlayer::withinAttackRange(Being *target)
+{
+ int dist_x = abs(target->mX - mX);
+ int dist_y = abs(target->mY - mY);
+
+ if (dist_x > getAttackRange() || dist_y > getAttackRange())
+ {
+ return false;
+ }
+
+ return true;
+}
+
+void LocalPlayer::setGotoTarget(Being *target)
+{
+ mTarget = target;
+ mGoingToTarget = true;
+ setDestination(target->mX, target->mY);
+}
diff --git a/src/localplayer.h b/src/localplayer.h
index b19a0c3f..bdf43fff 100644
--- a/src/localplayer.h
+++ b/src/localplayer.h
@@ -160,6 +160,16 @@ class LocalPlayer : public Player
*/
void setWalkingDir(int dir);
+ /**
+ * Sets going to being to attack
+ */
+ void setGotoTarget(Being *target);
+
+ /**
+ * Returns whether the target is in range to attack
+ */
+ bool withinAttackRange(Being *target);
+
void raiseAttribute(Attribute attr);
void raiseSkill(Uint16 skillId);
@@ -215,6 +225,7 @@ class LocalPlayer : public Player
FloorItem *mPickUpTarget;
bool mTrading;
+ bool mGoingToTarget;
int mLastAction; /**< Time stamp of the last action, -1 if none. */
int mWalkingDir; /**< The direction the player is walking in. */
int mDestX; /**< X coordinate of destination. */