akadjoker

md2 player

Jul 27th, 2021
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. extends Spatial
  2.  
  3.  
  4.  
  5. export var speed = 10
  6.  
  7. var x_rotation = 0
  8. var m_vPosition = Vector3()
  9. var m_vView = Vector3()
  10. var m_vUpVector= Vector3(0,1,0)
  11. var m_vStrafe= Vector3()
  12. export var turnSpeed=1.5
  13.  
  14. var isRun=false
  15. var isShoot=false
  16. var isJump=false
  17.  
  18. func Cross( vVector1, vVector2):
  19. var vNormal= Vector3()
  20. vNormal.x = ((vVector1.y * vVector2.z) - (vVector1.z * vVector2.y))
  21. vNormal.y = ((vVector1.z * vVector2.x) - (vVector1.x * vVector2.z))
  22. vNormal.z = ((vVector1.x * vVector2.y) - (vVector1.y * vVector2.x))
  23. return vNormal
  24.  
  25.  
  26.  
  27.  
  28.  
  29. func Magnitude( vNormal):
  30. return sqrt( (vNormal.x * vNormal.x) +
  31. (vNormal.y * vNormal.y) +
  32. (vNormal.z * vNormal.z) )
  33.  
  34.  
  35. func Strafe( speed):
  36. m_vPosition.x += m_vStrafe.x * speed
  37. m_vPosition.z += m_vStrafe.z * speed
  38. m_vView.x += m_vStrafe.x * speed
  39. m_vView.z += m_vStrafe.z * speed
  40.  
  41. func Move( speed):
  42. var vVector = m_vView - m_vPosition
  43. m_vPosition.x += vVector.x * speed
  44. m_vPosition.z += vVector.z * speed
  45. m_vView.x += vVector.x * speed
  46. m_vView.z += vVector.z * speed
  47.  
  48.  
  49. func Normalize( vVector):
  50. var magnitude = Magnitude(vVector)
  51. vVector = vVector / magnitude
  52. return vVector
  53.  
  54.  
  55. func Rotate( angle, x, y, z):
  56. var vNewView = Vector3()
  57. var vView = m_vView - m_vPosition
  58. var cosTheta = cos(deg2rad(angle))
  59. var sinTheta = sin(deg2rad(angle))
  60.  
  61.  
  62. vNewView.x = (cosTheta + (1 - cosTheta) * x * x) * vView.x
  63. vNewView.x += ((1 - cosTheta) * x * y - z * sinTheta) * vView.y
  64. vNewView.x += ((1 - cosTheta) * x * z + y * sinTheta) * vView.z
  65.  
  66.  
  67. vNewView.y = ((1 - cosTheta) * x * y + z * sinTheta) * vView.x
  68. vNewView.y += (cosTheta + (1 - cosTheta) * y * y) * vView.y
  69. vNewView.y += ((1 - cosTheta) * y * z - x * sinTheta) * vView.z
  70.  
  71.  
  72. vNewView.z = ((1 - cosTheta) * x * z - y * sinTheta) * vView.x
  73. vNewView.z += ((1 - cosTheta) * y * z + x * sinTheta) * vView.y
  74. vNewView.z += (cosTheta + (1 - cosTheta) * z * z) * vView.z
  75.  
  76. m_vView = m_vPosition + vNewView
  77. transform=transform.rotated(Vector3(x,y,z),deg2rad(angle))
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84. # Called when the node enters the scene tree for the first time.
  85. func _ready():
  86. $MeshInstance.addAnimation("stand", 0, 39, 9)
  87. $MeshInstance.addAnimation("run", 40, 45, 10)
  88. $MeshInstance.addAnimation("attack", 46, 53, 10)
  89. $MeshInstance.addAnimation("pain_a", 54, 57, 7)
  90. $MeshInstance.addAnimation("pain_b", 58, 61, 7)
  91. $MeshInstance.addAnimation("pain_c", 62, 65, 7)
  92. $MeshInstance.addAnimation("jump", 66, 71, 7)
  93. $MeshInstance.addAnimation("flip", 72, 83, 7)
  94. $MeshInstance.addAnimation("salute", 84, 94, 7)
  95. $MeshInstance.addAnimation("fallback", 95, 111, 10)
  96. $MeshInstance.addAnimation("wave", 112, 122, 7)
  97. $MeshInstance.addAnimation("point", 123, 134, 6)
  98. $MeshInstance.addAnimation("crouch_stand", 135, 153, 10)
  99. $MeshInstance.addAnimation("crouch_walk", 154, 159, 7)
  100. $MeshInstance.addAnimation("crouch_attack", 160, 168, 10)
  101. $MeshInstance.addAnimation("crouch_pain", 169, 172, 7)
  102. $MeshInstance.addAnimation("crouch_death", 173, 177, 5)
  103. $MeshInstance.addAnimation("death_fallback", 178, 183, 7)
  104. $MeshInstance.addAnimation("death_fallbackforward", 184, 189, 7)
  105. $MeshInstance.addAnimation("death_fallbackslow", 190, 197, 7)
  106. $MeshInstance.addAnimation("boom", 198, 198, 5)
  107. $MeshInstance.setAnimationByName("stand")
  108. m_vPosition = transform.origin
  109.  
  110.  
  111.  
  112.  
  113. func _process(delta):
  114. var vCross = Cross(m_vView - m_vPosition, m_vUpVector)
  115. m_vStrafe = Normalize(vCross)
  116. var kspeed =speed * delta
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. if Input.is_action_pressed("up") :
  126. Move(kspeed)
  127. $MeshInstance.setAnimationByName("run")
  128. #$MeshInstance.SetAnimationRollOverByName("run","stand")
  129. isRun=true
  130. if Input.is_action_pressed("turn_left") :
  131. Rotate(-turnSpeed, 0, 1, 0)
  132.  
  133. if Input.is_action_pressed("turn_right") :
  134. Rotate(turnSpeed, 0, 1, 0)
  135.  
  136. else:
  137. if Input.is_action_pressed("shoot") :
  138. #$MeshInstance.SetAnimationRollOverByName("attack","stand")
  139. $MeshInstance.setAnimationByName("attack")
  140. isShoot=true
  141. else:
  142. if Input.is_action_pressed("jump") :
  143. $MeshInstance.SetAnimationRollOverByName("jump","stand")
  144. isJump=true
  145. else:
  146. if Input.is_action_just_released("up") :
  147. isRun=false
  148. if not isShoot and not isJump and not isRun:
  149. #print($MeshInstance.get_Current_frame())
  150. $MeshInstance.setAnimationByName("stand")
  151. pass
  152.  
  153.  
  154.  
  155.  
  156. if isJump:
  157. if $MeshInstance.get_Current_frame()>=71:
  158. isJump=false
  159.  
  160. if isShoot:
  161. if $MeshInstance.get_Current_frame()>=53:
  162. isShoot=false
  163. if isRun:
  164. if $MeshInstance.get_Current_frame()>=45:
  165. isRun=false
  166.  
  167.  
  168. transform.origin=m_vPosition
  169.  
  170.  
  171.  
  172.  
  173. func _physics_process(delta):
  174. pass
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
Advertisement
Add Comment
Please, Sign In to add comment