2018-01-21 12:12:49 +01:00
|
|
|
/*
|
|
|
|
Copyright (C) 2018 Parallel Realities
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "camera.h"
|
|
|
|
|
2018-02-02 09:00:27 +01:00
|
|
|
void clipCamera(void);
|
2018-01-21 12:12:49 +01:00
|
|
|
|
|
|
|
void cameraTrack(Entity *e)
|
|
|
|
{
|
|
|
|
camera.x = (int) e->x + e->w / 2;
|
|
|
|
camera.y = (int) e->y + e->h / 2;
|
|
|
|
|
|
|
|
camera.x -= (SCREEN_WIDTH / 2);
|
|
|
|
camera.y -= (SCREEN_HEIGHT / 2);
|
|
|
|
|
2018-02-02 09:00:27 +01:00
|
|
|
clipCamera();
|
2018-01-21 12:12:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
float cameraChase(Entity *e, int maxSpeed)
|
|
|
|
{
|
|
|
|
float x, y, tx, ty, diffX, diffY, dist;
|
|
|
|
|
|
|
|
x = camera.x;
|
|
|
|
y = camera.y;
|
|
|
|
|
|
|
|
tx = e->x - (SCREEN_WIDTH / 2);
|
|
|
|
ty = e->y - (SCREEN_HEIGHT / 2);
|
|
|
|
|
|
|
|
diffX = abs(tx - x);
|
|
|
|
diffY = abs(ty - y);
|
|
|
|
|
|
|
|
dist = MAX(diffX, diffY);
|
|
|
|
|
|
|
|
diffX /= 20;
|
|
|
|
diffY /= 20;
|
|
|
|
|
|
|
|
diffX = MAX(0, MIN(maxSpeed, diffX));
|
|
|
|
diffY = MAX(0, MIN(maxSpeed, diffY));
|
|
|
|
|
|
|
|
if (x > tx)
|
|
|
|
{
|
|
|
|
x -= diffX;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x < tx)
|
|
|
|
{
|
|
|
|
x += diffX;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y > ty)
|
|
|
|
{
|
|
|
|
y -= diffY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y < ty)
|
|
|
|
{
|
|
|
|
y += diffY;
|
|
|
|
}
|
|
|
|
|
|
|
|
camera.x = (int) x;
|
|
|
|
camera.y = (int) y;
|
|
|
|
|
2018-02-02 09:00:27 +01:00
|
|
|
clipCamera();
|
2018-01-21 12:12:49 +01:00
|
|
|
|
|
|
|
return dist;
|
|
|
|
}
|
|
|
|
|
2018-02-02 09:00:27 +01:00
|
|
|
void clipCamera(void)
|
2018-01-21 12:12:49 +01:00
|
|
|
{
|
2018-01-31 09:37:01 +01:00
|
|
|
camera.x = (int) limit(camera.x, world.map.bounds.x, world.map.bounds.w);
|
|
|
|
camera.y = (int) limit(camera.y, world.map.bounds.y, world.map.bounds.h);
|
2018-01-21 12:12:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int isOnScreen(Entity *e)
|
|
|
|
{
|
2018-02-07 20:21:13 +01:00
|
|
|
int x, y;
|
|
|
|
|
|
|
|
x = (-camera.x + e->x);
|
|
|
|
y = (-camera.y + e->y);
|
|
|
|
|
|
|
|
return (x >= 0 && y >= 0 && x - e->w < SCREEN_WIDTH && y - e->h < SCREEN_HEIGHT);
|
2018-01-21 12:12:49 +01:00
|
|
|
}
|