Advertisement
Eynopinate69

player mvm code

Jan 12th, 2024
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 1.98 KB | Gaming | 0 0
  1. extends CharacterBody2D
  2.  
  3.  
  4. @onready var player_value = "res://DefaultMovementData.tres"
  5. @export var movement_data : PlayerMovementData
  6.  
  7.  
  8. var motion = Vector2()
  9. var motion_prev = Vector2()
  10. var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
  11. var hit_the_ground = false
  12.  
  13.  
  14. #@onready var animated_sprite_2d = $AnimatedSprite2D
  15. @onready var cayote_jump_timer = $CayoteJumpTimer
  16. @onready var starting_pos = global_position
  17.  
  18. func _physics_process(delta):
  19.     apply_gravity(delta)
  20.     handle_jump()
  21.     var input_axis = Input.get_axis("Left", "Right ")
  22.     handle_acceleration(input_axis,delta)
  23.     apply_friction(input_axis, delta)
  24.     apply_air_resistance(input_axis,delta)
  25.     update_animations(input_axis)
  26.    
  27.    
  28.     var was_on_floor = is_on_floor()
  29.     move_and_slide()
  30.    
  31.     var just_left_ledge = was_on_floor and not is_on_floor() and velocity.y >= 0
  32.     if just_left_ledge:
  33.         cayote_jump_timer.start()
  34.    
  35. func apply_gravity(delta):
  36.     if not is_on_floor():
  37.         velocity.y += gravity * movement_data.gravity_scale * delta
  38.    
  39. func handle_jump():
  40.     if is_on_floor() or cayote_jump_timer.time_left > 0.0:
  41.         if Input.is_action_just_pressed("ui_up"):
  42.             velocity.y = movement_data.jump_velocity
  43.     if not is_on_floor():
  44.         if Input.is_action_just_released("ui_up") and velocity.y < movement_data.jump_velocity / 2 :
  45.             velocity.y = movement_data.jump_velocity / 2
  46.    
  47.    
  48.    
  49. func handle_acceleration(input_axis,delta):
  50.     if input_axis !=0:
  51.         velocity.x = move_toward(velocity.x,movement_data.speed * input_axis , movement_data.acceleration * delta)
  52.    
  53. func apply_friction(input_axis ,delta):
  54.     if input_axis == 0 and is_on_floor():
  55.         velocity.x = move_toward(velocity.x, 0 , movement_data.friction * delta)
  56.    
  57. func apply_air_resistance(input_axis , delta):
  58.     if input_axis == 0 and not is_on_floor():
  59.         velocity.x = move_toward(velocity.x, 0,movement_data.air_resistance * delta)
  60.    
  61.     pass
  62. func update_animations(input_axis):
  63.     pass
  64.  
  65. func _on_hazard_detector_area_entered(area):
  66.     global_position = starting_pos
  67.  
Tags: Godot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement