Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 87cb7bca573735f0a677cbacb1a3d4cfb049cbeb (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
   render/particlesystem.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include "render/gl.h"
#include "render/particlesystem.h"

namespace render {

ParticleSystem::ParticleSystem(const ParticleSystemScript *script, const core::Entity *entity, const model::Particles *modelclass)
{
	// constructor asserts that modelclass->parent == entity->model
	
	particlesystem_modelscale = 1.0f;
	
	particlesystem_entity = entity;
	
	if (modelclass) {
		particlesystem_location.assign(modelclass->location());
		particlesystem_axis.assign(modelclass->axis());
	}
	
	if (entity && entity->model() && entity->model()->radius()) {
		particlesystem_modelscale = entity->radius() / entity->model()->radius();
		particlesystem_location *= particlesystem_modelscale;
		
	}
	
	// add ParticleEjectors as defined by the ParticleSystemScript
	for (ParticleSystemScript::Ejectors::const_iterator it = script->ejectors().begin(); it != script->ejectors().end(); ++it) {
		ParticleEjector *ejector = 0;
		switch ((*it)->type()) {
			case ParticleEjector::Sprite:
				ejector = new ParticleEjectorSprite(*(*it));
				break;
				
			case ParticleEjector::Flare:
				ejector = new ParticleEjectorFlare(*(*it));
				break;
				
			case ParticleEjector::Trail:
				ejector = new ParticleEjectorTrail(*(*it));
				break;
				
			case ParticleEjector::Streak:
				ejector = new ParticleEjectorStreak(*(*it));
				break;
				
			default:
				break;
		}
		
		if (ejector) {
			// safety values
			if (!ejector->interval()) {
				ejector->set_interval(1000);
			}
			
			ejectors().push_back(ejector);
			
			if (entity) {
				if (ejector->entity()) {
					if (ejector->entity_second()) {
						for (size_t i = 0; i < 3; i++) {
							ejector->get_color()[i] = (entity->color()[i] + entity->color_second()[i]) * 0.5f;
						}
					} else {
						ejector->set_color(entity->color());
					}
				} else if (ejector->entity_second()) {
					ejector->set_color(entity->color_second());
				} else if (ejector->engine()) {
					if (entity->model()) {
						ejector->set_color(entity->model()->enginecolor());
					}
				}
			}
			
			if (modelclass) {
				// modelclass overrides script parameters
				if (modelclass->has_color()) {
					ejector->set_color(modelclass->color());
				}
				if (entity) {
					if (modelclass->entity()) {
						if (modelclass->entity_second()) {
							for (size_t i = 0; i < 3; i++) {
								ejector->get_color()[i] = (entity->color()[i] + entity->color_second()[i]) * 0.5f;
							}
						} else {
							ejector->set_color(entity->color());
						}
					} else if (modelclass->entity_second()) {
						ejector->set_color(entity->color_second());
					} else if (modelclass->engine()) {
						if (entity->model()) {
							ejector->set_color(entity->model()->enginecolor());
						}
					}
				}
			}
			
			if (entity && ejector->scaled()) {
				ejector->get_speed_vec() *= entity->radius();
				ejector->get_tailspeed_vec() *= entity->radius();
				ejector->get_radius_vec() *= entity->radius();
				ejector->set_acceleration(ejector->acceleration() * entity->radius());
			}
		}
	}
}

ParticleSystem::~ParticleSystem()
{
	for (Ejectors::iterator it = ejectors().begin(); it != ejectors().end(); ++it) {
		delete (*it);
		(*it) = 0;
	}
	ejectors().clear();
		
}

void ParticleSystem::clear()
{
	for (Ejectors::iterator it = ejectors().begin(); it != ejectors().end(); ++it) {
		(*it)->clear();
	}
}
void ParticleSystem::draw(const float seconds)
{
	// clear particles for docked entities
	if ( entity() && (entity()->type() == core::Entity::Controlable)) {
		const core::EntityControlable *controlable = static_cast<const core::EntityControlable *>(entity());
		if (controlable->state() == core::Entity::Docked) {
			clear();
			return;
		}
	}

	math::Vector3f current_location(entity() ? entity()->location() + (entity()->axis() * location()) : location());
	math::Axis current_axis(entity() ? entity()->axis() * axis() : axis());
	
	for (Ejectors::iterator it = ejectors().begin(); it != ejectors().end(); ++it) {
		
		ParticleEjector *ejector = (*it);
		
		if (entity()) {
			bool ejector_active = true;
			
			if (entity()->type() == core::Entity::Controlable) {
				const core::EntityControlable *controlable = static_cast<const core::EntityControlable *>(entity());
				
				if (ejector->impulse() || ejector->thrust()) {
					ejector_active = false;
				
					switch (controlable->state()) {
						case core::Entity::Impulse:
						case core::Entity::ImpulseInitiate:
							if (ejector->impulse() || ejector->thrust()) {
								ejector_active = true;
							}
							break;
							
						case core::Entity::Normal:
							if (ejector->thrust()) {
								if (controlable->thrust() > 0.0f) {
									ejector_active = true;
								}
							}
							break;
							
						default:
							ejector_active = false;
							break;
					}
				}
				
			} else if (entity()->type() == core::Entity::Dynamic) {
				
				const core::EntityDynamic *dynamic = static_cast<const core::EntityDynamic *>(entity());
				if (dynamic->state() == core::Entity::NoPower) {
					ejector_active = false;
				}
			}
			
			ejector->enable(ejector_active);
			
		}
		
		(*it)->frame(seconds, current_location, current_axis  * ejector->axis());

	}
}

}