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"
|
2017-01-09 20:39:22 +01:00
|
|
|
#include "client.h"
|
2012-12-31 19:33:36 +01:00
|
|
|
#include "collision.h"
|
2017-08-28 20:02:23 +02:00
|
|
|
#include "client/clientevent.h"
|
2017-06-26 20:11:17 +02:00
|
|
|
#include "client/renderingengine.h"
|
2012-12-31 19:33:36 +01:00
|
|
|
#include "util/numeric.h"
|
|
|
|
#include "light.h"
|
|
|
|
#include "environment.h"
|
|
|
|
#include "clientmap.h"
|
|
|
|
#include "mapnode.h"
|
2017-08-18 19:25:07 +02:00
|
|
|
#include "nodedef.h"
|
2015-01-05 18:34:59 +01:00
|
|
|
#include "client.h"
|
2017-05-20 16:46:35 +02:00
|
|
|
#include "settings.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,
|
|
|
|
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,
|
2017-01-14 16:48:49 +01:00
|
|
|
v2f texsize,
|
|
|
|
const struct TileAnimationParams &anim,
|
2017-01-12 15:46:30 +01:00
|
|
|
u8 glow,
|
|
|
|
video::SColor color
|
2012-12-31 19:33:36 +01:00
|
|
|
):
|
2017-06-26 20:11:17 +02:00
|
|
|
scene::ISceneNode(RenderingEngine::get_scene_manager()->getRootSceneNode(),
|
|
|
|
RenderingEngine::get_scene_manager())
|
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;
|
2017-01-14 16:48:49 +01:00
|
|
|
m_animation = anim;
|
2012-12-31 19:33:36 +01:00
|
|
|
|
2017-01-12 15:46:30 +01:00
|
|
|
// Color
|
|
|
|
m_base_color = color;
|
|
|
|
m_color = color;
|
|
|
|
|
2012-12-31 19:33:36 +01:00
|
|
|
// Particle related
|
|
|
|
m_pos = pos;
|
|
|
|
m_velocity = velocity;
|
|
|
|
m_acceleration = acceleration;
|
|
|
|
m_expiration = expirationtime;
|
|
|
|
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;
|
2017-01-14 16:48:49 +01:00
|
|
|
m_glow = glow;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2017-01-14 16:48:49 +01:00
|
|
|
if (m_animation.type != TAT_NONE) {
|
|
|
|
m_animation_time += dtime;
|
|
|
|
int frame_length_i, frame_count;
|
|
|
|
m_animation.determineParams(
|
|
|
|
m_material.getTexture(0)->getSize(),
|
|
|
|
&frame_count, &frame_length_i, NULL);
|
|
|
|
float frame_length = frame_length_i / 1000.0;
|
|
|
|
while (m_animation_time > frame_length) {
|
|
|
|
m_animation_frame++;
|
|
|
|
m_animation_time -= frame_length;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2017-01-12 15:46:30 +01:00
|
|
|
u8 m_light = decode_light(light + m_glow);
|
|
|
|
m_color.set(255,
|
|
|
|
m_light * m_base_color.getRed() / 255,
|
|
|
|
m_light * m_base_color.getGreen() / 255,
|
|
|
|
m_light * m_base_color.getBlue() / 255);
|
2012-12-31 19:33:36 +01:00
|
|
|
}
|
|
|
|
|
2013-01-23 18:32:02 +01:00
|
|
|
void Particle::updateVertices()
|
|
|
|
{
|
2017-01-14 16:48:49 +01:00
|
|
|
f32 tx0, tx1, ty0, ty1;
|
|
|
|
|
|
|
|
if (m_animation.type != TAT_NONE) {
|
|
|
|
const v2u32 texsize = m_material.getTexture(0)->getSize();
|
|
|
|
v2f texcoord, framesize_f;
|
|
|
|
v2u32 framesize;
|
|
|
|
texcoord = m_animation.getTextureCoords(texsize, m_animation_frame);
|
|
|
|
m_animation.determineParams(texsize, NULL, NULL, &framesize);
|
|
|
|
framesize_f = v2f(framesize.X / (float) texsize.X, framesize.Y / (float) texsize.Y);
|
|
|
|
|
|
|
|
tx0 = m_texpos.X + texcoord.X;
|
|
|
|
tx1 = m_texpos.X + texcoord.X + framesize_f.X * m_texsize.X;
|
|
|
|
ty0 = m_texpos.Y + texcoord.Y;
|
|
|
|
ty1 = m_texpos.Y + texcoord.Y + framesize_f.Y * m_texsize.Y;
|
|
|
|
} else {
|
|
|
|
tx0 = m_texpos.X;
|
|
|
|
tx1 = m_texpos.X + m_texsize.X;
|
|
|
|
ty0 = m_texpos.Y;
|
|
|
|
ty1 = m_texpos.Y + m_texsize.Y;
|
|
|
|
}
|
2013-06-24 04:17:50 +02:00
|
|
|
|
2017-01-12 15:46:30 +01:00
|
|
|
m_vertices[0] = video::S3DVertex(-m_size / 2, -m_size / 2,
|
|
|
|
0, 0, 0, 0, m_color, tx0, ty1);
|
|
|
|
m_vertices[1] = video::S3DVertex(m_size / 2, -m_size / 2,
|
|
|
|
0, 0, 0, 0, m_color, tx1, ty1);
|
|
|
|
m_vertices[2] = video::S3DVertex(m_size / 2, m_size / 2,
|
|
|
|
0, 0, 0, 0, m_color, tx1, ty0);
|
|
|
|
m_vertices[3] = video::S3DVertex(-m_size / 2, m_size / 2,
|
|
|
|
0, 0, 0, 0, m_color, tx0, ty0);
|
2013-01-23 18:32:02 +01:00
|
|
|
|
2014-01-26 11:40:21 +01:00
|
|
|
v3s16 camera_offset = m_env->getCameraOffset();
|
2017-08-20 19:37:29 +02:00
|
|
|
for (video::S3DVertex &vertex : m_vertices) {
|
2013-04-22 20:35:10 +02:00
|
|
|
if (m_vertical) {
|
|
|
|
v3f ppos = m_player->getPosition()/BS;
|
2017-08-20 19:37:29 +02:00
|
|
|
vertex.Pos.rotateXZBy(atan2(ppos.Z-m_pos.Z, ppos.X-m_pos.X)/core::DEGTORAD+90);
|
2013-04-22 20:35:10 +02:00
|
|
|
} else {
|
2017-08-20 19:37:29 +02:00
|
|
|
vertex.Pos.rotateYZBy(m_player->getPitch());
|
|
|
|
vertex.Pos.rotateXZBy(m_player->getYaw());
|
2013-04-22 20:35:10 +02:00
|
|
|
}
|
2017-08-20 19:37:29 +02:00
|
|
|
m_box.addInternalPoint(vertex.Pos);
|
|
|
|
vertex.Pos += m_pos*BS - intToFloat(camera_offset, BS);
|
2013-01-23 18:32:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
ParticleSpawner
|
|
|
|
*/
|
|
|
|
|
2017-06-26 20:11:17 +02:00
|
|
|
ParticleSpawner::ParticleSpawner(IGameDef *gamedef, LocalPlayer *player,
|
2013-01-23 18:32:02 +01:00
|
|
|
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,
|
2017-01-14 16:48:49 +01:00
|
|
|
video::ITexture *texture, u32 id, const struct TileAnimationParams &anim,
|
|
|
|
u8 glow,
|
|
|
|
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_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;
|
2017-01-14 16:48:49 +01:00
|
|
|
m_animation = anim;
|
|
|
|
m_glow = glow;
|
2013-01-23 18:32:02 +01:00
|
|
|
|
|
|
|
for (u16 i = 0; i<=m_amount; i++)
|
|
|
|
{
|
|
|
|
float spawntime = (float)rand()/(float)RAND_MAX*m_spawntime;
|
|
|
|
m_spawntimes.push_back(spawntime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 20:40:59 +02:00
|
|
|
void ParticleSpawner::spawnParticle(ClientEnvironment *env, float radius,
|
|
|
|
bool is_attached, const v3f &attached_pos, float attached_yaw)
|
|
|
|
{
|
|
|
|
v3f ppos = m_player->getPosition() / BS;
|
|
|
|
v3f pos = random_v3f(m_minpos, m_maxpos);
|
|
|
|
|
|
|
|
// Need to apply this first or the following check
|
|
|
|
// will be wrong for attached spawners
|
|
|
|
if (is_attached) {
|
|
|
|
pos.rotateXZBy(attached_yaw);
|
|
|
|
pos += attached_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos.getDistanceFrom(ppos) > radius)
|
|
|
|
return;
|
|
|
|
|
|
|
|
v3f vel = random_v3f(m_minvel, m_maxvel);
|
|
|
|
v3f acc = random_v3f(m_minacc, m_maxacc);
|
|
|
|
|
|
|
|
if (is_attached) {
|
|
|
|
// Apply attachment yaw
|
|
|
|
vel.rotateXZBy(attached_yaw);
|
|
|
|
acc.rotateXZBy(attached_yaw);
|
|
|
|
}
|
|
|
|
|
|
|
|
float exptime = rand() / (float)RAND_MAX
|
|
|
|
* (m_maxexptime - m_minexptime)
|
|
|
|
+ m_minexptime;
|
|
|
|
float size = rand() / (float)RAND_MAX
|
|
|
|
* (m_maxsize - m_minsize)
|
|
|
|
+ m_minsize;
|
|
|
|
|
|
|
|
m_particlemanager->addParticle(new Particle(
|
|
|
|
m_gamedef,
|
|
|
|
m_player,
|
|
|
|
env,
|
|
|
|
pos,
|
|
|
|
vel,
|
|
|
|
acc,
|
|
|
|
exptime,
|
|
|
|
size,
|
|
|
|
m_collisiondetection,
|
|
|
|
m_collision_removal,
|
|
|
|
m_vertical,
|
|
|
|
m_texture,
|
|
|
|
v2f(0.0, 0.0),
|
|
|
|
v2f(1.0, 1.0),
|
|
|
|
m_animation,
|
|
|
|
m_glow
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-06-11 13:58:43 +02:00
|
|
|
static thread_local const float radius =
|
2017-05-20 16:46:35 +02:00
|
|
|
g_settings->getS16("max_block_send_distance") * MAP_BLOCKSIZE;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-02 20:40:59 +02: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) {
|
2013-01-23 18:32:02 +01:00
|
|
|
m_amount--;
|
|
|
|
|
2017-05-20 16:46:35 +02:00
|
|
|
// Pretend to, but don't actually spawn a particle if it is
|
|
|
|
// attached to an unloaded object or distant from player.
|
2017-10-02 20:40:59 +02:00
|
|
|
if (!unloaded)
|
|
|
|
spawnParticle(env, radius, is_attached, attached_pos, attached_yaw);
|
|
|
|
|
2017-05-20 16:46:35 +02:00
|
|
|
i = m_spawntimes.erase(i);
|
2017-10-02 20:40:59 +02:00
|
|
|
} else {
|
2017-05-20 16:46:35 +02:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2017-10-02 20:40:59 +02:00
|
|
|
} else {
|
|
|
|
// Spawner exists for an infinity timespan, spawn on a per-second base
|
|
|
|
|
2017-05-20 16:46:35 +02:00
|
|
|
// Skip this step if attached to an unloaded object
|
|
|
|
if (unloaded)
|
|
|
|
return;
|
2017-10-02 20:40:59 +02:00
|
|
|
|
|
|
|
for (int i = 0; i <= m_amount; i++) {
|
|
|
|
if (rand() / (float)RAND_MAX < dtime)
|
|
|
|
spawnParticle(env, radius, is_attached, attached_pos, attached_yaw);
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 20:39:22 +01:00
|
|
|
void ParticleManager::handleParticleEvent(ClientEvent *event, Client *client,
|
2017-06-26 20:11:17 +02:00
|
|
|
LocalPlayer *player)
|
2015-01-05 18:34:59 +01:00
|
|
|
{
|
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 =
|
2017-01-09 20:39:22 +01:00
|
|
|
client->tsrc()->getTextureForMesh(*(event->add_particlespawner.texture));
|
2016-03-30 00:06:47 +02:00
|
|
|
|
2017-06-26 20:11:17 +02:00
|
|
|
ParticleSpawner *toadd = new ParticleSpawner(client, player,
|
2016-03-30 00:06:47 +02:00
|
|
|
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,
|
2017-01-14 16:48:49 +01:00
|
|
|
event->add_particlespawner.animation,
|
|
|
|
event->add_particlespawner.glow,
|
2016-03-30 00:06:47 +02:00
|
|
|
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 =
|
2017-01-09 20:39:22 +01:00
|
|
|
client->tsrc()->getTextureForMesh(*(event->spawn_particle.texture));
|
2016-03-30 00:06:47 +02:00
|
|
|
|
2017-06-26 20:11:17 +02:00
|
|
|
Particle *toadd = new Particle(client, player, m_env,
|
2016-03-30 00:06:47 +02:00
|
|
|
*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),
|
2017-01-14 16:48:49 +01:00
|
|
|
v2f(1.0, 1.0),
|
|
|
|
event->spawn_particle.animation,
|
|
|
|
event->spawn_particle.glow);
|
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;
|
2017-01-14 16:48:49 +01:00
|
|
|
delete event->spawn_particle.texture;
|
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
|
|
|
|
2017-01-12 15:46:30 +01:00
|
|
|
void ParticleManager::addDiggingParticles(IGameDef* gamedef,
|
2017-06-26 20:11:17 +02:00
|
|
|
LocalPlayer *player, v3s16 pos, const MapNode &n, const ContentFeatures &f)
|
2015-01-05 18:34:59 +01:00
|
|
|
{
|
2017-09-08 18:39:24 +02:00
|
|
|
// No particles for "airlike" nodes
|
|
|
|
if (f.drawtype == NDT_AIRLIKE)
|
|
|
|
return;
|
|
|
|
|
2017-06-26 20:11:17 +02:00
|
|
|
// set the amount of particles here
|
|
|
|
for (u16 j = 0; j < 32; j++) {
|
|
|
|
addNodeParticle(gamedef, player, pos, n, f);
|
2015-01-05 18:34:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 15:46:30 +01:00
|
|
|
void ParticleManager::addNodeParticle(IGameDef* gamedef,
|
2017-06-26 20:11:17 +02:00
|
|
|
LocalPlayer *player, v3s16 pos, const MapNode &n, const ContentFeatures &f)
|
2015-01-05 18:34:59 +01:00
|
|
|
{
|
2017-09-08 18:39:24 +02:00
|
|
|
// No particles for "airlike" nodes
|
|
|
|
if (f.drawtype == NDT_AIRLIKE)
|
|
|
|
return;
|
|
|
|
|
2015-01-05 18:34:59 +01:00
|
|
|
// Texture
|
|
|
|
u8 texid = myrand_range(0, 5);
|
2017-04-21 15:34:59 +02:00
|
|
|
const TileLayer &tile = f.tiles[texid].layers[0];
|
2016-12-23 13:48:32 +01:00
|
|
|
video::ITexture *texture;
|
2017-01-14 16:48:49 +01:00
|
|
|
struct TileAnimationParams anim;
|
|
|
|
anim.type = TAT_NONE;
|
2015-01-05 18:34:59 +01:00
|
|
|
|
|
|
|
// Only use first frame of animated texture
|
2017-01-12 15:46:30 +01:00
|
|
|
if (tile.material_flags & MATERIAL_FLAG_ANIMATION)
|
2017-07-26 20:12:48 +02:00
|
|
|
texture = (*tile.frames)[0].texture;
|
2016-12-23 13:48:32 +01:00
|
|
|
else
|
2017-01-12 15:46:30 +01:00
|
|
|
texture = tile.texture;
|
2015-01-05 18:34:59 +01:00
|
|
|
|
|
|
|
float size = rand() % 64 / 512.;
|
|
|
|
float visual_size = BS * size;
|
2016-12-23 13:48:32 +01:00
|
|
|
v2f texsize(size * 2, size * 2);
|
2015-01-05 18:34:59 +01:00
|
|
|
v2f texpos;
|
|
|
|
texpos.X = ((rand() % 64) / 64. - texsize.X);
|
2016-12-23 13:48:32 +01:00
|
|
|
texpos.Y = ((rand() % 64) / 64. - texsize.Y);
|
2015-01-05 18:34:59 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
);
|
|
|
|
|
2017-01-12 15:46:30 +01:00
|
|
|
video::SColor color;
|
|
|
|
if (tile.has_color)
|
|
|
|
color = tile.color;
|
|
|
|
else
|
|
|
|
n.getColor(f, &color);
|
|
|
|
|
2015-01-05 18:34:59 +01:00
|
|
|
Particle* toadd = new Particle(
|
|
|
|
gamedef,
|
|
|
|
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,
|
2017-01-14 16:48:49 +01:00
|
|
|
texsize,
|
|
|
|
anim,
|
2017-01-12 15:46:30 +01:00
|
|
|
0,
|
|
|
|
color);
|
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);
|
|
|
|
}
|