Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
star_system_jump.cpp
Go to the documentation of this file.
1 #include "vegastrike.h"
2 #include "star_system.h"
3 #include "cmd/planet.h"
4 #include "lin_time.h"
5 #include "hashtable.h"
6 #include "gfx/animation.h"
7 #include "vs_globals.h"
8 #include "config_xml.h"
9 #include "cmd/container.h"
10 #include "xml_support.h"
11 #include <assert.h>
12 #include "gfx/cockpit.h"
13 #include "audiolib.h"
14 #include "cmd/images.h"
15 #include "cmd/script/flightgroup.h"
16 #include <string>
17 #include <vector>
18 
19 #include "options.h"
20 
22 
23 void CacheJumpStar( bool destroy )
24 {
25  static Animation *cachedani = new Animation( game_options.jumpgate.c_str(), true, .1, MIPMAP, false );
26  if (destroy)
27  delete cachedani;
28 }
29 
30 extern std::vector< unorigdest* > pendingjump;
31 static std::vector< unsigned int >AnimationNulls;
32 class ResizeAni
33 {
34 public:
36  float percent;
37  ResizeAni( Animation *ani, float percent )
38  {
39  a = ani;
40  this->percent = percent;
41  }
42 };
43 static std::vector< ResizeAni >JumpAnimations;
44 static std::vector< ResizeAni >VolatileJumpAnimations;
45 Animation * GetVolatileAni( unsigned int which )
46 {
47  if ( which < VolatileJumpAnimations.size() )
48  return VolatileJumpAnimations[which].a;
49  return NULL;
50 }
51 
52 unsigned int AddAnimation( const QVector &pos, const float size, bool mvolatile, const std::string &name, float percentgrow )
53 {
54  std::vector< ResizeAni > *ja = mvolatile ? &VolatileJumpAnimations : &JumpAnimations;
55 
56  Animation *ani = new Animation( name.c_str(), true, .1, MIPMAP, false );
57  unsigned int i;
58  if ( mvolatile || AnimationNulls.empty() ) {
59  i = ja->size();
60  ja->push_back( ResizeAni( ani, percentgrow ) );
61  } else {
62  assert( JumpAnimations[AnimationNulls.back()].a == NULL );
63  JumpAnimations[AnimationNulls.back()] = ResizeAni( ani, percentgrow );
64  i = AnimationNulls.back();
65  AnimationNulls.pop_back();
66  }
67  (*ja)[i].a->SetDimensions( size, size );
68  (*ja)[i].a->SetPosition( pos );
69  return i;
70 }
71 
72 static unsigned int AddJumpAnimation( const QVector &pos, const float size, bool mvolatile = false )
73 {
74  return AddAnimation( pos, size, mvolatile, game_options.jumpgate, .95 );
75 }
76 
78 {
79  if (ani != -1) {
80  VolatileJumpAnimations.push_back( ResizeAni( JumpAnimations[ani].a, game_options.jumpanimationshrink ) );
81  JumpAnimations[ani].a = NULL;
82  AnimationNulls.push_back( ani );
83  }
84 }
85 
87 {
88  for (unsigned int kk = 0; kk < pendingjump.size(); ++kk) {
89  int k = pendingjump[kk]->animation;
90  if (k != -1) {
91  Unit *un = pendingjump[kk]->un.GetUnit();
92  if (un) {
93  Vector p, q, r;
94  un->GetOrientation( p, q, r );
95 
96  JumpAnimations[k].a->SetPosition( un->Position()+r.Cast()*un->rSize()*(pendingjump[kk]->delay+.25) );
97  JumpAnimations[k].a->SetOrientation( p, q, r );
98  float dd = un->rSize()*game_options.jumpgatesize
99  *(un->GetJumpStatus().delay-pendingjump[kk]->delay)/(float) un->GetJumpStatus().delay;
100  JumpAnimations[k].a->SetDimensions( dd, dd );
101  }
102  }
103  }
104  unsigned int i;
105  for (i = 0; i < JumpAnimations.size(); ++i)
106  if (JumpAnimations[i].a)
107  JumpAnimations[i].a->Draw();
108  for (i = 0; i < VolatileJumpAnimations.size(); ++i)
109  if (VolatileJumpAnimations[i].a) {
110  float hei, wid;
111  VolatileJumpAnimations[i].a->GetDimensions( hei, wid );
112  VolatileJumpAnimations[i].a->SetDimensions( VolatileJumpAnimations[i].percent*hei,
113  VolatileJumpAnimations[i].percent*wid );
114  if ( VolatileJumpAnimations[i].a->Done() ) {
115  delete VolatileJumpAnimations[i].a;
117  --i;
118  } else {
119  VolatileJumpAnimations[i].a->Draw();
120  }
121  }
122 }
123 
125 {
126  Vector p, q, r;
127  un->GetOrientation( p, q, r );
128  unsigned int myani = AddJumpAnimation( un->LocalPosition(), un->rSize()*game_options.jumpgatesize, true );
129  VolatileJumpAnimations[myani].a->SetOrientation( p, q, r );
130 }
131 
133 {
134  int ani;
135  Vector p, q, r;
136  un->GetOrientation( p, q, r );
137  ani = AddJumpAnimation( un->Position()+r.Cast()*un->rSize()*(un->GetJumpStatus().delay+.25), 10*un->rSize() );
138  static int jumpleave = AUDCreateSound( game_options.jumpleave, false );
139  AUDPlay( jumpleave, un->LocalPosition(), un->GetVelocity(), 1 );
140  return ani;
141 }
142