From b57af96f94c19bc6064ac158a6996f0a01c86d68 Mon Sep 17 00:00:00 2001 From: Yohann Ferreira Date: Mon, 11 Apr 2011 14:38:02 +0200 Subject: Introduced the Being::lookAt() function. We're now using it when picking up items. Also, de-overnested the Game::handleInput() function when handling pickups. --- src/being.cpp | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) (limited to 'src/being.cpp') 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) -- cgit v1.2.3-60-g2f50