Advertisement
JohnDesumith

Untitled

Nov 22nd, 2023
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Godot GLSL 6.47 KB | Source Code | 0 0
  1. extends CharacterBody2D
  2.  
  3. @export var walk_speed = 4.0
  4. const TILE_SIZE = 32
  5.  
  6. signal player_entering_door_signal
  7. signal player_entered_door_signal
  8.  
  9. signal player_is_running
  10. signal player_event_signal
  11.  
  12. var initial_position = Vector2(0, 0)
  13. var input_direction = Vector2(0, 1)
  14. var is_moving = false
  15. var can_run = true
  16. var is_running = false
  17. var stop_input:bool = false
  18. var percented_moved_to_next_tile = 0.0
  19. var action_key = "Action-UiSelect"
  20. var animation_list = ["Idle", "Walk", "Turn", "Run"]
  21.  
  22. @onready var anim_tree = $AnimationTree
  23. @onready var anim_state = anim_tree.get("parameters/playback")
  24.  
  25. @onready var ray = $BlockRayCast2D
  26. @onready var tile_over_ray = $TileOverRayCast2D
  27. @onready var door_ray = $DoorRayCast2D
  28. @onready var map_edge_ray = $MapEdgeRayCast2D
  29. @onready var event_ray = $EventRayCast2D
  30. @onready var npc_ray = $NPCRayCast2D
  31.  
  32. @onready var anim_player = $AnimationPlayer
  33. @onready var player_camera = $PlayerCamera
  34.  
  35. @onready var current_collision_layer = self.get_collision_layer()
  36. @onready var current_collision_mask = self.ray.get_collision_mask()
  37.  
  38. enum PlayerState {IDLE, TURNING, WALKING}
  39. enum FacingDirection {LEFT, RIGHT, UP, DOWN}
  40.  
  41. var player_state = PlayerState.IDLE
  42. var facing_direction = FacingDirection.DOWN
  43.  
  44. func _ready():
  45.     $Sprite.visible = true
  46.     anim_tree.active = true
  47.     initial_position = position
  48.     anim_tree.set("parameters/Idle/blend_position", Vector2(0,1))
  49.     for item in animation_list:
  50.         var parameter = "parameters/%s/blend_position" %item
  51.         anim_tree.set(parameter, input_direction)
  52.    
  53. func set_spawn(location: Vector2, direction: Vector2):
  54.     for item in animation_list:
  55.         var parameter = "parameters/%s/blend_position" %item
  56.         anim_tree.set(parameter, direction)
  57.     position = location
  58.    
  59. func change_position(direction: Vector2):
  60.     for item in animation_list:
  61.         var parameter = "parameters/%s/blend_position" %item
  62.         anim_tree.set(parameter, direction)
  63.    
  64. func _physics_process(delta):
  65.     if player_state == PlayerState.TURNING or stop_input:
  66.         return
  67.     elif is_moving == false:
  68.         process_player_input()
  69.     elif input_direction != Vector2.ZERO:
  70.         if walk_speed == 4.0:
  71.             anim_state.travel("Walk")
  72.         elif is_running:
  73.             anim_state.travel("Run")
  74.         move(delta)
  75.     else:
  76.         anim_state.travel("Idle")
  77.         is_moving = false
  78.        
  79. func process_player_input():
  80.     if input_direction.y == 0:
  81.         input_direction.x = int(Input.is_action_pressed("ui_right")) - int(Input.is_action_pressed("ui_left"))
  82.     if input_direction.x == 0:
  83.         input_direction.y = int(Input.is_action_pressed("ui_down")) - int(Input.is_action_pressed("ui_up"))
  84.    
  85.     if can_run:
  86.         if Input.is_action_pressed(action_key):
  87.             walk_speed = 8.0
  88.             is_running = true
  89.             player_is_running.emit()
  90.         else:
  91.             walk_speed = 4.0
  92.             is_running = false
  93.    
  94.     if input_direction != Vector2.ZERO:
  95.         for item in animation_list:
  96.             var parameter = "parameters/%s/blend_position" %item
  97.             anim_tree.set(parameter, input_direction)
  98.        
  99.         if need_to_turn():
  100.             player_state = PlayerState.TURNING
  101.             anim_state.travel("Turn")
  102.        
  103.         else:
  104.             initial_position = position
  105.             is_moving = true
  106.     else:
  107.         anim_state.travel("Idle")
  108. #TutorialStuff
  109. func move(delta):
  110.     var desired_step: Vector2 = input_direction * TILE_SIZE / 2
  111.    
  112.     ray.set_target_position(desired_step)
  113.     ray.force_raycast_update()
  114.    
  115.     tile_over_ray.set_target_position(desired_step)
  116.     tile_over_ray.force_raycast_update()
  117.    
  118.     door_ray.set_target_position(desired_step)
  119.     door_ray.force_raycast_update()
  120.    
  121.     map_edge_ray.set_target_position(desired_step)
  122.     map_edge_ray.force_raycast_update()
  123.    
  124.     event_ray.set_target_position(desired_step)
  125.     event_ray.force_raycast_update()
  126.    
  127.     npc_ray.set_target_position(desired_step)
  128.     npc_ray.force_raycast_update()
  129.    
  130.     #DoorStuff
  131.     if door_ray.is_colliding():
  132.         if percented_moved_to_next_tile == 0.0:
  133.             $PlayerCamera.make_current()
  134.             anim_state.travel("Idle")
  135.             stop_input = true
  136.             player_entering_door_signal.emit()
  137.         percented_moved_to_next_tile += walk_speed * delta
  138.         if percented_moved_to_next_tile >= 1:
  139.             position = initial_position + (input_direction * TILE_SIZE)
  140.             percented_moved_to_next_tile = 0.0
  141.             is_moving = false
  142.             anim_state.travel("Idle")
  143.         else:
  144.             position = initial_position + (input_direction * TILE_SIZE * percented_moved_to_next_tile)
  145.  
  146.     elif event_ray.is_colliding():
  147.         percented_moved_to_next_tile += walk_speed * delta
  148.         if percented_moved_to_next_tile >= 0.9:
  149.             position = (initial_position + (input_direction * TILE_SIZE))
  150.             player_event_signal.emit()
  151.             is_moving = false
  152.             percented_moved_to_next_tile = 0.0
  153.         else:
  154.             position = initial_position + (input_direction * TILE_SIZE * percented_moved_to_next_tile)
  155.    
  156.     elif !ray.is_colliding() || tile_over_ray.is_colliding():
  157.         percented_moved_to_next_tile += walk_speed * delta
  158.         if percented_moved_to_next_tile >= 1.0:
  159.             position = initial_position + (TILE_SIZE * input_direction)
  160.             percented_moved_to_next_tile = 0.0
  161.             is_moving = false
  162.         else:
  163.             position = initial_position + (TILE_SIZE * input_direction * percented_moved_to_next_tile)
  164.     else:
  165.         percented_moved_to_next_tile = 0.0
  166.         is_moving = false
  167.  
  168. func need_to_turn():
  169.     var new_facing_direction
  170.     if input_direction.x < 0:
  171.         new_facing_direction = FacingDirection.LEFT
  172.     elif input_direction.x > 0:
  173.         new_facing_direction = FacingDirection.RIGHT
  174.     elif input_direction.y < 0:
  175.         new_facing_direction = FacingDirection.UP
  176.     elif input_direction.y > 0:
  177.         new_facing_direction = FacingDirection.DOWN
  178.    
  179.     if facing_direction != new_facing_direction:
  180.         facing_direction = new_facing_direction
  181.         return true
  182.     facing_direction = new_facing_direction
  183.     return false
  184.    
  185. func finished_turning():
  186.     player_state = PlayerState.IDLE
  187.     var desired_step: Vector2 = input_direction * TILE_SIZE / 2
  188.    
  189.     ray.set_target_position(desired_step)
  190.     ray.force_raycast_update()
  191.    
  192.     tile_over_ray.set_target_position(desired_step)
  193.     tile_over_ray.force_raycast_update()
  194.    
  195.     door_ray.set_target_position(desired_step)
  196.     door_ray.force_raycast_update()
  197.    
  198.     map_edge_ray.set_target_position(desired_step)
  199.     map_edge_ray.force_raycast_update()
  200.    
  201.     event_ray.set_target_position(desired_step)
  202.     event_ray.force_raycast_update()
  203.    
  204.     npc_ray.set_target_position(desired_step)
  205.     npc_ray.force_raycast_update()
  206.    
  207. func entered_door():
  208.     emit_signal("player_entered_door_signal")
  209.  
  210. func disable_collision():
  211.     self.collision_layer = 0
  212.     self.ray.collision_mask = 0
  213.    
  214. func enable_collision():
  215.     self.collision_layer = current_collision_layer
  216.     self.ray.collision_mask = current_collision_mask
  217.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement