Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.86 KB | None | 0 0
  1. extends Spatial
  2.  
  3. # Modified version of script from Top Down Twin Stick Shooter example
  4.  
  5. export(NodePath) var PlayerPath = "" #You must specify this in the inspector!
  6. export(NodePath) var CameraPath = ""
  7. export(NodePath) var MeshInstancePath = ""
  8. export(float) var MovementSpeed = 15
  9. export(float) var Acceleration = 3
  10. export(float) var Deacceleration = 5
  11. export(float) var MaxJump = 19
  12. export(float) var RotationSpeed = 3
  13. export(float) var MaxZoom = 0.5
  14. export(float) var MinZoom = 1.5
  15. export(float) var ZoomSpeed = 2
  16.  
  17. var Player
  18. var Camera
  19. var MeshInstance
  20. var BulletPosition
  21. var RayCast
  22. var InnerGimbal
  23. var Direction = Vector3()
  24. var LastDirection = Vector3()
  25. var CameraRotation
  26. var gravity = -10
  27. var Accelerate = Acceleration
  28. var Movement = Vector3()
  29. var ZoomFactor = 1
  30. var ActualZoom = 1
  31. var Speed = Vector3()
  32. var CurrentVerticalSpeed = Vector3()
  33. var JumpAcceleration = 3
  34. var IsAirborne = false
  35. var Joystick_Deadzone = 0.2
  36. var Mouse_Deadzone = 20
  37. onready var anim = str(get_path_to(get_parent())) + "/AnimationPlayer"
  38. onready var walktoidle = str(get_path_to(get_parent())) + "/WalkToIdle"
  39.  
  40. enum ROTATION_INPUT{MOUSE, JOYSTICK, MOVE_DIR}
  41.  
  42.  
  43. func _ready():
  44. Input.set_mouse_mode(Input.MOUSE_MODE_CONFINED)
  45. Player = get_node(PlayerPath)
  46. Camera = get_node(CameraPath)
  47. MeshInstance = get_node(MeshInstancePath)
  48. BulletPosition = MeshInstance.get_child(0)
  49. RayCast = get_node(str(get_path_to(get_parent().get_parent())) + "/RayCast")
  50. InnerGimbal = $InnerGimbal
  51.  
  52. func _unhandled_input(event):
  53.  
  54. #Rotation Mesh with Joystick
  55. if event is InputEventJoypadMotion :
  56. var horizontal = Input.get_action_strength("look_right") - Input.get_action_strength("look_left")
  57. var vertical = Input.get_action_strength("look_up") - Input.get_action_strength("look_back")
  58. if abs(horizontal) > Joystick_Deadzone or abs(vertical) > Joystick_Deadzone:
  59. rotateMesh(Vector2(horizontal,vertical), ROTATION_INPUT.JOYSTICK)
  60. else:
  61. #Rotate Mesh from last Moved Direction (Left joystick)
  62. rotateMesh(Speed,ROTATION_INPUT.MOVE_DIR)
  63.  
  64. #Rotate Mesh with Mouse Motion
  65. elif event is InputEventMouseMotion:
  66. if magnitude(event.get_speed()) > Mouse_Deadzone: # or event is InputEventMouseButton:
  67. rotateMesh(event, ROTATION_INPUT.MOUSE)
  68.  
  69. else:
  70. #Rotate Mesh from last Moved Direction (WASD Key presses)
  71. rotateMesh(Speed, ROTATION_INPUT.MOVE_DIR)
  72.  
  73. #Rotate Mesh with Mouse Button Left
  74. elif event is InputEventMouseButton and event.get_button_index() == BUTTON_LEFT:
  75. rotateMesh(event, ROTATION_INPUT.MOUSE)
  76.  
  77. #Zoom
  78. if event is InputEventMouseButton:
  79. match event.button_index:
  80. BUTTON_WHEEL_UP:
  81. ZoomFactor -= 0.05
  82. BUTTON_WHEEL_DOWN:
  83. ZoomFactor += 0.05
  84. ZoomFactor = clamp(ZoomFactor, MaxZoom, MinZoom)
  85.  
  86. #Quit Game
  87. if event is InputEventKey and event.pressed:
  88. match event.scancode:
  89. KEY_ESCAPE:
  90. get_tree().quit()
  91.  
  92. func rotateMesh(event_data, input_method):
  93. match input_method:
  94. ROTATION_INPUT.MOUSE:
  95. #event_data is mouse position in viewport
  96. var rayLength = 100
  97. var from = Camera.project_ray_origin(event_data.position)
  98. var to = from + Camera.project_ray_normal(event_data.position)*rayLength
  99. RayCast.translation = from
  100. RayCast.cast_to = to
  101. RayCast.force_raycast_update()
  102. var collision_point = RayCast.get_collision_point()
  103. MeshInstance.look_at(collision_point,Vector3.UP)
  104. var rotationDegree = MeshInstance.get_rotation_degrees().y
  105. MeshInstance.set_rotation_degrees(Vector3(0,rotationDegree + 180,0))
  106. ROTATION_INPUT.JOYSTICK:
  107. #event_data is right joystick axis strength
  108. var rot = atan2(event_data.y,event_data.x)*180/PI
  109. rot += InnerGimbal.get_rotation_degrees().y
  110. rot += 90
  111. MeshInstance.set_rotation_degrees(Vector3(0,rot,0))
  112. ROTATION_INPUT.MOVE_DIR:
  113. #event_data is directional vector to rotate player
  114. #Check if Player is moving and new movement is different than last direction
  115. if magnitude(event_data) > 0 and LastDirection.dot(event_data.normalized()) != 0:
  116. #Rotate in Direction of Movement
  117. var angle = atan2(event_data.x, event_data.z)
  118. var char_rot = MeshInstance.get_rotation()
  119. var rot_y = angle - char_rot.y
  120. print(angle)
  121. MeshInstance.rotate_y(rot_y)
  122.  
  123. #Helper math function
  124. func magnitude(vector):
  125. if typeof(vector) == typeof(Vector2()):
  126. return sqrt(vector.x*vector.x + vector.y*vector.y)
  127. elif typeof(vector) == typeof(Vector3()):
  128. return sqrt(vector.x*vector.x + vector.z*vector.z)
  129.  
  130. func _process(delta):
  131. """
  132. #Shoot
  133. if (Input.is_action_pressed("shoot")):
  134. var bullet = BULLET.instance()
  135. get_node("/root/").add_child(bullet)
  136. bullet.set_translation(BulletPosition.get_global_transform().origin)
  137. bullet.direction = BulletPosition.get_global_transform().basis.z
  138. """
  139.  
  140. #Jump
  141. if (Input.is_action_pressed("jump")) and not IsAirborne:
  142. CurrentVerticalSpeed = Vector3(0,MaxJump,0)
  143. IsAirborne = true
  144.  
  145. func _physics_process(delta):
  146. #Rotation[Camera]
  147. CameraRotation = RotationSpeed * delta
  148. if (Input.is_action_pressed("rotate_left")):
  149. InnerGimbal.rotate(Vector3.UP, CameraRotation)
  150. elif (Input.is_action_pressed("rotate_right")):
  151. InnerGimbal.rotate(Vector3.UP, -CameraRotation)
  152.  
  153. #Movement
  154. var CameraTransform = Camera.get_global_transform()
  155. if(Input.is_action_pressed("move_up")):
  156. Direction += -CameraTransform.basis[2]
  157. setPlayerWalk()
  158. if(Input.is_action_pressed("move_back")):
  159. Direction += CameraTransform.basis[2]
  160. setPlayerWalk()
  161. if(Input.is_action_pressed("move_left")):
  162. Direction += -CameraTransform.basis[0]
  163. setPlayerWalk()
  164. if(Input.is_action_pressed("move_right")):
  165. Direction += CameraTransform.basis[0]
  166. setPlayerWalk()
  167.  
  168. """
  169. if Input.is_action_pressed("move"):
  170. get_node(anim).play("walk")
  171. get_node(walktoidle).set_wait_time(0.5)
  172. get_node(walktoidle).start()
  173. """
  174.  
  175. Direction.y = 0
  176. LastDirection = Direction.normalized()
  177. var MaxSpeed = MovementSpeed * Direction.normalized()
  178. Accelerate = Deacceleration
  179. if(Direction.dot(Speed) > 0):
  180. Accelerate = Acceleration
  181. Direction = Vector3.ZERO
  182. Speed = Speed.linear_interpolate(MaxSpeed, delta * Accelerate)
  183. Movement = Player.transform.basis * (Speed)
  184. Movement = Speed
  185. CurrentVerticalSpeed.y += gravity * delta * JumpAcceleration
  186. Movement += CurrentVerticalSpeed
  187. Player.move_and_slide(Movement,Vector3.UP)
  188. if Player.is_on_floor() :
  189. CurrentVerticalSpeed.y = 0
  190. IsAirborne = false
  191.  
  192. #Zoom
  193. ActualZoom = lerp(ActualZoom, ZoomFactor, delta * ZoomSpeed)
  194. InnerGimbal.set_scale(Vector3(ActualZoom,ActualZoom,ActualZoom))
  195.  
  196. func setPlayerWalk():
  197. get_node(anim).play("walk")
  198. get_node(walktoidle).set_wait_time(0.5)
  199. get_node(walktoidle).start()
  200.  
  201. func _on_WalkToIdle_timeout():
  202. get_node(str(get_path_to(get_parent())) + "/WalkToIdle").stop()
  203. print(get_node(str(get_path_to(get_parent())) + "/WalkToIdle").get_wait_time())
  204. get_node(str(get_path_to(get_parent())) + "/AnimationPlayer").play("idle")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement