Advertisement
samine

Untitled

Apr 11th, 2024 (edited)
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.99 KB | Source Code | 0 0
  1. extends CharacterBody2D
  2.  
  3. # Constants for movement
  4. @export var speed = 300.0
  5. var SPEED = speed
  6. @export var acceleration = 300.0
  7. var ACCELERATION = acceleration
  8. @export var friction = 300.0
  9. var FRICTION = friction * 16
  10.  
  11. # Gravity and jump settings
  12. @export var jump_peack = 0.5
  13. @export var jump_height = 75.0
  14. @export var air_acceleration = 300
  15. @export var air_friction = 18.0
  16. @export var air_jump = true
  17.  
  18. # Player and animator references
  19. @onready var player_1 = "."
  20. @onready var animator = $animator
  21. @onready var coyote_jump_timer = $CoyoteJump
  22. @onready var run = $run
  23. @onready var jump = $jump
  24. @onready var gravity = (2 * jump_height) / pow(jump_peack, 2)
  25. @onready var JUMP_VELOCITY = gravity * jump_peack * -1
  26. @onready var health = 5
  27. @onready var label = $Life
  28. @onready var death = $Death
  29. @onready var puchy = false
  30.  
  31. # Time window for coyote jump
  32. var COYOTE_TIME_WINDOW = 0.1 # Adjust as needed (in seconds)
  33.  
  34. # Update physics
  35. func _physics_process(delta):
  36.     apply_gravity(delta)
  37.     handle_jump(delta)
  38.     handle_movement(delta)
  39.     var was_on_floor = is_on_floor()
  40.     move_and_slide()
  41.     var just_left_ledge = was_on_floor and not is_on_floor() and velocity.y >= 0
  42.     if just_left_ledge:
  43.         coyote_jump_timer.start(COYOTE_TIME_WINDOW) # Start coyote jump timer
  44.  
  45. # Update animations
  46. func _process(delta):
  47.     update_sprite()
  48.     update_animation()
  49.  
  50. # Apply gravity to velocity
  51. func apply_gravity(delta):
  52.     if not is_on_floor():
  53.         jump.set_emitting(true)
  54.         velocity.y += gravity * delta
  55.     else:
  56.         jump.set_emitting(false)
  57.  
  58. # Handle jumping logic
  59. func handle_jump(delta):
  60.     if is_on_floor():
  61.         air_jump = true
  62.     if is_on_floor() or coyote_jump_timer.time_left > 0.0:
  63.         if Input.is_action_just_pressed("JumpP1"):
  64.             velocity.y = JUMP_VELOCITY
  65.     elif not is_on_floor():
  66.         pass
  67.  
  68. # Handle player movement
  69. func handle_movement(delta):
  70.     var direction = Input.get_axis("leftP1", "rightP1")
  71.     if is_on_floor():
  72.         if direction != 0:
  73.             velocity.x = move_toward(velocity.x, SPEED * direction, ACCELERATION * delta)
  74.             run.set_amount(SPEED / 8)
  75.             run.set_emitting(true)
  76.         else:
  77.             velocity.x = move_toward(velocity.x, 0, FRICTION * delta)
  78.             run.set_emitting(false)
  79.     else:
  80.         run.set_emitting(false)
  81.         if direction != 0:
  82.             velocity.x = move_toward(velocity.x, SPEED * direction, air_acceleration * delta)
  83.         else:
  84.             velocity.x = move_toward(velocity.x, 0, air_friction * delta)
  85.  
  86. # Update sprite direction
  87. func update_sprite():
  88.     var right = Input.is_action_pressed("rightP1")
  89.     var left = Input.is_action_pressed("leftP1")
  90.     if right:
  91.         puchy = false
  92.         $Playersprite.flip_h = false
  93.     if left:
  94.         puchy = false
  95.         $Playersprite.flip_h = true
  96.  
  97. # Update animation based on player state
  98. func update_animation():
  99.     var right = Input.is_action_pressed("rightP1")
  100.     var left = Input.is_action_pressed("leftP1")
  101.     if puchy == false:
  102.         if velocity.y > -10 and not is_on_floor():
  103.             animator.play("fall")
  104.         elif velocity.y < 3 and not is_on_floor():
  105.             animator.play("jump")
  106.         elif right or left:
  107.             animator.play("run")
  108.         else:
  109.             animator.play("idle")
  110.  
  111. # Restart scene on collision
  112. func _on_dead_zones_body_shape_entered(body_rid, body, body_shape_index, local_shape_index):
  113.     restart_scene()
  114.  
  115. # Restart scene function
  116. func restart_scene():
  117.     health -= 1
  118.     if health > 0:
  119.         var text_to_show = "X" + str(health)
  120.         label.set_text(text_to_show)
  121.         player_1.set_position(Vector2(0,0))
  122.         player_1.visible = false
  123.         death.start()
  124.         player_1.visible = true
  125.     else:
  126.         player_1.queue_free()
  127.  
  128. # Handle collisions with other entities
  129. func collide_right(body):
  130.     if body.is_in_group("Player") and body != $".":
  131.         puchy = true
  132.         if abs(body.velocity.x) > abs(velocity.x):
  133.             $Playersprite.flip_h = true
  134.             animator.stop()
  135.         $RIGHT/colition.debug_color = Color(0.90, 0.6, 0.0, 0.41)
  136.         if velocity.x == 0:
  137.             velocity.x = body.velocity.x + body.SPEED
  138.         animator.play("puched")
  139.  
  140. func collide_left(body):
  141.     if body.is_in_group("Player") and body != $".":
  142.         puchy = true
  143.         if abs(body.velocity.x) > abs(velocity.x):
  144.             $Playersprite.flip_h = false
  145.             animator.stop()
  146.         $LEFT/colition.debug_color = Color(0.90, 0.6, 0.0, 0.41)
  147.         if velocity.x == 0:
  148.             velocity.x = body.velocity.x + body.SPEED
  149.         animator.play("puched")
  150.  
  151. func _on_right_body_exited(body):
  152.     puchy = false
  153.     $RIGHT/colition.debug_color = Color(0.0, 0.6, 0.7, 0.41)
  154.  
  155. func _on_left_body_exited(body):
  156.     puchy = false
  157.     $LEFT/colition.debug_color = Color(0.0, 0.6, 0.7, 0.41)
  158.  
  159. func get_damaged(amount):
  160.     puchy = true
  161.     $animator.stop()
  162.     $animator.play("damage")
  163.     $Dammage.emitting = true
  164.     health -= amount
  165.     if health < 0:
  166.         health = 0
  167.     var text_to_show = "X" + str(health)
  168.     label.set_text(text_to_show)
  169.     await get_tree().create_timer(1).timeout
  170.     $Dammage.emitting = false
  171.  
  172. func STOMP(body):
  173.     if body.is_in_group("Player") and body != $".":
  174.         puchy = true
  175.         velocity = -velocity / 2
  176.         animator.stop()
  177.         animator.play("stomped on")
  178.  
  179. func BUMP(body):
  180.     coyote_jump_timer.start()
  181.     if body.is_in_group("Player") and body != $".":
  182.         velocity.y = -velocity.y
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement