Advertisement
Redxone

[ZSC] radixdoom_lk : HUD Source

Dec 29th, 2018
675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.67 KB | None | 0 0
  1.  
  2.  
  3. class Radix_HUD : BaseStatusBar
  4. {
  5.     HUDFont mHUDFont;
  6.     Font numFont;
  7.    
  8.     int defwidth, defheight;
  9.    
  10.     int xhair_lock;
  11.     int seconds_timer;
  12.     float fade;
  13.     bool dofadeout;
  14.    
  15.     bool draw_penergy;
  16.     bool draw_pshield;
  17.     bool draw_pjets;
  18.     bool draw_pinvul;
  19.    
  20.     Array<Int> timers;
  21.    
  22.     float HUDAlpha;
  23.     string TmpHUDText; // Used for displaying things like "weapon" selected stuff.
  24.     float TmpTextTimer; // TTT :P
  25.    
  26.     bool Blink(int tics, bool onoff, int tselect=0)
  27.     {
  28.         int dotime;
  29.         dotime = timers[tselect];
  30.        
  31.         if(onoff)
  32.         {
  33.             dotime++;
  34.             if(dotime == tics) onoff = false;
  35.         }
  36.         else
  37.         {
  38.             dotime--;
  39.             if(dotime == 0) onoff = true;
  40.         }
  41.         timers[tselect] = dotime;
  42.        
  43.         return onoff;
  44.     }
  45.    
  46.     // References:
  47.     //   DrawImage(image, pos, flags, alpha, boxsize, scale);
  48.     //   DrawString(mHUDFont, str, pos, flags, translation, HUDAlpha, wrap, spacing);
  49.    
  50.     // All H_ function auto adjust to the screen resolution based on 1080p coords.
  51.     void H_DrawImage(Name img, vector2 pos, uint flags=0, vector2 scale=(1,1), vector2 bsize = (-1,-1))
  52.     {
  53.         vector2 hscale = GetHUDScale();
  54.         hscale = ( int(hscale.x), int(hscale.x) ); // Force even scalar.
  55.         float rwidth  = (Screen.getWidth() / float(defwidth)) / hscale.x;
  56.         float rheight = Screen.getHeight() / float(defheight) / hscale.y;
  57.         vector2 rscale = (scale.x * rwidth, scale.y * rheight);
  58.         vector2 rpos = ( pos.x*rwidth, pos.y*rheight );
  59.        
  60.         DrawImage(img, rpos, flags, HUDAlpha, bsize, rscale);
  61.     }
  62.    
  63.     void H_SetClipRect(int x, int y, int width, int height)
  64.     {
  65.         vector2 hscale = GetHUDScale();
  66.         hscale = ( int(hscale.x), int(hscale.x) ); // Force even scalar.   
  67.         float rwidth  = (Screen.getWidth() / float(defwidth)) / hscale.x;
  68.         float rheight = Screen.getHeight() / float(defheight) / hscale.y;
  69.         vector2 rpos = ( x*rwidth, y*rheight );
  70.        
  71.         SetClipRect( rpos.x, rpos.y, width*rwidth, height*rheight );
  72.     }
  73.    
  74.     void H_DrawString(String str, vector2 pos, uint flags = 0, int translation = Font.CR_UNTRANSLATED)
  75.     {
  76.         vector2 hscale = GetHUDScale();
  77.         hscale = ( int(hscale.x), int(hscale.x) ); // Force even scalar.
  78.         float rwidth  = (Screen.getWidth() / float(defwidth)) / hscale.x;
  79.         float rheight = Screen.getHeight() / float(defheight) / hscale.y;
  80.         vector2 rpos = ( pos.x*rwidth, pos.y*rheight );
  81.        
  82.         DrawString(mHUDFont, str, rpos, flags, translation, HUDAlpha);
  83.     }
  84.    
  85.     void H_DrawNumbers(String strnums, vector2 pos, vector2 scale=(1,1), int spacing=1, bool transp=false)
  86.     {  
  87.         // Due to the way the scaling is done this needs to be completely custom done.
  88.         String lmp = "HNUMS";
  89.         if(transp) lmp = "TNUMS";
  90.         for(int i = 0; i < strnums.Length(); i++)
  91.         {
  92.             String lmp_num = lmp..strnums.CharAt(i);
  93.             H_DrawImage(lmp_num,pos,0,scale);
  94.             pos.x += (numFont.GetCharWidth("0")+spacing)*scale.x;
  95.         }
  96.     }
  97.  
  98.     override void Init()
  99.     {
  100.        
  101.         Super.Init();
  102.         SetSize(32, 320, 200);
  103.        
  104.         defwidth  = 1920;
  105.         defheight = 1080;
  106.        
  107.         HUDAlpha = 1.0;
  108.        
  109.         timers.Resize(4); // Setup powerup blink timers.
  110.  
  111.         // Create the font used for the fullscreen HUD
  112.         Font fnt = "dbigfont";
  113.         mHUDFont = HUDFont.Create(fnt);
  114.         numFont = "RADIXNUMFONT";
  115.     }
  116.  
  117.     override void Draw (int state, double TicFrac)
  118.     {
  119.         Super.Draw (state, TicFrac);
  120.    
  121.         if (state == HUD_StatusBar)
  122.         {
  123.             BeginStatusBar();
  124.             DrawMainBar (TicFrac);
  125.             DrawGlobalHUD();
  126.         }
  127.         else if (state == HUD_Fullscreen)
  128.         {
  129.             BeginHUD();
  130.             DrawFullScreenStuff ();
  131.             DrawGlobalHUD();
  132.         }
  133.     }
  134.  
  135.    
  136.     override void Tick()
  137.     {
  138.         //console.printf("%d",seconds_timer);
  139.    
  140.         // Easy way to detect whenever another map is started.
  141.         if(Level.maptime == 0) seconds_timer = 0;
  142.    
  143.         let plr = RadixShip(CPlayer.mo);
  144.         if(plr)
  145.         {
  146.             if(Level.maptime%35 == 0 && plr.health > 0) seconds_timer++;
  147.             if( TmpHUDText != plr.hVars.TextMsg )
  148.             {
  149.                 TmpTextTimer = 35*5;
  150.                 TmpHUDText = plr.hVars.TextMsg;
  151.             }
  152.             // Clear HUD text so we are able to print the same thing later.
  153.             if(TmpTextTimer <= 0 && TmpHUDText != "")
  154.             {
  155.                 TmpHUDText = "";
  156.                 plr.ClearHUDText();
  157.             }
  158.         } // We don't simply return here because super.tick.
  159.        
  160.         if(TmpTextTimer > 0) TmpTextTimer --;
  161.        
  162.         Super.tick();
  163.     }
  164.    
  165.     void Draw_Weapons()
  166.     {
  167.         let plr = RadixShip(CPlayer.mo);
  168.         if(!plr) return;
  169.        
  170.         // Draw ammo counts.
  171.         H_DrawNumbers(""..plr.countinv("RadixShells"),(198,810),(6.0,4.6));
  172.         H_DrawNumbers(""..plr.countinv("RadixMissiles"),(456,810),(6.01,4.6));
  173.         H_DrawNumbers(""..plr.countinv("RadixTorpedos"),(198,860),(6.0,4.6));
  174.         H_DrawNumbers(""..plr.countinv("RadixNukes"),(456,860),(6.01,4.6));
  175.    
  176.         // Display got weapons and selected weapon/weapon ammo use.
  177.         for( let w = plr.Inv; w != null; w = w.Inv )
  178.         {
  179.             let weap = RadixWeapon(w);
  180.             if(!weap) continue;
  181.            
  182.             int slotnum = weap.SlotNumber;
  183.             int selnum = plr.currentWeaponID;
  184.             int xspacing = (slotnum-1)*50;
  185.                    
  186.             if(slotnum != selnum)
  187.             {
  188.                 H_DrawImage("WEAPGOT"..slotnum,(175+xspacing,769),0,(6.0,4.5));
  189.             }
  190.             else
  191.             {
  192.                 H_DrawImage("WEAPSEL"..slotnum,(175+xspacing,769),0,(6.0,4.5));
  193.                 if(weap.AmmoType1)
  194.                 {
  195.                     String wammo = getDefaultByType(weap.AmmoType1).GetClassName();
  196.                     if(wammo == "RadixShells")
  197.                     {
  198.                         H_DrawImage("AMMOSEL",(192,824),0,(6.0,4.5));
  199.                     }
  200.                     if(wammo == "RadixMissiles")
  201.                     {
  202.                         H_DrawImage("AMMOSEL",(450,824),0,(6.0,4.5));
  203.                     }
  204.                     if(wammo == "RadixTorpedos")
  205.                     {
  206.                         H_DrawImage("AMMOSEL",(192,873),0,(6.0,4.5));
  207.                     }
  208.                     if(wammo == "RadixNukes")
  209.                     {
  210.                         H_DrawImage("AMMOSEL",(450,873),0,(6.0,4.5));
  211.                     }
  212.                     if(wammo == "GravityWaves")
  213.                     {
  214.                         int num = plr.countinv("GravityWaves");
  215.                         String formatnum = num < 10 ? "0"..num : ""..num;
  216.                         H_DrawNumbers(formatnum,(442,719),(6.0,4.6),1,true);
  217.                     }
  218.                 }
  219.             }
  220.            
  221.         }
  222.     }
  223.    
  224.     void Draw_Radar()
  225.     {
  226.         if(CPlayer && CPlayer.mo)
  227.         {
  228.             float scale = 10;
  229.             let plr = RadixShip(CPlayer.mo);
  230.             if(!plr) return;
  231.             ThinkerIterator act_it = ThinkerIterator.Create("Actor");
  232.             Actor obj;
  233.                        
  234.             // Draw player on radar.
  235.             H_DrawImage("RADARPLR",(981,773),0,(3.0,3.0));     
  236.            
  237.             while(obj = Actor(act_it.Next()))
  238.             {
  239.                 int dis = ExtendedMaths.point_distance(obj,Actor(CPlayer.mo),scale);
  240.                 if( dis < 70 )
  241.                 {
  242.                     int x = plr.x - obj.x;
  243.                     int y = plr.y - obj.y;
  244.                     int dx = ( y * cos((plr.angle) )) - ( x * sin((plr.angle)) );
  245.                     int dy = ( y * sin((plr.angle) )) + ( x * cos((plr.angle)) );
  246.                     dx /= scale;
  247.                     dy /= scale;
  248.                     // If we're not the player.
  249.                     let nplr = RadixShip(obj);
  250.                     if( nplr != plr )
  251.                     {
  252.                         if(obj.bSHOOTABLE) H_DrawImage("RADARIND",(dx-940,dy-124),0,(3.0,3.0));
  253.                     }
  254.                 }
  255.             }
  256.         }
  257.     }
  258.  
  259.     void DrawKeys()
  260.     {
  261.         if(!CPlayer) return;
  262.        
  263.         bool locks[6];
  264.         String image;
  265.         for(int i = 0; i < 6; i++) locks[i] = CPlayer.mo.CheckKeys(i + 1, false, true);
  266.         // key 1
  267.         if (locks[1] && locks[4]) image = "STKEYS6";
  268.         else if (locks[1]) image = "STKEYS0";
  269.         else if (locks[4]) image = "STKEYS3";
  270.         H_DrawImage(image, (32, 48+32),0,(6.0,4.6));
  271.         // key 2
  272.         if (locks[2] && locks[5]) image = "STKEYS7";
  273.         else if (locks[2]) image = "STKEYS1";
  274.         else if (locks[5]) image = "STKEYS4";
  275.         else image = "";
  276.         H_DrawImage(image, (32, 96+32),0,(6.0,4.6));
  277.         // key 3
  278.         if (locks[0] && locks[3]) image = "STKEYS8";
  279.         else if (locks[0]) image = "STKEYS2";
  280.         else if (locks[3]) image = "STKEYS5";
  281.         else image = "";
  282.         H_DrawImage(image, (32, 144+32),0,(6.0,4.6));
  283.     }
  284.  
  285.     // Drawn on Both statusbar AND Fullscreen HUD.
  286.     void DrawGlobalHUD()
  287.     {
  288.         if(TmpTextTimer > 0)
  289.         {
  290.             H_DrawString(TmpHUDText,(16,16),0,Font.CR_ORANGE);
  291.         }
  292.     }
  293.  
  294.     void DrawMainBar (double TicFrac)
  295.     {  
  296.     }
  297.  
  298.     void DrawFullScreenStuff ()
  299.     {
  300.         let plr = RadixShip(CPlayer.mo);
  301.         if(!plr) return;
  302.        
  303.         // Scalar for energy bar.
  304.         float energyFactor = plr.energy > 0 ? plr.energy / plr.maxenergy : 0;
  305.         int energyScalar = (325*energyFactor);
  306.         // Scalar for armor.
  307.         float armorFactor = plr.shields > 0 ? plr.shields / plr.maxShields : 0;
  308.         float armorScalar = 325*armorFactor;
  309.         // Scalar for health.
  310.         float healthFactor = (plr.health > 0 && plr.MaxHealth > 0) ? float(plr.health) / plr.MaxHealth : 0;
  311.         float healthScalar = 325*healthFactor;
  312.         // Scalar for throttle
  313.         float throttlemod = plr.boosting ? 1.0 : plr.IsMoving() ? plr.throttle : plr.cruise;
  314.        
  315.         float throttleFactor = throttlemod > 0 ? (throttlemod*100) / 100 : 0;
  316.         float throttleScalar = 174*throttleFactor;
  317.        
  318.         Name wgraphic = plr.weapGraphic;
  319.         xhair_lock = clamp(int(plr.locktimer*10)+1, 1, 7);
  320.        
  321.         int threat = plr.Threat ? 1 : 0;
  322.  
  323.         // Draw background
  324.         H_DrawImage( "CPITBG", (960,900), 0, (6.0, 4.5) );
  325.        
  326.         // Cockpit
  327.         H_DrawImage( "COCKPIT", (960,900), 0, (6.0, 4.5) );
  328.         // Threat
  329.         H_DrawImage( "THREAT" .. threat, (960,140), 0, (6.0, 5.3) );
  330.         // Radar
  331.         H_DrawImage( "RADAR", (978,842), 0, (6.0, 4.6) );
  332.         // Make sure we're not in the middle of a kewl powerup animation thingy.
  333.         if(plr.powerup) return;
  334.         Draw_Radar();
  335.         // Crosshair
  336.         H_DrawImage( "CRSHAIR" .. xhair_lock, (960,480), 0, (5.0, 3.5) );
  337.         // Select Weapon Graphic
  338.         H_DrawImage(wgraphic, (327,729), 0, (6.0,4.6));
  339.         Draw_Weapons();
  340.         // Throttle
  341.         H_SetClipRect(648, 666, throttleScalar,32);
  342.         String thrott = "THROTTLE";
  343.         if(plr.countinv("Power_Jets") && plr.boosting) thrott = "THRTBOST";
  344.         H_DrawImage( thrott, (735,699), 0, (6.0,4.85));
  345.         // Armor bar
  346.         H_SetClipRect(1212, 702, healthScalar,30);
  347.         H_DrawImage( "ARMRBAR", (1376,720), 0, (6.05,3.5));
  348.         // Sheild bar
  349.         H_SetClipRect(1212, 769, armorScalar,30);
  350.         H_DrawImage( "SHLDBAR", (1376,788), 0, (6.05,3.6));
  351.         // Energy bar
  352.         H_SetClipRect(1398, 837, energyScalar,30);
  353.         H_DrawImage( "ENRGBAR", (1562,855), 0, (6.05,3.6));
  354.        
  355.         ClearClipRect(); // Clear all clip rects.
  356.  
  357.         // Draw PlasmaBombs (Use transparent nums)
  358.         H_DrawImage( "PBOMBX", (1725,60), 0, (6.05,3.6));
  359.         int numbombs = plr.countinv("RadixBombs");
  360.         string strbombs = numbombs < 10 ? "0"..numbombs : ""..numbombs;
  361.         H_DrawNumbers(strbombs,(1777,50),(6.0,4.6),1,true);
  362.        
  363.         // Draw level time.
  364.         int lvlmins = clamp(seconds_timer/60, 0, 99); // 99 mins maximum time.
  365.         int lvlsecs = seconds_timer - (lvlmins*60);
  366.         String secs = (lvlsecs < 10 ? "0" .. lvlsecs : ""..lvlsecs);
  367.         String mins = (lvlmins < 10 ? "0" .. lvlmins : ""..lvlmins);
  368.  
  369.         H_DrawNumbers(secs,(738,851),(6.0,4.6));
  370.         H_DrawNumbers(mins,(654,851),(6.0,4.6));
  371.        
  372.         // Draw enemy count
  373.         int kills = Level.killed_monsters;
  374.         int enemies = Level.total_monsters;
  375.         String kprefix = "";
  376.         String eprefix = "";
  377.         if(kills < 10){
  378.             kprefix = "00";
  379.         } else if(kills < 100){
  380.             kprefix = "0";
  381.         }
  382.         if(enemies < 10){
  383.             eprefix = "00";
  384.         } else if(enemies < 100){
  385.             eprefix = "0";
  386.         }
  387.         String formatkills   = kprefix .. kills;
  388.         String formatenemies = eprefix .. enemies;
  389.        
  390.         H_DrawNumbers(formatkills.."",(875,662),(6.1,4.6));
  391.         H_DrawNumbers(formatenemies.."",(1020,662),(6.01,4.6));
  392.        
  393.         let penergy = Power_EnergyRegen(plr.FindInventory("Power_EnergyRegen"));
  394.         let pshield = Power_ShieldRegen(plr.FindInventory("Power_ShieldRegen"));
  395.         let pjets   = Power_Jets(plr.FindInventory("Power_Jets"));
  396.         let pinvuln = Power_UltraShields(plr.FindInventory("Power_UltraShields"));
  397.         if(!penergy) timers[0] = 0;
  398.         if(!pshield) timers[1] = 0;
  399.         if(!pjets  ) timers[2] = 0;
  400.         if(!pinvuln) timers[3] = 0;
  401.         // Energy regen.
  402.         if( penergy && penergy.EffectTics <= 35*5 )
  403.             draw_penergy = Blink(15,draw_penergy,0);
  404.         else if(penergy)
  405.             draw_penergy = true;
  406.         else
  407.             draw_penergy = false;
  408.         // Shield regens
  409.         if( pshield && pshield.EffectTics <= 35*5 )
  410.             draw_pshield = Blink(15,draw_pshield,1);
  411.         else if(pshield)
  412.             draw_pshield = true;
  413.         else
  414.             draw_pshield = false;
  415.         // Jets.
  416.         if( pjets && pjets.EffectTics <= 35*5 )
  417.             draw_pjets = Blink(15,draw_pjets,2);
  418.         else if(pjets)
  419.             draw_pjets = true;
  420.         else
  421.             draw_pjets = false;
  422.         // Invulnurability (Ultrashields).
  423.         if( pinvuln && pinvuln.EffectTics <= 35*5 )
  424.             draw_pinvul = Blink(15,draw_pinvul,3);
  425.         else if(pinvuln)
  426.             draw_pinvul = true;
  427.         else
  428.             draw_pinvul = false;
  429.        
  430.         if(plr.countinv("Effect_PBombFlash"))
  431.         {  
  432.             Screen.Dim(Color(255,255,255,255), fade, 0, 0, Screen.GetWidth(), Screen.GetHeight());
  433.             fade += 0.06;
  434.             dofadeout = true;
  435.         }
  436.         else if(dofadeout)
  437.         {
  438.             if(fade <= 0) dofadeout = false;
  439.             Screen.Dim(Color(255,255,255,255), fade, 0, 0, Screen.GetWidth(), Screen.GetHeight());
  440.             fade -= 0.06;
  441.         }
  442.        
  443.         // Draw powerups
  444.         if(draw_pshield)H_DrawImage( "POWSHLD", (1792,146), 0, (6.0, 4.6) );
  445.         if(draw_penergy)H_DrawImage( "POWENRG", (1792,238), 0, (6.0, 4.6) );
  446.         if( draw_pjets )H_DrawImage( "POWJETS", (1792,338), 0, (6.0, 4.6) );
  447.         if( draw_pinvul)H_DrawImage( "POWUSLD", (1792,438), 0, (6.0, 4.6) );
  448.        
  449.         //Draw doom keys
  450.         DrawKeys();
  451.        
  452.     }
  453. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement