-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
7,528 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include "utils.h" | ||
#include "camera.h" | ||
#include "input.h" | ||
#include "game.h" | ||
#include "world.h" | ||
|
||
#include <cmath> | ||
#include <algorithm> | ||
#include <vector> | ||
|
||
Camera::Camera() | ||
{ | ||
smoothing = true; | ||
// shake_elapsed = 0; | ||
shake_length = 0; | ||
shake_amount = 0; | ||
} | ||
|
||
Camera::~Camera() | ||
{ | ||
|
||
} | ||
|
||
Camera::Camera(int x, int y) | ||
{ | ||
this->x = x; | ||
this->y = y; | ||
smoothing = true; | ||
// shake_elapsed = 0; | ||
shake_length = 0; | ||
shake_amount = 0; | ||
} | ||
|
||
void Camera::update(double seconds_elapsed) | ||
{ | ||
this->follow(this->character); | ||
|
||
if (shake_length > 0) | ||
{ | ||
int shakeX = random() * shake_amount * 2 - shake_amount; | ||
int shakeY = random() * shake_amount * 2 - shake_amount; | ||
|
||
/*int x = this->x + shakeX; | ||
int y = this->y + shakeY;*/ | ||
|
||
this->x += shakeX; | ||
this->y += shakeY; | ||
|
||
shake_length--; | ||
|
||
/*int gameMapSize = World::instance->map.width; | ||
int bufferSize = Game::instance->bufferSize; | ||
int margin = bufferSize / 2; | ||
this->x = clamp(this->x, margin, gameMapSize - margin) - bufferSize / 2; | ||
this->y = clamp(this->y, margin, gameMapSize - margin) - bufferSize / 2;*/ | ||
} | ||
} | ||
|
||
void Camera::follow(Character * chtr) | ||
{ | ||
int gameMapSize = World::instance->map.width; | ||
int bufferSize = Game::instance->bufferSize; | ||
|
||
int margin = bufferSize / 2; | ||
int targetx = clamp(chtr->x, margin, gameMapSize - margin) - bufferSize/2; | ||
int targety = clamp(chtr->y, margin, gameMapSize - margin) - bufferSize/2; | ||
|
||
this->x = smoothing ? lerp(targetx, this->x, 0.9) : targetx; | ||
this->y = smoothing ? lerp(targety, this->y, 0.9) : targety; | ||
} | ||
|
||
void Camera::lookAt(int x, int y, bool smooth) | ||
{ | ||
int gameMapSize = World::instance->map.width; | ||
int bufferSize = Game::instance->bufferSize; | ||
|
||
int margin = bufferSize / 2; | ||
int targetx = clamp(x, margin, gameMapSize - margin) - bufferSize / 2; | ||
int targety = clamp(y, margin, gameMapSize - margin) - bufferSize / 2; | ||
|
||
this->x = smooth ? lerp(targetx, this->x, 0.9) : targetx; | ||
this->y = smooth ? lerp(targety, this->y, 0.9) : targety; | ||
} | ||
|
||
void Camera::shake(int amount, int length) | ||
{ | ||
shake_amount = amount; | ||
shake_length = length; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#ifndef CAMERA_H | ||
#define CAMERA_H | ||
|
||
#include "includes.h" | ||
#include "utils.h" | ||
#include "character.h" | ||
|
||
class Camera | ||
{ | ||
private: | ||
|
||
Character* character; | ||
|
||
int shake_elapsed; | ||
int shake_length; | ||
int shake_amount; | ||
|
||
public: | ||
|
||
int x; | ||
int y; | ||
|
||
bool smoothing; | ||
|
||
Camera(); | ||
Camera(int x, int y); | ||
~Camera(); | ||
|
||
inline void setCharacter(Character* c) { this->character = c; }; | ||
|
||
void update(double seconds_elapsed); | ||
void lookAt(int x, int y, bool smooth = false); | ||
void follow(Character* chtr); | ||
void shake(int amount, int length); | ||
|
||
}; | ||
|
||
|
||
#endif |
Oops, something went wrong.