summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2011-04-11 14:38:02 +0200
committerYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2011-04-16 00:18:31 +0200
commitb57af96f94c19bc6064ac158a6996f0a01c86d68 (patch)
treeaf4bff8033c54de67b645db1b8d213cd624af655 /src
parent0b1ea91fcc77961913af35b89751171327656fb1 (diff)
downloadmana-client-b57af96f94c19bc6064ac158a6996f0a01c86d68.tar.gz
mana-client-b57af96f94c19bc6064ac158a6996f0a01c86d68.tar.bz2
mana-client-b57af96f94c19bc6064ac158a6996f0a01c86d68.tar.xz
mana-client-b57af96f94c19bc6064ac158a6996f0a01c86d68.zip
Introduced the Being::lookAt() function.
We're now using it when picking up items. Also, de-overnested the Game::handleInput() function when handling pickups.
Diffstat (limited to 'src')
-rw-r--r--src/being.cpp80
-rw-r--r--src/being.h5
-rw-r--r--src/game.cpp71
3 files changed, 120 insertions, 36 deletions
diff --git a/src/being.cpp b/src/being.cpp
index 3f375e03..dcad9f9e 100644
--- a/src/being.cpp
+++ b/src/being.cpp
@@ -649,6 +649,86 @@ void Being::setAction(Action action, int attackType)
mActionTime = tick_time;
}
+void Being::lookAt(const Vector &destPos)
+{
+ // We first handle simple cases
+
+ // If the two positions are the same,
+ // don't update the direction since it's only a matter of keeping
+ // the previous one.
+ if (mPos.x == destPos.x && mPos.y == destPos.y)
+ return;
+
+ if (mPos.x == destPos.x)
+ {
+ if (mPos.y > destPos.y)
+ setDirection(UP);
+ else
+ setDirection(DOWN);
+ return;
+ }
+
+ if (mPos.y == destPos.y)
+ {
+ if (mPos.x > destPos.x)
+ setDirection(LEFT);
+ else
+ setDirection(RIGHT);
+ return;
+ }
+
+ // Now let's handle diagonal cases
+ // First, find the lower angle:
+ if (mPos.x < destPos.x)
+ {
+ // Up-right direction
+ if (mPos.y > destPos.y)
+ {
+ // Compute tan of the angle
+ if ((mPos.y - destPos.y) / (destPos.x - mPos.x) < 1)
+ // The angle is less than 45°, we look to the right
+ setDirection(RIGHT);
+ else
+ setDirection(UP);
+ return;
+ }
+ else // Down-right
+ {
+ // Compute tan of the angle
+ if ((destPos.y - mPos.y) / (destPos.x - mPos.x) < 1)
+ // The angle is less than 45°, we look to the right
+ setDirection(RIGHT);
+ else
+ setDirection(DOWN);
+ return;
+ }
+ }
+ else
+ {
+ // Up-left direction
+ if (mPos.y > destPos.y)
+ {
+ // Compute tan of the angle
+ if ((mPos.y - destPos.y) / (mPos.x - destPos.x) < 1)
+ // The angle is less than 45°, we look to the left
+ setDirection(LEFT);
+ else
+ setDirection(UP);
+ return;
+ }
+ else // Down-left
+ {
+ // Compute tan of the angle
+ if ((destPos.y - mPos.y) / (mPos.x - destPos.x) < 1)
+ // The angle is less than 45°, we look to the left
+ setDirection(LEFT);
+ else
+ setDirection(DOWN);
+ return;
+ }
+ }
+}
+
void Being::setDirection(Uint8 direction)
{
if (!direction || mDirection == direction)
diff --git a/src/being.h b/src/being.h
index f19e3df4..6831d5ad 100644
--- a/src/being.h
+++ b/src/being.h
@@ -450,6 +450,11 @@ class Being : public ActorSprite, public Mana::Listener
void setMap(Map *map);
+ /**
+ * Make the being look at a given pixel position.
+ */
+ void lookAt(const Vector &destPos);
+
protected:
/**
* Sets the new path for this being.
diff --git a/src/game.cpp b/src/game.cpp
index 206be6c5..c54f7abe 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -398,6 +398,40 @@ void Game::logic()
}
/**
+ * handle item pick up case.
+ */
+static void handleItemPickUp()
+{
+ int x = player_node->getTileX();
+ int y = player_node->getTileY();
+
+ // Let's look for items around until you find one.
+ bool found = false;
+ for (int xX = x - 1; xX < x + 2; ++xX)
+ {
+ for (int yY = y - 1; yY < y + 2; ++yY)
+ {
+ FloorItem *item = actorSpriteManager->findItem(xX, yY);
+ if (item)
+ {
+ found = true;
+ player_node->pickUp(item);
+
+ // We found it, so set the player
+ // direction accordingly,
+ player_node->lookAt(
+ player_node->getMap()->getTileCenter(xX, yY));
+
+ // Get out of the loops
+ break;
+ }
+ }
+ if (found)
+ break;
+ }
+}
+
+/**
* The huge input handling method.
*/
void Game::handleInput()
@@ -629,42 +663,7 @@ void Game::handleInput()
{
case KeyboardConfig::KEY_PICKUP:
{
- int x = player_node->getTileX();
- int y = player_node->getTileY();
- // Let's look for items around until you find one.
- bool found = false;
- for (int xX = x - 1; xX < x + 2; ++xX)
- {
- for (int yY = y - 1; yY < y + 2; ++yY)
- {
- FloorItem *item =
- actorSpriteManager->findItem(xX, yY);
- if (item)
- {
- found = true;
- player_node->pickUp(item);
- // We found it, so set the player
- // direction accordingly,
- Uint8 dir = 0;
- if (xX < x)
- dir |= Being::LEFT;
- else if (xX > x)
- dir |= Being::RIGHT;
- if (yY < y)
- dir |= Being::UP;
- else if (yY > y)
- dir |= Being::DOWN;
-
- if (dir)
- player_node->setDirection(dir);
-
- // Get out of the loops
- break;
- }
- }
- if (found)
- break;
- }
+ handleItemPickUp();
used = true;
}