Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright (c) 2013 yvt
- This file is part of OpenSpades.
- OpenSpades is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- OpenSpades is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
- */
- namespace spades {
- class ViewSMGSkin:
- IToolSkin, IViewToolSkin, IWeaponSkin,
- BasicViewWeapon {
- private AudioDevice@ audioDevice;
- private Model@ gunModel;
- private Model@ magazineModel;
- private Model@ sightModel1;
- private Image@ reflexImage;
- private AudioChunk@[] fireSounds(4);
- private AudioChunk@ fireFarSound;
- private AudioChunk@ fireStereoSound;
- private AudioChunk@ reloadSound;
- ViewSMGSkin(Renderer@ r, AudioDevice@ dev){
- super(r);
- @audioDevice = dev;
- @gunModel = renderer.RegisterModel
- ("Models/Weapons/SMG/WeaponNoMagazine.kv6");
- @magazineModel = renderer.RegisterModel
- ("Models/Weapons/SMG/Magazine.kv6");
- @sightModel1 = renderer.RegisterModel
- ("Models/Weapons/SMG/Sight1.kv6");
- @reflexImage = renderer.RegisterImage
- ("Gfx/hcog.png");
- @fireSounds[0] = dev.RegisterSound
- ("Sounds/Weapons/SMG/FireLocal1.wav");
- @fireSounds[1] = dev.RegisterSound
- ("Sounds/Weapons/SMG/FireLocal2.wav");
- @fireSounds[2] = dev.RegisterSound
- ("Sounds/Weapons/SMG/FireLocal3.wav");
- @fireSounds[3] = dev.RegisterSound
- ("Sounds/Weapons/SMG/FireLocal4.wav");
- @fireFarSound = dev.RegisterSound
- ("Sounds/Weapons/SMG/FireFar.wav");
- @fireStereoSound = dev.RegisterSound
- ("Sounds/Weapons/SMG/FireStereo.wav");
- @reloadSound = dev.RegisterSound
- ("Sounds/Weapons/SMG/ReloadLocal.wav");
- @sightImage = renderer.RegisterImage
- ("Gfx/smgSight.tga");
- }
- void Update(float dt) {
- BasicViewWeapon::Update(dt);
- }
- void WeaponFired(){
- BasicViewWeapon::WeaponFired();
- if(!IsMuted){
- Vector3 origin = Vector3(0.4f, -0.3f, 0.5f);
- AudioParam param;
- param.volume = 8.f;
- audioDevice.PlayLocal(fireSounds[GetRandom(fireSounds.length)], origin, param);
- param.volume = 4.f;
- audioDevice.PlayLocal(fireFarSound, origin, param);
- param.volume = 1.f;
- audioDevice.PlayLocal(fireStereoSound, origin, param);
- }
- }
- void ReloadingWeapon() {
- if(!IsMuted){
- Vector3 origin = Vector3(0.4f, -0.3f, 0.5f);
- AudioParam param;
- param.volume = 0.2f;
- audioDevice.PlayLocal(reloadSound, origin, param);
- }
- }
- float GetZPos() {
- return 0.2f - AimDownSightStateSmooth * 0.038f;
- }
- // rotates gun matrix to ensure the sight is in
- // the center of screen (0, ?, 0).
- Matrix4 AdjustToAlignSight(Matrix4 mat, Vector3 sightPos, float fade) {
- Vector3 p = mat * sightPos;
- mat = CreateRotateMatrix(Vector3(0.f, 0.f, 1.f), atan(p.x / p.y) * fade) * mat;
- mat = CreateRotateMatrix(Vector3(-1.f, 0.f, 0.f), atan(p.z / p.y) * fade) * mat;
- return mat;
- }
- void Draw2D() {
- if(AimDownSightState > 0.6) {
- // draw reflex image
- float reflexOpacity = (AimDownSightState > 0.8f) ? AimDownSightState * 5.f - 4.f : 0.f;
- renderer.ColorP = Vector4(reflexOpacity, reflexOpacity, reflexOpacity, 0.0f); // premultiplied alpha
- float sightSize = 0.019f;
- // scale sight according to the fov value
- float fov = cg_fov.FloatValue;
- fov = tan(fov * 0.5f * 3.141592654f / 180.f);
- sightSize /= fov;
- sightSize *= renderer.ScreenHeight;
- // scale sight according to the distance from the eye to the sight
- Matrix4 sightMat = GetViewWeaponMatrix();
- Vector3 sightPos = sightMat * Vector3(0.f, 0.f, 0.f);
- float scale = 1.f / sightPos.y;
- sightSize *= scale;
- renderer.DrawImage(reflexImage,
- AABB2((renderer.ScreenWidth - sightSize) * 0.5f,
- (renderer.ScreenHeight - sightSize) * 0.5f,
- sightSize, sightSize));
- return;
- }
- BasicViewWeapon::Draw2D();
- }
- void AddToScene() {
- Matrix4 mat = CreateScaleMatrix(0.033f);
- mat = GetViewWeaponMatrix() * mat;
- bool reloading = IsReloading;
- float reload = ReloadProgress;
- Vector3 leftHand, rightHand;
- leftHand = mat * Vector3(1.f, 6.f, 1.f);
- rightHand = mat * Vector3(0.f, -8.f, 2.f);
- Vector3 leftHand2 = mat * Vector3(5.f, -10.f, 4.f);
- Vector3 leftHand3 = mat * Vector3(1.f, 6.f, -4.f);
- Vector3 leftHand4 = mat * Vector3(1.f, 9.f, -6.f);
- if(AimDownSightStateSmooth > 0.8f){
- mat = AdjustToAlignSight(mat, Vector3(0.f, 5.f, -4.9f), (AimDownSightStateSmooth - 0.8f) / 0.2f);
- }
- ModelRenderParam param;
- Matrix4 weapMatrix = eyeMatrix * mat;
- param.matrix = weapMatrix * CreateScaleMatrix(0.25f) *
- CreateTranslateMatrix(-0.25f, 0.0f, 2.0f);
- param.depthHack = true;
- renderer.AddModel(gunModel, param);
- // draw sights
- Matrix4 sightMat = weapMatrix;
- sightMat *= CreateTranslateMatrix(0.0f, -4.0f, 0.1f);
- sightMat *= CreateScaleMatrix(0.06f);
- param.matrix = sightMat;
- renderer.AddModel(sightModel1, param); // HCOG
- // magazine/reload action
- mat *= CreateTranslateMatrix(0.f, 3.f, 1.f);
- reload *= 2.5f;
- if(reloading) {
- if(reload < 0.7f){
- // magazine release
- float per = reload / 0.7f;
- mat *= CreateTranslateMatrix(0.f, 0.f, per*per*50.f);
- leftHand = Mix(leftHand, leftHand2, SmoothStep(per));
- }else if(reload < 1.4f) {
- // insert magazine
- float per = (1.4f - reload) / 0.7f;
- if(per < 0.3f) {
- // non-smooth insertion
- per *= 4.f; per -= 0.4f;
- per = Clamp(per, 0.0f, 0.3f);
- }
- mat *= CreateTranslateMatrix(0.f, 0.f, per*per*10.f);
- leftHand = mat * Vector3(0.f, 0.f, 4.f);
- }else if(reload < 1.9f){
- // move the left hand to the original position
- // and start doing something with the right hand
- float per = (reload - 1.4f) / 0.5f;
- leftHand = mat * Vector3(0.f, 0.f, 4.f);
- leftHand = Mix(leftHand, leftHand3, SmoothStep(per));
- }else if(reload < 2.2f){
- float per = (reload - 1.9f) / 0.3f;
- leftHand = Mix(leftHand3, leftHand4, SmoothStep(per));
- }else{
- float per = (reload - 2.2f) / 0.3f;
- leftHand = Mix(leftHand4, leftHand, SmoothStep(per));
- }
- }
- param.matrix = weapMatrix * CreateScaleMatrix(0.25f) *
- CreateTranslateMatrix(-0.25f, 0.0f, 2.0f);
- renderer.AddModel(magazineModel, param);
- LeftHandPosition = leftHand;
- RightHandPosition = rightHand;
- }
- }
- IWeaponSkin@ CreateViewSMGSkin(Renderer@ r, AudioDevice@ dev) {
- return ViewSMGSkin(r, dev);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement