code hitting state (fix empty commit)
This commit is contained in:
parent
4429984b2b
commit
9650dc9fdb
3 changed files with 77 additions and 2 deletions
|
|
@ -24,8 +24,8 @@ func _on_hit(hitbox: Area2D) -> void:
|
||||||
if not is_hit:
|
if not is_hit:
|
||||||
if hitbox.is_in_group("hit"):
|
if hitbox.is_in_group("hit"):
|
||||||
print("ball not hit yet")
|
print("ball not hit yet")
|
||||||
var player: CharacterBody2D = hitbox.get_parent()
|
var player: Player = hitbox.get_parent()
|
||||||
var timer: Timer = hitbox.timer
|
var timer: Timer = player.hit_timer
|
||||||
launch()
|
launch()
|
||||||
timer.connect("timeout", _on_hit_end)
|
timer.connect("timeout", _on_hit_end)
|
||||||
EventBus.ball_hit.emit(player.id, linear_velocity)
|
EventBus.ball_hit.emit(player.id, linear_velocity)
|
||||||
|
|
|
||||||
74
scenes/player/states/hitting.gd
Normal file
74
scenes/player/states/hitting.gd
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
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()
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
player.hit_timer.timeout.connect(_on_hit_end)
|
||||||
|
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("hit_left"):
|
||||||
|
print("transitioning to hitting left")
|
||||||
|
finished.emit(HITTING, {hit_dir = "left"})
|
||||||
|
return
|
||||||
|
elif Input.is_action_pressed("hit_right"):
|
||||||
|
print("transitioning to hitting right")
|
||||||
|
finished.emit(HITTING, {hit_dir = "right"})
|
||||||
|
return
|
||||||
|
printerr("hit cooldown did not return")
|
||||||
1
scenes/player/states/hitting.gd.uid
Normal file
1
scenes/player/states/hitting.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
uid://n7v8leojhykt
|
||||||
Loading…
Add table
Reference in a new issue