2013-08-29 11:45:22 +09:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 yvt
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-29 11:45:22 +09:00
|
|
|
This file is part of OpenSpades.
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-29 11:45:22 +09:00
|
|
|
OpenSpades 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-29 11:45:22 +09:00
|
|
|
OpenSpades 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 General Public License for more details.
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-29 11:45:22 +09:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-29 11:45:22 +09:00
|
|
|
*/
|
2013-08-18 16:18:06 +09:00
|
|
|
|
|
|
|
#include "ParticleSpriteEntity.h"
|
2016-12-03 18:49:07 +09:00
|
|
|
#include <Core/Debug.h>
|
2013-08-18 16:18:06 +09:00
|
|
|
#include "GameMap.h"
|
2016-12-03 18:23:47 +09:00
|
|
|
#include "World.h"
|
2013-08-18 16:18:06 +09:00
|
|
|
|
|
|
|
namespace spades {
|
2016-12-03 18:23:47 +09:00
|
|
|
namespace client {
|
|
|
|
ParticleSpriteEntity::ParticleSpriteEntity(Client *cli, IImage *image, Vector4 color)
|
|
|
|
: image(image), color(color) {
|
|
|
|
position = MakeVector3(0, 0, 0);
|
2013-08-18 16:18:06 +09:00
|
|
|
velocity = MakeVector3(0, 0, 0);
|
|
|
|
radius = 1.f;
|
|
|
|
radiusVelocity = 0.f;
|
|
|
|
angle = 0.f;
|
|
|
|
rotationVelocity = 0.f;
|
|
|
|
velocityDamp = 1;
|
|
|
|
gravityScale = 1.f;
|
|
|
|
lifetime = 1.f;
|
|
|
|
radiusDamp = 1.f;
|
|
|
|
time = 0.f;
|
|
|
|
fadeInDuration = .1f;
|
|
|
|
fadeOutDuration = .5f;
|
|
|
|
additive = false;
|
|
|
|
blockHitAction = Delete;
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
if (image != NULL)
|
2013-11-17 00:26:05 +09:00
|
|
|
image->AddRef();
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
renderer = cli->GetRenderer();
|
2016-12-03 18:23:47 +09:00
|
|
|
if (cli->GetWorld())
|
|
|
|
map = cli->GetWorld()->GetMap();
|
2013-08-18 16:18:06 +09:00
|
|
|
else
|
|
|
|
map = NULL;
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-11-17 00:26:05 +09:00
|
|
|
ParticleSpriteEntity::~ParticleSpriteEntity() {
|
2016-12-03 18:23:47 +09:00
|
|
|
if (image != NULL) {
|
2013-11-17 00:26:05 +09:00
|
|
|
image->Release();
|
|
|
|
}
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
void ParticleSpriteEntity::SetLifeTime(float lifeTime, float fadeIn, float fadeOut) {
|
2013-08-18 16:18:06 +09:00
|
|
|
lifetime = lifeTime;
|
|
|
|
fadeInDuration = fadeIn;
|
|
|
|
fadeOutDuration = fadeOut;
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
void ParticleSpriteEntity::SetTrajectory(Vector3 pos, Vector3 vel, float damp, float grav) {
|
2013-08-18 16:18:06 +09:00
|
|
|
position = pos;
|
|
|
|
velocity = vel;
|
|
|
|
velocityDamp = damp;
|
|
|
|
gravityScale = grav;
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
void ParticleSpriteEntity::SetRotation(float initialAngle, float angleVelocity) {
|
2013-08-18 16:18:06 +09:00
|
|
|
angle = initialAngle;
|
|
|
|
rotationVelocity = angleVelocity;
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
void ParticleSpriteEntity::SetRadius(float initialRadius, float radiusVelocity,
|
|
|
|
float damp) {
|
2013-08-18 16:18:06 +09:00
|
|
|
radius = initialRadius;
|
|
|
|
this->radiusVelocity = radiusVelocity;
|
|
|
|
radiusDamp = damp;
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
bool ParticleSpriteEntity::Update(float dt) {
|
|
|
|
SPADES_MARK_FUNCTION_DEBUG();
|
|
|
|
Vector3 lastPos = position;
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
time += dt;
|
2016-12-03 18:23:47 +09:00
|
|
|
if (time > lifetime)
|
2013-08-18 16:18:06 +09:00
|
|
|
return false;
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
position += velocity * dt;
|
|
|
|
velocity.z += 32.f * dt * gravityScale;
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
// TODO: control clip action
|
2016-12-03 18:23:47 +09:00
|
|
|
if (blockHitAction != Ignore && map) {
|
|
|
|
if (map->ClipWorld(position.x, position.y, position.z)) {
|
|
|
|
if (blockHitAction == Delete) {
|
2013-08-18 16:18:06 +09:00
|
|
|
return false;
|
2016-12-03 18:23:47 +09:00
|
|
|
} else {
|
2013-08-18 16:18:06 +09:00
|
|
|
IntVector3 lp2 = lastPos.Floor();
|
|
|
|
IntVector3 lp = position.Floor();
|
|
|
|
if (lp.z != lp2.z && ((lp.x == lp2.x && lp.y == lp2.y) ||
|
2016-12-03 18:23:47 +09:00
|
|
|
!map->ClipWorld(lp.x, lp.y, lp2.z)))
|
2013-08-18 16:18:06 +09:00
|
|
|
velocity.z = -velocity.z;
|
2016-12-03 18:23:47 +09:00
|
|
|
else if (lp.x != lp2.x && ((lp.y == lp2.y && lp.z == lp2.z) ||
|
|
|
|
!map->ClipWorld(lp2.x, lp.y, lp.z)))
|
2013-08-18 16:18:06 +09:00
|
|
|
velocity.x = -velocity.x;
|
2016-12-03 18:23:47 +09:00
|
|
|
else if (lp.y != lp2.y && ((lp.x == lp2.x && lp.z == lp2.z) ||
|
|
|
|
!map->ClipWorld(lp.x, lp2.y, lp.z)))
|
2013-08-18 16:18:06 +09:00
|
|
|
velocity.y = -velocity.y;
|
|
|
|
velocity *= .36f;
|
|
|
|
position = lastPos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
// radius
|
2016-12-03 18:23:47 +09:00
|
|
|
if (radiusVelocity != 0.f)
|
2013-08-18 16:18:06 +09:00
|
|
|
radius += radiusVelocity * dt;
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
if (rotationVelocity != 0.f)
|
2013-08-18 16:18:06 +09:00
|
|
|
angle += rotationVelocity * dt;
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
if (velocityDamp != 1.f)
|
2013-08-18 16:18:06 +09:00
|
|
|
velocity *= powf(velocityDamp, dt);
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
if (radiusDamp != 1.f)
|
2013-08-18 16:18:06 +09:00
|
|
|
radiusVelocity *= powf(radiusDamp, dt);
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
return true;
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
void ParticleSpriteEntity::Render3D() {
|
|
|
|
SPADES_MARK_FUNCTION_DEBUG();
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
float fade = 1.f;
|
2016-12-03 18:23:47 +09:00
|
|
|
if (time < fadeInDuration) {
|
2013-08-18 16:18:06 +09:00
|
|
|
fade *= time / fadeInDuration;
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
if (time > lifetime - fadeOutDuration) {
|
2013-08-18 16:18:06 +09:00
|
|
|
fade *= (lifetime - time) / fadeOutDuration;
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
Vector4 col = color;
|
|
|
|
col.w *= fade;
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
// premultiplied alpha!
|
2013-08-18 16:18:06 +09:00
|
|
|
col.x *= col.w;
|
|
|
|
col.y *= col.w;
|
|
|
|
col.z *= col.w;
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
if (additive)
|
2013-08-18 16:18:06 +09:00
|
|
|
col.w = 0.f;
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-12-09 01:00:09 +09:00
|
|
|
renderer->SetColorAlphaPremultiplied(col);
|
2016-12-03 18:23:47 +09:00
|
|
|
renderer->AddSprite(image, position, radius, angle);
|
2013-08-18 16:18:06 +09:00
|
|
|
}
|
2013-09-14 13:28:19 +09:00
|
|
|
void ParticleSpriteEntity::SetImage(IImage *img) {
|
2016-12-03 18:23:47 +09:00
|
|
|
if (img == image)
|
|
|
|
return;
|
|
|
|
if (image != NULL)
|
2013-11-17 00:26:05 +09:00
|
|
|
image->Release();
|
2013-09-14 13:28:19 +09:00
|
|
|
image = img;
|
2016-12-03 18:23:47 +09:00
|
|
|
if (image != NULL)
|
2013-11-17 00:26:05 +09:00
|
|
|
image->AddRef();
|
2013-09-14 13:28:19 +09:00
|
|
|
}
|
2013-08-18 16:18:06 +09:00
|
|
|
}
|
|
|
|
}
|