Advertisement
KishgalYS

view.as

Dec 4th, 2014
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.96 KB | None | 0 0
  1. /*
  2.  Copyright (c) 2013 yvt
  3.  
  4.  This file is part of OpenSpades.
  5.  
  6.  OpenSpades is free software: you can redistribute it and/or modify
  7.  it under the terms of the GNU General Public License as published by
  8.  the Free Software Foundation, either version 3 of the License, or
  9.  (at your option) any later version.
  10.  
  11.  OpenSpades is distributed in the hope that it will be useful,
  12.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  GNU General Public License for more details.
  15.  
  16.  You should have received a copy of the GNU General Public License
  17.  along with OpenSpades.  If not, see <http://www.gnu.org/licenses/>.
  18.  
  19.  */
  20.  
  21.  namespace spades {
  22.     class ViewSMGSkin:
  23.     IToolSkin, IViewToolSkin, IWeaponSkin,
  24.     BasicViewWeapon {
  25.        
  26.         private AudioDevice@ audioDevice;
  27.         private Model@ gunModel;
  28.         private Model@ magazineModel;
  29.         private Model@ sightModel1;
  30.        
  31.         private Image@ reflexImage;
  32.        
  33.         private AudioChunk@[] fireSounds(4);
  34.         private AudioChunk@ fireFarSound;
  35.         private AudioChunk@ fireStereoSound;
  36.         private AudioChunk@ reloadSound;
  37.        
  38.         ViewSMGSkin(Renderer@ r, AudioDevice@ dev){
  39.             super(r);
  40.             @audioDevice = dev;
  41.             @gunModel = renderer.RegisterModel
  42.                 ("Models/Weapons/SMG/WeaponNoMagazine.kv6");
  43.             @magazineModel = renderer.RegisterModel
  44.                 ("Models/Weapons/SMG/Magazine.kv6");
  45.             @sightModel1 = renderer.RegisterModel
  46.                 ("Models/Weapons/SMG/Sight1.kv6");
  47.             @reflexImage = renderer.RegisterImage
  48.                 ("Gfx/hcog.png");
  49.                
  50.             @fireSounds[0] = dev.RegisterSound
  51.                 ("Sounds/Weapons/SMG/FireLocal1.wav");
  52.             @fireSounds[1] = dev.RegisterSound
  53.                 ("Sounds/Weapons/SMG/FireLocal2.wav");
  54.             @fireSounds[2] = dev.RegisterSound
  55.                 ("Sounds/Weapons/SMG/FireLocal3.wav");
  56.             @fireSounds[3] = dev.RegisterSound
  57.                 ("Sounds/Weapons/SMG/FireLocal4.wav");
  58.             @fireFarSound = dev.RegisterSound
  59.                 ("Sounds/Weapons/SMG/FireFar.wav");
  60.             @fireStereoSound = dev.RegisterSound
  61.                 ("Sounds/Weapons/SMG/FireStereo.wav");
  62.             @reloadSound = dev.RegisterSound
  63.                 ("Sounds/Weapons/SMG/ReloadLocal.wav");
  64.             @sightImage = renderer.RegisterImage
  65.                 ("Gfx/smgSight.tga");
  66.                
  67.         }
  68.        
  69.         void Update(float dt) {
  70.             BasicViewWeapon::Update(dt);
  71.         }
  72.        
  73.         void WeaponFired(){
  74.             BasicViewWeapon::WeaponFired();
  75.            
  76.             if(!IsMuted){
  77.                 Vector3 origin = Vector3(0.4f, -0.3f, 0.5f);
  78.                 AudioParam param;
  79.                 param.volume = 8.f;
  80.                 audioDevice.PlayLocal(fireSounds[GetRandom(fireSounds.length)], origin, param);
  81.                
  82.                 param.volume = 4.f;
  83.                 audioDevice.PlayLocal(fireFarSound, origin, param);
  84.                 param.volume = 1.f;
  85.                 audioDevice.PlayLocal(fireStereoSound, origin, param);
  86.                
  87.             }
  88.         }
  89.        
  90.         void ReloadingWeapon() {
  91.             if(!IsMuted){
  92.                 Vector3 origin = Vector3(0.4f, -0.3f, 0.5f);
  93.                 AudioParam param;
  94.                 param.volume = 0.2f;
  95.                 audioDevice.PlayLocal(reloadSound, origin, param);
  96.             }
  97.         }
  98.        
  99.         float GetZPos() {
  100.             return 0.2f - AimDownSightStateSmooth * 0.038f;
  101.         }
  102.        
  103.         // rotates gun matrix to ensure the sight is in
  104.         // the center of screen (0, ?, 0).
  105.         Matrix4 AdjustToAlignSight(Matrix4 mat, Vector3 sightPos, float fade) {
  106.             Vector3 p = mat * sightPos;
  107.             mat = CreateRotateMatrix(Vector3(0.f, 0.f, 1.f), atan(p.x / p.y) * fade) * mat;
  108.             mat = CreateRotateMatrix(Vector3(-1.f, 0.f, 0.f), atan(p.z / p.y) * fade) * mat;
  109.             return mat;
  110.         }
  111.        
  112.         void Draw2D() {
  113.             if(AimDownSightState > 0.6) {
  114.                 // draw reflex image
  115.                 float reflexOpacity = (AimDownSightState > 0.8f) ? AimDownSightState * 5.f - 4.f : 0.f;
  116.                 renderer.ColorP = Vector4(reflexOpacity, reflexOpacity, reflexOpacity, 0.0f); // premultiplied alpha
  117.                
  118.                 float sightSize = 0.019f;
  119.                
  120.                 // scale sight according to the fov value
  121.                 float fov = cg_fov.FloatValue;
  122.                 fov = tan(fov * 0.5f * 3.141592654f / 180.f);
  123.                 sightSize /= fov;
  124.                 sightSize *= renderer.ScreenHeight;
  125.                
  126.                 // scale sight according to the distance from the eye to the sight
  127.                 Matrix4 sightMat = GetViewWeaponMatrix();
  128.                 Vector3 sightPos = sightMat * Vector3(0.f, 0.f, 0.f);
  129.                 float scale = 1.f / sightPos.y;
  130.                 sightSize *= scale;
  131.                
  132.                 renderer.DrawImage(reflexImage,
  133.                     AABB2((renderer.ScreenWidth - sightSize) * 0.5f,
  134.                             (renderer.ScreenHeight - sightSize) * 0.5f,
  135.                             sightSize, sightSize));
  136.                 return;
  137.             }
  138.             BasicViewWeapon::Draw2D();
  139.         }
  140.        
  141.         void AddToScene() {
  142.             Matrix4 mat = CreateScaleMatrix(0.033f);
  143.             mat = GetViewWeaponMatrix() * mat;
  144.            
  145.             bool reloading = IsReloading;
  146.             float reload = ReloadProgress;
  147.             Vector3 leftHand, rightHand;
  148.            
  149.             leftHand = mat * Vector3(1.f, 6.f, 1.f);
  150.             rightHand = mat * Vector3(0.f, -8.f, 2.f);
  151.            
  152.                 Vector3 leftHand2 = mat * Vector3(5.f, -10.f, 4.f);
  153.             Vector3 leftHand3 = mat * Vector3(1.f, 6.f, -4.f);
  154.             Vector3 leftHand4 = mat * Vector3(1.f, 9.f, -6.f);
  155.            
  156.             if(AimDownSightStateSmooth > 0.8f){
  157.                 mat = AdjustToAlignSight(mat, Vector3(0.f, 5.f, -4.9f), (AimDownSightStateSmooth - 0.8f) / 0.2f);
  158.             }
  159.            
  160.             ModelRenderParam param;
  161.             Matrix4 weapMatrix = eyeMatrix * mat;
  162.             param.matrix = weapMatrix * CreateScaleMatrix(0.25f) *
  163.                 CreateTranslateMatrix(-0.25f, 0.0f, 2.0f);
  164.             param.depthHack = true;
  165.             renderer.AddModel(gunModel, param);
  166.            
  167.             // draw sights
  168.             Matrix4 sightMat = weapMatrix;
  169.             sightMat *= CreateTranslateMatrix(0.0f, -4.0f, 0.1f);
  170.             sightMat *= CreateScaleMatrix(0.06f);
  171.             param.matrix = sightMat;
  172.             renderer.AddModel(sightModel1, param); // HCOG
  173.            
  174.             // magazine/reload action
  175.             mat *= CreateTranslateMatrix(0.f, 3.f, 1.f);
  176.             reload *= 2.5f;
  177.             if(reloading) {
  178.                 if(reload < 0.7f){
  179.                     // magazine release
  180.                     float per = reload / 0.7f;
  181.                     mat *= CreateTranslateMatrix(0.f, 0.f, per*per*50.f);
  182.                     leftHand = Mix(leftHand, leftHand2, SmoothStep(per));
  183.                 }else if(reload < 1.4f) {
  184.                     // insert magazine
  185.                     float per = (1.4f - reload) / 0.7f;
  186.                     if(per < 0.3f) {
  187.                         // non-smooth insertion
  188.                         per *= 4.f; per -= 0.4f;
  189.                         per = Clamp(per, 0.0f, 0.3f);
  190.                     }
  191.                    
  192.                     mat *= CreateTranslateMatrix(0.f, 0.f, per*per*10.f);
  193.                     leftHand = mat * Vector3(0.f, 0.f, 4.f);
  194.                 }else if(reload < 1.9f){
  195.                     // move the left hand to the original position
  196.                     // and start doing something with the right hand
  197.                     float per = (reload - 1.4f) / 0.5f;
  198.                     leftHand = mat * Vector3(0.f, 0.f, 4.f);
  199.                     leftHand = Mix(leftHand, leftHand3, SmoothStep(per));
  200.                 }else if(reload < 2.2f){
  201.                     float per = (reload - 1.9f) / 0.3f;
  202.                     leftHand = Mix(leftHand3, leftHand4, SmoothStep(per));
  203.                 }else{
  204.                     float per = (reload - 2.2f) / 0.3f;
  205.                     leftHand = Mix(leftHand4, leftHand, SmoothStep(per));
  206.                 }
  207.             }
  208.            
  209.             param.matrix = weapMatrix * CreateScaleMatrix(0.25f) *
  210.                 CreateTranslateMatrix(-0.25f, 0.0f, 2.0f);
  211.             renderer.AddModel(magazineModel, param);
  212.            
  213.             LeftHandPosition = leftHand;
  214.             RightHandPosition = rightHand;
  215.         }
  216.        
  217.     }
  218.    
  219.     IWeaponSkin@ CreateViewSMGSkin(Renderer@ r, AudioDevice@ dev) {
  220.         return ViewSMGSkin(r, dev);
  221.     }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement