Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody2D
- class_name Player
- signal dead
- var current_dir: int
- var gravity: float = 400 # 1100, 550, 400
- var max_jump_velocity: float = -200
- var min_jump_velocity: float = -50
- var move_speed: float = 75
- var velocity: Vector2 = Vector2.ZERO
- var current_state: int = states.idle
- var can_throw_rock: bool = false
- var can_coyote_jump: bool = false
- onready var animation: AnimationPlayer = $AnimationPlayer
- onready var sprite: Sprite = $Sprite
- onready var rock_projectile: PackedScene = preload("res://objects/RockProjectile.tscn")
- enum states {idle, running, jumping, falling, throwing, dead}
- func gravity_check():
- if position.y > 88:
- gravity = 1100
- elif position.y > 40:
- gravity = 550
- else:
- gravity = 400
- func enable_throw_rock() -> void:
- if not can_throw_rock:
- $PowerUp.play()
- can_throw_rock = true
- func _input(event: InputEvent) -> void:
- if Input.is_action_just_pressed("jump") and (is_on_floor() or can_coyote_jump):
- velocity.y = max_jump_velocity if not can_coyote_jump else max_jump_velocity * 1.1
- current_state = states.jumping
- if not $Jump.playing:
- $Jump.play()
- if Input.is_action_just_released("jump") and current_state == states.jumping:
- velocity.y = 0
- current_state = states.falling
- if Input.is_action_just_pressed("throw") and can_throw_rock and not GameMode.normal_mode:
- current_state = states.throwing
- var rock_instance = rock_projectile.instance()
- rock_instance.position = global_position
- if Input.is_action_pressed("down"):
- velocity.y = max_jump_velocity * 1.1
- rock_instance.linear_velocity = Vector2(0, 200)
- else:
- velocity.y = max_jump_velocity / 2
- velocity.x = 200 if $Sprite.flip_h else -200
- rock_instance.linear_velocity = Vector2(-200, 0) if $Sprite.flip_h else Vector2(200, 0)
- if not $Throw.playing:
- $Throw.play()
- get_parent().add_child(rock_instance)
- can_throw_rock = false
- if GameMode.normal_mode and can_throw_rock and (Input.is_action_pressed("throw")
- or Input.is_action_pressed("throw_right") or Input.is_action_pressed("down")):
- current_state = states.throwing
- var rock_instance = rock_projectile.instance()
- rock_instance.position = global_position
- if Input.is_action_pressed("throw"):
- $Sprite.flip_h = true
- velocity.y = max_jump_velocity / 2
- velocity.x = 200
- rock_instance.linear_velocity = Vector2(-200, 0)
- if Input.is_action_pressed("throw_right"):
- $Sprite.flip_h = false
- velocity.y = max_jump_velocity / 2
- velocity.x = -200
- rock_instance.linear_velocity = Vector2(200, 0)
- if Input.is_action_pressed("down"):
- velocity.y = max_jump_velocity * 1.1
- rock_instance.linear_velocity = Vector2(0, 200)
- if not $Throw.playing:
- $Throw.play()
- get_parent().add_child(rock_instance)
- can_throw_rock = false
- func idle_process(delta: float) -> void:
- if not animation.current_animation == "idle":
- animation.play("idle")
- if current_dir != 0:
- current_state = states.running
- if not is_on_floor():
- current_state = states.falling
- velocity.x = current_dir * move_speed
- func running_process(delta: float) -> void:
- if not animation.current_animation == "run":
- animation.play("run")
- if current_dir == 0:
- current_state = states.idle
- if not is_on_floor():
- current_state = states.falling
- $CoyoteTime.start()
- can_coyote_jump = true
- velocity.x = current_dir * move_speed
- func jumping_process(delta: float) -> void:
- if not animation.current_animation == "jump":
- animation.play("jump")
- if velocity.y > 0:
- current_state = states.falling
- velocity.x = current_dir * move_speed
- func falling_process(delta: float) -> void:
- if not animation.current_animation == "fall":
- animation.play("fall")
- if is_on_floor():
- current_state = states.idle
- velocity.x = current_dir * move_speed
- func throwing_process(delta: float) -> void:
- if not animation.current_animation == "throw":
- animation.play("throw")
- yield(get_tree().create_timer(0.2), "timeout")
- current_state = states.idle
- func dead_process(delta: float) -> void:
- velocity = Vector2.ZERO
- gravity = 0
- func _physics_process(delta: float) -> void:
- if current_state != states.dead:
- gravity_check()
- current_dir = Input.get_action_strength("right") - Input.get_action_strength("left")
- if current_dir == 1:
- sprite.flip_h = false
- elif current_dir == -1:
- sprite.flip_h = true
- velocity.y += gravity * delta
- velocity = move_and_slide(velocity, Vector2.UP)
- match current_state:
- 0:
- idle_process(delta)
- 1:
- running_process(delta)
- 2:
- jumping_process(delta)
- 3:
- falling_process(delta)
- 4:
- throwing_process(delta)
- 5:
- dead_process(delta)
- func _on_SpikeHitBox_body_entered(body: Node) -> void:
- current_state = states.dead
- emit_signal("dead")
- func _on_SpikeHitBox_area_entered(area: Area2D) -> void:
- current_state = states.dead
- emit_signal("dead")
- func _on_CoyoteTime_timeout() -> void:
- can_coyote_jump = false
Advertisement
Add Comment
Please, Sign In to add comment