-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameObject.cpp
78 lines (65 loc) · 1.88 KB
/
GameObject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "Include/GameObject.h"
void gameObject::init(float x, float y, int s, double r, int t) {
size = s;
setPosition(x, y);
setRenderPosition(x, y);
rotation = r;
type = t;
}
void gameObject::setPosition(float x, float y) {
px = x;
py = y;
}
void gameObject::setRenderPosition(float x, float y) {
rx = int(x - size / 2);
ry = int(y - size / 2);
}
void gameObject::updateRenderPosition() {
rx = int(px - size / 2);
ry = int(py - size / 2);
}
void gameObject::calRotation(int x, int y) {
int deltaX = px - x;
int deltaY = py - y;
rotation = (atan2(deltaY, deltaX) * 180.0000) / M_PI;
}
float gameObject::calDistance(gameObject target) {
return sqrt(pow(target.px - px, 2) + pow(target.py - py, 2));
}
bool gameObject::checkCollision(gameObject target, float padding) {
if (calDistance(target) < float(target.size / COLLIDER_TOLERANCE) + padding) {
return true;
}
return false;
}
void gameObject::resize(int s) {
size = s;
}
void gameObject::render(LTexture target)
{
target.render(rx, ry, size, size, NULL, rotation);
}
void gameObject::render(LTexture target, SDL_Rect& camera)
{
target.render(rx - camera.x, ry - camera.y, size, size, NULL, rotation);
}
void renderGameObject(SDL_Rect camera, LTexture& sourceTexture, std::vector<gameObject>& vectorList)
{
if (vectorList.size() > 0)
{
for (int i = 0; i < vectorList.size(); i++)
{
sourceTexture.render(camera, vectorList[i].rx, vectorList[i].ry, vectorList[i].size, vectorList[i].size, NULL, vectorList[i].rotation);
}
}
}
void renderGameObject(SDL_Rect camera, LTexture& sourceTexture, std::vector<gameObject>& vectorList, std::vector<SDL_Rect> clips)
{
if (vectorList.size() > 0 && clips.size() > 0)
{
for (int i = 0; i < vectorList.size(); i++)
{
sourceTexture.render(camera, vectorList[i].rx, vectorList[i].ry, vectorList[i].size, vectorList[i].size, &clips[vectorList[i].type], vectorList[i].rotation);
}
}
}