-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntity.cpp
More file actions
80 lines (61 loc) · 1.91 KB
/
Entity.cpp
File metadata and controls
80 lines (61 loc) · 1.91 KB
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 "Entity.h"
std::vector<SDL_Rect> Entity::generateRectsFromSheet(ReadableTexture* sheet, SDL_Point imgSize) {
int sw = sheet->getWidth(), sh = sheet->getHeight();
std::vector<SDL_Rect> result;
for (int y = 0; y < sh; y += imgSize.y) {
for (int x = 0; x < sw; x += imgSize.x) {
SDL_Rect tempRect = { x, y, imgSize.x, imgSize.y };
result.push_back(tempRect);
}
}
return result;
}
Entity::Entity(std::vector<ReadableTexture*>* images, SDL_Point imgSize, int imgScale) {
pos = new Point();
accel = new Point();
Entity::imgScale = imgScale;
entityScreenRect = { 0, 0, imgSize.x * imgScale, imgSize.y * imgScale };
entityImages = images;
for (int i = 0; i < entityImages->size(); i += 1) {
entityClipRects.push_back(generateRectsFromSheet(entityImages->at(i), imgSize));
}
}
Entity::Entity(Point* pos, Point* a, std::vector<ReadableTexture*>* images, SDL_Point imgSize, int imgScale) {
Entity::pos = pos;
Entity::accel = a;
Entity::imgScale = imgScale;
entityScreenRect = { 0, 0, imgSize.x * imgScale, imgSize.y * imgScale };
entityImages = images;
for (int i = 0; i < entityImages->size(); i += 1) {
entityClipRects.push_back(generateRectsFromSheet(entityImages->at(i), imgSize));
}
}
void Entity::update() {
pos->add(accel);
entityScreenRect.x = trunc(pos->x);
entityScreenRect.y = trunc(pos->y);
}
std::vector<ReadableTexture*>* Entity::getImages() {
return entityImages;
}
SDL_Point Entity::getImgSize() {
return { entityScreenRect.w / imgScale, entityScreenRect.h / imgScale };
}
int Entity::getImgScale() {
return imgScale;
}
SDL_Texture* Entity::getCurrentImage() {
return entityImages->at(curState)->getTexture();
}
SDL_Rect* Entity::getCurrentClip() {
return entityClipRects[curState].data() + (curFrame / (ANIMATIONSLOW));
}
SDL_Rect Entity::getScreenRect() {
return entityScreenRect;
}
Point* Entity::getPosition() {
return pos;
}
Point* Entity::getAccel() {
return accel;
}