From b50649ee5e28511df72cc3a08023747727c5b6f0 Mon Sep 17 00:00:00 2001 From: David Athay Date: Fri, 11 Apr 2008 14:12:30 +0000 Subject: Players now need to click on the monster sprites rather than the tile. Players will now move to the target before attacking it. --- src/beingmanager.cpp | 21 +++++++++++++++++++++ src/beingmanager.h | 1 + src/gui/viewport.cpp | 21 +++++++++++++++++++-- src/localplayer.cpp | 33 ++++++++++++++++++++++++++++++++- src/localplayer.h | 11 +++++++++++ 5 files changed, 84 insertions(+), 3 deletions(-) (limited to 'src') 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. */ -- cgit v1.2.3-70-g09d2