smashball/scenes/player/states/hitting.gd

98 lines
3.1 KiB
GDScript

extends PlayerState
var previous_state_path: String
var hit_type: 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") == "left" or
_data.get("hit") == "right" or
_data.get("hit") == "power",
"Invalid hit_type ("+str(_data.get("hit"))+")."
)
print("previous state path: "+_previous_state_path)
previous_state_path = _previous_state_path
hit_type = _data.get("hit")
#player.sprite.play('up' if player.id == 1 else 'down')
#player.sprite.frame = 1
#player.sprite.pause()
player.anim_player.play("hit_"+hit_type+"_up" if player.id == 1 else "hit_"+hit_type+"_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" or
player.anim_player.current_animation == "hit_power_up" or
player.anim_player.current_animation == "hit_power_down",
"invalid animation ("+player.anim_player.current_animation+")"
)
var sprite_texture: Texture2D = player.sprite.sprite_frames.get_frame_texture(player.sprite.animation, 1)
var hit_node: Hit
if hit_type == "left" or hit_type == "right":
hit_node = player.hit_scene.instantiate()
# flip entire node horizontally if spawning left
# flip vertically if player 2
hit_node.scale = Vector2(
-1 if hit_type == "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_type == "left" else 1),
(sprite_texture.get_height()*-1) if player.id == 1 else 4
)
elif hit_type == "power":
hit_node = player.hit_scene.instantiate()
player.add_child(hit_node)
@warning_ignore("integer_division")
hit_node.global_position = player.global_position + Vector2(0,(sprite_texture.get_height()/2)*-1)
assert(hit_node != null, "hit_node is null")
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: Hit = 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_just_pressed('p'+str(player.id)+'_hit_left'):
print("transitioning to hitting left")
finished.emit(HITTING, {"hit": "left"})
return
elif Input.is_action_just_pressed('p'+str(player.id)+'_hit_right'):
print("transitioning to hitting right")
finished.emit(HITTING, {"hit": "right"})
return
printerr("hit cooldown did not return")