summaryrefslogtreecommitdiff
path: root/src/engine.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine.cpp')
-rw-r--r--src/engine.cpp81
1 files changed, 59 insertions, 22 deletions
diff --git a/src/engine.cpp b/src/engine.cpp
index eb36a9fc..9a93b03e 100644
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -27,6 +27,7 @@
#include "being.h"
#include "beingmanager.h"
+#include "configuration.h"
#include "flooritemmanager.h"
#include "game.h"
#include "graphics.h"
@@ -160,36 +161,72 @@ void Engine::logic()
void Engine::draw(Graphics *graphics)
{
+ // calculate viewpoint
int midTileX = graphics->getWidth() / 32 / 2;
int midTileY = graphics->getHeight() / 32 / 2;
- int map_x = (player_node->mX - midTileX) * 32 + player_node->getXOffset();
- int map_y = (player_node->mY - midTileY) * 32 + player_node->getYOffset();
+ int player_x = (player_node->mX - midTileX) * 32 + player_node->getXOffset();
+ int player_y = (player_node->mY - midTileY) * 32 + player_node->getYOffset();
+
+ scrollLaziness = (int)config.getValue("ScrollLaziness", 32);
+ scrollRadius = (int)config.getValue("ScrollRadius", 32);
+
+ if (scrollLaziness < 1)
+ scrollLaziness = 1; //avoids division by zero
+
+ if (player_x > view_x + scrollRadius)
+ {
+ view_x += (player_x - view_x - scrollRadius) / scrollLaziness;
+ };
+ if (player_x < view_x - scrollRadius)
+ {
+ view_x += (player_x - view_x + scrollRadius) / scrollLaziness;
+ };
+ if (player_y > view_y + scrollRadius)
+ {
+ view_y += (player_y - view_y - scrollRadius) / scrollLaziness;
+ };
+ if (player_y < view_y - scrollRadius)
+ {
+ view_y += (player_y - view_y + scrollRadius) / scrollLaziness;
+ };
+
+ //auto center when player is off screen
+ if ( player_x - view_x > graphics->getWidth() / 2
+ || view_x - player_x > graphics->getWidth() / 2
+ || view_y - player_y > graphics->getHeight() / 2
+ || player_y - view_y > graphics->getHeight() / 2
+ )
+ {
+ view_x = player_x;
+ view_y = player_y;
+ };
if (mCurrentMap) {
- if (map_x < 0) {
- map_x = 0;
+ if (view_x < 0) {
+ view_x = 0;
}
- if (map_y < 0) {
- map_y = 0;
+ if (view_y < 0) {
+ view_y = 0;
}
- if (map_x > (mCurrentMap->getWidth() - midTileX) * 32) {
- map_x = (mCurrentMap->getWidth() - midTileX) * 32;
+ if (view_x > (mCurrentMap->getWidth() - midTileX) * 32) {
+ view_x = (mCurrentMap->getWidth() - midTileX) * 32;
}
- if (map_y > (mCurrentMap->getHeight() - midTileY) * 32) {
- map_y = (mCurrentMap->getHeight() - midTileY) * 32;
+ if (view_y > (mCurrentMap->getHeight() - midTileY) * 32) {
+ view_y = (mCurrentMap->getHeight() - midTileY) * 32;
}
}
- camera_x = map_x / 32;
- camera_y = map_y / 32;
+ camera_x = int(view_x + 16) / 32;
+ camera_y = int(view_y + 16) / 32;
// Draw tiles and sprites
if (mCurrentMap != NULL)
{
- mCurrentMap->draw(graphics, map_x, map_y, 0);
- mCurrentMap->draw(graphics, map_x, map_y, 1);
- mCurrentMap->draw(graphics, map_x, map_y, 2);
+ mCurrentMap->draw(graphics, (int)view_x, (int)view_y, 0);
+ mCurrentMap->draw(graphics, (int)view_x, (int)view_y, 1);
+ mCurrentMap->draw(graphics, (int)view_x, (int)view_y, 2);
+ mCurrentMap->drawOverlay(graphics, view_x, view_y);
}
// Find a path from the player to the mouse, and draw it. This is for debug
@@ -210,8 +247,8 @@ void Engine::draw(Graphics *graphics)
graphics->setColor(gcn::Color(255, 0, 0));
for (PathIterator i = debugPath.begin(); i != debugPath.end(); i++)
{
- int squareX = i->x * 32 - map_x + 12;
- int squareY = i->y * 32 - map_y + 12;
+ int squareX = i->x * 32 - int(view_x) + 12;
+ int squareY = i->y * 32 - int(view_y) + 12;
graphics->fillRectangle(gcn::Rectangle(squareX, squareY, 8, 8));
graphics->drawText(
@@ -224,9 +261,9 @@ void Engine::draw(Graphics *graphics)
Beings *beings = beingManager->getAll();
for (BeingIterator i = beings->begin(); i != beings->end(); i++)
{
- (*i)->drawSpeech(graphics, -map_x, -map_y);
- (*i)->drawName(graphics, -map_x, -map_y);
- (*i)->drawEmotion(graphics, -map_x, -map_y);
+ (*i)->drawSpeech(graphics, -(int)view_x, -(int)view_y);
+ (*i)->drawName(graphics, -(int)view_x, -(int)view_y);
+ (*i)->drawEmotion(graphics, -(int)view_x, -(int)view_y);
}
// Draw target marker if needed
@@ -237,8 +274,8 @@ void Engine::draw(Graphics *graphics)
graphics->setColor(gcn::Color(255, 255, 255));
int dy = (target->getType() == Being::PLAYER) ? 90 : 52;
- graphics->drawText("[TARGET]", target->getPixelX() - map_x + 15,
- target->getPixelY() - map_y - dy, gcn::Graphics::CENTER);
+ graphics->drawText("[TARGET]", target->getPixelX() - (int)view_x + 15,
+ target->getPixelY() - (int)view_y - dy, gcn::Graphics::CENTER);
}
gui->draw();