Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody2D
- const UP = Vector2(0, -1)
- const STOP_SLOPE = 1 * Globals.UNIT_SIZE
- var velocity = Vector2()
- var move_speed = 7.5 * Globals.UNIT_SIZE
- var gravity = 0
- var max_jump_velocity = 0
- var min_jump_velocity = 0
- var max_jump_height = 3.25 * Globals.UNIT_SIZE
- var min_jump_height = 0.8 * Globals.UNIT_SIZE
- var jump_duration = 0.5
- var is_grounded
- # Player stats
- var health = 0
- var health_max = 100
- #from variables Script
- signal health_changed(health)
- export(int) var max_health = 100
- onready var raycasts = $Raycasts
- onready var anim_player = $AnimationPlayer
- func _ready():
- gravity = 2 * max_jump_height / pow(jump_duration, 2)
- max_jump_velocity = -sqrt(2 * gravity * max_jump_height)
- min_jump_velocity = -sqrt(2 * gravity * min_jump_height)
- #from variables script
- health = max_health
- emit_signal("health_changed", health)
- func take_damage(amount):
- health -= amount
- health = max(0, health)
- emit_signal("health_changed", health)
- func heal(amount):
- health += amount
- health = max(health, max_health)
- emit_signal("health_changed", health)
- func _physics_process(delta):
- _get_input()
- velocity.y += gravity * delta
- velocity = move_and_slide(velocity, UP, STOP_SLOPE)
- #to prevent multiple jumps
- is_grounded = _check_is_grounded()
- _assign_animation()
- func _input(event):
- if event.is_action_pressed("jump") && is_grounded:
- $JumpSound.play()
- if is_grounded:
- velocity.y = max_jump_velocity
- if event.is_action_released("jump") && velocity.y < min_jump_velocity:
- velocity.y = min_jump_velocity
- func _get_input():
- var move_direction = -int(Input.is_action_pressed("move_left")) + int(Input.is_action_pressed("move_right"))
- velocity.x = move_speed * move_direction
- if move_direction !=0:
- $Sprite.scale.x = move_direction
- # 0.05 is too low
- func _get_h_weight():
- return 0.2 if is_grounded else 0.1
- func _check_is_grounded():
- for raycast in $Raycasts.get_children():
- if raycast.is_colliding():
- return true
- #if loop completes then raycast was not detected
- return false
- func _assign_animation():
- var anim = "idle"
- if !is_grounded:
- anim = "jump"
- elif velocity.x != 0:
- anim = "running"
- if Input.is_action_pressed("attack"):
- anim = "attack"
- if health <= 0:
- anim = "dead"
- set_physics_process(false)
- if anim_player.assigned_animation != anim:
- anim_player.play(anim)
- func knockback(var enemypos):
- velocity.y = min_jump_velocity
- var direction = 1
- if direction > 0:
- velocity.x = -800
- elif direction < 0:
- velocity.x = 800
- Input.action_release("move_left")
- Input.action_release("move_right")
- $AnimationPlayer.play("stagger")
- yield($AnimationPlayer, "animation_finished")
- func _on_PHitbox_area_entered(area):
- if area.name == "Hitbox":
- take_damage(15)
- anim_player.play("stagger")
- emit_signal("health_changed", health)
- if area.name == "FBHitbox":
- take_damage(25)
- emit_signal("health_changed", health)
- if health <=0:
- yield($AnimationPlayer, "animation_finished")
- get_tree().reload_current_scene()
- else:
- pass
- func _boss_dead(life):
- if life <= 0:
- $Win.play
- func _on_Health_Up_health_changed(area):
- if area.name == "Health_Up":
- heal(10)
- emit_signal("health_changed", health)
- func _on_PHitbox_body_entered(body):
- if body.name == "Enemy":
- knockback(position)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement