home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2007 September / maximum-cd-2007-09.iso / Assets / data / AssaultCube_v0.93.exe / source / src / hudgun.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2007-06-01  |  5.0 KB  |  154 lines

  1. #include "cube.h"
  2.  
  3. VAR(swayspeeddiv, 1, 125, 1000);
  4. VAR(swaymovediv, 1, 200, 1000); 
  5.  
  6. VAR(swayupspeeddiv, 1, 125, 1000);
  7. VAR(swayupmovediv, 1, 200, 1000); 
  8.  
  9. struct weaponmove
  10. {
  11.     static vec swaydir;
  12.     static int swaymillis, lastsway;
  13.  
  14.     float k_rot, kick;
  15.     vec pos;
  16.     int anim;
  17.     
  18.     weaponmove() : k_rot(0), kick(0), anim(0) { pos.x = pos.y = pos.z = 0.0f; }
  19.  
  20.     void calcmove(vec base, int basetime)
  21.     {
  22.         int timediff = NADE_THROWING ? (lastmillis-player1->thrownademillis) : lastmillis-basetime;
  23.         int animtime = attackdelay(player1->gunselect);
  24.         int rtime = reloadtime(player1->gunselect);
  25.         
  26.         kick = k_rot = 0.0f;
  27.         pos = player1->o;
  28.         anim = ANIM_GUN_IDLE;
  29.        
  30.         float k = pow(0.7f, (lastmillis-lastsway)/10.0f);
  31.         swaydir.mul(k);
  32.         vec dv(player1->vel);
  33.         dv.mul((1-k)/max(player1->vel.magnitude(), player1->maxspeed));
  34.         dv.x *= 1.5f;
  35.         dv.y *= 1.5f;
  36.         dv.z *= 0.4f;
  37.         swaydir.add(dv);
  38.         pos.add(swaydir);
  39.         if(player1->onfloor || player1->onladder || player1->inwater) swaymillis += lastmillis-lastsway;
  40.         lastsway = lastmillis;
  41.  
  42.         if(player1->weaponchanging)
  43.         {
  44.             anim = ANIM_GUN_RELOAD;
  45.             float percent_done = (float)(timediff)*100.0f/(float) WEAPONCHANGE_TIME;
  46.             if(percent_done>=100 || percent_done<0) percent_done = 100;
  47.             k_rot = -(sinf((float)(percent_done*2/100.0f*90.0f)*PI/180.0f)*90);
  48.         }
  49.         else if(player1->reloading)
  50.         {
  51.             anim = ANIM_GUN_RELOAD;
  52.             float percent_done = (float)(timediff)*100.0f/(float)rtime;
  53.             if(percent_done>=100 || percent_done<0) percent_done = 100;
  54.             k_rot = -(sinf((float)(percent_done*2/100.0f*90.0f)*PI/180.0f)*90);
  55.         }
  56.         else
  57.         {
  58.             vec sway = base;
  59.             float percent_done = 0.0f;
  60.             float k_back = 0.0f;
  61.             
  62.             if(player1->gunselect==player1->lastattackgun)
  63.             {
  64.                 percent_done = timediff*100.0f/(float)animtime;
  65.                 if(percent_done > 100.0f) percent_done = 100.0f;
  66.                 // f(x) = -sin(x-1.5)^3
  67.                 kick = -sinf(pow((1.5f/100.0f*percent_done)-1.5f,3));
  68.             }
  69.             
  70.             if(player1->lastaction && player1->lastattackgun==player1->gunselect)
  71.             {
  72.                 if(NADE_THROWING && timediff<animtime) anim = ANIM_GUN_THROW;
  73.                 else if(lastmillis-player1->lastaction<animtime || NADE_IN_HAND) 
  74.                     anim = ANIM_GUN_SHOOT|(player1->gunselect!=GUN_KNIFE && player1->gunselect!=GUN_GRENADE ? ANIM_LOOP : 0);
  75.             }
  76.             
  77.             if(player1->gunselect!=GUN_GRENADE && player1->gunselect!=GUN_KNIFE)
  78.             {
  79.                 k_rot = kick_rot(player1->gunselect)*kick;
  80.                 k_back = kick_back(player1->gunselect)*kick/10;
  81.             }
  82.     
  83.             float swayspeed = sinf((float)swaymillis/swayspeeddiv)/(swaymovediv/10.0f);
  84.             float swayupspeed = cosf((float)swaymillis/swayupspeeddiv)/(swayupmovediv/10.0f);
  85.  
  86.             float plspeed = min(1.0f, sqrt(player1->vel.x*player1->vel.x + player1->vel.y*player1->vel.y));
  87.             
  88.             swayspeed *= plspeed/2;
  89.             swayupspeed *= plspeed/2;
  90.  
  91.             swap(float, sway.x, sway.y);
  92.             sway.y = -sway.y;
  93.             
  94.             swayupspeed = fabs(swayupspeed); // sway a semicirle only
  95.             sway.z = 1.0f;
  96.             
  97.             sway.x *= swayspeed;
  98.             sway.y *= swayspeed;
  99.             sway.z *= swayupspeed;
  100.             
  101.             pos.x -= base.x*k_back+sway.x;
  102.             pos.y -= base.y*k_back+sway.y;
  103.             pos.z -= base.z*k_back+sway.z;
  104.         }
  105.     }
  106. };
  107.  
  108. vec weaponmove::swaydir(0, 0, 0);
  109. int weaponmove::lastsway = 0, weaponmove::swaymillis = 0;
  110.  
  111. VARP(hudgun,0,1,1);
  112.  
  113. void renderhudgun(int gun, int lastaction, int index = 0)
  114. {
  115.     vec unitv;
  116.     float dist = worldpos.dist(player1->o, unitv);
  117.     unitv.div(dist);
  118.     
  119.     weaponmove wm;
  120.     if(!intermission) wm.calcmove(unitv, lastaction);
  121.     s_sprintfd(path)("weapons/%s", gunnames[gun]);
  122.     static int lastanim[2], lastswitch[2];
  123.     if(lastanim[index]!=(wm.anim|(gun<<16)))
  124.     {
  125.         lastanim[index] = wm.anim|(gun<<16);
  126.         lastswitch[index] = lastmillis;
  127.     }
  128.     rendermodel(path, wm.anim|(index ? ANIM_MIRROR : 0), 0, 0, wm.pos.x, wm.pos.y, wm.pos.z, player1->yaw+90, player1->pitch+wm.k_rot, 40.0f, lastswitch[index], NULL, NULL, 1.28f);  
  129. }
  130.  
  131. void renderhudgun()
  132. {
  133.     if(!hudgun) return;
  134.     if(player1->akimbo && player1->gunselect==GUN_PISTOL) // akimbo
  135.     {
  136.         renderhudgun(GUN_PISTOL, player1->akimbolastaction[0], 0);
  137.         renderhudgun(GUN_PISTOL, player1->akimbolastaction[1], 1);
  138.     }
  139.     else
  140.     {
  141.         renderhudgun(player1->gunselect, player1->lastaction);
  142.     }
  143. }
  144.  
  145. void preload_hudguns()
  146. {
  147.     loopi(NUMGUNS)
  148.     {
  149.         s_sprintfd(path)("weapons/%s", gunnames[i]);
  150.         loadmodel(path);
  151.     }
  152. }
  153.  
  154.