2012-12-31 19:33:36 +01:00
|
|
|
/*
|
2013-02-24 18:40:43 +01:00
|
|
|
Minetest
|
2013-02-24 19:38:45 +01:00
|
|
|
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2012-12-31 19:33:36 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "particles.h"
|
|
|
|
#include "constants.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "settings.h"
|
2015-03-05 11:52:57 +01:00
|
|
|
#include "client/tile.h"
|
2012-12-31 19:33:36 +01:00
|
|
|
#include "gamedef.h"
|
|
|
|
#include "collision.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "util/numeric.h"
|
|
|
|
#include "light.h"
|
|
|
|
#include "environment.h"
|
|
|
|
#include "clientmap.h"
|
|
|
|
#include "mapnode.h"
|
2015-01-05 18:34:59 +01:00
|
|
|
#include "client.h"
|
2012-12-31 19:33:36 +01:00
|
|
|
|
2013-01-23 18:32:02 +01:00
|
|
|
/*
|
|
|
|
Utility
|
|
|
|
*/
|
|
|
|
|
|
|
|
v3f random_v3f(v3f min, v3f max)
|
|
|
|
{
|
|
|
|
return v3f( rand()/(float)RAND_MAX*(max.X-min.X)+min.X,
|
|
|
|
rand()/(float)RAND_MAX*(max.Y-min.Y)+min.Y,
|
|
|
|
rand()/(float)RAND_MAX*(max.Z-min.Z)+min.Z);
|
|
|
|
}
|
|
|
|
|
2012-12-31 19:33:36 +01:00
|
|
|
Particle::Particle(
|
|
|
|
IGameDef *gamedef,
|
|
|
|
scene::ISceneManager* smgr,
|
|
|
|
LocalPlayer *player,
|
2015-01-05 18:34:59 +01:00
|
|
|
ClientEnvironment *env,
|
2012-12-31 19:33:36 +01:00
|
|
|
v3f pos,
|
|
|
|
v3f velocity,
|
|
|
|
v3f acceleration,
|
|
|
|
float expirationtime,
|
|
|
|
float size,
|
2013-01-23 18:32:02 +01:00
|
|
|
bool collisiondetection,
|
2016-05-28 06:08:23 +02:00
|
|
|
bool collision_removal,
|
2013-04-22 20:35:10 +02:00
|
|
|
bool vertical,
|
2013-06-24 04:17:50 +02:00
|
|
|
video::ITexture *texture,
|
|
|
|
v2f texpos,
|
2016-11-14 15:28:06 +01:00
|
|
|
v2f texsize
|
2012-12-31 19:33:36 +01:00
|
|
|
):
|
2013-01-23 18:32:02 +01:00
|
|
|
scene::ISceneNode(smgr->getRootSceneNode(), smgr)
|
2012-12-31 19:33:36 +01:00
|
|
|
{
|
|
|
|
// Misc
|
|
|
|
m_gamedef = gamedef;
|
2015-01-05 18:34:59 +01:00
|
|
|
m_env = env;
|
2012-12-31 19:33:36 +01:00
|
|
|
|
|
|
|
// Texture
|
|
|
|
m_material.setFlag(video::EMF_LIGHTING, false);
|
|
|
|
m_material.setFlag(video::EMF_BACK_FACE_CULLING, false);
|
|
|
|
m_material.setFlag(video::EMF_BILINEAR_FILTER, false);
|
|
|
|
m_material.setFlag(video::EMF_FOG_ENABLE, true);
|
2016-11-14 15:28:06 +01:00
|
|
|
m_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
|
2013-06-24 04:17:50 +02:00
|
|
|
m_material.setTexture(0, texture);
|
|
|
|
m_texpos = texpos;
|
|
|
|
m_texsize = texsize;
|
2016-11-14 15:28:06 +01:00
|
|
|
|
2012-12-31 19:33:36 +01:00
|
|
|
|
|
|
|
// Particle related
|
|
|
|
m_pos = pos;
|
|
|
|
m_velocity = velocity;
|
|
|
|
m_acceleration = acceleration;
|
|
|
|
m_expiration = expirationtime;
|
|
|
|
m_time = 0;
|
|
|
|
m_player = player;
|
|
|
|
m_size = size;
|
2013-01-23 18:32:02 +01:00
|
|
|
m_collisiondetection = collisiondetection;
|
2016-05-28 06:08:23 +02:00
|
|
|
m_collision_removal = collision_removal;
|
2013-04-22 20:35:10 +02:00
|
|
|
m_vertical = vertical;
|
2012-12-31 19:33:36 +01:00
|
|
|
|
2013-01-23 18:32:02 +01:00
|
|
|
// Irrlicht stuff
|
2016-02-11 15:21:21 +01:00
|
|
|
m_collisionbox = aabb3f
|
2013-01-23 18:32:02 +01:00
|
|
|
(-size/2,-size/2,-size/2,size/2,size/2,size/2);
|
2012-12-31 19:33:36 +01:00
|
|
|
this->setAutomaticCulling(scene::EAC_OFF);
|
2013-01-23 18:32:02 +01:00
|
|
|
|
|
|
|
// Init lighting
|
2014-01-26 11:40:21 +01:00
|
|
|
updateLight();
|
2013-01-23 18:32:02 +01:00
|
|
|
|
|
|
|
// Init model
|
|
|
|
updateVertices();
|
2012-12-31 19:33:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Particle::~Particle()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Particle::OnRegisterSceneNode()
|
|
|
|
{
|
|
|
|
if (IsVisible)
|
2015-02-26 14:45:31 +01:00
|
|
|
SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT_EFFECT);
|
2012-12-31 19:33:36 +01:00
|
|
|
|
|
|
|
ISceneNode::OnRegisterSceneNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Particle::render()
|
|
|
|
{
|
|
|
|
video::IVideoDriver* driver = SceneManager->getVideoDriver();
|
|
|
|
driver->setMaterial(m_material);
|
|
|
|
driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
|
|
|
|
|
|
|
|
u16 indices[] = {0,1,2, 2,3,0};
|
2013-01-23 18:32:02 +01:00
|
|
|
driver->drawVertexPrimitiveList(m_vertices, 4,
|
|
|
|
indices, 2, video::EVT_STANDARD,
|
|
|
|
scene::EPT_TRIANGLES, video::EIT_16BIT);
|
2012-12-31 19:33:36 +01:00
|
|
|
}
|
|
|
|
|
2014-01-26 11:40:21 +01:00
|
|
|
void Particle::step(float dtime)
|
2012-12-31 19:33:36 +01:00
|
|
|
{
|
|
|
|
m_time += dtime;
|
2016-05-28 06:08:23 +02:00
|
|
|
if (m_collisiondetection) {
|
2016-02-11 15:21:21 +01:00
|
|
|
aabb3f box = m_collisionbox;
|
2016-05-28 06:08:23 +02:00
|
|
|
v3f p_pos = m_pos * BS;
|
|
|
|
v3f p_velocity = m_velocity * BS;
|
|
|
|
collisionMoveResult r = collisionMoveSimple(m_env,
|
|
|
|
m_gamedef, BS * 0.5, box, 0, dtime, &p_pos,
|
|
|
|
&p_velocity, m_acceleration * BS);
|
|
|
|
if (m_collision_removal && r.collides) {
|
|
|
|
// force expiration of the particle
|
|
|
|
m_expiration = -1.0;
|
|
|
|
} else {
|
|
|
|
m_pos = p_pos / BS;
|
|
|
|
m_velocity = p_velocity / BS;
|
|
|
|
}
|
|
|
|
} else {
|
2013-01-23 18:32:02 +01:00
|
|
|
m_velocity += m_acceleration * dtime;
|
|
|
|
m_pos += m_velocity * dtime;
|
|
|
|
}
|
2012-12-31 19:33:36 +01:00
|
|
|
|
|
|
|
// Update lighting
|
2014-01-26 11:40:21 +01:00
|
|
|
updateLight();
|
2013-01-23 18:32:02 +01:00
|
|
|
|
|
|
|
// Update model
|
|
|
|
updateVertices();
|
|
|
|
}
|
|
|
|
|
2014-01-26 11:40:21 +01:00
|
|
|
void Particle::updateLight()
|
2013-01-23 18:32:02 +01:00
|
|
|
{
|
2012-12-31 19:33:36 +01:00
|
|
|
u8 light = 0;
|
2014-11-14 09:05:34 +01:00
|
|
|
bool pos_ok;
|
|
|
|
|
|
|
|
v3s16 p = v3s16(
|
|
|
|
floor(m_pos.X+0.5),
|
|
|
|
floor(m_pos.Y+0.5),
|
|
|
|
floor(m_pos.Z+0.5)
|
|
|
|
);
|
|
|
|
MapNode n = m_env->getClientMap().getNodeNoEx(p, &pos_ok);
|
|
|
|
if (pos_ok)
|
2014-01-26 11:40:21 +01:00
|
|
|
light = n.getLightBlend(m_env->getDayNightRatio(), m_gamedef->ndef());
|
2014-11-14 09:05:34 +01:00
|
|
|
else
|
2014-01-26 11:40:21 +01:00
|
|
|
light = blend_light(m_env->getDayNightRatio(), LIGHT_SUN, 0);
|
2014-11-14 09:05:34 +01:00
|
|
|
|
2016-11-14 15:28:06 +01:00
|
|
|
m_light = decode_light(light);
|
2012-12-31 19:33:36 +01:00
|
|
|
}
|
|
|
|
|
2013-01-23 18:32:02 +01:00
|
|
|
void Particle::updateVertices()
|
|
|
|
{
|
|
|
|
video::SColor c(255, m_light, m_light, m_light);
|
2016-11-14 15:28:06 +01:00
|
|
|
f32 tx0 = m_texpos.X;
|
|
|
|
f32 tx1 = m_texpos.X + m_texsize.X;
|
|
|
|
f32 ty0 = m_texpos.Y;
|
|
|
|
f32 ty1 = m_texpos.Y + m_texsize.Y;
|
2013-06-24 04:17:50 +02:00
|
|
|
|
2013-01-23 18:32:02 +01:00
|
|
|
m_vertices[0] = video::S3DVertex(-m_size/2,-m_size/2,0, 0,0,0,
|
2013-06-24 04:17:50 +02:00
|
|
|
c, tx0, ty1);
|
2013-01-23 18:32:02 +01:00
|
|
|
m_vertices[1] = video::S3DVertex(m_size/2,-m_size/2,0, 0,0,0,
|
2013-06-24 04:17:50 +02:00
|
|
|
c, tx1, ty1);
|
2013-01-23 18:32:02 +01:00
|
|
|
m_vertices[2] = video::S3DVertex(m_size/2,m_size/2,0, 0,0,0,
|
2013-06-24 04:17:50 +02:00
|
|
|
c, tx1, ty0);
|
2013-01-23 18:32:02 +01:00
|
|
|
m_vertices[3] = video::S3DVertex(-m_size/2,m_size/2,0, 0,0,0,
|
2013-06-24 04:17:50 +02:00
|
|
|
c, tx0, ty0);
|
2013-01-23 18:32:02 +01:00
|
|
|
|
2014-01-26 11:40:21 +01:00
|
|
|
v3s16 camera_offset = m_env->getCameraOffset();
|
2013-01-23 18:32:02 +01:00
|
|
|
for(u16 i=0; i<4; i++)
|
|
|
|
{
|
2013-04-22 20:35:10 +02:00
|
|
|
if (m_vertical) {
|
|
|
|
v3f ppos = m_player->getPosition()/BS;
|
|
|
|
m_vertices[i].Pos.rotateXZBy(atan2(ppos.Z-m_pos.Z, ppos.X-m_pos.X)/core::DEGTORAD+90);
|
|
|
|
} else {
|
|
|
|
m_vertices[i].Pos.rotateYZBy(m_player->getPitch());
|
|
|
|
m_vertices[i].Pos.rotateXZBy(m_player->getYaw());
|
|
|
|
}
|
2013-01-23 18:32:02 +01:00
|
|
|
m_box.addInternalPoint(m_vertices[i].Pos);
|
2014-01-26 11:40:21 +01:00
|
|
|
m_vertices[i].Pos += m_pos*BS - intToFloat(camera_offset, BS);
|
2013-01-23 18:32:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
ParticleSpawner
|
|
|
|
*/
|
|
|
|
|
|
|
|
ParticleSpawner::ParticleSpawner(IGameDef* gamedef, scene::ISceneManager *smgr, LocalPlayer *player,
|
|
|
|
u16 amount, float time,
|
|
|
|
v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc,
|
|
|
|
float minexptime, float maxexptime, float minsize, float maxsize,
|
2016-08-04 22:09:21 +02:00
|
|
|
bool collisiondetection, bool collision_removal, u16 attached_id, bool vertical,
|
2016-11-14 15:28:06 +01:00
|
|
|
video::ITexture *texture, u32 id, ParticleManager *p_manager) :
|
2015-01-05 18:34:59 +01:00
|
|
|
m_particlemanager(p_manager)
|
2013-01-23 18:32:02 +01:00
|
|
|
{
|
|
|
|
m_gamedef = gamedef;
|
|
|
|
m_smgr = smgr;
|
|
|
|
m_player = player;
|
|
|
|
m_amount = amount;
|
|
|
|
m_spawntime = time;
|
|
|
|
m_minpos = minpos;
|
|
|
|
m_maxpos = maxpos;
|
|
|
|
m_minvel = minvel;
|
|
|
|
m_maxvel = maxvel;
|
|
|
|
m_minacc = minacc;
|
|
|
|
m_maxacc = maxacc;
|
|
|
|
m_minexptime = minexptime;
|
|
|
|
m_maxexptime = maxexptime;
|
|
|
|
m_minsize = minsize;
|
|
|
|
m_maxsize = maxsize;
|
|
|
|
m_collisiondetection = collisiondetection;
|
2016-05-28 06:08:23 +02:00
|
|
|
m_collision_removal = collision_removal;
|
2016-08-04 22:09:21 +02:00
|
|
|
m_attached_id = attached_id;
|
2013-04-22 20:35:10 +02:00
|
|
|
m_vertical = vertical;
|
2013-06-24 04:17:50 +02:00
|
|
|
m_texture = texture;
|
2013-01-23 18:32:02 +01:00
|
|
|
m_time = 0;
|
|
|
|
|
|
|
|
for (u16 i = 0; i<=m_amount; i++)
|
|
|
|
{
|
|
|
|
float spawntime = (float)rand()/(float)RAND_MAX*m_spawntime;
|
|
|
|
m_spawntimes.push_back(spawntime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ParticleSpawner::~ParticleSpawner() {}
|
|
|
|
|
2015-01-05 18:34:59 +01:00
|
|
|
void ParticleSpawner::step(float dtime, ClientEnvironment* env)
|
2013-01-23 18:32:02 +01:00
|
|
|
{
|
|
|
|
m_time += dtime;
|
2016-11-14 15:28:06 +01:00
|
|
|
|
2016-08-04 22:09:21 +02:00
|
|
|
bool unloaded = false;
|
2016-10-24 19:56:27 +02:00
|
|
|
bool is_attached = false;
|
|
|
|
v3f attached_pos = v3f(0,0,0);
|
|
|
|
float attached_yaw = 0;
|
2016-08-04 22:09:21 +02:00
|
|
|
if (m_attached_id != 0) {
|
2016-10-24 19:56:27 +02:00
|
|
|
if (ClientActiveObject *attached = env->getActiveObject(m_attached_id)) {
|
|
|
|
attached_pos = attached->getPosition() / BS;
|
|
|
|
attached_yaw = attached->getYaw();
|
|
|
|
is_attached = true;
|
|
|
|
} else {
|
2016-08-04 22:09:21 +02:00
|
|
|
unloaded = true;
|
2016-10-24 19:56:27 +02:00
|
|
|
}
|
2016-08-04 22:09:21 +02:00
|
|
|
}
|
|
|
|
|
2013-01-23 18:32:02 +01:00
|
|
|
if (m_spawntime != 0) // Spawner exists for a predefined timespan
|
|
|
|
{
|
|
|
|
for(std::vector<float>::iterator i = m_spawntimes.begin();
|
|
|
|
i != m_spawntimes.end();)
|
|
|
|
{
|
|
|
|
if ((*i) <= m_time && m_amount > 0)
|
|
|
|
{
|
|
|
|
m_amount--;
|
|
|
|
|
2016-08-04 22:09:21 +02:00
|
|
|
// Pretend to, but don't actually spawn a
|
|
|
|
// particle if it is attached to an unloaded
|
|
|
|
// object.
|
|
|
|
if (!unloaded) {
|
2016-10-25 17:12:50 +02:00
|
|
|
v3f pos = random_v3f(m_minpos, m_maxpos);
|
2016-08-04 22:09:21 +02:00
|
|
|
v3f vel = random_v3f(m_minvel, m_maxvel);
|
|
|
|
v3f acc = random_v3f(m_minacc, m_maxacc);
|
2016-10-24 19:56:27 +02:00
|
|
|
|
|
|
|
if (is_attached) {
|
|
|
|
// Apply attachment yaw and position
|
|
|
|
pos.rotateXZBy(attached_yaw);
|
|
|
|
pos += attached_pos;
|
|
|
|
vel.rotateXZBy(attached_yaw);
|
|
|
|
acc.rotateXZBy(attached_yaw);
|
|
|
|
}
|
|
|
|
|
2016-08-04 22:09:21 +02:00
|
|
|
float exptime = rand()/(float)RAND_MAX
|
|
|
|
*(m_maxexptime-m_minexptime)
|
|
|
|
+m_minexptime;
|
|
|
|
float size = rand()/(float)RAND_MAX
|
|
|
|
*(m_maxsize-m_minsize)
|
|
|
|
+m_minsize;
|
2016-11-14 15:28:06 +01:00
|
|
|
|
2016-08-04 22:09:21 +02:00
|
|
|
Particle* toadd = new Particle(
|
|
|
|
m_gamedef,
|
|
|
|
m_smgr,
|
|
|
|
m_player,
|
|
|
|
env,
|
|
|
|
pos,
|
|
|
|
vel,
|
|
|
|
acc,
|
|
|
|
exptime,
|
|
|
|
size,
|
|
|
|
m_collisiondetection,
|
|
|
|
m_collision_removal,
|
|
|
|
m_vertical,
|
|
|
|
m_texture,
|
|
|
|
v2f(0.0, 0.0),
|
2016-11-14 15:28:06 +01:00
|
|
|
v2f(1.0, 1.0));
|
2016-08-04 22:09:21 +02:00
|
|
|
m_particlemanager->addParticle(toadd);
|
|
|
|
}
|
2013-12-23 15:11:53 +01:00
|
|
|
i = m_spawntimes.erase(i);
|
2013-01-23 18:32:02 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-25 22:23:05 +02:00
|
|
|
++i;
|
2013-01-23 18:32:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // Spawner exists for an infinity timespan, spawn on a per-second base
|
|
|
|
{
|
2016-08-04 22:09:21 +02:00
|
|
|
// Skip this step if attached to an unloaded object
|
|
|
|
if (unloaded)
|
|
|
|
return;
|
2013-01-23 18:32:02 +01:00
|
|
|
for (int i = 0; i <= m_amount; i++)
|
|
|
|
{
|
|
|
|
if (rand()/(float)RAND_MAX < dtime)
|
|
|
|
{
|
2016-10-24 19:56:27 +02:00
|
|
|
v3f pos = random_v3f(m_minpos, m_maxpos);
|
2013-01-23 18:32:02 +01:00
|
|
|
v3f vel = random_v3f(m_minvel, m_maxvel);
|
|
|
|
v3f acc = random_v3f(m_minacc, m_maxacc);
|
2016-10-24 19:56:27 +02:00
|
|
|
|
|
|
|
if (is_attached) {
|
|
|
|
// Apply attachment yaw and position
|
|
|
|
pos.rotateXZBy(attached_yaw);
|
|
|
|
pos += attached_pos;
|
|
|
|
vel.rotateXZBy(attached_yaw);
|
|
|
|
acc.rotateXZBy(attached_yaw);
|
|
|
|
}
|
|
|
|
|
2013-01-23 18:32:02 +01:00
|
|
|
float exptime = rand()/(float)RAND_MAX
|
|
|
|
*(m_maxexptime-m_minexptime)
|
|
|
|
+m_minexptime;
|
|
|
|
float size = rand()/(float)RAND_MAX
|
|
|
|
*(m_maxsize-m_minsize)
|
|
|
|
+m_minsize;
|
2016-11-14 15:28:06 +01:00
|
|
|
|
2015-04-05 22:02:01 +02:00
|
|
|
Particle* toadd = new Particle(
|
2013-01-23 18:32:02 +01:00
|
|
|
m_gamedef,
|
|
|
|
m_smgr,
|
|
|
|
m_player,
|
|
|
|
env,
|
|
|
|
pos,
|
|
|
|
vel,
|
|
|
|
acc,
|
|
|
|
exptime,
|
|
|
|
size,
|
|
|
|
m_collisiondetection,
|
2016-05-28 06:08:23 +02:00
|
|
|
m_collision_removal,
|
2013-04-22 20:35:10 +02:00
|
|
|
m_vertical,
|
2013-06-24 04:17:50 +02:00
|
|
|
m_texture,
|
|
|
|
v2f(0.0, 0.0),
|
2016-11-14 15:28:06 +01:00
|
|
|
v2f(1.0, 1.0));
|
2015-04-05 22:02:01 +02:00
|
|
|
m_particlemanager->addParticle(toadd);
|
2013-01-23 18:32:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 18:34:59 +01:00
|
|
|
|
|
|
|
ParticleManager::ParticleManager(ClientEnvironment* env) :
|
|
|
|
m_env(env)
|
|
|
|
{}
|
|
|
|
|
|
|
|
ParticleManager::~ParticleManager()
|
|
|
|
{
|
|
|
|
clearAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleManager::step(float dtime)
|
2013-01-23 18:32:02 +01:00
|
|
|
{
|
2015-01-05 18:34:59 +01:00
|
|
|
stepParticles (dtime);
|
|
|
|
stepSpawners (dtime);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleManager::stepSpawners (float dtime)
|
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock lock(m_spawner_list_lock);
|
2016-03-30 00:06:47 +02:00
|
|
|
for (std::map<u32, ParticleSpawner*>::iterator i =
|
2015-01-05 18:34:59 +01:00
|
|
|
m_particle_spawners.begin();
|
|
|
|
i != m_particle_spawners.end();)
|
2013-01-23 18:32:02 +01:00
|
|
|
{
|
|
|
|
if (i->second->get_expired())
|
|
|
|
{
|
|
|
|
delete i->second;
|
2015-01-05 18:34:59 +01:00
|
|
|
m_particle_spawners.erase(i++);
|
2013-01-23 18:32:02 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-05 18:34:59 +01:00
|
|
|
i->second->step(dtime, m_env);
|
2015-08-25 22:23:05 +02:00
|
|
|
++i;
|
2013-01-23 18:32:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 18:34:59 +01:00
|
|
|
void ParticleManager::stepParticles (float dtime)
|
2013-01-23 18:32:02 +01:00
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock lock(m_particle_list_lock);
|
2015-01-05 18:34:59 +01:00
|
|
|
for(std::vector<Particle*>::iterator i = m_particles.begin();
|
|
|
|
i != m_particles.end();)
|
2013-01-23 18:32:02 +01:00
|
|
|
{
|
2015-01-05 18:34:59 +01:00
|
|
|
if ((*i)->get_expired())
|
|
|
|
{
|
|
|
|
(*i)->remove();
|
|
|
|
delete *i;
|
|
|
|
i = m_particles.erase(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
(*i)->step(dtime);
|
2015-08-25 22:23:05 +02:00
|
|
|
++i;
|
2015-01-05 18:34:59 +01:00
|
|
|
}
|
2013-01-23 18:32:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 18:34:59 +01:00
|
|
|
void ParticleManager::clearAll ()
|
2013-01-23 18:32:02 +01:00
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock lock(m_spawner_list_lock);
|
|
|
|
MutexAutoLock lock2(m_particle_list_lock);
|
2013-01-23 18:32:02 +01:00
|
|
|
for(std::map<u32, ParticleSpawner*>::iterator i =
|
2015-01-05 18:34:59 +01:00
|
|
|
m_particle_spawners.begin();
|
|
|
|
i != m_particle_spawners.end();)
|
2013-01-23 18:32:02 +01:00
|
|
|
{
|
|
|
|
delete i->second;
|
2015-01-05 18:34:59 +01:00
|
|
|
m_particle_spawners.erase(i++);
|
2013-01-23 18:32:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for(std::vector<Particle*>::iterator i =
|
2015-01-05 18:34:59 +01:00
|
|
|
m_particles.begin();
|
|
|
|
i != m_particles.end();)
|
2013-01-23 18:32:02 +01:00
|
|
|
{
|
|
|
|
(*i)->remove();
|
|
|
|
delete *i;
|
2015-01-05 18:34:59 +01:00
|
|
|
i = m_particles.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleManager::handleParticleEvent(ClientEvent *event, IGameDef *gamedef,
|
|
|
|
scene::ISceneManager* smgr, LocalPlayer *player)
|
|
|
|
{
|
2016-03-30 00:06:47 +02:00
|
|
|
switch (event->type) {
|
|
|
|
case CE_DELETE_PARTICLESPAWNER: {
|
|
|
|
MutexAutoLock lock(m_spawner_list_lock);
|
|
|
|
if (m_particle_spawners.find(event->delete_particlespawner.id) !=
|
|
|
|
m_particle_spawners.end()) {
|
|
|
|
delete m_particle_spawners.find(event->delete_particlespawner.id)->second;
|
|
|
|
m_particle_spawners.erase(event->delete_particlespawner.id);
|
|
|
|
}
|
|
|
|
// no allocated memory in delete event
|
|
|
|
break;
|
2015-01-05 18:34:59 +01:00
|
|
|
}
|
2016-03-30 00:06:47 +02:00
|
|
|
case CE_ADD_PARTICLESPAWNER: {
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(m_spawner_list_lock);
|
|
|
|
if (m_particle_spawners.find(event->add_particlespawner.id) !=
|
|
|
|
m_particle_spawners.end()) {
|
|
|
|
delete m_particle_spawners.find(event->add_particlespawner.id)->second;
|
|
|
|
m_particle_spawners.erase(event->add_particlespawner.id);
|
|
|
|
}
|
|
|
|
}
|
2015-01-05 18:34:59 +01:00
|
|
|
|
2016-03-30 00:06:47 +02:00
|
|
|
video::ITexture *texture =
|
|
|
|
gamedef->tsrc()->getTextureForMesh(*(event->add_particlespawner.texture));
|
|
|
|
|
|
|
|
ParticleSpawner* toadd = new ParticleSpawner(gamedef, smgr, player,
|
|
|
|
event->add_particlespawner.amount,
|
|
|
|
event->add_particlespawner.spawntime,
|
|
|
|
*event->add_particlespawner.minpos,
|
|
|
|
*event->add_particlespawner.maxpos,
|
|
|
|
*event->add_particlespawner.minvel,
|
|
|
|
*event->add_particlespawner.maxvel,
|
|
|
|
*event->add_particlespawner.minacc,
|
|
|
|
*event->add_particlespawner.maxacc,
|
|
|
|
event->add_particlespawner.minexptime,
|
|
|
|
event->add_particlespawner.maxexptime,
|
|
|
|
event->add_particlespawner.minsize,
|
|
|
|
event->add_particlespawner.maxsize,
|
|
|
|
event->add_particlespawner.collisiondetection,
|
2016-05-28 06:08:23 +02:00
|
|
|
event->add_particlespawner.collision_removal,
|
2016-08-04 22:09:21 +02:00
|
|
|
event->add_particlespawner.attached_id,
|
2016-03-30 00:06:47 +02:00
|
|
|
event->add_particlespawner.vertical,
|
|
|
|
texture,
|
|
|
|
event->add_particlespawner.id,
|
|
|
|
this);
|
|
|
|
|
|
|
|
/* delete allocated content of event */
|
|
|
|
delete event->add_particlespawner.minpos;
|
|
|
|
delete event->add_particlespawner.maxpos;
|
|
|
|
delete event->add_particlespawner.minvel;
|
|
|
|
delete event->add_particlespawner.maxvel;
|
|
|
|
delete event->add_particlespawner.minacc;
|
|
|
|
delete event->add_particlespawner.texture;
|
|
|
|
delete event->add_particlespawner.maxacc;
|
2015-01-05 18:34:59 +01:00
|
|
|
|
|
|
|
{
|
2016-03-30 00:06:47 +02:00
|
|
|
MutexAutoLock lock(m_spawner_list_lock);
|
|
|
|
m_particle_spawners.insert(
|
|
|
|
std::pair<u32, ParticleSpawner*>(
|
|
|
|
event->add_particlespawner.id,
|
|
|
|
toadd));
|
2015-01-05 18:34:59 +01:00
|
|
|
}
|
2016-03-30 00:06:47 +02:00
|
|
|
break;
|
2015-01-05 18:34:59 +01:00
|
|
|
}
|
2016-03-30 00:06:47 +02:00
|
|
|
case CE_SPAWN_PARTICLE: {
|
|
|
|
video::ITexture *texture =
|
|
|
|
gamedef->tsrc()->getTextureForMesh(*(event->spawn_particle.texture));
|
|
|
|
|
|
|
|
Particle* toadd = new Particle(gamedef, smgr, player, m_env,
|
|
|
|
*event->spawn_particle.pos,
|
|
|
|
*event->spawn_particle.vel,
|
|
|
|
*event->spawn_particle.acc,
|
|
|
|
event->spawn_particle.expirationtime,
|
|
|
|
event->spawn_particle.size,
|
|
|
|
event->spawn_particle.collisiondetection,
|
2016-05-28 06:08:23 +02:00
|
|
|
event->spawn_particle.collision_removal,
|
2016-03-30 00:06:47 +02:00
|
|
|
event->spawn_particle.vertical,
|
|
|
|
texture,
|
|
|
|
v2f(0.0, 0.0),
|
2016-11-14 15:28:06 +01:00
|
|
|
v2f(1.0, 1.0));
|
2015-01-05 18:34:59 +01:00
|
|
|
|
2016-03-30 00:06:47 +02:00
|
|
|
addParticle(toadd);
|
2015-01-05 18:34:59 +01:00
|
|
|
|
2016-03-30 00:06:47 +02:00
|
|
|
delete event->spawn_particle.pos;
|
|
|
|
delete event->spawn_particle.vel;
|
|
|
|
delete event->spawn_particle.acc;
|
2015-01-05 18:34:59 +01:00
|
|
|
|
2016-03-30 00:06:47 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: break;
|
2013-12-23 15:11:53 +01:00
|
|
|
}
|
2012-12-31 19:33:36 +01:00
|
|
|
}
|
2015-01-05 18:34:59 +01:00
|
|
|
|
|
|
|
void ParticleManager::addDiggingParticles(IGameDef* gamedef, scene::ISceneManager* smgr,
|
|
|
|
LocalPlayer *player, v3s16 pos, const TileSpec tiles[])
|
|
|
|
{
|
|
|
|
for (u16 j = 0; j < 32; j++) // set the amount of particles here
|
|
|
|
{
|
|
|
|
addNodeParticle(gamedef, smgr, player, pos, tiles);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleManager::addPunchingParticles(IGameDef* gamedef, scene::ISceneManager* smgr,
|
|
|
|
LocalPlayer *player, v3s16 pos, const TileSpec tiles[])
|
|
|
|
{
|
|
|
|
addNodeParticle(gamedef, smgr, player, pos, tiles);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleManager::addNodeParticle(IGameDef* gamedef, scene::ISceneManager* smgr,
|
|
|
|
LocalPlayer *player, v3s16 pos, const TileSpec tiles[])
|
|
|
|
{
|
|
|
|
// Texture
|
|
|
|
u8 texid = myrand_range(0, 5);
|
|
|
|
video::ITexture *texture = tiles[texid].texture;
|
|
|
|
|
|
|
|
// Only use first frame of animated texture
|
|
|
|
f32 ymax = 1;
|
|
|
|
if(tiles[texid].material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES)
|
|
|
|
ymax /= tiles[texid].animation_frame_count;
|
|
|
|
|
|
|
|
float size = rand() % 64 / 512.;
|
|
|
|
float visual_size = BS * size;
|
|
|
|
v2f texsize(size * 2, ymax * size * 2);
|
|
|
|
v2f texpos;
|
|
|
|
texpos.X = ((rand() % 64) / 64. - texsize.X);
|
|
|
|
texpos.Y = ymax * ((rand() % 64) / 64. - texsize.Y);
|
|
|
|
|
|
|
|
// Physics
|
|
|
|
v3f velocity((rand() % 100 / 50. - 1) / 1.5,
|
|
|
|
rand() % 100 / 35.,
|
|
|
|
(rand() % 100 / 50. - 1) / 1.5);
|
|
|
|
|
|
|
|
v3f acceleration(0,-9,0);
|
|
|
|
v3f particlepos = v3f(
|
|
|
|
(f32) pos.X + rand() %100 /200. - 0.25,
|
|
|
|
(f32) pos.Y + rand() %100 /200. - 0.25,
|
|
|
|
(f32) pos.Z + rand() %100 /200. - 0.25
|
|
|
|
);
|
|
|
|
|
|
|
|
Particle* toadd = new Particle(
|
|
|
|
gamedef,
|
|
|
|
smgr,
|
|
|
|
player,
|
|
|
|
m_env,
|
|
|
|
particlepos,
|
|
|
|
velocity,
|
|
|
|
acceleration,
|
|
|
|
rand() % 100 / 100., // expiration time
|
|
|
|
visual_size,
|
|
|
|
true,
|
|
|
|
false,
|
2016-05-28 06:08:23 +02:00
|
|
|
false,
|
2015-01-05 18:34:59 +01:00
|
|
|
texture,
|
|
|
|
texpos,
|
2016-11-14 15:28:06 +01:00
|
|
|
texsize);
|
2015-01-05 18:34:59 +01:00
|
|
|
|
|
|
|
addParticle(toadd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleManager::addParticle(Particle* toadd)
|
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock lock(m_particle_list_lock);
|
2015-01-05 18:34:59 +01:00
|
|
|
m_particles.push_back(toadd);
|
|
|
|
}
|