/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program 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 2, or (at your option) any later version. ### File Specific: main-programmer: hdavid, amaechler */ #include "snow_effect.h" #include "util/loading/load_param.h" #include "util/loading/factory.h" #include "sound/resource_sound_buffer.h" #include "glincl.h" #include "debug.h" #include "p_node.h" #include "state.h" #include "particles/sprite_particles.h" #include "particles/plane_emitter.h" #include "shell_command.h" #include "script_class.h" #include "cloud_effect.h" #include "class_id_DEPRECATED.h" ObjectListDefinitionID(SnowEffect, CL_SNOW_EFFECT); SHELL_COMMAND(activate, SnowEffect, activateSnow); SHELL_COMMAND(deactivate, SnowEffect, deactivateSnow); CREATE_SCRIPTABLE_CLASS(SnowEffect, addMethod("activate", Executor0(&SnowEffect::activate)) ->addMethod("deactivate", Executor0(&SnowEffect::deactivate)) ); CREATE_FACTORY(SnowEffect); SnowEffect::SnowEffect(const TiXmlElement* root) { this->registerObject(this, SnowEffect::_objectList); this->init(); if (root != NULL) this->loadParams(root); //load wind sound if (this->snowWindForce >= 1) { this->windBuffer = OrxSound::ResourceSoundBuffer("sound/atmosphere/wind.wav"); } if(snowActivate) { this->activate(); SnowEffect::snowParticles->precache((int) this->snowLife); } } SnowEffect::~SnowEffect() { this->deactivate(); } SpriteParticles* SnowEffect::snowParticles = NULL; void SnowEffect::loadParams(const TiXmlElement* root) { WeatherEffect::loadParams(root); LoadParam(root, "numParticles", this, SnowEffect, numParticles); LoadParam(root, "materialTexture", this, SnowEffect, materialTexture); LoadParam(root, "lifeSpans", this, SnowEffect, lifeSpan); LoadParam(root, "radius", this, SnowEffect, radius); LoadParam(root, "mass", this, SnowEffect, mass); LoadParam(root, "emissionRate", this, SnowEffect, emissionRate); LoadParam(root, "emissionVelocity", this, SnowEffect, emissionVelocity); LoadParam(root, "wind", this, SnowEffect, wind); LoadParam(root, "size", this, SnowEffect, size); LoadParam(root, "coord", this, SnowEffect, coord); LoadParam(root, "cloudcolor", this, SnowEffect, setCloudColor); LoadParam(root, "skycolor", this, SnowEffect, setSkyColor); LoadParam(root, "fadetime", this, SnowEffect, setFadeTime); LOAD_PARAM_START_CYCLE(root, element); { LoadParam_CYCLE(element, "option", this, SnowEffect, setSnowOption); } LOAD_PARAM_END_CYCLE(element); } void SnowEffect::init() { this->emitter = new PlaneEmitter(); // Default values this->snowActivate = false; this->snowMove = false; this->particles = 12000; this->texture = "maps/snow_flake_01_32x32.png"; this->snowLife = 8; this->randomLife = 2; this->snowRadius = 3.5; this->randomRadius = 1; this->snowMass = 1.0; this->randomMass = 0.3; this->rate = 900; this->velocity = -100; this->randomVelocity = 5; this->angle = 0.5; this->randomAngle = 0.2; this->alpha = 0.5; this->snowSize = Vector2D(2500, 2500); this->snowCoord = Vector(100,450,400); this->snowWindForce = 1; this->fadeTime = 10; this->cloudColor = Vector(0.2f, 0.2f, 0.2f); this->skyColor = Vector(0.0f, 0.0f, 0.0f); } void SnowEffect::activate() { PRINTF(3)("Activating SnowEffect\n"); this->snowActivate = true; SnowEffect::snowParticles = new SpriteParticles(particles); SnowEffect::snowParticles->setName("SnowEffectTrailParticles"); SnowEffect::snowParticles->setMaterialTexture(texture); SnowEffect::snowParticles->setLifeSpan(snowLife, randomLife); SnowEffect::snowParticles->setRadius(0.0, snowRadius, randomRadius); SnowEffect::snowParticles->setRadius(0.2, snowRadius, randomRadius*0.8); SnowEffect::snowParticles->setRadius(1.0, snowRadius, randomRadius*0.5); SnowEffect::snowParticles->setMass(0, snowMass, randomMass); SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha); SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2); SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0); this->emitter->setSystem(SnowEffect::snowParticles); this->emitter->setRelCoor(snowCoord); this->emitter->setEmissionRate(rate); this->emitter->setEmissionVelocity(velocity, randomVelocity); this->emitter->setSpread(angle * this->snowWindForce , randomAngle * this->snowWindForce); this->emitter->setSize(snowSize); if (this->snowWindForce != 0) this->soundSource.play(this->windBuffer, 0.1f * this->snowWindForce, true); // Store cloud- and sky color before the snow this->oldCloudColor = CloudEffect::cloudColor; this->oldSkyColor = CloudEffect::skyColor; // Change the colors CloudEffect::changeCloudColor(this->cloudColor, this->fadeTime); CloudEffect::changeSkyColor(this->skyColor, this->fadeTime); } void SnowEffect::deactivate() { PRINTF(3)("Deactivating SnowEffect\n"); this->snowActivate = false; this->emitter->setSystem(NULL); // Restore the old cloud- and sky color CloudEffect::changeCloudColor(this->oldCloudColor, this->fadeTime); CloudEffect::changeSkyColor(this->oldSkyColor, this->fadeTime); } void SnowEffect::draw() const { if (!this->snowActivate) return; } void SnowEffect::tick(float dt) { if (!this->snowActivate) return; /* float distance = (State::getCameraNode()->getAbsCoor() - Vector(snowCoord.x, State::getCameraNode()->getAbsCoor().y, snowCoord.z)).len(); if(activated) { if(distance > 0.3*snowSize.x || distance > 0.3*snowSize.y) this->deactivate(); else if(distance > 0.25*snowSize.x || distance > 0.25*snowSize.y) this->alpha = 0.15; else if(distance > 0.2*snowSize.x || distance > 0.2*snowSize.y) this->alpha = 0.25; else if(distance > 0.1*snowSize.x || distance > 0.1*snowSize.y) this->alpha = 0.4; SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha); SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2); SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0); } else { if(distance < 0.3*snowSize.x || distance < 0.3*snowSize.y ) this->activate(); if( distance < 0.25*snowSize.x || distance < 0.25*snowSize.y ) this->alpha = 0.25; else if( distance < 0.2*snowSize.x || distance < 0.2*snowSize.y ) this->alpha = 0.4; else if( distance < 0.1*snowSize.x || distance < 0.1*snowSize.y ) this->alpha = 0.5; SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha); SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2); SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0); }*/ if (this->snowMove) { this->snowCoord = State::getCameraNode()->getAbsCoor(); this->emitter->setRelCoor(this->snowCoord.x , this->snowCoord.y+300, this->snowCoord.z); } }