forked from gameprogcpp/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHUD.cpp
More file actions
146 lines (133 loc) · 3.97 KB
/
HUD.cpp
File metadata and controls
146 lines (133 loc) · 3.97 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE.txt for full details.
// ----------------------------------------------------------------
#include "HUD.h"
#include "Texture.h"
#include "Shader.h"
#include "Game.h"
#include "Renderer.h"
#include "PhysWorld.h"
#include "TargetActor.h"
#include "FollowActor.h"
#include <algorithm>
#include "GBuffer.h"
HUD::HUD(Game* game)
:UIScreen(game)
,mRadarRange(2000.0f)
,mRadarRadius(92.0f)
,mTargetEnemy(false)
{
Renderer* r = mGame->GetRenderer();
mHealthBar = r->GetTexture("Assets/HealthBar.png");
mRadar = r->GetTexture("Assets/Radar.png");
mCrosshair = r->GetTexture("Assets/Crosshair.png");
mCrosshairEnemy = r->GetTexture("Assets/CrosshairRed.png");
mBlipTex = r->GetTexture("Assets/Blip.png");
mRadarArrow = r->GetTexture("Assets/RadarArrow.png");
}
HUD::~HUD()
{
}
void HUD::Update(float deltaTime)
{
UIScreen::Update(deltaTime);
UpdateCrosshair(deltaTime);
UpdateRadar(deltaTime);
}
void HUD::Draw(Shader* shader)
{
// Crosshair
//Texture* cross = mTargetEnemy ? mCrosshairEnemy : mCrosshair;
//DrawTexture(shader, cross, Vector2::Zero, 2.0f);
// Radar
const Vector2 cRadarPos(-390.0f, 275.0f);
DrawTexture(shader, mRadar, cRadarPos, 1.0f);
// Blips
for (Vector2& blip : mBlips)
{
DrawTexture(shader, mBlipTex, cRadarPos + blip, 1.0f);
}
// Radar arrow
DrawTexture(shader, mRadarArrow, cRadarPos);
//// Health bar
//DrawTexture(shader, mHealthBar, Vector2(-350.0f, -350.0f));
// Draw the mirror (bottom left)
Texture* mirror = mGame->GetRenderer()->GetMirrorTexture();
DrawTexture(shader, mirror, Vector2(-350.0f, -250.0f), 1.0f, true);
//Texture* tex = mGame->GetRenderer()->GetGBuffer()->GetTexture(GBuffer::EDiffuse);
//DrawTexture(shader, tex, Vector2::Zero, 1.0f, true);
}
void HUD::AddBlipActor(Actor* actor)
{
mBlipActors.emplace_back(actor);
}
void HUD::RemoveBlipActor(Actor* actor)
{
auto iter = std::find(mBlipActors.begin(), mBlipActors.end(),
actor);
if (iter != mBlipActors.end())
{
// Swap to end of vector and pop off (avoid erase copies)
std::iter_swap(iter, mBlipActors.end() - 1);
mBlipActors.pop_back();
}
}
void HUD::UpdateCrosshair(float deltaTime)
{
// Reset to regular cursor
mTargetEnemy = false;
// Make a line segment
const float cAimDist = 5000.0f;
Vector3 start, dir;
mGame->GetRenderer()->GetScreenDirection(start, dir);
LineSegment l(start, start + dir * cAimDist);
// Segment cast
PhysWorld::CollisionInfo info;
if (mGame->GetPhysWorld()->SegmentCast(l, info))
{
// Is this a target?
if (dynamic_cast<TargetActor*>(info.mActor) != nullptr)
{
mTargetEnemy = true;
}
}
}
void HUD::UpdateRadar(float deltaTime)
{
// Clear blip positions from last frame
mBlips.clear();
// Convert player position to radar coordinates (x forward, z up)
Vector3 playerPos = mGame->GetPlayer()->GetPosition();
Vector2 playerPos2D(playerPos.y, playerPos.x);
// Ditto for player forward
Vector3 playerForward = mGame->GetPlayer()->GetForward();
Vector2 playerForward2D(playerForward.x, playerForward.y);
// Use atan2 to get rotation of radar
float angle = Math::Atan2(playerForward2D.y, playerForward2D.x);
// Make a 2D rotation matrix
Matrix3 rotMat = Matrix3::CreateRotation(angle);
// Get positions of blips
for (auto actor : mBlipActors)
{
Vector3 actorPos = actor->GetPosition();
Vector2 actorPos2D(actorPos.y, actorPos.x);
// Calculate vector between player and actor
Vector2 playerToActor = actorPos2D - playerPos2D;
// See if within range
if (playerToActor.LengthSq() <= (mRadarRange * mRadarRange))
{
// Convert playerToActor into an offset from
// the center of the on-screen radar
Vector2 blipPos = playerToActor;
blipPos *= 1.0f/mRadarRange;
blipPos *= mRadarRadius;
// Rotate blipPos
blipPos = Vector2::Transform(blipPos, rotMat);
mBlips.emplace_back(blipPos);
}
}
}