-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entity.cpp
100 lines (72 loc) · 3.17 KB
/
Entity.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "Entity.h"
#include "Resolution.h"
#include "math.h"
#include <GL/gl.h>
void Entity::render(
EntityProperties* entity,
Player *player,
Textures* textures,
Resolution* resolution,
Wall** walls
) {
int
spriteX = (entity->tileLeft * (TILE_SIZE) - (TILE_SIZE / 2)),
spriteY = (entity->tileTop * (TILE_SIZE) - (TILE_SIZE / 2));
float spriteDistance = sqrt(pow(*player->getX() - spriteX, 2) + pow(*player->getY() - spriteY, 2));
spriteX = spriteX - *player->getX(),
spriteY = spriteY - *player->getY();
float eyeX, eyeY, objectAngle;
eyeX = sinf(player->getLookingDirectionInRadians());
eyeY = cosf(player->getLookingDirectionInRadians());
objectAngle = atan2f(eyeY, eyeX) - atan2f(spriteX, spriteY);
if (objectAngle < M_PI) {
objectAngle += 2 * M_PI;
}
if (objectAngle > M_PI) {
objectAngle -= 2 * M_PI;
}
bool objectVisible = (fabs(objectAngle) < getRadians(FOV) / 2) && spriteDistance > 40;
int spriteHeight = textures->getTextureHeight(3);
int spriteWidth = textures->getTextureWidth(3);
if (objectVisible == false) {
return;
}
float screenHeight = resolution->getResolutionHeight() * TILE_SIZE;
float objectCeiling = (resolution->getResolutionHeight() / 2) - (screenHeight / spriteDistance / 2);
float objectFloor = objectCeiling + (screenHeight / spriteDistance / 2);
float objectAspectRatio = ((float)spriteHeight / (float)spriteWidth);
float objectHeight = (objectFloor - objectCeiling) * entity->size;
float objectWidth = objectHeight / objectAspectRatio;
int objectMiddle = (.5 * (objectAngle / (getRadians(FOV) / 2)) + .5) * resolution->getResolutionWidth();
glPointSize(resolution->getPaintSize() * 2);
glBegin(GL_POINTS);
int renderRowOffset = (objectFloor - objectCeiling) * entity->distanceFromCeiling;
for (int renderColumn = 0; renderColumn < objectWidth; renderColumn++) {
for (int renderRow = renderRowOffset; renderRow < objectHeight + renderRowOffset; renderRow++) {
int columnX = objectMiddle + renderColumn - (objectWidth / 2);
if (columnX <= 0 || columnX >= resolution->getResolutionWidth()) {
continue;
}
if (walls[columnX]->distance < spriteDistance && (spriteDistance - walls[columnX]->distance) > 50) {
continue;
}
short
textureX = (spriteWidth / objectWidth) * renderColumn,
textureY = (spriteHeight / objectHeight) * (renderRow - renderRowOffset);
int
colorR = textures->getTextureRFromXandY(entity->textureId, textureX, textureY),
colorG = textures->getTextureGFromXandY(entity->textureId, textureX, textureY),
colorB = textures->getTextureBFromXandY(entity->textureId, textureX, textureY);
if (colorR == 255 && colorG == 0 && colorB == 255) {
continue;
}
glColor3ub(colorR, colorG, colorB);
glVertex2i(columnX, objectCeiling + renderRow);
}
}
glEnd();
}
float Entity::getRadians(float degrees)
{
return M_PI * degrees / 180;
}