From c8132c56f0d1d712e4841a60f9f0f853e7177715 Mon Sep 17 00:00:00 2001 From: nathansmithsmith Date: Sat, 23 Sep 2023 21:25:54 -0600 Subject: Radar working --- src/radar.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/radar.c (limited to 'src/radar.c') diff --git a/src/radar.c b/src/radar.c new file mode 100644 index 0000000..1aca7ca --- /dev/null +++ b/src/radar.c @@ -0,0 +1,106 @@ +#include "radar.h" +#include "game.h" +#include "entity.h" +#include "world.h" +#include "assets.h" + +void initRadar(Radar * radar) { + resetRadarPosition(radar); + + radar->camera = (Camera3D){ + .fovy = 45.0, + .projection = CAMERA_PERSPECTIVE, + .target = Vector3Zero(), + .position = (Vector3){RADAR_CAMERA_DISTANCE, RADAR_CAMERA_DISTANCE, RADAR_CAMERA_DISTANCE}, + .up = (Vector3){0.0, 1.0, 0.0} + }; + + radar->texture = LoadRenderTexture(RADAR_TEXTURE_SIZE, RADAR_TEXTURE_SIZE); +} + +void closeRadar(Radar * radar) { + UnloadRenderTexture(radar->texture); +} + +void resetRadarPosition(Radar * radar) { + float width = GetScreenWidth(); + float height = GetScreenHeight(); + + radar->position = (Vector2){ + width - RADAR_TEXTURE_SIZE, + height - RADAR_TEXTURE_SIZE + }; +} + +void drawRadar3DParts(Game * game, Radar * radar) { + int i; + + World * world = &game->world; + Entity * player = getEntityFromWorld(*world, 0); + + BeginTextureMode(radar->texture); + ClearBackground(RADAR_COLOR); + BeginMode3D(radar->camera); + + // Draw entities on radar. Skip player. + for (i = 1; i < world->entitiesCount; ++i) { + Entity * entity = &world->entities[i]; + + // Too far. + if (Vector3Distance(player->position, entity->position) > RADAR_MAX_DISTANCE) + continue; + + DrawSphere( + Vector3Scale(Vector3Subtract(entity->position, player->position), RADAR_WORLD_SCALE), + RADAR_POINT_SIZE, + BLACK + ); + } + + Vector3 direction = Vector3Scale( + Vector3RotateByQuaternion((Vector3){0.0, 0.0, 1.0}, player->rotation), + RADAR_POINT_SIZE * 2.0 + ); + + // Draw player. + DrawCylinderWiresEx( + Vector3Scale(direction, -1.0), + direction, + RADAR_POINT_SIZE, + 0.0, + 8, + BLUE + ); + + EndMode3D(); + EndTextureMode(); + + // Draw render texture. + DrawTextureV(radar->texture.texture, radar->position, WHITE); + +} + +void drawRadar(Game * game, Radar * radar) { + drawRadar3DParts(game, radar); + + // Draw cross. + float halfWayPoint = RADAR_TEXTURE_SIZE / 2.0; + + // Top to bottom. + // DrawLine( + // radar->position.x + halfWayPoint, + // radar->position.y, + // radar->position.x + halfWayPoint, + // radar->position.y + RADAR_TEXTURE_SIZE, + // RADAR_CROSS_COLOR + // ); + // + // // Left to right. + // DrawLine( + // radar->position.x, + // radar->position.y + halfWayPoint, + // radar->position.x + RADAR_TEXTURE_SIZE, + // radar->position.y + halfWayPoint, + // RADAR_CROSS_COLOR + // ); +} -- cgit v1.2.3