1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
// $Id: ETRotate.cpp,v 1.1 2003/02/08 13:14:51 mrq Exp $
#include "ETRotate.h"
#include "Application.h"
#include <sstream>
ETRotate::ETRotate (World *world) : EntityTool (world), m_Entity (NULL)
{}
ETRotate::~ETRotate()
{
if (m_Entity) m_Entity->Unref();
}
/// Apply tool at (x,z)
void
ETRotate::Apply3D(Ark::Vector3 newpos, const Ark::Collision &col)
{
if (col.GetType() == Ark::Collision::ENTITY && m_Entity == NULL)
{
m_Entity = static_cast<Entity*>(col.m_Entity);
m_Entity->Ref();
m_OldPos = col.m_Pos;
m_OldRot = m_Entity->m_MState.m_Rotation;
return;
}
else if (m_Entity)
{
m_NewPos = col.m_Pos;
m_NewRot =
m_Entity->m_MState.m_Rotation =
(m_OldRot *
Ark::Quat (0.0f, (newpos.Z-m_OldPos.Z)*20.0, 0.0f));
g_Application->GetWorld()->Update(0.0);
}
}
/// Update or reset tool
void
ETRotate::Update()
{
if (m_Entity)
{
m_Entity->Unref();
m_Entity = NULL;
}
}
/// Create a command with a correct state.
Command*
ETRotate::CreateCommand()
{
if (m_Entity == NULL)
return NULL;
else
{
m_Entity->m_MState.m_Rotation = m_OldRot;
Command *c = new ECRotate (g_Application->GetWorld(), m_Entity,
m_NewRot);
c->Execute();
return c;
}
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
ECRotate::ECRotate(World *world, Entity *entity, Ark::Quat newrot) :
m_World (world),
m_Entity (entity)
{
assert (m_Entity != 0);
m_Entity->Ref();
m_NewRot = newrot;
m_OldRot = m_Entity->m_MState.m_Rotation;
}
ECRotate::~ECRotate()
{
if (m_Entity)
m_Entity->Unref();
}
/// Return the command type.
std::string
ECRotate::Type() const
{
return "Rotate entity";
}
/// Execute this command.
void
ECRotate::Execute()
{
//std::cerr << "Execute move";
m_Entity->m_MState.m_Rotation = m_NewRot;
g_Application->GetWorld()->Update(0.0);
}
/// Undo the action done by this command.
void
ECRotate::Unexecute()
{
//std::cerr << "Unexecute move";
m_Entity->m_MState.m_Rotation = m_OldRot;
g_Application->GetWorld()->Update(0.0);
}
|