Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.00 KB | None | 0 0
  1. --this script is from the Leadwerks game engine and only for private use
  2.  
  3.  
  4. import "Scripts/AnimationManager.lua"
  5.  
  6. Script.index=1--int "Weapon slot"
  7. Script.offset=Vec3(0,0,0)--Vec3 "Offset"
  8. Script.rotation=Vec3(0,0,0)--Vec3 "Rotation"
  9. Script.clipsize=6--int "Clip size"
  10. Script.ammo=200--int "Ammunition"
  11. Script.maxswayamplitude=0.01
  12. Script.amplitude=0
  13. Script.swayspeed=0
  14. Script.timeunits=0
  15. Script.smoothedposition=Vec3(0)
  16. Script.smoothedrotation=Vec3(0)
  17. Script.verticalbob=0
  18. Script.jumpoffset=0
  19. Script.bulletspeed=200--float "Bullet speed"
  20. Script.landoffset=0
  21. Script.pellets=1--int "Pellets"
  22. Script.scatter=0.01--float "Scatter"
  23. Script.firetime=0
  24. Script.refirerate=100--int "Refire rate"
  25. Script.bulletrange=1000
  26. Script.muzzleflashscale=0.2--float "Flash size"
  27. Script.bulletforce=500--float "Force"
  28. Script.bulletdamage=10--int "Damage"
  29. Script.automatic=true--bool "Automatic"
  30. Script.dryfiresoundfile=""--path "Dry fire" "Wav File (*wav):wav|Sound"
  31. Script.fire1soundfile=""--path "Fire sound 1" "Wav File (*wav):wav|Sound"
  32. Script.fire2soundfile=""--path "Fire sound 2" "Wav File (*wav):wav|Sound"
  33. Script.fire3soundfile=""--path "Fire sound 3" "Wav File (*wav):wav|Sound"
  34. Script.pumpsoundfile=""--path "Pump sound" "Wav File (*wav):wav|Sound"
  35. Script.ricochet1soundfile=Sound:Load("Sound/Ricochet/bullet_impact_dirt_01.wav")
  36. Script.ricochet2soundfile=Sound:Load("Sound/Ricochet/bullet_impact_dirt_02.wav")
  37. Script.ricochet3soundfile=Sound:Load("Sound/Ricochet/bullet_impact_dirt_03.wav")
  38. Script.reloadsoundfile=""--path "Reload sound" "Wav File (*wav):wav|Sound"
  39. Script.reloadspeed=0.05--float "Reload speed"
  40. Script.firespeed=0.05--float "Fire speed"
  41. Script.tracer = Sprite:Create()
  42. Script.tracer:Hide()
  43. Script.currentaction=nil
  44.  
  45. function Script:Start()
  46.     if self.started then return end
  47.     self.started=true
  48.  
  49.     self.entity:SetPickMode(0,true)
  50.  
  51.     self.entity:SetAnimationFrame(0,1,"fire")
  52.  
  53.     self.entity:Hide()
  54.    
  55.     self.bullets = {}
  56.    
  57.     self.entity:SetShadowMode(0)
  58.     self.entity:SetOcclusionCullingMode(false)
  59.    
  60.     self.tracer:SetViewMode(6)--Rotate around z axis
  61.     self.tracer:SetSize(0.1,2)
  62.     local mtl = Material:Load("Materials/Effects/tracer.mat")
  63.     if mtl then
  64.         self.tracer:SetMaterial(mtl)
  65.         mtl:Release()
  66.     end
  67.  
  68.     self.currentaction=nil
  69.     self.clipammo=self.clipsize
  70.  
  71.     self.ammo = self.ammo - self.clipammo
  72.     self.muzzle = self.entity:FindChild("muzzle")
  73.     if self.muzzle1 == self.entity:FindChild("muzzle1")~= nil then
  74.         self.muzzle1 = self.entity:FindChild("muzzle1")
  75.         self.muzzle2 = self.entity:FindChild("muzzle2")
  76.     end
  77.     self.sound={}
  78.     if self.sound.dryfiresoundfile~="" then self.sound.dryfire=Sound:Load(self.dryfiresoundfile) end
  79.     self.sound.fire={}
  80.     if self.pumpsoundfile~="" then self.sound.pump=Sound:Load(self.pumpsoundfile) end
  81.     if self.fire1soundfile~="" then self.sound.fire[1]=Sound:Load(self.fire1soundfile) end
  82.     if self.fire2soundfile~="" then self.sound.fire[2]=Sound:Load(self.fire2soundfile) end
  83.     if self.fire3soundfile~="" then self.sound.fire[3]=Sound:Load(self.fire3soundfile) end
  84.     if self.reloadsoundfile~="" then
  85.         self.sound.reload=Sound:Load(self.reloadsoundfile)
  86.     end
  87.     self.sound.ricochet={}
  88.     self.sound.ricochet[1]=Sound:Load("Sound/Ricochet/bullet_impact_dirt_01.wav")
  89.     self.sound.ricochet[2]=Sound:Load("Sound/Ricochet/bullet_impact_dirt_02.wav")
  90.     self.sound.ricochet[3]=Sound:Load("Sound/Ricochet/bullet_impact_dirt_03.wav")
  91.    
  92.     self.entity:SetPickMode(0)
  93.    
  94.     self.muzzleflash = Sprite:Create()
  95.     self.muzzleflash:SetSize(self.muzzleflashscale,self.muzzleflashscale)
  96.     self.muzzleflash:SetCollisionType(0)
  97.     local material = Material:Load("Materials/Effects/muzzleflash.mat")
  98.     self.muzzleflash:SetMaterial(material)
  99.     self.muzzlelight = PointLight:Create()
  100.     self.muzzlelight:SetColor(1,0.75,0)
  101.     self.muzzlelight:SetRange(4)
  102.     self.muzzleflash:SetShadowMode(0)
  103.     local tag = self.entity:FindChild("muzzle")
  104.     self.muzzlelight:SetParent(tag)
  105.     self.muzzlelight:SetPosition(0,0,0)
  106.     self.muzzlelight:Hide()
  107.     self.muzzleflash:SetParent(self.muzzlelight,false)
  108.     self.animationmanager = AnimationManager:Create(self.entity)
  109.     self.originalposition = self.entity:GetPosition()
  110.     self.originalrotation = self.entity:GetRotation()
  111.     self.emitter={}
  112.    
  113.     --Debris emitter - This will throw chunks off of walls and make it look like they are breaking
  114.     self.emitter[0]=Emitter:Create()
  115.     self.emitter[0]:SetCollisionType(Collision.Prop)--Enables particle bouncing
  116.     self.emitter[0]:SetMaterial("Materials/Effects/default.mat")
  117.     self.emitter[0]:SetEmissionVolume(0.05,0.05,0.05)  
  118.     self.emitter[0]:SetColor(0.1,0.1,0.1,1)
  119.     self.emitter[0]:SetVelocity(1.5,1.5,1.5,1)
  120.     self.emitter[0]:SetParticleCount(5)
  121.     self.emitter[0]:SetReleaseQuantity(5)
  122.     self.emitter[0]:SetMaxScale(0.3)
  123.     self.emitter[0]:SetDuration(1500)
  124.     self.emitter[0]:SetAcceleration(0,-12,0)
  125.     self.emitter[0]:Hide()
  126.    
  127.     --Smoke emitter - This will provide a soft dust effect around bullet impacts
  128.     self.emitter[1]=Emitter:Create()
  129.     self.emitter[1]:SetColor(1,1,1,0.25)
  130.     self.emitter[1]:SetMaterial("Materials/Effects/smoke.mat")
  131.     self.emitter[1]:SetEmissionVolume(0.1,0.1,0.1)
  132.     self.emitter[1]:SetVelocity(0.3,0.3,0.3,1)
  133.     self.emitter[1]:SetParticleCount(3)
  134.     self.emitter[1]:SetReleaseQuantity(3)
  135.     self.emitter[1]:SetMaxScale(4)
  136.     self.emitter[1]:SetDuration(2500)
  137.     self.emitter[1]:AddScaleControlPoint(0,0.5)
  138.     self.emitter[1]:AddScaleControlPoint(1,1)
  139.     self.emitter[1]:SetRotationSpeed(10)
  140.     self.emitter[1]:Hide()
  141.    
  142.     --Blood emitter - This will provide a visual cue when an enemy is shot
  143.     self.emitter[2]=self.emitter[1]:Instance()
  144.     self.emitter[2] = tolua.cast(self.emitter[2],"Emitter")
  145.     self.emitter[2]:SetMaterial("Materials/Effects/bloodspatter.mat")
  146.     self.emitter[2]:SetColor(1,1,1,0.25)
  147.     self.emitter[2]:SetParticleCount(3)
  148.     self.emitter[2]:SetReleaseQuantity(3)
  149.     self.emitter[2]:SetDuration(200)
  150.     self.emitter[2]:SetEmissionVolume(0,0,0)
  151.     self.emitter[2]:SetMaxScale(1)
  152.     self.emitter[2]:SetRotationSpeed(10)
  153.     self.emitter[2]:AddScaleControlPoint(0,0)
  154.     self.emitter[2]:AddScaleControlPoint(1,1)
  155.     self.emitter[2]:SetVelocity(0,0,0,0)
  156.     self.emitter[2]:SetVelocity(0,0,0,1)
  157.     self.emitter[2]:Hide()
  158.    
  159.     --[[if self.autogive then
  160.         local player,n
  161.         for n,player in ipairs(players) do
  162.             player:AddWeapon(self,self.index)
  163.         end
  164.     end]]--
  165.  
  166.     self.entity:Hide()
  167. end
  168.  
  169. function Script:Holster()
  170.     if self.entity:FindAnimationSequence("holster")>-1 then
  171.         self.animationmanager:SetAnimationSequence("holster",self.reloadspeed,0,1)--,self,self.EndHolster,25)      
  172.     end
  173. end
  174.  
  175. function Script:Unholster()
  176.     if self.entity:FindAnimationSequence("unholster")>-1 then
  177.         self.animationmanager:SetAnimationSequence("unholster",self.reloadspeed,0,1)
  178.         self.currentaction="unholster"
  179.     end
  180. end
  181.  
  182. function Script:Hide()
  183.     self.entity:Hide()
  184.     self.muzzlelight:Hide()
  185. end
  186.  
  187. function Script:FindScriptedParent(entity,func)
  188.     while entity~=nil do
  189.         if entity.script then
  190.             if type(entity.script[func])=="function" then
  191.                 --if entity.script.enabled~=false then
  192.                     return entity
  193.                 --else
  194.                 --  return nil
  195.                 --end
  196.             end
  197.         end
  198.         entity = entity:GetParent()
  199.     end
  200.     return nil
  201. end
  202.  
  203. function Script:BeginJump()
  204.     self.jumpoffset = -180
  205. end
  206.  
  207. function Script:BeginLand()
  208.     self.landoffset = -180
  209. end
  210.  
  211. function Script:Reload()
  212.     if self.currentaction==nil then
  213.         self.cancelreload=false
  214.         if self.clipammo<self.clipsize and self.ammo>0 and self.currentaction==nil then
  215.             self.currentaction="reload"
  216.             self.reloadedshells=0
  217.             if self.entity:FindAnimationSequence("Reload")>-1 then
  218.                 self.animationmanager:SetAnimationSequence("Reload",self.reloadspeed,300,1,self,self.EndReload)
  219.                 self.reloadstarttime=Time:GetCurrent()
  220.                 if self.sound.reload~=nil then self.sound.reload:Play() end
  221.             elseif self.entity:FindAnimationSequence("ReloadStart")>-1 then
  222.                 self.animationmanager:SetAnimationSequence("ReloadStart",self.reloadspeed,300,1,self,self.EndReload)
  223.             end            
  224.         end
  225.     end
  226. end
  227.  
  228. function Script:FinalizeFire()
  229.     self.currentaction=nil
  230. end
  231.  
  232. function Script:EndFire()
  233.     if self.entity:FindAnimationSequence("Pump")>-1 then
  234.         self.animationmanager:SetAnimationSequence("Pump",0.03,300,1,self,self.FinalizeFire)
  235.         if self.sound.pump~=nil then self.sound.pump:Play() end
  236.     else
  237.         self.currentaction=nil
  238.     end
  239. end
  240.  
  241. function Script:UpdateWorld()
  242.     local bullet,n,dist
  243.     local pickinfo=PickInfo()
  244.     local firstbullet=true
  245.     local travel
  246.     self.clipsize=100
  247.     for n,bullet in ipairs(self.bullets) do
  248.        
  249.         --Check how far the bullet has travelled
  250.         dist = (bullet.position-bullet.origin):Length()
  251.         if dist>self.bulletrange then
  252.             table.remove(self.bullets,n)
  253.             bullet.sprite:Release()
  254.             bullet=nil
  255.         end
  256.        
  257.         if bullet~=nil then
  258.             travel = bullet.velocity/60.0*Time:GetSpeed()
  259.             if self.entity.world:Pick(bullet.position,bullet.position+travel,pickinfo,0,true,Collision.Projectile) then
  260.                
  261.                 --Find first parent with the Hurt() function
  262.                 local enemy = self:FindScriptedParent(pickinfo.entity,"Hurt")
  263.                
  264.                 --Bullet mark decal
  265.                 local mtl
  266.                 local scale = 0.1
  267.                 if enemy~=nil then
  268.                     mtl = Material:Load("Materials/Decals/wound.mat")
  269.                     scale = 0.1
  270.                 else
  271.                     if pickinfo.surface~=nil then
  272.                         local pickedmaterial = pickinfo.surface:GetMaterial()
  273.                         if pickedmaterial~=nil then
  274.                             rendermode = pickedmaterial:GetDecalMode()
  275.                         end
  276.                     end
  277.                     mtl = Material:Load("Materials/Decals/bulletmark.mat")
  278.                 end
  279.                 local decal = Decal:Create(mtl)
  280.                 decal:AlignToVector(pickinfo.normal,2)
  281.                 decal:Turn(0,0,Math:Random(0,360))
  282.                 decal:SetScript("Scripts/Objects/Effects/BulletMark.lua")
  283.                 if mtl~=nil then mtl:Release() end
  284.                 decal:SetPosition(pickinfo.position)
  285.                 decal:SetParent(pickinfo.entity)
  286.                
  287.                 --Apply global scaling
  288.                 local mat = decal:GetMatrix()
  289.                 mat[0] = mat[0]:Normalize() * scale
  290.                 mat[1] = mat[1]:Normalize() * scale
  291.                 mat[2] = mat[2]:Normalize() * scale
  292.                 decal:SetMatrix(mat)
  293.                
  294.                 table.remove(self.bullets,n)
  295.                
  296.                 bullet.sprite:Release()
  297.                
  298.                 if enemy~=nil then
  299.                     if enemy.script.health>0 then
  300.                         enemy.script:Hurt(self.bulletdamage,self.player)
  301.                     end
  302.                    
  303.                     --Blood emitter
  304.                     --[[e = self.emitter[2]:Instance()
  305.                     e = tolua.cast(e,"Emitter")
  306.                     e:Show()
  307.                     e:SetLoopMode(false,true)
  308.                     e:SetPosition(pickinfo.position+pickinfo.normal*0.1)
  309.                     e:SetVelocity(0,0,0)]]--
  310.                    
  311.                 else
  312.                    
  313.                     --Add a temporary particle emitter for bullet effects
  314.                     local e
  315.                    
  316.                     e = self.emitter[0]:Instance()
  317.                     e = tolua.cast(e,"Emitter")
  318.                     e:Show()
  319.                     e:SetLoopMode(false,true)
  320.                     e:SetPosition(pickinfo.position)
  321.                     local v=3
  322.                     e:SetVelocity(pickinfo.normal.x*v,pickinfo.normal.y*v,pickinfo.normal.z*v,0)
  323.                    
  324.                     --Smoke emitter
  325.                     e = self.emitter[1]:Instance()
  326.                     e = tolua.cast(e,"Emitter")
  327.                     e:Show()
  328.                     e:SetLoopMode(false,true)
  329.                     e:SetPosition(pickinfo.position+pickinfo.normal*0.1)
  330.                     local v=0.2
  331.                     e:SetVelocity(pickinfo.normal.x*v,pickinfo.normal.y*v,pickinfo.normal.z*v,0)
  332.                    
  333.                     --Play bullet impact noise
  334.                     e:EmitSound(self.sound.ricochet[math.random(#self.sound.ricochet)],30)
  335.                    
  336.                     if pickinfo.entity~=nil then
  337.                        
  338.                         --Add impulse to the hit object
  339.                         if pickinfo.entity:GetMass()>0 then
  340.                             --local force = pickinfo.normal*-1*self.bulletforce
  341.                             local dir = bullet.velocity:Normalize()
  342.                             local force = dir * self.bulletforce * math.max(0,-pickinfo.normal:Dot(dir))
  343.                             --force = force * math.max(0,-pickinfo.normal:Dot(d))--multiply by dot product of velocity and collided normal, to weaken glancing blows
  344.                             pickinfo.entity:AddPointForce(force,pickinfo.position)
  345.                         end
  346.                        
  347.                         --Extract a partial surface from the hit surface and make a bullet mark
  348.                         --To be added later
  349.                         --if pickinfo.surface~=nil then
  350.                         --  local aabb = AABB(pickinfo.position-radius,pickinfo.position+radius)   
  351.                         --  local surf = pickinfo.surface:Extract(aabb)
  352.                         --end
  353.                     end
  354.                 end
  355.                
  356.             else
  357.                 bullet.position = bullet.position+travel
  358.                 bullet.sprite:SetPosition(bullet.position - bullet.velocity:Normalize()*1)
  359.                 if bullet.sprite:Hidden() then
  360.                     dist = (bullet.position-bullet.origin):Length()
  361.                     if dist>bullet.sprite:GetSize().y then
  362.                         bullet.sprite:Show()
  363.                     end
  364.                 end
  365.             end
  366.         end
  367.         firstbullet = false
  368.     end
  369. end
  370.  
  371. function Script:Fire()
  372.         local currenttime=Time:GetCurrent()
  373.         if self.lastfiretime==nil then self.lastfiretime=0 end
  374.         if currenttime-self.lastfiretime>self.refirerate then
  375.             if self.currentaction==nil then        
  376.                 self.lastfiretime = currenttime
  377.                 if self.clipammo==0 then
  378.                     if self.sound.dryfire then
  379.                         if self.suspenddryfire~=true then
  380.                             self.sound.dryfire:Play()
  381.                         end
  382.                     end
  383.                 else
  384.                     self.currentaction="fire"
  385.                     if #self.sound.fire>0 then
  386.                         self.sound.fire[math.random(#self.sound.fire)]:Play()                  
  387.                     end
  388.                     self.firetime = Time:GetCurrent()
  389.                     self.muzzlelight:Point(Camera1,1)
  390.                     self.muzzlelight:Show()
  391.                     local a1 = 1
  392.                     if self.muzzle1 ~= nil and a1 == 1 then
  393.                         self.muzzleflash:SetPosition(self.muzzle1:GetPosition())
  394.                         a1 = 2
  395.                     end
  396.                     if self.muzzle2 ~=nil and a1 == 2 then
  397.                         self.muzzleflash:SetPosition(self.muzzle2:GetPosition())
  398.                         a1=1
  399.                     end
  400.                     self.muzzleflash:SetAngle(math.random(0,360))
  401.                     self.animationmanager:SetAnimationSequence("Fire",self.firespeed,300,1,self,self.EndFire)
  402.                    
  403.                     --Spawn bullet
  404.                     local n
  405.                     for n=1,self.pellets do
  406.                         local d = Transform:Normal(0,0,1,self.muzzleflash,nil)
  407.                         d = d + Vec3(math.random(-1,1),math.random(-1,1),math.random(-1,1)) * self.scatter
  408.                         d = d:Normalize()
  409.                         local p
  410.                         if self.muzzle then
  411.                             p=self.muzzle:GetPosition(true)
  412.                             self:SpawnBullet(p,d*self.bulletspeed)
  413.                         else
  414.                             System:Print("Warning: Muzzle entity not found.")
  415.                         end                                            
  416.                     end
  417.                 end
  418.             else
  419.                 self.cancelreload=true
  420.             end
  421.         end
  422.  
  423. end
  424.  
  425. --Creates a bullet
  426. function Script:SpawnBullet(position,velocity)
  427.     local bullet = {}
  428.     bullet.sprite = tolua.cast(self.tracer:Instance(),"Sprite")
  429.     bullet.sprite:SetPosition(position)
  430.     bullet.sprite:AlignToVector(velocity)
  431.     bullet.sprite:Hide()
  432.     bullet.position = position
  433.     bullet.origin = Vec3(position.x,position.y,position.z)
  434.     bullet.velocity = velocity
  435.     table.insert(self.bullets,bullet)
  436. end
  437.  
  438. function Script:Draw()
  439.    
  440.     local t = Time:GetCurrent()
  441.  
  442.     if self.muzzlelight:Hidden()==false then
  443.         if t-self.firetime>50 then
  444.             self.muzzlelight:Hide()
  445.         end
  446.     end
  447.    
  448.     local jumpbob = 0
  449.    
  450.     if self.jumpoffset<0 then
  451.         jumpbob = (Math:Sin(self.jumpoffset))*0.01
  452.         self.jumpoffset = self.jumpoffset + 8*Time:GetSpeed()
  453.     end
  454.    
  455.     if self.landoffset<0 then
  456.         jumpbob = jumpbob + (Math:Sin(self.landoffset))*0.01
  457.         self.landoffset = self.landoffset + 10*Time:GetSpeed()
  458.     end
  459.  
  460.     --Animate the weapon
  461.     local bob = 0;
  462.     local speed = math.max(0.1,self.player.entity:GetVelocity():xz():Length())
  463.     if self.player.entity:GetAirborne() then speed = 0.1 end
  464.     self.swayspeed = Math:Curve(speed,self.swayspeed,20)
  465.     self.swayspeed = math.max(0.5,self.swayspeed)
  466.     self.amplitude = math.max(2,Math:Curve(speed,self.amplitude,20))
  467.     self.timeunits = self.timeunits + self.swayspeed*4*Time:GetSpeed()
  468.     local sway = math.sin(self.timeunits/120.0) * self.amplitude * self.maxswayamplitude
  469.     bob = (1-math.cos(self.timeunits/60.0)) * self.maxswayamplitude * 0.1 * self.amplitude
  470.     local campos = self.player.camera:GetPosition(true)
  471.    
  472.     self.smoothedposition.x = campos.x
  473.     self.smoothedposition.y = Math:Curve(campos.y,self.smoothedposition.y,2)
  474.     self.smoothedposition.z = campos.z
  475.     self.entity:SetRotation(self.rotation) 
  476.     self.entity:SetPosition(sway*self.entity.scale.x,bob+jumpbob,0)
  477.     self.entity:Translate(self.offset,false)
  478.    
  479.     self.animationmanager:Update()
  480. end
  481.  
  482. function Script:Release()
  483.     if self.emitter~=nil then
  484.         self.emitter[0]:Release()
  485.         self.emitter[1]:Release()
  486.         self.emitter[2]:Release()
  487.         self.emitter=nil
  488.     end
  489.     if self.sound~=nil then
  490.         ReleaseTableObjects(self.sound)
  491.         self.sound=nil
  492.     end
  493. end
  494.  
  495. function Script:Cleanup()
  496.     self.tracer:Release()
  497. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement