File: Radar.cpp

package info (click to toggle)
endless-sky 0.10.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 414,608 kB
  • sloc: cpp: 73,435; python: 893; xml: 666; sh: 271; makefile: 28
file content (178 lines) | stat: -rw-r--r-- 4,767 bytes parent folder | download
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* Radar.cpp
Copyright (c) 2014 by Michael Zahniser

Endless Sky is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.

Endless Sky is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "Radar.h"

#include "GameData.h"
#include "shader/LineShader.h"
#include "shader/PointerShader.h"
#include "shader/RingShader.h"

#include <cmath>

using namespace std;

const int Radar::PLAYER = 0;
const int Radar::FRIENDLY = 1;
const int Radar::UNFRIENDLY = 2;
const int Radar::HOSTILE = 3;
const int Radar::INACTIVE = 4;
const int Radar::SPECIAL = 5;
const int Radar::ANOMALOUS = 6;
const int Radar::BLINK = 7;
const int Radar::VIEWPORT = 8;
const int Radar::STAR = 9;



void Radar::Clear()
{
	objects.clear();
	pointers.clear();
	lines.clear();
}



void Radar::SetCenter(const Point &center)
{
	this->center = center;
}



// Add an object. If "inner" is 0 it is a dot; otherwise, it is a ring. The
// given position should be in world units (not shrunk to radar units).
void Radar::Add(int type, Point position, double outer, double inner)
{
	objects.emplace_back(GetColor(type).Opaque(), position - center, outer, inner);
}



// Add a pointer, pointing in the direction of the given vector.
void Radar::AddPointer(int type, const Point &position)
{
	pointers.emplace_back(GetColor(type), position.Unit());
}



// Create a "corner" from a vertical and horizontal leg.
void Radar::AddViewportBoundary(const Point &vertex)
{
	Point start(vertex.X() - copysign(200., vertex.X()), vertex.Y());
	Point end(vertex.X(), vertex.Y() - copysign(200., vertex.Y()));

	// Add the horizontal leg, pointing from start to vertex.
	lines.emplace_back(GetColor(VIEWPORT), start, vertex - start);
	// Add the vertical leg, pointing from end to vertex.
	lines.emplace_back(GetColor(VIEWPORT), end, vertex - end);
}



// Draw the radar display at the given coordinates.
void Radar::Draw(const Point &center, double scale, double radius, double pointerRadius) const
{
	// Draw any desired line vectors.
	for(const Line &line : lines)
	{
		Point start = line.base * scale;
		Point v = line.vector * scale;

		// At least one endpoint must be within the radar display.
		double startExcess = start.Length() - radius;
		double endExcess = (start + v).Length() - radius;
		if(startExcess > 0 && endExcess > 0)
			continue;
		else if(startExcess > 0)
		{
			// Move "start" along "v" until it is within the radius.
			start += startExcess * v.Unit();
			// Shorten "v" to keep the desired length.
			v -= startExcess * v.Unit();
		}
		else if(endExcess > 0)
			v -= endExcess * v.Unit();

		LineShader::Draw(start + center, start + v + center, 1.f, line.color);
	}

	// Draw StellarObjects and ships.
	RingShader::Bind();
	for(const Object &object : objects)
	{
		Point position = object.position * scale;
		double length = position.Length();
		if(length > radius)
			position *= radius / length;
		position += center;

		RingShader::Add(position, object.outer, object.inner, object.color);
	}
	RingShader::Unbind();

	// Draw neighboring system indicators.
	PointerShader::Bind();
	for(const Pointer &pointer : pointers)
		PointerShader::Add(center, pointer.unit, 10.f, 10.f, pointerRadius, pointer.color);
	PointerShader::Unbind();
}



const Color &Radar::GetColor(int type)
{
	static const vector<Color> color = {
		*GameData::Colors().Get("radar player"),
		*GameData::Colors().Get("radar friendly"),
		*GameData::Colors().Get("radar unfriendly"),
		*GameData::Colors().Get("radar hostile"),
		*GameData::Colors().Get("radar inactive"),
		*GameData::Colors().Get("radar special"),
		*GameData::Colors().Get("radar anomalous"),
		*GameData::Colors().Get("radar blink"),
		*GameData::Colors().Get("radar viewport"),
		*GameData::Colors().Get("radar star")
	};

	if(static_cast<size_t>(type) >= color.size())
		type = INACTIVE;

	return color[type];
}



Radar::Object::Object(const Color &color, const Point &pos, double out, double in)
	: color(color), position(pos), outer(out), inner(in)
{
}



Radar::Pointer::Pointer(const Color &color, const Point &unit)
	: color(color), unit(unit)
{
}



// Create a line starting from "base" with length and angle described by "vector."
Radar::Line::Line(const Color &color, const Point &base, const Point &vector)
	: color(color), base(base), vector(vector)
{
}