summaryrefslogtreecommitdiff
path: root/Camera.cpp
diff options
context:
space:
mode:
authorXaviDCR92 <xavi.dcr@gmail.com>2018-07-26 21:15:59 +0200
committerXaviDCR92 <xavi.dcr@gmail.com>2018-07-26 21:15:59 +0200
commitbfdc0b9f497ef10f6687abcc55d93405c611af11 (patch)
tree43914b2eada0bf5fb09093d61a250d4930b57a5d /Camera.cpp
parent853c6cddaa2713a9eb0c1f1c55e3f61592f04a46 (diff)
* Menu.cpp: actions for CHOICE_SINGLE_PLAYER_GAME have been moved to a new function called MainMenuSinglePlayer().
+ BaseUnit.cpp, BaseUnit.h: new _selected flag. + Camera.cpp, Camera.h: linear movement has been taken over quadratic movement. Also, cursor now moves if dealing with screen borders. + Cursor.cpp, Cursor.h: new Cursor class holds cursor X/Y information. It is meant to be contained inside a HumanPlayer object. * Game.cpp: minor changes in casts and comments. + HumanPlayer.cpp: added callbacks for button release events. * HumanPlayer.cpp, HumanPlayerBtn.cpp: button handling has been transferred from HumanPlayer.cpp to HumanPlayerBtn.cpp in order to improve modularity. + Sprite.cpp: sprite data was not being checked against NULL. - Unit.cpp: drawHandler() is now executed by Player object.
Diffstat (limited to 'Camera.cpp')
-rw-r--r--Camera.cpp165
1 files changed, 87 insertions, 78 deletions
diff --git a/Camera.cpp b/Camera.cpp
index 6ba6f29..1f75875 100644
--- a/Camera.cpp
+++ b/Camera.cpp
@@ -3,16 +3,20 @@
* ******************************************************************/
#include "Camera.h"
+#include "Cursor.h"
#include <stdint.h>
-#include <limits.h>
+#include <stdbool.h>
/* *******************************************************************
* Defines
* ******************************************************************/
-#define MAX_CAMERA_SPEED ((int8_t)3)
-#define SPEED_CALCULATION_TIME ((uint8_t)3)
-#define CAMERA_ACCELERATION ((int8_t)1)
+/*****************************************************************//**
+ *
+ * \brief This macro defines camera speed, in pixels per frame.
+ *
+ *********************************************************************/
+#define CAMERA_SPEED (static_cast<int8_t>(4))
/* *******************************************************************
* Types definition
@@ -43,54 +47,10 @@ Camera::Camera(void) :
_bLocked(false),
_xOffset(0),
_yOffset(0),
- _xSpeed(0),
- _ySpeed(0),
_speedTimer(0)
{
}
-void Camera::handler(void)
-{
- if (((_xOffset + _xSpeed) >= -16)
- ||
- ((_xOffset + _xSpeed) <= 0) )
- {
- _xOffset += _xSpeed;
- //~_yOffset += _ySpeed;
- }
-
- if (++_speedTimer >= SPEED_CALCULATION_TIME)
- {
- _speedTimer = 0;
-
- if (_ySpeed < 0)
- {
- _ySpeed += CAMERA_ACCELERATION;
- }
- else if (_ySpeed > 0)
- {
- _ySpeed -= CAMERA_ACCELERATION;
- }
- else
- {
- /* Y speed is already still. Continue. */
- }
-
- if (_xSpeed < 0)
- {
- _xSpeed += CAMERA_ACCELERATION;
- }
- else if (_xSpeed > 0)
- {
- _xSpeed -= CAMERA_ACCELERATION;
- }
- else
- {
- /* x speed is alreadx still. Continue. */
- }
- }
-}
-
/*****************************************************************//**
*
* \brief This function transforms X coordinates for a given
@@ -99,7 +59,7 @@ void Camera::handler(void)
*********************************************************************/
uint8_t Camera::getX(const uint8_t x) const
{
- return x + _xOffset;
+ return x - _xOffset;
}
/*****************************************************************//**
@@ -110,7 +70,7 @@ uint8_t Camera::getX(const uint8_t x) const
*********************************************************************/
uint8_t Camera::getY(const uint8_t y) const
{
- return y + _yOffset;
+ return y - _yOffset;
}
/*****************************************************************//**
@@ -119,15 +79,23 @@ uint8_t Camera::getY(const uint8_t y) const
* left arrow button.
*
*********************************************************************/
-void Camera::onLeftBtnPressed(void)
+void Camera::onLeftBtnPressed(Cursor& cursor)
{
- if (_xSpeed < 0)
+ if (not cursor.isXCentered())
{
- _xSpeed += CAMERA_ACCELERATION << 1;
+ /* Move cursor to initial position. */
+ cursor.move(-CAMERA_SPEED);
}
- else if (_xSpeed < MAX_CAMERA_SPEED)
+ else if (_xOffset >= CAMERA_SPEED)
{
- _xSpeed += CAMERA_ACCELERATION;
+ /* Move camera to the left. */
+ _xOffset -= CAMERA_SPEED;
+ }
+ else
+ {
+ /* Left screen margin reached.
+ * Cursor has to be moved. */
+ cursor.move(-CAMERA_SPEED);
}
}
@@ -137,26 +105,23 @@ void Camera::onLeftBtnPressed(void)
* right arrow button.
*
*********************************************************************/
-void Camera::onRightBtnPressed(void)
+void Camera::onRightBtnPressed(Cursor& cursor)
{
- int8_t acc = 0;
-
- if (_xSpeed > 0)
+ if (not cursor.isXCentered())
{
- acc = CAMERA_ACCELERATION << 1;
+ /* Move cursor to initial position. */
+ cursor.move(CAMERA_SPEED);
}
- else if (_xSpeed > -MAX_CAMERA_SPEED)
+ else if (_xOffset < 512)
{
- acc = CAMERA_ACCELERATION;
- }
-
- if ((_xOffset + (_xSpeed - acc)) >= -16)
- {
- _xSpeed -= acc;
+ /* Move camera to the right. */
+ _xOffset += CAMERA_SPEED;
}
else
{
- _xSpeed = 0;
+ /* Right screen margin reached.
+ * Cursor has to be moved. */
+ cursor.move(CAMERA_SPEED);
}
}
@@ -166,15 +131,23 @@ void Camera::onRightBtnPressed(void)
* up arrow button.
*
*********************************************************************/
-void Camera::onUpBtnPressed(void)
+void Camera::onUpBtnPressed(Cursor& cursor)
{
- if (_ySpeed < 0)
+ if (not cursor.isYCentered())
+ {
+ /* Move cursor to initial position. */
+ cursor.move(0, -CAMERA_SPEED);
+ }
+ else if (_yOffset > 0)
{
- _ySpeed += CAMERA_ACCELERATION << 1;
+ /* Move camera to the right. */
+ _yOffset -= CAMERA_SPEED;
}
- else if (_ySpeed < MAX_CAMERA_SPEED)
+ else
{
- _ySpeed += CAMERA_ACCELERATION;
+ /* Upper screen margin reached.
+ * Cursor has to be moved. */
+ cursor.move(0, -CAMERA_SPEED);
}
}
@@ -184,14 +157,50 @@ void Camera::onUpBtnPressed(void)
* down arrow button.
*
*********************************************************************/
-void Camera::onDownBtnPressed(void)
+void Camera::onDownBtnPressed(Cursor& cursor)
{
- if (_ySpeed > 0)
+ if (not cursor.isYCentered())
+ {
+ /* Move cursor to initial position. */
+ cursor.move(0, CAMERA_SPEED);
+ }
+ else if (_yOffset < 512)
{
- _ySpeed -= CAMERA_ACCELERATION << 1;
+ /* Move camera to the right. */
+ _yOffset += CAMERA_SPEED;
}
- else if (_ySpeed < MAX_CAMERA_SPEED)
+ else
{
- _ySpeed -= CAMERA_ACCELERATION;
+ /* Lower screen margin reached.
+ * Cursor has to be moved. */
+ cursor.move(0, CAMERA_SPEED);
}
}
+
+/*****************************************************************//**
+ *
+ * \brief As its name suggests, this function adjusts camera lock.
+ *
+ * When camera is locked, it cannot be moved when pressing
+ * direction keys.
+ *
+ * \param bLock
+ * True if camera must be locked, false otherwise.
+ *
+ *********************************************************************/
+void Camera::adjustLock(const bool bLock)
+{
+ _bLocked = bLock;
+}
+
+/*****************************************************************//**
+ *
+ * \brief Returns camera lock state.
+ *
+ * \return Returns true if camera is locked, false otherwise.
+ *
+ *********************************************************************/
+bool Camera::isLocked(void) const
+{
+ return _bLocked;
+}