File: ParticleProperties.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-3
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,740 kB
  • sloc: cpp: 595,005; ansic: 21,741; python: 1,174; sh: 457; makefile: 243; xml: 181
file content (161 lines) | stat: -rw-r--r-- 4,851 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

#include "particle/util/ParticleProperties.h"
#include "particle/ParticleManager.h"

#include "bmpman/bmpman.h"
#include "math/curve.h"
#include "utils/RandomRange.h"

namespace particle {
namespace util {
ParticleProperties::ParticleProperties() : m_radius(::util::UniformFloatRange(0.0f, 1.0f)), m_lifetime(::util::UniformFloatRange(0.0f, 1.0f)), m_length(::util::UniformFloatRange(0.0f)), m_size_lifetime_curve(-1), m_vel_lifetime_curve (-1), m_rotation_type(RotationType::DEFAULT), m_manual_offset (vmd_zero_vector) {
}

void ParticleProperties::parse(bool nocreate) {
	if (internal::required_string_if_new("+Filename:", nocreate)) {
		m_bitmap_list = internal::parseAnimationList(true);
		m_bitmap_range = ::util::UniformRange<size_t>(0, m_bitmap_list.size() - 1);
	}

	if (optional_string("+Size:")) {
		m_radius = ::util::ParsedRandomFloatRange::parseRandomRange();
	} else if (optional_string("+Parent Size Factor:")) {
		m_radius = ::util::ParsedRandomFloatRange::parseRandomRange();
		m_parentScale = true;
	} else if (!nocreate) {
		error_display(1, "Missing +Size or +Parent Size Factor");
	}

	if (optional_string("+Length:")) {
		m_length = ::util::ParsedRandomFloatRange::parseRandomRange();
	}

	if (optional_string("+Lifetime:")) {
		if (optional_string("<none>")) {
			// Use lifetime of effect
			m_hasLifetime = false;
		} else {
			m_hasLifetime = true;
			m_lifetime = ::util::ParsedRandomFloatRange::parseRandomRange();
		}
	} else if (optional_string("+Parent Lifetime Factor:")) {
		m_hasLifetime = true;
		m_parentLifetime = true;
		m_lifetime = ::util::ParsedRandomFloatRange::parseRandomRange();
	}

	if (optional_string("+Size over lifetime curve:")) {
		SCP_string buf;
		stuff_string(buf, F_NAME);
		m_size_lifetime_curve = curve_get_by_name(buf);

		if (m_size_lifetime_curve < 0) {
			error_display(0, "Could not find curve '%s'", buf.c_str());
		}
	}

	if (optional_string("+Velocity scalar over lifetime curve:")) {
		SCP_string buf;
		stuff_string(buf, F_NAME);
		m_vel_lifetime_curve = curve_get_by_name(buf);

		if (m_vel_lifetime_curve < 0) {
			error_display(0, "Could not find curve '%s'", buf.c_str());
		}
	}

	if (optional_string("+Rotation:")) {
		char buf[NAME_LENGTH];
		stuff_string(buf, F_NAME, NAME_LENGTH);
		if (!stricmp(buf, "DEFAULT")) {
			m_rotation_type = RotationType::DEFAULT;
		} else if (!stricmp(buf, "RANDOM")) {
			m_rotation_type = RotationType::RANDOM;
		} else if (!stricmp(buf, "SCREEN_ALIGNED") || !stricmp(buf, "SCREEN-ALIGNED") || !stricmp(buf, "SCREEN ALIGNED")) {
			m_rotation_type = RotationType::SCREEN_ALIGNED;
		} else {
			// in the future we may want to support additional types, or even a specific angle, but that is TBD
			error_display(0, "Rotation Type %s not supported", buf);
		}
	}

	if (optional_string("+Offset:")) {
		stuff_vec3d(&m_manual_offset);
	}

	if (optional_string("+Remain local to parent:")) {
		stuff_boolean(&m_parent_local);
	}
}

int ParticleProperties::chooseBitmap()
{
	// failsafe check, though we should not have been able to get to this point
	Assertion(!m_bitmap_list.empty(), "No bitmaps found!");

	return m_bitmap_list[m_bitmap_range.next()];
}

void ParticleProperties::createParticle(particle_info& info) {
	info.optional_data = ParticleProperties::chooseBitmap();
	info.type = PARTICLE_BITMAP;
	if (m_parentScale)
		// if we were spawned by a particle, info.rad is the parent's radius and m_radius is a factor of that
		info.rad *= m_radius.next(); 
	else
		info.rad = m_radius.next();
	info.length = m_length.next();
	if (m_hasLifetime) {
		if (m_parentLifetime)
			// if we were spawned by a particle, info.lifetime is the parent's remaining liftime and m_lifetime is a factor of that
			info.lifetime *= m_lifetime.next();
		else
			info.lifetime = m_lifetime.next();
		info.lifetime_from_animation = false;
	}
	info.size_lifetime_curve = m_size_lifetime_curve;
	info.vel_lifetime_curve = m_vel_lifetime_curve;

	switch (m_rotation_type) {
		case RotationType::DEFAULT:
			info.use_angle = Randomize_particle_rotation;
			break;
		case RotationType::RANDOM:
			info.use_angle = true;
			break;
		case RotationType::SCREEN_ALIGNED:
			info.use_angle = false;
			break;
		default:
			UNREACHABLE("Rotation type not supported");
	}

	create(&info);
}

WeakParticlePtr ParticleProperties::createPersistentParticle(particle_info& info) {
	info.optional_data = ParticleProperties::chooseBitmap();
	info.type = PARTICLE_BITMAP;
	info.rad = m_radius.next();
	info.length = m_length.next();
	info.size_lifetime_curve = m_size_lifetime_curve;
	info.vel_lifetime_curve = m_vel_lifetime_curve;

	auto p = createPersistent(&info);

	if (m_hasLifetime && !p.expired()) {
		p.lock()->max_life = m_lifetime.next();
	}

	return p;
}

void ParticleProperties::pageIn() {

	for (int bitmap : m_bitmap_list) {
		bm_page_in_texture(bitmap);
	}

}
}
}