File: RepulseGfx.cpp

package info (click to toggle)
spring 105.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 108,860 kB
  • sloc: cpp: 467,785; ansic: 302,607; python: 12,925; java: 12,201; awk: 5,889; sh: 2,371; xml: 655; perl: 405; php: 276; objc: 194; makefile: 75; sed: 2
file content (179 lines) | stat: -rw-r--r-- 6,744 bytes parent folder | download | duplicates (3)
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
179
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */


#include "RepulseGfx.h"
#include "Rendering/GlobalRendering.h"
#include "Rendering/Env/Particles/ProjectileDrawer.h"
#include "Rendering/GL/RenderDataBuffer.hpp"
#include "Rendering/Textures/TextureAtlas.h"
#include "Sim/Units/Unit.h"

CR_BIND_DERIVED(CRepulseGfx, CProjectile, )

CR_REG_METADATA(CRepulseGfx,(
	CR_MEMBER(repulsed),
	CR_MEMBER(sqMaxOwnerDist),
	CR_MEMBER(age),
	CR_MEMBER(color),
	CR_MEMBER(vertexDists)
))


CRepulseGfx::CRepulseGfx(CUnit* owner, CProjectile* repulsee, float maxOwnerDist, const float4& gfxColor):
	CProjectile((repulsee != nullptr)? repulsee->pos: ZeroVector, (repulsee != nullptr)? repulsee->speed: ZeroVector, owner, false, false, false),
	repulsed(repulsee),
	age(0),
	sqMaxOwnerDist((maxOwnerDist * maxOwnerDist) + 100.0f),
	color(gfxColor)
{
	if (repulsed != nullptr)
		AddDeathDependence(repulsed, DEPENDENCE_REPULSE);

	checkCol = false;
	useAirLos = true;

	SetRadiusAndHeight(maxOwnerDist, 0.0f);

	for (int y = 0; y < 5; ++y) {
		float yp = (y / 4.0f - 0.5f);

		for (int x = 0; x < 5; ++x) {
			float xp = (x / 4.0f - 0.5f);
			float d = 0;

			if (xp != 0 || yp != 0)
				d = fastmath::apxsqrt2(xp * xp + yp * yp);

			vertexDists[y * 5 + x] = (1.0f - fastmath::cos(d * 2.0f)) * 20.0f;
		}
	}
}


void CRepulseGfx::DependentDied(CObject* o)
{
	if (o != repulsed)
		return;

	repulsed = nullptr;
	deleteMe = true;
}

