Guest User

Untitled

a guest
Jun 3rd, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.86 KB | None | 0 0
  1. import "Scripts/Functions/ReleaseTableObjects.lua"
  2.  
  3. Script.health = 100 --float "Health"
  4. Script.maxHealth = 100 --float "Max Health"
  5. Script.mouseSensitivity = 15 --float "Mouse sensitivity"
  6. Script.camSmoothing = 2 --float "Cam smoothing"
  7. Script.moveSpeed = 2.5 --float "Move Speed"
  8. Script.speedMultiplier = 1.5 --float "Run Multiplier"
  9. Script.strafeSpeed = 4 --float "Strafe Speed"
  10. Script.playerHeight = 1.8 --float "Player Height"
  11. Script.jumpForce = 8 --float "Jump Force"
  12. Script.flashlighton = false --bool "Flashlight on"
  13. Script.fluctuation=1.0 --float "Zippo fluctation"
  14. Script.fluctuationRange = Vec2(0.2,1.8) --Vec2 "Fluctuation Range"
  15. Script.smoothedfluctuation=1.0
  16. Script.useDistance = 2
  17. Script.alive=true
  18. Script.eyeheight=1.6
  19. Script.footstepwalkdelay = 500
  20. Script.footsteprundelay = 300
  21. Script.weaponfile=""--path "Weapon" "Prefab (*.pfb):pfb|Prefabs"
  22. Script.input={}
  23. Script.maxcarryweight=5
  24. Script.throwforce = 500
  25. Script.isairborne=false
  26. Script.bloodindex=1
  27. Script.teamid=1--choice "Team" "Neutral,Good,Bad"
  28. Script.hurtoffset=Vec3(0)
  29. Script.smoothedhurtoffset=Vec3(0)
  30. Script.mouseDifference = Vec2(0,0)
  31. Script.playerMovement = Vec3(0,0,0)
  32. Script.tempJumpForce = 0
  33. Script.drawHUD = true
  34.  
  35. function Script:CycleWeapon(direction)
  36. local n,weapon
  37. local foundindex=false
  38. local prevweapon
  39.  
  40. if direction==1 then
  41. for n,weapon in pairs(self.weapons) do
  42. if foundindex then
  43. self:SelectWeapon(n)
  44. return
  45. end
  46. if self.currentweaponindex==n then
  47. foundindex=true
  48. end
  49. end
  50. if foundindex then
  51. for n,weapon in pairs(self.weapons) do
  52. self:SelectWeapon(n)
  53. return
  54. end
  55. end
  56. else
  57. for n,weapon in pairs(self.weapons) do
  58. if prevweapon then
  59. if self.currentweaponindex==n then
  60. self:SelectWeapon(prevweapon)
  61. return
  62. end
  63. end
  64. prevweapon=n
  65. end
  66. if prevweapon then
  67. self:SelectWeapon(prevweapon)
  68. end
  69. end
  70. end
  71. function Script:DropWeapon()--in
  72. for n,weapon in pairs(self.weapons) do
  73. weapon.entity:Hide()
  74. end
  75.  
  76. self.suspendfire = true
  77. self.drawHUD = false
  78. end
  79. function Script:AddWeapon(weapon)
  80. if weapon.index==nil then
  81. weapon.index=1
  82. end
  83. if self.weapons[weapon.index]==nil then
  84. self.weapons[weapon.index]=weapon
  85. self.weapons[weapon.index].player = self
  86. self.weapons[weapon.index].entity:SetParent(self.weapontag)
  87. self.weapons[weapon.index].entity:SetPosition(self.weapons[weapon.index].offset)
  88. if self.weapons[weapon.index].rotation~=nil then
  89. self.weapons[weapon.index].entity:SetPosition(self.weapons[weapon.index].rotation)
  90. end
  91. self.weapons[weapon.index].entity:Hide()
  92. if weapon.index>self.currentweaponindex then self:SelectWeapon(weapon.index) end
  93. if self.sound.pickupweapon~=nil then self.sound.pickupweapon:Play() end
  94. return true
  95. end
  96. return false
  97. end
  98.  
  99. function Script:SelectWeapon(index)
  100. if index~=self.currentweaponindex then
  101. if self.weapons[self.currentweaponindex]~=nil then
  102. self.weapons[self.currentweaponindex].entity:Hide()
  103. end
  104. self.currentweaponindex = index
  105. if self.weapons[self.currentweaponindex]~=nil then
  106. self.weapons[self.currentweaponindex].entity:Show()
  107. end
  108. self.weaponlowerangle=90
  109. self.suspendfire=false
  110. self.weapontag:SetRotation(self.weaponlowerangle,0,0)
  111. end
  112. end
  113.  
  114. --This function will be called when an entity is loaded in a map. Use this for intitial setup stuff.
  115. function Script:Start()
  116.  
  117. self.weapons={}
  118. self.currentweaponindex=-1
  119.  
  120. self.camRotation = self.entity:GetRotation(true)
  121. self.weaponlowerangle=0
  122.  
  123. self.image={}
  124. self.image.crosshair = Texture:Load("Materials/HUD/crosshair.tex")
  125. self.image.hand = Texture:Load("Materials/HUD/use.tex")
  126.  
  127. self.image.blood={}
  128. self.image.blood[1]=Texture:Load("Materials/HUD/blood1.tex")
  129. self.image.blood[2]=Texture:Load("Materials/HUD/blood2.tex")
  130. self.image.blood[3]=Texture:Load("Materials/HUD/blood3.tex")
  131. self.image.blood[4]=Texture:Load("Materials/HUD/blood4.tex")
  132.  
  133. --Load shared sounds
  134. self.sound={}--table to store sound in
  135. self.sound.flashlight=Sound:Load("Sound/Player/flashlight_02_on.wav")
  136. self.sound.damage={}
  137. self.sound.damage[1]=Sound:Load("Sound/Impact/body_punch_03.wav")
  138. self.sound.damage[2]=Sound:Load("Sound/Impact/body_punch_04.wav")
  139. self.sound.pickupweapon=Sound:Load("Sound/Player/pickupammo.wav")
  140. self.sound.footsteps={}
  141. self.sound.footsteps.concrete={}
  142. self.sound.footsteps.concrete.step={}
  143. self.sound.footsteps.concrete.step[1] = Sound:Load("Sound/Footsteps/Concrete/step1.wav")
  144. self.sound.footsteps.concrete.step[2] = Sound:Load("Sound/Footsteps/Concrete/step2.wav")
  145. self.sound.footsteps.concrete.step[3] = Sound:Load("Sound/Footsteps/Concrete/step3.wav")
  146. self.sound.footsteps.concrete.step[4] = Sound:Load("Sound/Footsteps/Concrete/step4.wav")
  147. self.sound.footsteps.concrete.jump = Sound:Load("Sound/Footsteps/Concrete/jump.wav")
  148.  
  149. self.bloodoverlay={}
  150.  
  151. self.entity:SetPickMode(0)
  152.  
  153. --Set the type for this object to player
  154. self.entity:SetKeyValue("type","player")
  155.  
  156. local mass = self.entity:GetMass()
  157. if self.entity:GetMass()==0 then Debug:Error("Player mass should be greater than 0.") end
  158.  
  159. --Create a camera
  160. self.camera = Camera:Create()
  161. self.camera:SetFOV(70)
  162. self.camera:SetRange(0.05,1000)
  163. self.camera:SetMultisampleMode((System:GetProperty("multisample","1")))
  164.  
  165. --Set the camera's rotation to match the player
  166. self.camera:SetRotation(self.entity:GetRotation(true))
  167.  
  168. --Set the camera position at eye height
  169. self.camera:SetPosition(self.entity:GetPosition(true)+Vec3(0,self.eyeheight,0))
  170.  
  171. --Create listener
  172. self.listener = Listener:Create(self.camera)
  173.  
  174. --Create flashlight
  175. self.flashlight = PointLight:Create()
  176. self.flashlight:SetParent(self.camera,false)
  177. self.flashlight:SetPosition(0.2,-0.1,0)
  178. self.flashlight:SetRotation(10,-3,0)
  179.  
  180. --self.flashlight:SetConeAngles(25,15)
  181. self.flashlight:SetShadowMode(Light.Dynamic+Light.Static)
  182. if self.flashlighton==false then
  183. self.flashlight:Hide()
  184. end
  185.  
  186. --Load the default weapon, if one is set
  187. self.weapontag = Pivot:Create(self.camera)
  188.  
  189. --Hitbox for bullets
  190. self.entity:SetCollisionType(Collision.Character)
  191. local hitbox = Model:Box(0.5,1.8,0.5)
  192. hitbox:SetPosition(self.entity:GetPosition())
  193. hitbox:Translate(0,0.9,0)
  194. hitbox:SetParent(self.entity,true)
  195. hitbox:SetCollisionType(Collision.Prop)
  196. hitbox:SetShadowMode(0)
  197.  
  198. ---------------------------------------------------------------------------
  199. --We want the player model visible in the editor, but invisible in the game
  200. --We can achieve this by creating a material, setting the blend mode to make
  201. --it invisible, and applying it to the model.
  202. ---------------------------------------------------------------------------
  203. local material = Material:Create()
  204. material:SetBlendMode(5)--Blend.Invisible
  205. self.entity:SetMaterial(material)
  206. hitbox:SetMaterial(material)
  207. material:Release()
  208. self.entity:SetShadowMode(0)
  209.  
  210. local window = Window:GetCurrent()
  211. local context = Context:GetCurrent()
  212. window:SetMousePosition(Math:Round(context:GetWidth()/2), Math:Round(context:GetHeight()/2))
  213.  
  214. self.camera:SetRotation(self.camRotation)
  215.  
  216. if self.weaponfile~="" then
  217. local prefab = Prefab:Load(self.weaponfile)
  218. if prefab~=nil then
  219. if prefab.script~=nil then
  220. self:AddWeapon(prefab.script)
  221. else
  222. prefab:Release()
  223. end
  224. end
  225. end
  226. end
  227.  
  228. function Script:Release()
  229. self.listener:Release()
  230. self.flashlight:Release()
  231. if self.corpse~=nil then
  232. self.corpse:Release()
  233. self.corpse=nil
  234. end
  235. ReleaseTableObjects(self.sound)
  236. ReleaseTableObjects(self.image)
  237. local k,v
  238. for k,v in pairs(self.weapons) do
  239. v:Release()
  240. end
  241. end
  242.  
  243. function Script:Collision(entity,position,normal,speed)
  244. if speed>20 then
  245. self:Hurt(100)
  246. end
  247. end
  248.  
  249. function Script:Hurt(damage,distributorOfPain)
  250. if self.health>0 then
  251. self.sound.damage[math.random(#self.sound.damage)]:Play()
  252. self.health = self.health - damage
  253. self.hurtoffset = Vec3(math.random(-1,1),math.random(-1,1),0):Normalize()*30
  254. local blood = {}
  255. local n=1
  256. blood.texture=self.image.blood[math.random(1,4)]
  257. blood.intensity=1
  258. table.insert(self.bloodoverlay,blood)
  259. if self.bloodindex>4 then self.bloodindex=1 end
  260. if self.health<=0 then
  261. self:Kill()
  262. end
  263. end
  264. end
  265.  
  266. function Script:Kill()
  267. self.corpse = Pivot:Create()
  268. local shape = Shape:Load("Models/Characters/Generic/corpse.phy")--this shape is made from a low-poly CSG sphere, so it will roll around a bit but come to a stop quickly
  269. self.corpse:SetShape(shape)
  270. if shape~=nil then shape:Release() end
  271. self.flashlight:Hide()
  272. if self.weapons[self.currentweaponindex]~=nil then
  273. self.weapons[self.currentweaponindex]:Hide()
  274. end
  275. self.corpse:SetMass(5)
  276. self.corpse:SetMatrix(self.camera:GetMatrix())
  277. self.camera:SetParent(self.corpse)
  278. self.camera:SetPosition(0,0,0)
  279. self.camera:SetRotation(0,0,0)
  280. self.corpse:SetCollisionType(Collision.Prop)
  281. self.corpse:SetSweptCollisionMode(true)
  282. self.entity:SetCollisionType(0)
  283. self.corpse:SetFriction(10,10)
  284. local maxomega=5
  285. self.corpse:SetOmega(Vec3(math.random(-maxomega,maxomega),math.random(-maxomega,maxomega),math.random(-maxomega,maxomega)))
  286. local v = self.entity:GetVelocity()
  287. if v:Length()>1 then
  288. v=v:Normalize()
  289. end
  290. self.corpse:SetVelocity(Vec3(math.random(-1,1),math.random(-1,1),math.random(-1,1)))
  291. self.entity:SetMass(0)
  292. self.entity:SetPhysicsMode(Entity.RigidBodyPhysics)
  293. end
  294.  
  295. function Script:FindUsableEntity(entity)
  296. while entity~=nil do
  297. if entity.script then
  298. if type(entity.script.Use)=="function" then
  299. --If "enable" has not been set, it still won't be "false" so this will pass:
  300. if entity.script.enabled~=false then
  301. return entity
  302. else
  303. return nil
  304. end
  305. end
  306. end
  307. entity = entity:GetParent()
  308. end
  309. return nil
  310. end
  311.  
  312. function Script:UpdateWorld()
  313. local currenttime = Time:GetCurrent()
  314.  
  315. if self.flashlight:Hidden() == false then
  316. --if self.fluctuation==nil then self:Start() end
  317. self.fluctuation=self.fluctuation+math.random(-100,100)/50.0*Time:GetSpeed()
  318. self.fluctuation=math.min(self.fluctuationRange.y,self.fluctuation)
  319. self.fluctuation=math.max(self.fluctuationRange.x,self.fluctuation)
  320. local pitlightmove = Math:Curve(self.fluctuation,self.smoothedfluctuation,5/Time:GetSpeed()*110)
  321. self.smoothedfluctuation=Math:Curve(self.fluctuation,self.smoothedfluctuation,5.0/Time:GetSpeed())
  322. self.flashlight:SetColor(1.0*self.smoothedfluctuation,0.6*self.smoothedfluctuation,0.25*self.smoothedfluctuation,1,0)
  323. end
  324.  
  325. if self.lastweaponhittesttime==nil then self.lastweaponhittesttime=0 end
  326. if currenttime - self.lastweaponhittesttime>100 then
  327. self.lastweaponhittesttime=currenttime
  328. local pickinfo=PickInfo()
  329. local p1 = Transform:Point(0,0,0,self.camera,nil)
  330. local p2 = Transform:Point(0,0,0.6,self.camera,nil)
  331. local pickmode = self.entity:GetPickMode()
  332. self.entity:SetPickMode(0)
  333. --if self.entity.world:Pick(p1,p2,pickinfo,0.25) then
  334. -- self.weaponlowered=true
  335. --else
  336. -- self.weaponlowered=false
  337. --end
  338. self.entity:SetPickMode(pickmode)
  339. end
  340. if self.weaponlowered then
  341. self.weaponlowerangle = self.weaponlowerangle + 4 * Time:GetSpeed()
  342. else
  343. self.weaponlowerangle = self.weaponlowerangle - 4 * Time:GetSpeed()
  344. end
  345. self.weaponlowerangle = math.max(0,math.min(self.weaponlowerangle,90))
  346. self.weapontag:SetRotation(self.weaponlowerangle,0,0)
  347.  
  348. --Exit the function early if the player is dead
  349. if self.health<=0 then return end
  350.  
  351. local window = Window:GetCurrent()
  352. local context=Context:GetCurrent()
  353.  
  354. if window:KeyHit(Key.P) then
  355. self.camera:SetDebugPhysicsMode(true)
  356. end
  357.  
  358. if self.isairborne==true then
  359. if self.entity:GetAirborne()==false then
  360. if self.weapons[self.currentweaponindex]~=nil then
  361. self.weapons[self.currentweaponindex]:BeginLand()
  362. end
  363. end
  364. end
  365. self.isairborne = self.entity:GetAirborne()
  366.  
  367. --Mouse look
  368. self.currentMousePos = window:GetMousePosition()
  369. window:SetMousePosition(Math:Round(context:GetWidth()/2), Math:Round(context:GetHeight()/2))
  370. self.currentMousePos.x = Math:Round(self.currentMousePos.x)
  371. self.currentMousePos.y = Math:Round(self.currentMousePos.y)
  372.  
  373. if self.mousezpos==nil then self.mousezpos=0 end
  374. if self.currentMousePos.z~=self.mousezpos then
  375. if self.weapons[self.currentweaponindex] then
  376. if self.weapons[self.currentweaponindex].currentaction==nil then
  377. for n=1,math.abs(self.currentMousePos.z-self.mousezpos) do
  378. if self.currentMousePos.z>self.mousezpos then
  379. self:CycleWeapon(-1)
  380. else
  381. self:CycleWeapon(1)
  382. end
  383. end
  384. self.mousezpos=self.currentMousePos.z
  385. end
  386. else
  387. self.mousezpos=self.currentMousePos.z
  388. end
  389. end
  390.  
  391. self.mouseDifference.x = Math:Curve(self.currentMousePos.x - Math:Round(context:GetWidth()/2),self.mouseDifference.x,3)
  392. self.mouseDifference.y = Math:Curve(self.currentMousePos.y - Math:Round(context:GetHeight()/2),self.mouseDifference.y,3)
  393. self.camRotation.x = Math:Clamp(self.camRotation.x + self.mouseDifference.y / self.mouseSensitivity,-90,90)
  394. self.camRotation.y = self.camRotation.y + (self.mouseDifference.x / self.mouseSensitivity)
  395.  
  396. --Adjust the view shake
  397. self.hurtoffset.x = Math:Inc(0,self.hurtoffset.x,2*Time:GetSpeed())
  398. self.hurtoffset.y = Math:Inc(0,self.hurtoffset.y,2*Time:GetSpeed())
  399. self.smoothedhurtoffset.x = Math:Curve(self.hurtoffset.x,self.smoothedhurtoffset.x,3)
  400. self.smoothedhurtoffset.y = Math:Curve(self.hurtoffset.y,self.smoothedhurtoffset.y,3)
  401.  
  402. --Set the camera angle
  403. self.camera:SetRotation(self.camRotation+self.smoothedhurtoffset)
  404.  
  405. --Picking and usage
  406. local pickInfo = PickInfo()
  407.  
  408. --Raycast Pick that is being send from the camera in to the world
  409. self.canUse = false
  410.  
  411. local fire = false
  412. local currentime = Time:GetCurrent()
  413. if self.carryingEntity==nil then
  414. if self.weapons[self.currentweaponindex]~=nil then
  415. if self.weapons[self.currentweaponindex].automatic then
  416. if window:MouseDown(1) then
  417. fire=true
  418. else
  419. self.suspendfire=false
  420. end
  421. else
  422. if window:MouseHit(1) then
  423. fire=true
  424. end
  425. end
  426. end
  427. end
  428.  
  429. --Fire weapon
  430. if self.carryingEntity==nil then
  431. if fire then
  432. if self.suspendfire~=true then
  433. if self.weapons[self.currentweaponindex].clipammo==0 and self.weapons[self.currentweaponindex].automatic==true then
  434. self.suspendfire=true
  435. end
  436. self.weapons[self.currentweaponindex]:Fire()
  437. end
  438. end
  439. end
  440.  
  441. --Throw object if holding one
  442. if self.carryingEntity then
  443. if window:MouseHit(1) then
  444. local dir = Transform:Vector(0,0,self.throwforce,self.camera,nil)
  445. self.carryingEntity:AddForce(dir)
  446. self:DropEntityCarrying()
  447. end
  448. end
  449.  
  450. if window:KeyHit(Key.R) then
  451. if self.weapons[self.currentweaponindex]~=nil then
  452. if type(self.weapons[self.currentweaponindex].CanReload)=="function" then
  453. if self.weapons[self.currentweaponindex]:CanReload() then
  454. self.suspendfire=false
  455. self.weapons[self.currentweaponindex]:Reload()
  456. end
  457. end
  458. end
  459. end
  460.  
  461. if window:KeyHit(Key.E) then
  462. if self.carryingEntity then
  463. self:DropEntityCarrying()
  464. else
  465. local p0 = self.camera:GetPosition(true)
  466. local p1 = Transform:Point(0,0,self.useDistance,self.camera,nil)
  467. if self.entity.world:Pick(p0,p1, pickInfo, 0, true) then
  468.  
  469. --Looks for any entity in the hierarchy that has a "Use" function
  470. local usableentity = self:FindUsableEntity(pickInfo.entity)
  471.  
  472. if usableentity~=nil then
  473.  
  474. --Use the object, whatever it may be
  475. usableentity.script:Use(self)
  476.  
  477. else
  478. if self.carryingEntity == nil then
  479. mass = pickInfo.entity:GetMass()
  480.  
  481. --Pick up object if it isn't too heavy
  482. if mass>0 and mass<=self.maxcarryweight then
  483. self.carryingEntity = pickInfo.entity
  484. self.carryingobjectcollisiontype = self.carryingEntity:GetCollisionType()
  485. self.carryingEntity:SetCollisionType(Collision.Debris)
  486. self.carryrotation = Transform:Rotation(pickInfo.entity:GetQuaternion(true),nil,self.camera)
  487. self.carryposition = Transform:Point(pickInfo.entity:GetPosition(true),nil,self.camera)
  488. end
  489. end
  490. end
  491. end
  492. end
  493. end
  494.  
  495. --The icon that shows that an object can be picked up or can be interacted with
  496. --Amnesia fan, I see. :D
  497. if self.carryingEntity == nil then
  498. local p0 = self.camera:GetPosition(true)
  499. local p1 = Transform:Point(0,0,self.useDistance,self.camera,nil)
  500. if self.entity.world:Pick(p0,p1, pickInfo, 0, true) then
  501. if self:FindUsableEntity(pickInfo.entity)~=nil then
  502. self.canUse=true
  503. else
  504. local mass = pickInfo.entity:GetMass()
  505. if mass>0 and mass<=self.maxcarryweight then
  506. self.canUse = true
  507. end
  508. end
  509. end
  510. end
  511. end
  512.  
  513. function Script:DropEntityCarrying()
  514. self.carryingEntity:SetCollisionType(self.carryingobjectcollisiontype)
  515. self.carryingEntity = nil
  516. end
  517.  
  518. --This function plays footstep sounds in regular intervals as the player walks
  519. function Script:UpdateFootsteps()
  520. if self.lastfootsteptime==nil then self.lastfootsteptime=0 end
  521. if self.input[0]~=0 or self.input[1]~=0 then
  522. local speed = self.entity:GetVelocity():xz():Length()
  523. if self.entity:GetAirborne()==false then
  524. if (speed>self.moveSpeed*0.5) then
  525. local t = Time:GetCurrent()
  526. local repeatdelay = self.footstepwalkdelay
  527. if speed>self.moveSpeed * (1+(self.speedMultiplier-1)*0.5) then repeatdelay = self.footsteprundelay end
  528. if t-self.lastfootsteptime>repeatdelay then
  529. self.lastfootsteptime = t
  530. local index = math.random(1,4)
  531. self.sound.footsteps.concrete.step[index]:Play()
  532. end
  533. end
  534. end
  535. end
  536. end
  537.  
  538. --This function will be called once per physics update
  539. function Script:UpdatePhysics()
  540.  
  541. --Exit the function early if the player is dead
  542. if self.health<=0 then return end
  543.  
  544. local window = Window:GetCurrent()
  545.  
  546. --Fade out the screen blood
  547. if self.bloodintensity~=nil then
  548. if self.bloodintensity>0 then
  549. self.bloodintensity = self.bloodintensity-0.01
  550. self.bloodintensity = math.max(0,self.bloodintensity)
  551. end
  552. end
  553.  
  554. --Update the footstep sounds when walking
  555. self:UpdateFootsteps()
  556.  
  557. --Toggle the flash light on and off
  558. if window:KeyHit(Key.F) then
  559. self.sound.flashlight:Play()
  560. if self.flashlight:Hidden() then
  561. self.flashlight:Show()
  562. else
  563. self.flashlight:Hide()
  564. end
  565. end
  566.  
  567. --Apply forces to make the carried object move the way we want
  568. if self.carryingEntity then
  569. local currentpos = self.carryingEntity:GetPosition(true)
  570.  
  571. local pos = Transform:Point(self.carryposition,self.camera,nil)
  572. local rot = Transform:Rotation(self.carryrotation,self.camera,nil)
  573.  
  574. local maxdiff = 0.5
  575. local diff = pos:DistanceToPoint(currentpos)
  576.  
  577. --Drop the carryinItem when the distance between camera and item exceed the pickdistance
  578. if diff>1.5 then
  579. self:DropEntityCarrying()
  580. else
  581. if diff>maxdiff then
  582. pos = currentpos + (pos-currentpos):Normalize()*maxdiff
  583. diff = maxdiff
  584. end
  585. self.carryingEntity:PhysicsSetPosition(pos.x,pos.y,pos.z,0.25)
  586. self.carryingEntity:PhysicsSetRotation(rot,0.5)
  587. end
  588. end
  589.  
  590. --Player Movement
  591. local movex=0
  592. local movez=0
  593. self.input[0]=0
  594. self.input[1]=0
  595. if window:KeyDown(Key.W) then self.input[1]=self.input[1]+1 end
  596. if window:KeyDown(Key.S) then self.input[1]=self.input[1]-1 end
  597. if window:KeyDown(Key.D) then self.input[0]=self.input[0]+1 end
  598. if window:KeyDown(Key.A) then self.input[0]=self.input[0]-1 end
  599.  
  600. local playerMovement = Vec3(0)
  601. playerMovement.x = self.input[0] * self.moveSpeed
  602. playerMovement.z = self.input[1] * self.moveSpeed
  603.  
  604. --This prevents "speed hack" strafing due to lazy programming
  605. if self.input[0]~=0 and self.input[1]~=0 then
  606. playerMovement = playerMovement * 0.70710678
  607. end
  608.  
  609. --if self.entity:GetAirborne() then
  610. -- playerMovement = playerMovement * 0.2
  611. --end
  612.  
  613. --Check for running with shift and when not carrying anything
  614. if self.carryingEntity == nil and window:KeyDown(Key.Shift) then
  615. playerMovement.z = playerMovement.z * self.speedMultiplier
  616. end
  617.  
  618. -- Check for jumping
  619. local jump = 0
  620. if window:KeyHit(Key.Space) and self:IsAirborne() == 0 then
  621. jump = self.jumpForce
  622.  
  623. self.sound.footsteps.concrete.jump:Play()
  624.  
  625. if self.weapons[self.currentweaponindex]~=nil then
  626. self.weapons[self.currentweaponindex]:BeginJump()
  627. end
  628.  
  629. --Give the player an extra boost when jumping
  630. playerMovement = playerMovement * 1.6
  631. end
  632.  
  633. -- Check for crouching
  634. --if App.window:KeyHit(Key.ControlKey) then
  635. -- crouched = not crouched
  636. --end
  637.  
  638. --With smoothing
  639. --Position camera at correct height and playerPosition
  640. self.entity:SetInput(self.camRotation.y, playerMovement.z, playerMovement.x, jump , false, 1.0, 0.5, true)
  641. local playerPos = self.entity:GetPosition()
  642. local newCameraPos = self.camera:GetPosition()
  643. --local playerTempHeight = ((self:IsCrouched() == 1) and crouchHeight or playerHeight)
  644. newCameraPos = Vec3(playerPos.x, newCameraPos.y ,playerPos.z)
  645.  
  646. if newCameraPos.y<playerPos.y + self.eyeheight then
  647. newCameraPos.y = Math:Curve(playerPos.y + self.eyeheight, newCameraPos.y, self.camSmoothing)
  648. else
  649. newCameraPos.y = playerPos.y + self.eyeheight
  650. end
  651.  
  652. self.camera:SetPosition(newCameraPos)
  653. end
  654.  
  655. --Return whether the player is airborne
  656. function Script:IsAirborne()
  657. return self.entity:GetAirborne() and 1 or 0
  658. end
  659.  
  660. function Script:PostRender(context)
  661. context:SetBlendMode(Blend.Alpha)
  662.  
  663. -----------------------------------------------------------------------
  664. --Draw the blood overlay on the screen to indicate damage
  665. local k,v
  666. for k,v in pairs(self.bloodoverlay) do
  667. if v.intensity>0 then
  668. context:SetColor(1,1,1,v.intensity*0.5)
  669. context:DrawImage(v.texture,0,0,context:GetWidth(),context:GetHeight())
  670. if self.health>0 then
  671. v.intensity = v.intensity-0.02 * Time:GetSpeed()
  672. end
  673. else
  674. self.bloodoverlay[k]=nil
  675. end
  676. end
  677. context:SetColor(1,1,1,1)
  678.  
  679. if self.health>0 then
  680. if self.canUse==true and self.carryingEntity == nil then
  681. local pickUpX = math.floor((context:GetWidth() - self.image.hand:GetWidth()))/2
  682. local pickUpY = math.floor((context:GetHeight() - self.image.hand:GetHeight()))/2
  683. context:SetBlendMode(Blend.Alpha)
  684. context:DrawImage(self.image.hand, pickUpX, pickUpY)
  685. else
  686. if self.carryingEntity==nil then
  687. if self.weapons[self.currentweaponindex]~=nil then
  688. if self.drawHUD then
  689. if self.image.crosshair then
  690. local crossHairX = math.floor((context:GetWidth() - self.image.crosshair:GetWidth()))/2
  691. local crossHairY = math.floor((context:GetHeight() - self.image.crosshair:GetHeight()))/2
  692. context:SetBlendMode(Blend.Alpha)
  693. context:DrawImage(self.image.crosshair, crossHairX, crossHairY)
  694. end
  695. end
  696. end
  697. end
  698. end
  699. end
  700.  
  701. context:SetBlendMode(1)
  702. context:SetColor(0,0,0,0.5)
  703. local indent=8
  704. local w = 180
  705. local h = 40
  706. end
  707.  
  708. --Return whether the player is crouching
  709. function Script:IsAlive()
  710. return self.alive and 1 or 0
  711. end
  712.  
  713. --TakeDamage
  714. function Script:TakeDamage(damage)
  715.  
  716. --Decrease health
  717. self.health = self.health - damage;
  718.  
  719. --Call OnHit output
  720. self:OnHit()
  721.  
  722. --If health lower or equal to zero, the player is dead
  723. if self.health <= 0 then
  724. self.alive = false
  725. --Call the OnDead output
  726. self:OnDead()
  727. end
  728. end
  729.  
  730. --Increase health
  731. function Script:ReceiveHealth(healthPoints)--in
  732. --Increase health
  733. self.health = self.health + healthPoints;
  734.  
  735. --Health can not be more then maximum health
  736. if self.health > self.maxHealth then
  737. self.health = self.maxHealth
  738. end
  739.  
  740. --Call Health received output
  741. self.component:CallOutputs("HealthReceived")
  742. end
  743.  
  744. --when health is zero or lower, an output call is made
  745. function Script:OnDead()--out
  746. --Extra check to make sure that the player is no longer alive
  747. if not(self:IsAlive()) then
  748. self.component:CallOutputs("Ondead")
  749. end
  750. end
  751.  
  752. --when the player gets damaged we call this output
  753. function Script:OnHit()--out
  754. self.component:CallOutputs("OnHit")
  755. end
Advertisement
Add Comment
Please, Sign In to add comment