Advertisement
pragmaticsystematic

Player_Raycast gd script

Sep 19th, 2019
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. extends Sprite
  2.  
  3. onready var ray = $RayCast2D
  4.  
  5. var speed = 256 # big number because it's multiplied by delta
  6. var tile_size = 64 # size in pixels of tiles on the grid
  7.  
  8. var last_position = Vector2() # last idle position
  9. var target_position = Vector2() # desired position to move towards
  10. var movedir = Vector2() # move direction
  11.  
  12. func _ready():
  13. position = position.snapped(Vector2(tile_size, tile_size)) # make sure player is snapped to grid
  14. last_position = position
  15. target_position = position
  16.  
  17. func _process(delta):
  18. # MOVEMENT
  19. if ray.is_colliding():
  20. var collision = ray.get_collider()
  21. if collision is TileMap:
  22. var tile_name = get_tile(collision)
  23. if !(tile_name.begins_with("Walkable")):
  24. position = last_position
  25. target_position = last_position
  26. else:
  27. position += speed * movedir * delta
  28.  
  29. if position.distance_to(last_position) >= tile_size - speed * delta: # if we've moved further than one space
  30. position = target_position # snap the player to the intended position
  31.  
  32.  
  33. else:
  34. position += speed * movedir * delta
  35.  
  36. if position.distance_to(last_position) >= tile_size - speed * delta: # if we've moved further than one space
  37. position = target_position # snap the player to the intended position
  38.  
  39. # IDLE
  40. if position == target_position:
  41. get_movedir()
  42. last_position = position # record the player's current idle position
  43. target_position += movedir * tile_size # if key is pressed, get new target (also shifts to moving state)
  44.  
  45. # GET DIRECTION THE PLAYER WANTS TO MOVE
  46. func get_movedir():
  47. var LEFT = Input.is_action_pressed("ui_left")
  48. var RIGHT = Input.is_action_pressed("ui_right")
  49. var UP = Input.is_action_pressed("ui_up")
  50. var DOWN = Input.is_action_pressed("ui_down")
  51.  
  52. movedir.x = -int(LEFT) + int(RIGHT) # if pressing both directions this will return 0
  53. movedir.y = -int(UP) + int(DOWN)
  54.  
  55. if movedir.x != 0 && movedir.y != 0: # prevent diagonals
  56. movedir = Vector2.ZERO
  57. if movedir != Vector2.ZERO:
  58. ray.cast_to = movedir * tile_size / 2
  59.  
  60. func get_tile(tile_map):
  61. if tile_map is TileMap:
  62. # Find the character's position in tile coordinates
  63. # print("World Position = {x},{y}".format({"x":position.x,"y":position.y}))
  64. # print("Raycast vector = {x},{y}".format({"x":ray.cast_to.x,"y":ray.cast_to.y}))
  65. var v = Vector2(tile_size / 2 , tile_size /2)
  66. var tile_pos = tile_map.world_to_map(position + v + ray.cast_to)
  67. var tile_id = tile_map.get_cellv(tile_pos)
  68. if (tile_id == -1):
  69. return "None"
  70. else:
  71. return tile_map.tile_set.tile_get_name(tile_id)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement