-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathParticle.cpp
124 lines (107 loc) · 3.84 KB
/
Particle.cpp
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
// @Begin License@
// This file is part of Coldest.
//
// Coldest 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.
//
// Coldest 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 Coldest. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2008-2012 Ben Nemec
// @End License@
#include "Particle.h"
#include "globals.h"
Particle::Particle(Mesh& meshin) : playernum(0), id(0), velocity(0.f), accel(0.f),
weight(0.f), radius(0.f), explode(true), lasttick(0), damage(0), dmgrad(0.f),
rewind(0), collide(false), ttl(10000), expired(false), weapid(-1), clientonly(false), tracertime(10000), mesh(meshin),
debug(false), source(new SoundSource)
{
t.start();
}
Particle::Particle(unsigned long nid, Vector3 p, Vector3 v, float vel, float acc, float w,
float rad, bool exp, Uint32 tick, Mesh& meshin) : playernum(0), id(nid),
dir(v), pos(p), origin(p), lasttracer(p), velocity(vel), accel(acc), weight(w), radius(rad), explode(exp),
lasttick(tick), damage(0), dmgrad(0.f), rewind(0), collide(false), ttl(10000), expired(false), weapid(-1),
clientonly(false), tracertime(10000), mesh(meshin), debug(false), source(new SoundSource)
{
dir.normalize();
t.start();
}
Particle::Particle(const string& filename) : playernum(0), id(0),
velocity(0.f), accel(0.f), weight(0.f), radius(0.f), explode(true), lasttick(0), damage(0), dmgrad(0.f),
rewind(0), collide(false), ttl(10000), expired(false), weapid(-1), clientonly(false), tracertime(10000), mesh(meshcache->GetMesh("models/empty")),
debug(false), source(new SoundSource)
{
NTreeReader read(filename);
read.Read(velocity, "Velocity");
read.Read(accel, "Accel");
read.Read(weight, "Weight");
read.Read(radius, "Radius");
read.Read(explode, "Explode");
read.Read(damage, "Damage");
read.Read(dmgrad, "DamageRadius");
read.Read(collide, "Collide");
int temp = 10000;
read.Read(temp, "TTL");
ttl = temp;
read.Read(tracer, "Tracer");
temp = 10000;
read.Read(temp, "TracerTime");
tracertime = temp;
string meshname = "models/empty";
read.Read(meshname, "Mesh");
mesh = meshcache->GetMesh(meshname);
t.start();
string sound;
read.Read(sound, "Sound");
}
Vector3 Particle::Update()
{
Vector3 oldpos = pos;
Uint32 currtick = SDL_GetTicks();
Uint32 interval = currtick - lasttick;
lasttick = currtick;
velocity += accel * float(interval) / 10.f;
dir.y -= weight * float(interval) / 1000.f;
pos += dir * (velocity * interval);
mesh.Move(pos);
#ifndef DEDICATED
source->SetPosition(pos);
#endif
if (ttl >= 0) // A negative ttl means never expire this particle
{
if (t.elapsed() >= Uint32(ttl))
{
expired = true;
}
}
return oldpos;
}
// Note: This function does not actually render the particle, it adds it to a collective
// mesh of all particles which is then rendered
void Particle::Render(Mesh* rendermesh, const Vector3& campos)
{
if (rendermesh)
{
mesh.EnsureMaterials();
mesh.Update(campos);
rendermesh->Add(mesh);
}
else
{
mesh.Update(campos);
}
}
void Particle::PlaySound(const string& filename, ResourceManager& resman)
{
#if !defined(DEDICATED) && !defined(EDITOR)
source = resman.soundman.PlaySound(filename, pos, true);
#endif
}