Files

195 lines
3.6 KiB
C++
Raw Permalink Normal View History

2019-01-18 15:55:06 +01:00
#include "object.hpp"
2019-01-18 21:26:42 +01:00
#include "animator.hpp"
#include <gxruntime.hpp>
#include <gxsound.hpp>
#include <gxchannel.hpp>
2014-01-31 08:23:00 +13:00
2019-01-18 17:04:17 +01:00
extern gxRuntime* gx_runtime;
2014-01-31 08:23:00 +13:00
2019-01-18 17:04:17 +01:00
Object::Object()
: order(0), animator(0), last_copy(0), coll_type(0), coll_radii(Vector(1, 1, 1)),
coll_box(Box(Vector(-1, -1, -1), Vector(1, 1, 1))), pick_geom(0), obscurer(false), captured(false)
{
2014-01-31 08:23:00 +13:00
reset();
}
2019-01-18 17:04:17 +01:00
Object::Object(const Object& o)
: Entity(o), order(o.order), animator(0), last_copy(0), coll_type(o.coll_type), coll_radii(o.coll_radii),
coll_box(o.coll_box), pick_geom(o.pick_geom), obscurer(o.obscurer), captured(false)
{
2014-01-31 08:23:00 +13:00
reset();
}
2019-01-18 17:04:17 +01:00
Object::~Object()
{
2014-01-31 08:23:00 +13:00
delete animator;
2019-01-18 17:04:17 +01:00
velocity = Vector();
2014-01-31 08:23:00 +13:00
updateSounds();
}
2019-01-18 17:04:17 +01:00
Object* Object::copy()
{
last_copy = clone()->getObject();
for (Entity* e = GetChildren(); e; e = e->GetSuccessor()) {
Object* cpy = e->getObject()->copy();
cpy->SetParent(last_copy);
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:17 +01:00
if (animator)
last_copy->setAnimator(new Animator(animator));
2014-01-31 08:23:00 +13:00
return last_copy;
}
2019-01-18 17:04:17 +01:00
void Object::reset()
{
2014-01-31 08:23:00 +13:00
colls.clear();
2019-01-18 17:04:17 +01:00
velocity = Vector();
prev_tform = GetWorldTransform();
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:17 +01:00
void Object::setCollisionType(int type)
{
coll_type = type;
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:17 +01:00
void Object::setCollisionRadii(const Vector& radii)
{
coll_radii = radii;
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:17 +01:00
void Object::setCollisionBox(const Box& box)
{
coll_box = box;
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:17 +01:00
void Object::setAnimator(Animator* t)
{
if (animator)
delete animator;
animator = t;
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:17 +01:00
void Object::beginUpdate(float e)
{
elapsed = e;
2014-01-31 08:23:00 +13:00
colls.clear();
2019-01-18 17:04:17 +01:00
animate(e);
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:17 +01:00
void Object::animate(float e)
{
if (animator)
animator->update(e);
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:17 +01:00
void Object::addCollision(const ObjCollision* c)
{
colls.push_back(c);
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:17 +01:00
void Object::endUpdate()
{
velocity = (GetWorldTransform().v - prev_tform.v) / elapsed;
prev_tform = GetWorldTransform();
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:17 +01:00
void Object::capture()
{
capt_pos = GetLocalPosition();
capt_scl = GetLocalScale();
capt_rot = GetLocalRotation();
captured = true;
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:17 +01:00
bool Object::beginRender(float tween)
{
2014-01-31 08:23:00 +13:00
updateSounds();
2019-01-18 17:04:17 +01:00
if (tween == 1 || !captured) {
render_tform = GetWorldTransform();
render_tform_valid = true;
} else {
Vector pos = (GetLocalPosition() - capt_pos) * tween + capt_pos;
Vector scl = (GetLocalScale() - capt_scl) * tween + capt_scl;
Quat rot = capt_rot.slerpTo(GetLocalRotation(), tween);
tween_tform.m = Matrix(rot);
tween_tform.m.i *= scl.x;
tween_tform.m.j *= scl.y;
tween_tform.m.k *= scl.z;
tween_tform.v = pos;
render_tform_valid = false;
2014-01-31 08:23:00 +13:00
}
return true;
}
2019-01-18 17:04:17 +01:00
void Object::endRender() {}
2014-01-31 08:23:00 +13:00
2019-01-18 17:04:17 +01:00
int Object::getCollisionType() const
{
2014-01-31 08:23:00 +13:00
return coll_type;
}
2019-01-18 17:04:17 +01:00
const Vector& Object::getCollisionRadii() const
{
2014-01-31 08:23:00 +13:00
return coll_radii;
}
2019-01-18 17:04:17 +01:00
const Box& Object::getCollisionBox() const
{
2014-01-31 08:23:00 +13:00
return coll_box;
}
2019-01-18 17:04:17 +01:00
const Vector& Object::getVelocity() const
{
2014-01-31 08:23:00 +13:00
return velocity;
}
2019-01-18 17:04:17 +01:00
const Object::Collisions& Object::getCollisions() const
{
2014-01-31 08:23:00 +13:00
return colls;
}
2019-01-18 17:04:17 +01:00
const Transform& Object::getRenderTform() const
{
if (render_tform_valid)
return render_tform;
2014-01-31 08:23:00 +13:00
2019-01-18 17:04:17 +01:00
Object* parent = (Object*)getParent();
render_tform = parent ? parent->getRenderTform() * tween_tform : tween_tform;
render_tform_valid = true;
2014-01-31 08:23:00 +13:00
return render_tform;
}
2019-01-18 17:04:17 +01:00
const Transform& Object::getPrevWorldTform() const
{
2014-01-31 08:23:00 +13:00
return prev_tform;
}
2019-01-18 17:04:17 +01:00
gxChannel* Object::emitSound(gxSound* sound)
{
if (!sound)
return 0;
2014-01-31 08:23:00 +13:00
2019-01-18 17:04:17 +01:00
gxChannel* chan = sound->play3d(&GetWorldTransform().v.x, &velocity.x);
for (int k = 0; k < channels.size(); ++k) {
if (chan == channels[k])
return chan;
if (!channels[k])
return channels[k] = chan;
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:17 +01:00
channels.push_back(chan);
2014-01-31 08:23:00 +13:00
return chan;
}
2019-01-18 17:04:17 +01:00
void Object::updateSounds()
{
for (int k = 0; k < channels.size(); ++k) {
if (gxChannel* chan = channels[k]) {
if (chan->isPlaying())
chan->set3d(&GetWorldTransform().v.x, &velocity.x);
else
channels[k] = 0;
2014-01-31 08:23:00 +13:00
}
}
}