Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.39 KB | None | 0 0
  1. class CEnvHypercube : public CBaseEntity {
  2.     DECLARE_CLASS( CEnvHypercube, CBaseEntity );
  3. public:
  4.     DECLARE_DATADESC();
  5.     virtual void Precache();
  6.     virtual void Spawn();
  7.     void UpdateSprites();
  8.     void InputAlpha( inputdata_t &inputdata );
  9.     void InputColor( inputdata_t &inputdata );
  10. private:
  11.     void doEdge(int& index,
  12.     int ax, int ay, int az, int aw,
  13.     int bx, int by, int bz, int bw);
  14.     EHANDLE m_hSprites[2][2][2][2];
  15.     EHANDLE m_hBeams[32];
  16. };
  17.  
  18. BEGIN_DATADESC( CEnvHypercube )
  19.     DEFINE_THINKFUNC( UpdateSprites ),
  20.     DEFINE_ARRAY( m_hSprites, FIELD_EHANDLE, 16 ),
  21.     DEFINE_ARRAY( m_hBeams, FIELD_EHANDLE, 32 ),
  22.     DEFINE_INPUTFUNC( FIELD_INTEGER, "Alpha", InputAlpha ),
  23.     DEFINE_INPUTFUNC( FIELD_COLOR32, "Color", InputColor ),
  24. END_DATADESC()
  25.  
  26. LINK_ENTITY_TO_CLASS( env_hypercube, CEnvHypercube );
  27.  
  28. // PASS COLOR TO SPRITES AND BEAMS
  29. void CEnvHypercube::InputColor( inputdata_t &inputdata ) {
  30.     for (int x = 0 ; x < 2 ; x++)
  31.     for (int y = 0 ; y < 2 ; y++)
  32.     for (int z = 0 ; z < 2 ; z++)
  33.     for (int w = 0 ; w < 2 ; w++) {
  34.         CSprite* sprite = (CSprite*) m_hSprites[x][y][z][w].Get();
  35.         if (!sprite) continue;
  36.         sprite->InputColor(inputdata);
  37.     }
  38.     for (int i = 0 ; i < 32 ; i++) {
  39.         CBeam* beam = (CBeam*) m_hBeams[i].Get();
  40.         if (!beam) continue;
  41.         beam->InputColor(inputdata);
  42.     }
  43. }
  44.  
  45. // PASS ALPHA TO SPRITES AND BEAMS
  46. void CEnvHypercube::InputAlpha( inputdata_t &inputdata ) {
  47.     for (int x = 0 ; x < 2 ; x++)
  48.     for (int y = 0 ; y < 2 ; y++)
  49.     for (int z = 0 ; z < 2 ; z++)
  50.     for (int w = 0 ; w < 2 ; w++) {
  51.         CSprite* sprite = (CSprite*) m_hSprites[x][y][z][w].Get();
  52.         if (!sprite) continue;
  53.         sprite->InputAlpha(inputdata);
  54.     }
  55.     for (int i = 0 ; i < 32 ; i++) {
  56.         CBeam* beam = (CBeam*) m_hBeams[i].Get();
  57.         if (!beam) continue;
  58.         beam->InputAlpha(inputdata);
  59.     }
  60. }
  61.  
  62. // HELPER FUNCTION TO SPAWN EDGE
  63. void CEnvHypercube::doEdge(int& index,
  64.         int ax, int ay, int az, int aw,
  65.         int bx, int by, int bz, int bw) {
  66.     CBaseEntity* a = m_hSprites[ax][ay][az][aw];
  67.     CBaseEntity* b = m_hSprites[bx][by][bz][bw];
  68.     CBeam* beam = CBeam::BeamCreate( "sprites/grenade/grenade_laser.vmt", 16.0f );
  69.     beam->PointEntInit( a->GetAbsOrigin(), b );
  70.     beam->SetWidth( 16.0f );
  71.     beam->SetEndWidth( 16.0f );
  72.     beam->SetRenderMode( kRenderTransAdd );
  73.     beam->SetRenderColor(255, 200, 255, 255);
  74.     beam->SetStartEntity(a);
  75.     beam->SetEndEntity(b);
  76.     beam->SetNoise(0.0f);
  77.     beam->Spawn();
  78.     m_hBeams[index++] = beam;
  79. }
  80.  
  81. // SPAWN
  82. void CEnvHypercube::Spawn() {
  83.     BaseClass::Spawn();
  84.     Precache();
  85.  
  86.     // create the vertices
  87.     for (int x = 0 ; x < 2 ; x++)
  88.     for (int y = 0 ; y < 2 ; y++)
  89.     for (int z = 0 ; z < 2 ; z++)
  90.     for (int w = 0 ; w < 2 ; w++) {
  91.         CSprite* sprite = CSprite::SpriteCreate( "sprites/grenade/grenade_glow.vmt", GetAbsOrigin(), true );
  92.         sprite->SetScale( 2.0f );
  93.         sprite->SetRenderMode( kRenderTransAdd );
  94.         sprite->SetRenderColor(255, 200, 255, 255);
  95.         sprite->Spawn();
  96.         m_hSprites[x][y][z][w] = sprite;
  97.     }
  98.  
  99.     int i = 0;
  100.  
  101.     // line
  102.     doEdge(i, 0, 0, 0, 0,       1, 0, 0, 0);
  103.     // square
  104.     doEdge(i, 0, 1, 0, 0,       1, 1, 0, 0);
  105.     doEdge(i, 0, 0, 0, 0,       0, 1, 0, 0);
  106.     doEdge(i, 1, 0, 0, 0,       1, 1, 0, 0);
  107.     // cube
  108.     doEdge(i, 0, 0, 1, 0,       1, 0, 1, 0);
  109.     doEdge(i, 0, 1, 1, 0,       1, 1, 1, 0);
  110.     doEdge(i, 0, 0, 1, 0,       0, 1, 1, 0);
  111.     doEdge(i, 1, 0, 1, 0,       1, 1, 1, 0);
  112.     doEdge(i, 0, 0, 0, 0,       0, 0, 1, 0);
  113.     doEdge(i, 0, 1, 0, 0,       0, 1, 1, 0);
  114.     doEdge(i, 1, 0, 0, 0,       1, 0, 1, 0);
  115.     doEdge(i, 1, 1, 0, 0,       1, 1, 1, 0);
  116.     // hypercube
  117.     doEdge(i, 0, 0, 0, 1,       1, 0, 0, 1);
  118.     doEdge(i, 0, 1, 0, 1,       1, 1, 0, 1);
  119.     doEdge(i, 0, 0, 0, 1,       0, 1, 0, 1);
  120.     doEdge(i, 1, 0, 0, 1,       1, 1, 0, 1);
  121.     doEdge(i, 0, 0, 1, 1,       1, 0, 1, 1);
  122.     doEdge(i, 0, 1, 1, 1,       1, 1, 1, 1);
  123.     doEdge(i, 0, 0, 1, 1,       0, 1, 1, 1);
  124.     doEdge(i, 1, 0, 1, 1,       1, 1, 1, 1);
  125.     doEdge(i, 0, 0, 0, 1,       0, 0, 1, 1);
  126.     doEdge(i, 0, 1, 0, 1,       0, 1, 1, 1);
  127.     doEdge(i, 1, 0, 0, 1,       1, 0, 1, 1);
  128.     doEdge(i, 1, 1, 0, 1,       1, 1, 1, 1);
  129.     doEdge(i, 0, 0, 0, 0,       0, 0, 0, 1);
  130.     doEdge(i, 0, 0, 1, 0,       0, 0, 1, 1);
  131.     doEdge(i, 0, 1, 0, 0,       0, 1, 0, 1);
  132.     doEdge(i, 0, 1, 1, 0,       0, 1, 1, 1);
  133.     doEdge(i, 1, 0, 0, 0,       1, 0, 0, 1);
  134.     doEdge(i, 1, 0, 1, 0,       1, 0, 1, 1);
  135.     doEdge(i, 1, 1, 0, 0,       1, 1, 0, 1);
  136.     doEdge(i, 1, 1, 1, 0,       1, 1, 1, 1);
  137.  
  138.     // start rotating right away
  139.     SetThink( &CEnvHypercube::UpdateSprites );
  140.     SetNextThink( gpGlobals->curtime + 0.05f );
  141. }
  142.  
  143. // PRECACHE
  144. void CEnvHypercube::Precache() {
  145.     BaseClass::Precache();
  146.     PrecacheModel( "sprites/grenade/grenade_glow.vmt" );
  147.     PrecacheModel( "sprites/grenade/grenade_laser.vmt" );
  148. }
  149.  
  150. // UPDTATE SPRITES POSITIONS
  151. void CEnvHypercube::UpdateSprites() {
  152.  
  153.     // update right after
  154.     SetNextThink( gpGlobals->curtime + 0.05f );
  155.  
  156.     // rotate in 4D
  157.     float ro = gpGlobals->curtime*-0.5f;
  158.     float cro = cosf(ro);
  159.     float sro = sinf(ro);
  160.     Vector forward, right, up;
  161.     GetVectors( &forward, &right, &up );
  162.  
  163.     // update vertices
  164.     for (int x = 0 ; x < 2 ; x++)
  165.     for (int y = 0 ; y < 2 ; y++)
  166.     for (int z = 0 ; z < 2 ; z++)
  167.     for (int w = 0 ; w < 2 ; w++) {
  168.         CSprite* sprite = (CSprite*) m_hSprites[x][y][z][w].Get();
  169.         if (!sprite) continue;
  170.         // find out 4D position
  171.         Vector pos3D = Vector(x - 0.5f, y - 0.5f, z - 0.5f)*2.0f;
  172.         float posW = (w - 0.5f)*2.0f;
  173.         // rotate
  174.         float temp = pos3D.x * cro - posW * sro;
  175.         posW = pos3D.x * sro + posW * cro;
  176.         pos3D.x = temp;
  177.         // project to 3D space
  178.         pos3D = 2.0*pos3D/(3.0+posW) * 750.0f;
  179.         sprite->SetAbsOrigin(GetAbsOrigin() + forward*pos3D.x + right*pos3D.y + up*pos3D.z);
  180.     }
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement