Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.29 KB | None | 0 0
  1. Script.rotSpeed = 0.1
  2. Script.target = -1 --entity "target"
  3. Script.Pivot = nil
  4. Script.sightRadius = 10--float
  5. Script.fireRate= 0.5 --float "FireRat"
  6. Script.fireRateTimer=0
  7. Script.muzzleTime = 0.1
  8. Script.muzzleTimer=0
  9. Script.weapon = ""--path "Weapon"
  10. Script.firesoundFile = nil
  11. Script.local1=0
  12. Script.bulletspeed=200--float "Bullet speed"
  13.  
  14. function Script:SetNearestEnemy(pWhich)
  15.         self.target= pWhich
  16. end
  17. function Callback(entity,extra)
  18.         if entity:GetKeyValue("type")=="enemy" then
  19.             extra.script:SetNearestEnemy(entity)
  20.         end
  21. end
  22.  
  23. function Script:Start()
  24.     self.emitter=Emitter:Create()
  25.     self.emitter:SetCollisionType(Collision.Prop)--Enables particle bouncing
  26.     self.emitter:SetMaterial("Materials/Effects/tracer1.mat")
  27.     self.emitter:SetScale(1,5,1)
  28.     self.emitter:SetRotation(90,180,0)
  29.     self.emitter:SetEmissionVolume(0.05,0.05,0.05) 
  30.     self.emitter:SetRotationByVelocityMode(true)
  31.     self.emitter:SetEmissionVolume(0,0,0)
  32.     self.emitter:SetVelocity(0,0,20)
  33.     self.emitter:SetParticleCount(1)
  34.     self.emitter:SetReleaseQuantity(5)
  35.     self.emitter:SetAcceleration(0,0,0)
  36.     self.emitter:Hide()
  37.     self.emitter:SetParent(self.entity)
  38.         self.local1 = self.entity:GetRotation()
  39.         self.muzzleFlashLight = self.entity:FindChild("MuzzleLight")
  40.         self.muzzle = self.entity:FindChild("muzzle")
  41.         self.emitter:SetPosition(self.muzzle:GetPosition())
  42.         self.muzzleFlashLight:Hide()
  43.         if self.firesoundFile ~= "" then
  44.                 self.firesoundFile = Sound:Load(self.fireSound)
  45.         end
  46.         self.entity:SetPickMode(0)
  47.         self.entity:SetRotation(self.local1)
  48. end
  49.  
  50. --[[
  51. function Script:UpdateWorld()
  52.        
  53. end
  54. --]]
  55.  
  56. function Script:Shoot()
  57. self.emitter:Reset()
  58.     self.emitter:Play()
  59. end
  60.  
  61. function Script:UpdateShooting()
  62.         self.fireRateTimer =self.fireRateTimer +(Time:GetSpeed()/100)
  63.         self.muzzleTimer =self.muzzleTimer +(Time:GetSpeed()/100)
  64.         if self.target ~= -1 and self.target ~= nil then
  65.         if(self.fireRateTimer > self.fireRate) then
  66.             self.muzzleFlashLight:Show()
  67.                 self.muzzleTimer=0
  68.                 self.fireRateTimer=0
  69.                 local d = Transform:Normal(0,0,1,self.muzzle,nil)
  70.                 local d = Vec3(math.random(-1,1),math.random(-1,1),math.random(-1,1))
  71.                 d = d:Normalize()
  72.                 local p=self.muzzle:GetPosition(true)
  73.                 self:Shoot(p,d*self.bulletspeed)
  74.         end
  75.         end
  76.         if(self.muzzleTimer > self.muzzleTime) then
  77.             self.muzzleFlashLight:Hide()
  78.         end
  79. end
  80.  
  81. function Script:UpdatePhysics()
  82.             self:UpdateShooting()
  83.             local position = self.entity:GetPosition(true)
  84.     local aabb = AABB()
  85.      aabb.min.x=position.x-self.sightRadius
  86.         aabb.min.y=position.y-self.sightRadius
  87.         aabb.min.z=position.z-self.sightRadius
  88.         aabb.max.x=position.x+self.sightRadius
  89.         aabb.max.y=position.y+self.sightRadius
  90.         aabb.max.z=position.z+self.sightRadius
  91.                 aabb:Update()
  92.  
  93.         if self.target~=-1 and self.target~= nil then if self.entity:GetPosition():DistanceToPoint(self.target:GetPosition())>30 then
  94.                 self.target=-1
  95.                 world:ForEachEntityInAABBDo(aabb,"Callback",self.entity)
  96.             end
  97.         end
  98.       --  world:ForEachEntityInAABBDo(aabb,"Callback",self.entity)
  99.        
  100.     --self.entity:Point(self.target, 2, Time:GetSpeed() * self.rotSpeed)
  101.     if self.target~=-1 and self.target~= nil then
  102.         self.vector = self.target:GetPosition(true)-self.entity:GetPosition(true)
  103.         self.entity:AlignToVector(self.vector:Normalize(),2)
  104.         local newRot = self.entity:GetRotation(true)
  105.         self.entity:SetRotation( Vec3( 90, -180+newRot.y, newRot.z ), true)
  106.     else
  107.         world:ForEachEntityInAABBDo(aabb,"Callback",self.entity)
  108.     end
  109. end
  110. --[[
  111. function Script:Collision(entity, position, normal, speed)
  112.    
  113. end
  114. --]]
  115.  
  116. --[[
  117. function Script:Draw()
  118.    
  119. end
  120. --]]
  121.  
  122. --[[
  123. function Script:DrawEach(entity)
  124.    
  125. end
  126. --]]
  127.  
  128. --[[
  129. --This function will be called after the world is rendered, before the screen is refreshed.
  130. --Use this to perform any 2D drawing you want the entity to display.
  131. function Script:PostRender(context)
  132.    
  133. end
  134. --]]
  135.  
  136. --[[
  137. --This function will be called when the entity is deleted.
  138. function Script:Detach()
  139.    
  140. end
  141. --]]
  142.  
  143. --[[
  144. --This function will be called when the last instance of this script is deleted.
  145. function Script:Cleanup()
  146.    
  147. end
  148. --]]
  149. function Script:Release()
  150.     if self.emitter~=nil then
  151.         self.emitter:Release()
  152.         self.emitter=nil
  153.     end
  154. end
  155.    
  156. function Script:Cleanup()
  157.     self.tracer:Release()
  158. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement