properly spawn player hit
This commit is contained in:
parent
7f3c910414
commit
760d943172
1 changed files with 27 additions and 11 deletions
|
|
@ -24,17 +24,9 @@ func _physics_process(_delta: float) -> void:
|
|||
var is_moving: bool = velocity.length() > 10
|
||||
|
||||
if Input.is_action_pressed("hit_left") and not is_hitting:
|
||||
if not is_hitting:
|
||||
sprite.play('up')
|
||||
sprite.frame = 1
|
||||
sprite.pause()
|
||||
|
||||
var hit: Area2D = preload("res://scenes/hit/hit.tscn").instantiate()
|
||||
add_child(hit)
|
||||
|
||||
hit.timer.connect("timeout", _on_hit_end)
|
||||
|
||||
is_hitting = true
|
||||
hit("left")
|
||||
elif Input.is_action_pressed("hit_right") and not is_hitting:
|
||||
hit("right")
|
||||
|
||||
if is_moving:
|
||||
if abs(velocity.x) > abs(velocity.y):
|
||||
|
|
@ -56,5 +48,29 @@ func get_movement_vector() -> Vector2:
|
|||
var y_mov: float = Input.get_action_strength('move_down') - Input.get_action_strength('move_up')
|
||||
return Vector2(x_mov, y_mov)
|
||||
|
||||
func hit(dir: String) -> void:
|
||||
if not is_hitting:
|
||||
sprite.play('up')
|
||||
sprite.frame = 1
|
||||
sprite.pause()
|
||||
|
||||
var sprite_texture: Texture2D = sprite.sprite_frames.get_frame_texture('up', 1)
|
||||
var hit_node: Area2D = preload("res://scenes/hit/hit.tscn").instantiate()
|
||||
|
||||
# set position according to frame width and height
|
||||
@warning_ignore("integer_division")
|
||||
hit_node.global_position = global_position + Vector2(
|
||||
(sprite_texture.get_width()/2)*(-1 if dir == "left" else 1),
|
||||
sprite_texture.get_height()*-1
|
||||
)
|
||||
|
||||
# flip entire node horizontally if spawning left
|
||||
hit_node.scale = Vector2(-1 if dir == "left" else 1, 1)
|
||||
get_parent().add_child(hit_node)
|
||||
|
||||
hit_node.timer.connect("timeout", _on_hit_end)
|
||||
|
||||
is_hitting = true
|
||||
|
||||
func _on_hit_end() -> void:
|
||||
is_hitting = false
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue