2009-11-04 07:06:36 -08:00
|
|
|
#include "libs.h"
|
|
|
|
#include "HyperspaceCloud.h"
|
|
|
|
#include "Pi.h"
|
|
|
|
#include "Ship.h"
|
|
|
|
#include "Serializer.h"
|
2011-08-20 08:12:27 -07:00
|
|
|
#include "render/Render.h"
|
2009-11-04 08:53:57 -08:00
|
|
|
#include "Space.h"
|
2011-04-17 15:29:36 -07:00
|
|
|
#include "Player.h"
|
2009-11-05 06:49:26 -08:00
|
|
|
#include "perlin.h"
|
2011-07-27 03:46:56 -07:00
|
|
|
#include "Lang.h"
|
2011-11-19 02:33:36 -08:00
|
|
|
#include "Game.h"
|
2009-11-04 07:06:36 -08:00
|
|
|
|
2009-11-04 08:53:57 -08:00
|
|
|
HyperspaceCloud::HyperspaceCloud(Ship *s, double dueDate, bool isArrival)
|
2009-11-04 07:06:36 -08:00
|
|
|
{
|
2009-11-05 06:49:26 -08:00
|
|
|
m_flags = Body::FLAG_CAN_MOVE_FRAME |
|
|
|
|
Body::FLAG_LABEL_HIDDEN;
|
2009-11-04 07:06:36 -08:00
|
|
|
m_ship = s;
|
|
|
|
m_pos = vector3d(0,0,0);
|
2009-11-05 06:49:26 -08:00
|
|
|
m_vel = (s ? s->GetVelocity() : vector3d(0.0));
|
2009-11-04 07:06:36 -08:00
|
|
|
m_birthdate = Pi::GetGameTime();
|
|
|
|
m_due = dueDate;
|
2009-11-05 06:49:26 -08:00
|
|
|
SetIsArrival(isArrival);
|
2009-11-04 07:06:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
HyperspaceCloud::HyperspaceCloud()
|
|
|
|
{
|
|
|
|
m_ship = 0;
|
|
|
|
m_pos = vector3d(0,0,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
HyperspaceCloud::~HyperspaceCloud()
|
|
|
|
{
|
|
|
|
if (m_ship) delete m_ship;
|
|
|
|
}
|
|
|
|
|
2009-11-05 06:49:26 -08:00
|
|
|
void HyperspaceCloud::SetIsArrival(bool isArrival)
|
|
|
|
{
|
|
|
|
m_isArrival = isArrival;
|
2011-07-27 03:46:56 -07:00
|
|
|
SetLabel(isArrival ? Lang::HYPERSPACE_ARRIVAL_CLOUD : Lang::HYPERSPACE_DEPARTURE_CLOUD);
|
2009-11-05 06:49:26 -08:00
|
|
|
}
|
|
|
|
|
2009-11-04 07:06:36 -08:00
|
|
|
vector3d HyperspaceCloud::GetPosition() const
|
|
|
|
{
|
|
|
|
return m_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HyperspaceCloud::SetPosition(vector3d p)
|
|
|
|
{
|
|
|
|
m_pos = p;
|
|
|
|
}
|
|
|
|
|
2011-11-17 21:41:00 -08:00
|
|
|
void HyperspaceCloud::Save(Serializer::Writer &wr, Space *space)
|
2009-11-04 07:06:36 -08:00
|
|
|
{
|
2011-11-17 21:41:00 -08:00
|
|
|
Body::Save(wr, space);
|
2010-05-22 10:26:25 -07:00
|
|
|
wr.Vector3d(m_pos);
|
|
|
|
wr.Vector3d(m_vel);
|
|
|
|
wr.Double(m_birthdate);
|
|
|
|
wr.Double(m_due);
|
|
|
|
wr.Bool(m_isArrival);
|
|
|
|
wr.Bool(m_ship != 0);
|
2011-11-17 21:41:00 -08:00
|
|
|
if (m_ship) m_ship->Serialize(wr, space);
|
2009-11-04 07:06:36 -08:00
|
|
|
}
|
|
|
|
|
2011-11-17 13:38:06 -08:00
|
|
|
void HyperspaceCloud::Load(Serializer::Reader &rd, Space *space)
|
2009-11-04 07:06:36 -08:00
|
|
|
{
|
2011-11-17 13:38:06 -08:00
|
|
|
Body::Load(rd, space);
|
2010-05-22 10:26:25 -07:00
|
|
|
m_pos = rd.Vector3d();
|
|
|
|
m_vel = rd.Vector3d();
|
|
|
|
m_birthdate = rd.Double();
|
|
|
|
m_due = rd.Double();
|
|
|
|
m_isArrival = rd.Bool();
|
|
|
|
if (rd.Bool()) {
|
2011-11-17 13:38:06 -08:00
|
|
|
m_ship = reinterpret_cast<Ship*>(Body::Unserialize(rd, space));
|
2010-01-21 16:30:54 -08:00
|
|
|
}
|
2009-11-04 07:06:36 -08:00
|
|
|
}
|
|
|
|
|
2011-11-17 13:38:06 -08:00
|
|
|
void HyperspaceCloud::PostLoadFixup(Space *space)
|
2009-11-04 07:06:36 -08:00
|
|
|
{
|
2011-11-17 13:38:06 -08:00
|
|
|
if (m_ship) m_ship->PostLoadFixup(space);
|
2009-11-04 07:06:36 -08:00
|
|
|
}
|
|
|
|
|
2009-11-04 08:53:57 -08:00
|
|
|
void HyperspaceCloud::TimeStepUpdate(const float timeStep)
|
|
|
|
{
|
|
|
|
m_pos += m_vel * timeStep;
|
|
|
|
|
2010-11-05 04:59:28 -07:00
|
|
|
if (m_isArrival && m_ship && (m_due < Pi::GetGameTime())) {
|
2009-11-04 08:53:57 -08:00
|
|
|
// spawn ship
|
2011-08-28 03:14:28 -07:00
|
|
|
// XXX some overlap with Space::DoHyperspaceTo(). should probably all
|
|
|
|
// be moved into EvictShip()
|
2009-11-04 08:53:57 -08:00
|
|
|
m_ship->SetPosition(m_pos);
|
|
|
|
m_ship->SetVelocity(m_vel);
|
2011-08-28 03:14:28 -07:00
|
|
|
m_ship->SetRotMatrix(matrix4x4d::Identity());
|
2009-11-04 08:53:57 -08:00
|
|
|
m_ship->SetFrame(GetFrame());
|
2011-11-19 02:33:36 -08:00
|
|
|
Pi::game->GetSpace()->AddBody(m_ship);
|
2009-11-04 08:53:57 -08:00
|
|
|
m_ship->Enable();
|
2011-04-17 15:29:36 -07:00
|
|
|
|
|
|
|
if (Pi::player->GetNavTarget() == this && !Pi::player->GetCombatTarget())
|
2011-11-02 23:30:38 -07:00
|
|
|
Pi::player->SetCombatTarget(m_ship, Pi::player->GetSetSpeedTarget() == this);
|
2011-04-17 15:29:36 -07:00
|
|
|
|
2011-11-14 13:44:48 -08:00
|
|
|
m_ship->EnterSystem();
|
2011-04-19 02:31:03 -07:00
|
|
|
|
2009-11-04 08:53:57 -08:00
|
|
|
m_ship = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-21 09:33:23 -07:00
|
|
|
Ship *HyperspaceCloud::EvictShip()
|
|
|
|
{
|
|
|
|
Ship *s = m_ship;
|
|
|
|
m_ship = 0;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-11-05 06:49:26 -08:00
|
|
|
static void make_circle_thing(float radius, const Color &colCenter, const Color &colEdge)
|
|
|
|
{
|
|
|
|
glColor4fv(colCenter);
|
|
|
|
glBegin(GL_TRIANGLE_FAN);
|
|
|
|
glVertex3f(0,0,0);
|
|
|
|
glColor4fv(colEdge);
|
|
|
|
for (float ang=0; ang<M_PI*2.0; ang+=0.1) {
|
|
|
|
glVertex3f(radius*sin(ang), radius*cos(ang), 0.0f);
|
|
|
|
}
|
|
|
|
glVertex3f(0, radius, 0.0f);
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
2010-11-16 04:29:49 -08:00
|
|
|
void HyperspaceCloud::UpdateInterpolatedTransform(double alpha)
|
|
|
|
{
|
|
|
|
m_interpolatedTransform = matrix4x4d::Identity();
|
|
|
|
const vector3d newPos = GetPosition();
|
|
|
|
const vector3d oldPos = newPos - m_vel*Pi::GetTimeStep();
|
|
|
|
const vector3d p = alpha*newPos + (1.0-alpha)*oldPos;
|
|
|
|
m_interpolatedTransform[12] = p.x;
|
|
|
|
m_interpolatedTransform[13] = p.y;
|
|
|
|
m_interpolatedTransform[14] = p.z;
|
|
|
|
}
|
|
|
|
|
2009-11-18 06:59:51 -08:00
|
|
|
void HyperspaceCloud::Render(const vector3d &viewCoords, const matrix4x4d &viewTransform)
|
2009-11-04 07:06:36 -08:00
|
|
|
{
|
2010-09-27 08:05:30 -07:00
|
|
|
Render::State::UseProgram(Render::simpleShader);
|
2009-11-04 07:06:36 -08:00
|
|
|
glDisable(GL_LIGHTING);
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glPushMatrix();
|
2011-05-10 17:57:37 -07:00
|
|
|
glTranslatef(float(viewCoords.x), float(viewCoords.y), float(viewCoords.z));
|
2009-11-05 06:49:26 -08:00
|
|
|
|
|
|
|
// face the camera dammit
|
2011-03-06 17:14:46 -08:00
|
|
|
vector3d zaxis = viewCoords.NormalizedSafe();
|
2011-02-17 19:11:10 -08:00
|
|
|
vector3d xaxis = vector3d(0,1,0).Cross(zaxis).Normalized();
|
|
|
|
vector3d yaxis = zaxis.Cross(xaxis);
|
2009-11-05 06:49:26 -08:00
|
|
|
matrix4x4d rot = matrix4x4d::MakeRotMatrix(xaxis, yaxis, zaxis).InverseOf();
|
|
|
|
glMultMatrixd(&rot[0]);
|
2010-11-16 04:29:49 -08:00
|
|
|
// precise to the rendered frame (better than PHYSICS_HZ granularity)
|
|
|
|
double preciseTime = Pi::GetGameTime() + Pi::GetGameTickAlpha()*Pi::GetTimeStep();
|
2009-11-05 06:49:26 -08:00
|
|
|
|
2011-09-05 22:47:43 -07:00
|
|
|
float radius = 1000.0f + 200.0f*float(noise(10.0*preciseTime, 0, 0));
|
2009-11-04 08:53:57 -08:00
|
|
|
if (m_isArrival) {
|
2009-11-05 06:49:26 -08:00
|
|
|
make_circle_thing(radius, Color(1.0,1.0,1.0,1.0), Color(0.0,0.0,1.0,0.0));
|
2009-11-04 08:53:57 -08:00
|
|
|
} else {
|
2009-11-05 06:49:26 -08:00
|
|
|
make_circle_thing(radius, Color(1.0,1.0,1.0,1.0), Color(1.0,0.0,0.0,0.0));
|
2009-11-04 08:53:57 -08:00
|
|
|
}
|
2009-11-04 07:06:36 -08:00
|
|
|
glPopMatrix();
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
glEnable(GL_LIGHTING);
|
|
|
|
}
|