2023-04-05 03:02:49 +08:00
|
|
|
#include "level.h"
|
2023-03-31 08:08:02 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2023-04-05 03:02:49 +08:00
|
|
|
#include "maths.h"
|
|
|
|
|
2023-03-31 08:08:02 +08:00
|
|
|
#define WIDTH 5
|
|
|
|
#define HEIGHT 5
|
|
|
|
|
2023-04-16 09:17:11 +08:00
|
|
|
static void drawGrid(sf::RenderTarget* renderTarget, unsigned int tileSize);
|
|
|
|
static void drawGridLine(sf::RenderTarget* renderTarget, float step, bool isHorizontal);
|
|
|
|
static sf::Vertex getGridLineVertex(float n, float maxDimension, bool isStart, bool isHorizontal);
|
2023-04-01 05:56:26 +08:00
|
|
|
|
2023-04-07 12:34:22 +08:00
|
|
|
static float castRay(sf::Vector2f point, float direction);
|
2023-04-05 03:02:49 +08:00
|
|
|
static void getGridIndex(sf::Vector2f point, int* x, int* y);
|
|
|
|
|
2023-03-31 08:08:02 +08:00
|
|
|
static unsigned int level[WIDTH * HEIGHT] = {
|
2023-04-16 09:17:11 +08:00
|
|
|
0, 0, 1, 1, 1,
|
2023-04-07 12:34:22 +08:00
|
|
|
0, 0, 0, 0, 0,
|
2023-04-16 09:17:11 +08:00
|
|
|
1, 0, 1, 0, 1,
|
|
|
|
1, 0, 0, 0, 1,
|
|
|
|
1, 0, 1, 1, 1,
|
2023-03-31 08:08:02 +08:00
|
|
|
};
|
|
|
|
|
2023-04-16 09:17:11 +08:00
|
|
|
int level_init()
|
2023-03-31 08:08:02 +08:00
|
|
|
{
|
|
|
|
printf("level_init()\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-04-16 09:17:11 +08:00
|
|
|
void level_update(sf::RenderTarget* renderTarget, unsigned int drawSize)
|
2023-03-31 08:08:02 +08:00
|
|
|
{
|
2023-04-16 09:17:11 +08:00
|
|
|
if (!renderTarget) return;
|
|
|
|
|
|
|
|
drawGrid(renderTarget, drawSize/WIDTH);
|
2023-03-31 08:08:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void level_end()
|
|
|
|
{
|
|
|
|
printf("level_end()\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-02 08:21:02 +08:00
|
|
|
float level_rayCastDistance(sf::Vector2f point, float direction)
|
|
|
|
{
|
2023-04-07 12:34:22 +08:00
|
|
|
return castRay(point, direction);
|
2023-04-02 08:21:02 +08:00
|
|
|
}
|
|
|
|
|
2023-04-16 09:17:11 +08:00
|
|
|
static void drawGrid(sf::RenderTarget* renderTarget, unsigned int tileSize)
|
2023-03-31 08:08:02 +08:00
|
|
|
{
|
|
|
|
for (unsigned int x = 0; x < WIDTH; x++) {
|
2023-04-01 05:56:26 +08:00
|
|
|
for (unsigned int y = 0; y < HEIGHT; y++) {
|
|
|
|
if (!level[y * HEIGHT + x]) continue;
|
2023-03-31 08:08:02 +08:00
|
|
|
|
2023-04-16 09:17:11 +08:00
|
|
|
sf::RectangleShape rectangle(sf::Vector2f(tileSize, tileSize));
|
|
|
|
rectangle.setPosition((float)x * tileSize, (float)y * tileSize);
|
|
|
|
renderTarget->draw(rectangle);
|
2023-04-01 05:56:26 +08:00
|
|
|
}
|
2023-03-31 08:08:02 +08:00
|
|
|
}
|
2023-04-02 07:41:16 +08:00
|
|
|
|
2023-04-16 09:17:11 +08:00
|
|
|
drawGridLine(renderTarget, tileSize, true);
|
|
|
|
drawGridLine(renderTarget, tileSize, false);
|
2023-04-01 05:56:26 +08:00
|
|
|
}
|
|
|
|
|
2023-04-16 09:17:11 +08:00
|
|
|
static void drawGridLine(sf::RenderTarget* renderTarget, float step, bool isHorizontal)
|
2023-04-01 05:56:26 +08:00
|
|
|
{
|
|
|
|
unsigned int lines = isHorizontal? WIDTH : HEIGHT;
|
2023-03-31 08:08:02 +08:00
|
|
|
|
2023-04-01 05:56:26 +08:00
|
|
|
for (unsigned int n = 0; n < lines; n++) {
|
|
|
|
if (n == 0) continue;
|
2023-04-16 09:17:11 +08:00
|
|
|
float offset = (float)n * step;
|
|
|
|
float maxDimension = (float)lines * step;
|
2023-03-31 08:08:02 +08:00
|
|
|
sf::Vertex line[] =
|
|
|
|
{
|
2023-04-01 05:56:26 +08:00
|
|
|
getGridLineVertex(offset, maxDimension, true, isHorizontal),
|
|
|
|
getGridLineVertex(offset, maxDimension, false, isHorizontal)
|
2023-03-31 08:08:02 +08:00
|
|
|
};
|
|
|
|
|
2023-04-16 09:17:11 +08:00
|
|
|
renderTarget->draw(line, 2, sf::Lines);
|
2023-03-31 08:08:02 +08:00
|
|
|
}
|
2023-04-01 05:56:26 +08:00
|
|
|
}
|
2023-03-31 08:08:02 +08:00
|
|
|
|
2023-04-16 09:17:11 +08:00
|
|
|
static sf::Vertex getGridLineVertex(float offset, float maxDimension, bool isStart, bool isHorizontal)
|
2023-04-01 05:56:26 +08:00
|
|
|
{
|
|
|
|
sf::Vertex start;
|
|
|
|
sf::Vertex end;
|
2023-03-31 08:08:02 +08:00
|
|
|
|
2023-04-01 05:56:26 +08:00
|
|
|
if (isHorizontal) {
|
|
|
|
start = sf::Vertex(sf::Vector2f(offset, 0));
|
|
|
|
end = sf::Vertex(sf::Vector2f(offset, maxDimension));
|
2023-03-31 08:08:02 +08:00
|
|
|
}
|
2023-04-01 05:56:26 +08:00
|
|
|
else {
|
|
|
|
start = sf::Vertex(sf::Vector2f(0, offset));
|
|
|
|
end = sf::Vertex(sf::Vector2f(maxDimension, offset));
|
|
|
|
}
|
|
|
|
|
2023-04-16 09:17:11 +08:00
|
|
|
sf::Color color(100, 100, 100);
|
|
|
|
start.color = color;
|
|
|
|
end.color = color;
|
2023-04-01 05:56:26 +08:00
|
|
|
return isStart? start : end;
|
2023-03-31 08:08:02 +08:00
|
|
|
}
|
2023-04-05 03:02:49 +08:00
|
|
|
|
2023-04-07 12:34:22 +08:00
|
|
|
static float castRay(sf::Vector2f point, float direction)
|
2023-04-05 03:02:49 +08:00
|
|
|
{
|
|
|
|
int indexX, indexY;
|
|
|
|
getGridIndex(point, &indexX, &indexY);
|
|
|
|
|
2023-04-07 12:34:22 +08:00
|
|
|
// The horizontal* and vertical* variables correspond to variables, that
|
|
|
|
// are used to calculate the horizontal and vertical grid intersection points
|
|
|
|
// respectively. The horizontal and vertical grid intersections are done
|
|
|
|
// separately.
|
|
|
|
//
|
|
|
|
// The *Dy and *Dx variables are the deltas to the nearest grid boundary.
|
|
|
|
//
|
|
|
|
// The *StepX and *StepY variables are the regular x and y steps from the
|
|
|
|
// initial boundary intersection along the ray.
|
|
|
|
//
|
|
|
|
// The *ProjectedX and *ProjectedY variables are projected coordinates of the
|
|
|
|
// grid intersections along the ray.
|
|
|
|
//
|
|
|
|
// The *DistCoeff variables store the coefficient of sin(direction) used to
|
|
|
|
// calculate distance travelled along the ray, without having to do extra
|
|
|
|
// calls to sin(), as the direction doesn't change.
|
|
|
|
direction = maths_modulo(direction, 2.0f*PI); // modulo to keep the angle between 0 and 2 PI radians
|
2023-04-05 03:02:49 +08:00
|
|
|
bool goingDown = direction < PI;
|
|
|
|
int signDown = goingDown? 1 : -1;
|
|
|
|
|
2023-04-16 09:17:11 +08:00
|
|
|
float horizontalDy = (float)(indexY + goingDown) - point.y;
|
2023-04-07 12:34:22 +08:00
|
|
|
float horizontalDx = horizontalDy/tan(direction);
|
2023-04-05 03:02:49 +08:00
|
|
|
|
2023-04-16 09:17:11 +08:00
|
|
|
float horizontalStepX = ((float)signDown * (1.f/tan(direction)));
|
|
|
|
float horizontalStepY = (float)signDown;
|
2023-04-07 12:34:22 +08:00
|
|
|
float horizontalProjectedX = point.x + horizontalDx;
|
2023-04-16 09:17:11 +08:00
|
|
|
float horizontalProjectedY = indexY + goingDown;
|
2023-04-05 03:02:49 +08:00
|
|
|
|
2023-04-07 12:34:22 +08:00
|
|
|
float horizontalDistCoeff = sin(direction);
|
|
|
|
float horizontalRayDist = std::abs(horizontalDy/horizontalDistCoeff);
|
|
|
|
|
|
|
|
direction = maths_modulo(direction + 0.5f*PI, 2.0f*PI); // rotate angle by 90 degrees for ease of calaculation
|
2023-04-05 03:02:49 +08:00
|
|
|
bool goingRight = direction < PI;
|
|
|
|
int signRight = goingRight? 1 : -1;
|
|
|
|
|
2023-04-16 09:17:11 +08:00
|
|
|
float verticalDx = (float)(indexX + goingRight) - point.x;
|
2023-04-07 12:34:22 +08:00
|
|
|
float verticalDy = -verticalDx/tan(direction); // y axis needs to be flipped
|
2023-04-05 03:02:49 +08:00
|
|
|
|
2023-04-16 09:17:11 +08:00
|
|
|
float verticalStepY = -((float)signRight * (1.f/tan(direction))); // y axis also flipped here
|
|
|
|
float verticalStepX = (float)signRight;
|
2023-04-07 12:34:22 +08:00
|
|
|
float verticalProjectedY = point.y + verticalDy;
|
2023-04-16 09:17:11 +08:00
|
|
|
float verticalProjectedX = indexX + goingRight;
|
2023-04-05 03:02:49 +08:00
|
|
|
|
2023-04-07 12:34:22 +08:00
|
|
|
float verticalDistCoeff = sin(direction);
|
|
|
|
float verticalRayDist = std::abs(verticalDx/verticalDistCoeff);
|
2023-04-05 03:02:49 +08:00
|
|
|
|
2023-04-16 09:17:11 +08:00
|
|
|
unsigned int tries = WIDTH * HEIGHT;
|
|
|
|
while (tries--) {
|
2023-04-07 12:34:22 +08:00
|
|
|
int indexX0, indexY0; // store grid indices for horizontal intersections
|
|
|
|
int indexX1, indexY1; // store grid indices for vertical intersections
|
|
|
|
getGridIndex(sf::Vector2f(horizontalProjectedX, horizontalProjectedY), &indexX0, &indexY0);
|
|
|
|
getGridIndex(sf::Vector2f(verticalProjectedX, verticalProjectedY), &indexX1, &indexY1);
|
2023-04-05 03:02:49 +08:00
|
|
|
|
2023-04-07 12:34:22 +08:00
|
|
|
// If the ray going up or to left, the intersection points will give an index
|
|
|
|
// of the cells below or to the right of the cell boundaries. For those cases,
|
|
|
|
// the appropriate indices will be reduced by one.
|
|
|
|
indexY0 -= !goingDown;
|
|
|
|
indexX1 -= !goingRight;
|
2023-04-05 03:02:49 +08:00
|
|
|
|
2023-04-07 12:34:22 +08:00
|
|
|
bool inLevel0 = indexX0 != -1 && indexY0 != -1;
|
|
|
|
bool inLevel1 = indexX1 != -1 && indexY1 != -1;
|
2023-04-05 03:02:49 +08:00
|
|
|
|
2023-04-07 12:34:22 +08:00
|
|
|
if (!(inLevel0 || inLevel1)) break;
|
2023-04-05 03:02:49 +08:00
|
|
|
|
2023-04-07 12:34:22 +08:00
|
|
|
if (horizontalRayDist < verticalRayDist) {
|
|
|
|
if (level[indexY0 * WIDTH + indexX0]) return horizontalRayDist;
|
2023-04-05 03:02:49 +08:00
|
|
|
|
2023-04-07 12:34:22 +08:00
|
|
|
horizontalProjectedX += horizontalStepX;
|
|
|
|
horizontalProjectedY += horizontalStepY;
|
|
|
|
|
|
|
|
horizontalRayDist += std::abs(horizontalStepY/horizontalDistCoeff);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (level[indexY1 * WIDTH + indexX1]) return verticalRayDist;
|
2023-04-05 03:02:49 +08:00
|
|
|
|
2023-04-07 12:34:22 +08:00
|
|
|
verticalProjectedX += verticalStepX;
|
|
|
|
verticalProjectedY += verticalStepY;
|
2023-04-05 03:02:49 +08:00
|
|
|
|
2023-04-07 12:34:22 +08:00
|
|
|
verticalRayDist += std::abs(verticalStepX/verticalDistCoeff);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return 1000.f;
|
2023-04-05 03:02:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void getGridIndex(sf::Vector2f point, int* x, int* y)
|
|
|
|
{
|
2023-04-16 09:17:11 +08:00
|
|
|
*x = point.x;
|
|
|
|
*y = point.y;
|
2023-04-05 03:02:49 +08:00
|
|
|
|
|
|
|
if (*x < 0 || WIDTH <= *x) *x = -1;
|
|
|
|
if (*y < 0 || HEIGHT <= *y) *y = -1;
|
|
|
|
}
|