Compare commits
2 commits
7f3c910414
...
d1822e98b1
| Author | SHA1 | Date | |
|---|---|---|---|
| d1822e98b1 | |||
| 760d943172 |
5 changed files with 44 additions and 11 deletions
|
|
@ -15,6 +15,10 @@ run/main_scene="uid://d3c6aioatcy20"
|
|||
config/features=PackedStringArray("4.5", "GL Compatibility")
|
||||
config/icon="res://icon.svg"
|
||||
|
||||
[autoload]
|
||||
|
||||
EventBus="*res://scenes/autoload/event_bus.tscn"
|
||||
|
||||
[display]
|
||||
|
||||
window/size/viewport_width=320
|
||||
|
|
|
|||
6
scenes/autoload/event_bus.gd
Normal file
6
scenes/autoload/event_bus.gd
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
extends Node
|
||||
|
||||
signal ball_hit(player: int, velocity: Vector2)
|
||||
|
||||
func _ready():
|
||||
process_mode = Node.PROCESS_MODE_ALWAYS
|
||||
1
scenes/autoload/event_bus.gd.uid
Normal file
1
scenes/autoload/event_bus.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://qk8k7twgaqtm
|
||||
6
scenes/autoload/event_bus.tscn
Normal file
6
scenes/autoload/event_bus.tscn
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[gd_scene load_steps=2 format=3 uid="uid://bmpm30vauuo6u"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://qk8k7twgaqtm" path="res://scenes/autoload/event_bus.gd" id="1_quhyk"]
|
||||
|
||||
[node name="EventBus" type="Node"]
|
||||
script = ExtResource("1_quhyk")
|
||||
|
|
@ -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