diff options
author | Yohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer> | 2011-04-11 14:38:02 +0200 |
---|---|---|
committer | Yohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer> | 2011-04-16 00:18:31 +0200 |
commit | b57af96f94c19bc6064ac158a6996f0a01c86d68 (patch) | |
tree | af4bff8033c54de67b645db1b8d213cd624af655 /src/being.cpp | |
parent | 0b1ea91fcc77961913af35b89751171327656fb1 (diff) | |
download | mana-b57af96f94c19bc6064ac158a6996f0a01c86d68.tar.gz mana-b57af96f94c19bc6064ac158a6996f0a01c86d68.tar.bz2 mana-b57af96f94c19bc6064ac158a6996f0a01c86d68.tar.xz mana-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/being.cpp')
-rw-r--r-- | src/being.cpp | 80 |
1 files changed, 80 insertions, 0 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) |