83 lines
2.6 KiB
GDScript
83 lines
2.6 KiB
GDScript
extends PlayerState
|
|
|
|
|
|
var previous_state_path: String
|
|
var hit_dir: String
|
|
var hit_node_ref: WeakRef
|
|
|
|
|
|
func _enter(_previous_state_path: String, _data: Dictionary = {}) -> void:
|
|
print("entering hitting")
|
|
player.velocity = Vector2.ZERO
|
|
player.move_and_slide()
|
|
assert(
|
|
_data.get("hit_dir") == "left" or _data.get("hit_dir") == "right",
|
|
"Invalid hit_dir ("+str(_data.get("hit_dir"))+")."
|
|
)
|
|
print("previous state path: "+_previous_state_path)
|
|
|
|
previous_state_path = _previous_state_path
|
|
hit_dir = _data.get("hit_dir")
|
|
|
|
#player.sprite.play('up' if player.id == 1 else 'down')
|
|
#player.sprite.frame = 1
|
|
#player.sprite.pause()
|
|
|
|
player.anim_player.play("hit_"+hit_dir+"_up" if player.id == 1 else "hit_"+hit_dir+"_down")
|
|
assert(
|
|
player.anim_player.current_animation == "hit_left_up" or
|
|
player.anim_player.current_animation == "hit_left_down" or
|
|
player.anim_player.current_animation == "hit_right_up" or
|
|
player.anim_player.current_animation == "hit_right_down",
|
|
"invalid animation ("+player.anim_player.current_animation+")"
|
|
)
|
|
|
|
var sprite_texture: Texture2D = player.sprite.sprite_frames.get_frame_texture('up', 1)
|
|
var hit_node: Area2D = preload("res://scenes/hit/hit.tscn").instantiate()
|
|
|
|
# flip entire node horizontally if spawning left
|
|
# flip vertically if player 2
|
|
hit_node.scale = Vector2(
|
|
-1 if hit_dir == "left" else 1,
|
|
1 if player.id == 1 else -1
|
|
)
|
|
player.add_child(hit_node)
|
|
|
|
# set position according to frame width and height
|
|
@warning_ignore("integer_division")
|
|
hit_node.global_position = player.global_position + Vector2(
|
|
(sprite_texture.get_width()/2)*(-1 if hit_dir == "left" else 1),
|
|
(sprite_texture.get_height()*-1) if player.id == 1 else 4
|
|
)
|
|
|
|
hit_node_ref = weakref(hit_node)
|
|
|
|
# cooldown
|
|
player.hit_timer.start()
|
|
|
|
func _on_hit_end() -> void:
|
|
print("cooldown end")
|
|
assert(hit_node_ref != null, "hit node is null")
|
|
|
|
var hit_node: Area2D = hit_node_ref.get_ref()
|
|
if hit_node and is_instance_valid(hit_node):
|
|
hit_node.queue_free()
|
|
|
|
var movement_vector: Vector2 = player.get_movement_vector()
|
|
if movement_vector.length() > player.DEADZONE:
|
|
print("transitioning to running")
|
|
finished.emit(RUNNING)
|
|
return
|
|
elif movement_vector.length() <= player.DEADZONE:
|
|
print("transitioning to idling")
|
|
finished.emit(IDLE)
|
|
return
|
|
elif Input.is_action_pressed('p'+str(player.id)+'_hit_left'):
|
|
print("transitioning to hitting left")
|
|
finished.emit(HITTING, {hit_dir = "left"})
|
|
return
|
|
elif Input.is_action_pressed('p'+str(player.id)+'_hit_right'):
|
|
print("transitioning to hitting right")
|
|
finished.emit(HITTING, {hit_dir = "right"})
|
|
return
|
|
printerr("hit cooldown did not return")
|