diff --git a/project.godot b/project.godot index 60a6eee..8b6d34a 100644 --- a/project.godot +++ b/project.godot @@ -27,6 +27,10 @@ window/stretch/scale_mode="integer" version_control/plugin_name="GitPlugin" version_control/autoload_on_startup=true +[global_group] + +paddle="objects that hit the ball" + [input] move_up={ @@ -54,6 +58,16 @@ shift={ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194325,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) ] } +hit_left={ +"deadzone": 0.2, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":79,"key_label":0,"unicode":111,"location":0,"echo":false,"script":null) +] +} +hit_right={ +"deadzone": 0.2, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":80,"key_label":0,"unicode":112,"location":0,"echo":false,"script":null) +] +} [layer_names] diff --git a/scenes/ball/ball.gd b/scenes/ball/ball.gd new file mode 100644 index 0000000..47185a4 --- /dev/null +++ b/scenes/ball/ball.gd @@ -0,0 +1,23 @@ +extends RigidBody2D + +@onready var hurtbox: Area2D = $HurtArea2D + +@export var launch_speed: float = 400 +@export var speed_mult: float = 1.08 + +var is_hit: bool = false + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + hurtbox.connect("area_entered", _on_hit) + launch_random() + + +func launch_random(): + var angle = randf_range(-PI/3, PI/3) + PI * float(randi()%2) + linear_velocity = Vector2(cos(angle), sin(angle)) * launch_speed + + +func _on_hit(area: Area2D) -> void: + if area.is_in_group("paddle"): + linear_velocity = linear_velocity.normalized() * linear_velocity.length() * speed_mult diff --git a/scenes/ball/ball.gd.uid b/scenes/ball/ball.gd.uid new file mode 100644 index 0000000..de5cc01 --- /dev/null +++ b/scenes/ball/ball.gd.uid @@ -0,0 +1 @@ +uid://d2s0fid28eihy diff --git a/scenes/ball/ball.tscn b/scenes/ball/ball.tscn index 3ba1c29..a058ae0 100644 --- a/scenes/ball/ball.tscn +++ b/scenes/ball/ball.tscn @@ -1,22 +1,36 @@ -[gd_scene load_steps=4 format=3 uid="uid://cmgwv41ht3q4j"] +[gd_scene load_steps=6 format=3 uid="uid://cmgwv41ht3q4j"] +[ext_resource type="Script" uid="uid://d2s0fid28eihy" path="res://scenes/ball/ball.gd" id="1_nv6q1"] [ext_resource type="Texture2D" uid="uid://c44hadxh2458n" path="res://scenes/ball/ball.png" id="1_oqv3d"] [sub_resource type="PhysicsMaterial" id="PhysicsMaterial_oqv3d"] friction = 0.0 bounce = 0.3 +[sub_resource type="CircleShape2D" id="CircleShape2D_oqv3d"] +radius = 11.0 + [sub_resource type="CircleShape2D" id="CircleShape2D_nv6q1"] radius = 8.062258 [node name="Ball" type="RigidBody2D"] -collision_layer = 2 +collision_layer = 18 collision_mask = 5 physics_material_override = SubResource("PhysicsMaterial_oqv3d") +gravity_scale = 0.0 continuous_cd = 1 +script = ExtResource("1_nv6q1") [node name="Sprite2D" type="Sprite2D" parent="."] texture = ExtResource("1_oqv3d") +[node name="HurtArea2D" type="Area2D" parent="."] +collision_layer = 16 +collision_mask = 0 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="HurtArea2D"] +shape = SubResource("CircleShape2D_oqv3d") +debug_color = Color(0.8785819, 0.30416045, 0.40790904, 0.41960785) + [node name="CollisionShape2D" type="CollisionShape2D" parent="."] shape = SubResource("CircleShape2D_nv6q1") diff --git a/scenes/hit/hit.ase b/scenes/hit/hit.ase new file mode 100644 index 0000000..71743f3 Binary files /dev/null and b/scenes/hit/hit.ase differ diff --git a/scenes/hit/hit.gd b/scenes/hit/hit.gd new file mode 100644 index 0000000..0406d4b --- /dev/null +++ b/scenes/hit/hit.gd @@ -0,0 +1,14 @@ +extends Area2D + +@onready var timer: Timer = $Timer + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + timer.connect("timeout", _despawn) + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta: float) -> void: + pass + +func _despawn() -> void: + queue_free() diff --git a/scenes/hit/hit.gd.uid b/scenes/hit/hit.gd.uid new file mode 100644 index 0000000..52a21a3 --- /dev/null +++ b/scenes/hit/hit.gd.uid @@ -0,0 +1 @@ +uid://c0m6xhbtgbgtu diff --git a/scenes/hit/hit.png b/scenes/hit/hit.png new file mode 100644 index 0000000..3206345 Binary files /dev/null and b/scenes/hit/hit.png differ diff --git a/scenes/hit/hit.png.import b/scenes/hit/hit.png.import new file mode 100644 index 0000000..e006a78 --- /dev/null +++ b/scenes/hit/hit.png.import @@ -0,0 +1,40 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://c6a62gvw7218s" +path="res://.godot/imported/hit.png-ab7094ea32cf7bd1cd8dffcb2c8ef7cb.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://scenes/hit/hit.png" +dest_files=["res://.godot/imported/hit.png-ab7094ea32cf7bd1cd8dffcb2c8ef7cb.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/scenes/hit/hit.tscn b/scenes/hit/hit.tscn new file mode 100644 index 0000000..4c37d13 --- /dev/null +++ b/scenes/hit/hit.tscn @@ -0,0 +1,28 @@ +[gd_scene load_steps=4 format=3 uid="uid://px67runjx6ex"] + +[ext_resource type="Script" uid="uid://c0m6xhbtgbgtu" path="res://scenes/hit/hit.gd" id="1_fxeki"] +[ext_resource type="Texture2D" uid="uid://c6a62gvw7218s" path="res://scenes/hit/hit.png" id="1_wjo4f"] + +[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_fxeki"] +radius = 7.0 +height = 18.0 + +[node name="Hit" type="Area2D" groups=["paddle"]] +collision_layer = 0 +collision_mask = 16 +script = ExtResource("1_fxeki") + +[node name="Sprite2D" type="Sprite2D" parent="."] +position = Vector2(7, -4) +texture = ExtResource("1_wjo4f") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2(5, -1) +rotation = 1.5707964 +shape = SubResource("CapsuleShape2D_fxeki") +debug_color = Color(0.6205005, 0.42311785, 0.8432588, 0.41960785) + +[node name="Timer" type="Timer" parent="."] +wait_time = 0.5 +one_shot = true +autostart = true diff --git a/scenes/player/player.gd b/scenes/player/player.gd index b6fe6c8..d819eb6 100644 --- a/scenes/player/player.gd +++ b/scenes/player/player.gd @@ -1,9 +1,11 @@ extends CharacterBody2D -const MAX_SPEED: float = 150 +@export var player: int = 1 +@export var max_speed: float = 150 @onready var sprite: AnimatedSprite2D = $AnimatedSprite2D +var is_hitting: bool = false var was_moving: bool = false var anim_dir: String = 'down' @@ -14,13 +16,26 @@ func _ready() -> void: func _physics_process(_delta: float) -> void: var movement_vector: Vector2 = get_movement_vector() - var direction: Vector2 = movement_vector.normalized() - velocity = direction * MAX_SPEED + var direction: Vector2 = movement_vector.normalized() if not is_hitting else Vector2.ZERO + velocity = direction * max_speed move_and_slide() 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 + if is_moving: if abs(velocity.x) > abs(velocity.y): anim_dir = 'right' if velocity.x > 0 else 'left' @@ -29,7 +44,7 @@ func _physics_process(_delta: float) -> void: sprite.play(anim_dir) else: - if was_moving: + if was_moving and not is_hitting: sprite.play('down') sprite.frame = 1 sprite.pause() @@ -40,3 +55,6 @@ func get_movement_vector() -> Vector2: var x_mov: float = Input.get_action_strength('move_right') - Input.get_action_strength('move_left') var y_mov: float = Input.get_action_strength('move_down') - Input.get_action_strength('move_up') return Vector2(x_mov, y_mov) + +func _on_hit_end() -> void: + is_hitting = false