File: FlareProjectile.cpp

package info (click to toggle)
spring 98.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 41,928 kB
  • ctags: 60,665
  • sloc: cpp: 356,167; ansic: 39,434; python: 12,228; java: 12,203; awk: 5,856; sh: 1,719; xml: 997; perl: 405; php: 253; objc: 194; makefile: 72; sed: 2
file content (132 lines) | stat: -rw-r--r-- 4,298 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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */


#include "FlareProjectile.h"
#include "Game/Camera.h"
#include "Rendering/GlobalRendering.h"
#include "Rendering/ProjectileDrawer.h"
#include "Rendering/GL/VertexArray.h"
#include "Rendering/Textures/TextureAtlas.h"
#include "Sim/Misc/GlobalSynced.h"
#include "Sim/Projectiles/WeaponProjectiles/MissileProjectile.h"
#include "Sim/Units/UnitDef.h"
#include "Sim/Units/Unit.h"

CR_BIND_DERIVED(CFlareProjectile, CProjectile, (ZeroVector, ZeroVector, 0, 0))

CR_REG_METADATA(CFlareProjectile,(
				CR_SETFLAG(CF_Synced),
				CR_MEMBER(activateFrame),
				CR_MEMBER(deathFrame),

				CR_MEMBER(numSub),
				CR_MEMBER(lastSub),
				CR_MEMBER(subPos),
				CR_MEMBER(subSpeed),
				CR_MEMBER(alphaFalloff),
				CR_RESERVED(8)
				))

CFlareProjectile::CFlareProjectile(const float3& pos, const float3& speed, CUnit* owner, int activateFrame):
	//! these are synced, but neither weapon nor piece
	//! (only created by units that can drop flares)
	CProjectile(pos, speed, owner, true, false, false),
	activateFrame(activateFrame),
	deathFrame(activateFrame + (owner? owner->unitDef->flareTime: 1)),
	numSub(0),
	lastSub(0)
{
	alphaFalloff = owner? 1.0f / owner->unitDef->flareTime: 1.0f;
	checkCol = false;
	useAirLos = true;

	SetRadiusAndHeight(45.0f, 0.0f);

	subPos.resize(owner->unitDef->flareSalvoSize);
	subSpeed.resize(owner->unitDef->flareSalvoSize);
	mygravity *= 0.3f; //! flares fall slower
}

CFlareProjectile::~CFlareProjectile()
{
}

void CFlareProjectile::Update()
{
	CUnit* owner = CProjectile::owner();

	if (gs->frameNum == activateFrame) {
		if (owner != NULL) {
			SetPosition(owner->pos);
			CWorldObject::SetVelocity(owner->speed);
			CWorldObject::SetVelocity(speed + (owner->rightdir * owner->unitDef->flareDropVector.x));
			CWorldObject::SetVelocity(speed + (owner->updir    * owner->unitDef->flareDropVector.y));
			CWorldObject::SetVelocity(speed + (owner->frontdir * owner->unitDef->flareDropVector.z));
			SetVelocityAndSpeed(speed);
		} else {
			deleteMe = true;
		}
	}
	if (gs->frameNum >= activateFrame) {
		SetPosition(pos + speed);
		SetVelocityAndSpeed((speed * 0.95f) + (UpVector * mygravity));

		//FIXME: just spawn new flares, if new missiles incoming?
		if(owner && lastSub < (gs->frameNum - owner->unitDef->flareSalvoDelay) && numSub<owner->unitDef->flareSalvoSize) {
			subPos[numSub] = owner->pos;
			float3 s = owner->speed;
			s += owner->rightdir * owner->unitDef->flareDropVector.x;
			s += owner->updir * owner->unitDef->flareDropVector.y;
			s += owner->frontdir * owner->unitDef->flareDropVector.z;
			subSpeed[numSub] = s;
			++numSub;
			lastSub = gs->frameNum;

			for (std::list<CMissileProjectile*>::iterator mi = owner->incomingMissiles.begin(); mi != owner->incomingMissiles.end(); ++mi) {
				if (gs->randFloat() < owner->unitDef->flareEfficiency) {
					CMissileProjectile* missile = *mi;
					missile->SetTargetObject(this);
					missile->AddDeathDependence(this, DEPENDENCE_DECOYTARGET);
				}
			}
		}
		for (int a = 0; a < numSub; ++a) {
			subPos[a] += subSpeed[a];
			subSpeed[a] *= 0.95f;
			subSpeed[a].y += mygravity;
		}
	}

	if (gs->frameNum >= deathFrame) {
		deleteMe = true;
	}
}

void CFlareProjectile::Draw()
{
	if (gs->frameNum <= activateFrame) {
		return;
	}

	inArray = true;
	unsigned char col[4];
	float alpha = std::max(0.0f,1-(gs->frameNum-activateFrame)*alphaFalloff);
	col[0] = (unsigned char) (alpha * 255);
	col[1] = (unsigned char) (alpha * 0.5f) * 255;
	col[2] = (unsigned char) (alpha * 0.2f) * 255;
	col[3] = 1;

	float rad = 6.0;
	va->EnlargeArrays(numSub * 4, 0, VA_SIZE_TC);
	for (int a = 0; a < numSub; ++a) {
		//! CAUTION: loop count must match EnlargeArrays above
		const float3 interPos = subPos[a] + subSpeed[a] * globalRendering->timeOffset;

		#define fpt projectileDrawer->flareprojectiletex
		va->AddVertexQTC(interPos - camera->right * rad - camera->up * rad, fpt->xstart, fpt->ystart, col);
		va->AddVertexQTC(interPos + camera->right * rad - camera->up * rad, fpt->xend,   fpt->ystart, col);
		va->AddVertexQTC(interPos + camera->right * rad + camera->up * rad, fpt->xend,   fpt->yend,   col);
		va->AddVertexQTC(interPos - camera->right * rad + camera->up * rad, fpt->xstart, fpt->yend,   col);
		#undef fpt
	}
}