void CRepulseGfx::Draw(GL::RenderDataBufferTC* va) const
{
	const CUnit* owner = CProjectile::owner();

	if (owner == nullptr || repulsed == nullptr)
		return;

	const float3 zdir = (repulsed->pos - owner->pos).SafeANormalize();
	const float3 xdir = (zdir.cross(UpVector)).SafeANormalize();
	const float3 ydir = xdir.cross(zdir);

	float3 xdirDS;
	float3 ydirDS;

	const float3 drawPos = pos - (zdir * 10.0f) + (repulsed->speed * globalRendering->timeOffset);

	float drawsize = 10.0f;
	float alpha = std::min(255.0f, age * 10.0f);

	unsigned char col[4] = {
		(unsigned char) (color.x * alpha       ),
		(unsigned char) (color.y * alpha       ),
		(unsigned char) (color.z * alpha       ),
		(unsigned char) (color.w * alpha * 0.2f),
	};
	constexpr unsigned char col2[4] = {0, 0, 0, 0};

	xdirDS = xdir * drawsize;
	ydirDS = ydir * drawsize;

	const AtlasedTexture* et = projectileDrawer->repulsetex;
	const float txo = et->xstart;
	const float tyo = et->ystart;
	const float txs = et->xend - et->xstart;
	const float tys = et->yend - et->ystart;

	static constexpr int loopCountY = 4;
	static constexpr int loopCountX = 4;

	for (int y = 0; y < loopCountY; ++y) {
		const float dy = y - 2.0f;
		const float ry = y * 0.25f;

		for (int x = 0; x < loopCountX; ++x) {
			const float dx = x - 2.00f;
			const float rx = x * 0.25f;

			va->SafeAppend({drawPos + xdirDS * (dx + 0) + ydirDS * (dy + 0) + zdir * vertexDists[(y    ) * 5 + x    ],  txo + (ry        ) * txs, tyo + (rx        ) * tys,  col});
			va->SafeAppend({drawPos + xdirDS * (dx + 0) + ydirDS * (dy + 1) + zdir * vertexDists[(y + 1) * 5 + x    ],  txo + (ry + 0.25f) * txs, tyo + (rx        ) * tys,  col});
			va->SafeAppend({drawPos + xdirDS * (dx + 1) + ydirDS * (dy + 1) + zdir * vertexDists[(y + 1) * 5 + x + 1],  txo + (ry + 0.25f) * txs, tyo + (rx + 0.25f) * tys,  col});

			va->SafeAppend({drawPos + xdirDS * (dx + 1) + ydirDS * (dy + 1) + zdir * vertexDists[(y + 1) * 5 + x + 1],  txo + (ry + 0.25f) * txs, tyo + (rx + 0.25f) * tys,  col});
			va->SafeAppend({drawPos + xdirDS * (dx + 1) + ydirDS * (dy + 0) + zdir * vertexDists[(y    ) * 5 + x + 1],  txo + (ry        ) * txs, tyo + (rx + 0.25f) * tys,  col});
			va->SafeAppend({drawPos + xdirDS * (dx + 0) + ydirDS * (dy + 0) + zdir * vertexDists[(y    ) * 5 + x    ],  txo + (ry        ) * txs, tyo + (rx        ) * tys,  col});
		}
	}

	drawsize = 7.0f;
	alpha = std::min(10.0f, age / 2.0f);

	col[0] = (unsigned char) (color.x * alpha       );
	col[1] = (unsigned char) (color.y * alpha       );
	col[2] = (unsigned char) (color.z * alpha       );
	col[3] = (unsigned char) (color.w * alpha * 0.4f);

	const AtlasedTexture* ct = projectileDrawer->repulsegfxtex;
	const float tx = (ct->xend + ct->xstart) * 0.5f;
	const float ty = (ct->yend + ct->ystart) * 0.5f;

	xdirDS = xdir * drawsize;
	ydirDS = ydir * drawsize;

	{
		va->SafeAppend({owner->pos + (-xdir   + ydir         ) * drawsize * 0.2f,  tx, ty, col2});
		va->SafeAppend({owner->pos + ( xdir   + ydir         ) * drawsize * 0.2f,  tx, ty, col2});
		va->SafeAppend({   drawPos +   xdirDS + ydirDS + zdir  *  vertexDists[6],  tx, ty, col });

		va->SafeAppend({   drawPos +   xdirDS + ydirDS + zdir  *  vertexDists[6],  tx, ty, col });
		va->SafeAppend({   drawPos -   xdirDS + ydirDS + zdir  *  vertexDists[6],  tx, ty, col });
		va->SafeAppend({owner->pos + (-xdir   + ydir         ) * drawsize * 0.2f,  tx, ty, col2});
	}
	{
		va->SafeAppend({owner->pos + (-xdir   - ydir         ) * drawsize * 0.2f,  tx, ty, col2});
		va->SafeAppend({owner->pos + ( xdir   - ydir         ) * drawsize * 0.2f,  tx, ty, col2});
		va->SafeAppend({   drawPos +   xdirDS - ydirDS + zdir  *  vertexDists[6],  tx, ty, col });

		va->SafeAppend({   drawPos +   xdirDS - ydirDS + zdir  *  vertexDists[6],  tx, ty, col });
		va->SafeAppend({   drawPos -   xdirDS - ydirDS + zdir  *  vertexDists[6],  tx, ty, col });
		va->SafeAppend({owner->pos + (-xdir   - ydir         ) * drawsize * 0.2f,  tx, ty, col2});
	}
	{
		va->SafeAppend({owner->pos + ( xdir   - ydir         ) * drawsize * 0.2f,  tx, ty, col2});
		va->SafeAppend({owner->pos + ( xdir   + ydir         ) * drawsize * 0.2f,  tx, ty, col2});
		va->SafeAppend({   drawPos +   xdirDS + ydirDS + zdir  *  vertexDists[6],  tx, ty, col });

		va->SafeAppend({   drawPos +   xdirDS + ydirDS + zdir  *  vertexDists[6],  tx, ty, col });
		va->SafeAppend({   drawPos +   xdirDS - ydirDS + zdir  *  vertexDists[6],  tx, ty, col });
		va->SafeAppend({owner->pos + ( xdir   - ydir         ) * drawsize * 0.2f,  tx, ty, col2});
	}
	{
		va->SafeAppend({owner->pos + (-xdir   - ydir         ) * drawsize * 0.2f,  tx, ty, col2});
		va->SafeAppend({owner->pos + (-xdir   + ydir         ) * drawsize * 0.2f,  tx, ty, col2});
		va->SafeAppend({   drawPos -   xdirDS + ydirDS + zdir  *  vertexDists[6],  tx, ty, col });

		va->SafeAppend({   drawPos -   xdirDS + ydirDS + zdir  *  vertexDists[6],  tx, ty, col });
		va->SafeAppend({   drawPos -   xdirDS - ydirDS + zdir  *  vertexDists[6],  tx, ty, col });
		va->SafeAppend({owner->pos + (-xdir   - ydir         ) * drawsize * 0.2f,  tx, ty, col2});
	}
}

void CRepulseGfx::Update()
{
	pos = repulsed->pos;

	age += 1;
	deleteMe |= (repulsed != nullptr && owner() != nullptr && (repulsed->pos - owner()->pos).SqLength() > sqMaxOwnerDist);
}