From 091d957d26a7aaf7fe107698b23b56acbe333f41 Mon Sep 17 00:00:00 2001 From: denizk0461 Date: Tue, 23 Jul 2024 20:07:13 +0200 Subject: [PATCH] ball now more consistently collides with arbitrary objects and bounces off them appropriately --- balls/ball.gd | 44 ++++++++++++++---- balls/basic/ball_basic.tscn | 42 ++++++++++++++++- blocks/block.gd | 19 ++++++++ blocks/block.tscn | 34 +++++++++++++- project.godot | 2 + scenes/level.tscn | 91 ++++--------------------------------- scenes/world_boundary.gd | 17 +++---- 7 files changed, 147 insertions(+), 102 deletions(-) create mode 100644 blocks/block.gd diff --git a/balls/ball.gd b/balls/ball.gd index 21e5171..ef48d52 100644 --- a/balls/ball.gd +++ b/balls/ball.gd @@ -1,17 +1,43 @@ extends CharacterBody2D @export var speed: float = 5 -const _speed_mult: float = 200 -var _direction: Vector2 = Vector2(1.0, 0.5) +@export var strength: int = 1 + +const _speed_mult: float = 20 +var _direction: Vector2 = Vector2(-1.0, -0.5) +var _velocity: Vector2 + +var deg: float + +func _ready(): + _velocity.x = 200#speed * _speed_mult * _direction.x * delta + _velocity.y = 250#speed * _speed_mult * _direction.y * delta + func _physics_process(delta): - velocity.x = speed * _speed_mult * _direction.x - velocity.y = speed * _speed_mult * _direction.y - move_and_slide() + var collide = move_and_collide(_velocity * delta) + if collide: + deg = rad_to_deg(collide.get_angle()) + if (deg >= 45 and deg < 135) or (deg >= 225 and deg < 315): + _invert_x() + if (deg >= 135 and deg < 225) or deg < 45 or deg >= 315: + _invert_y() + if collide.get_collider() is Block: + print(true) + collide.get_collider().take_damage(strength) -func invert_x(): - _direction.x *= -1 +func _invert_x(): + _velocity.x *= -1 -func invert_y(): - _direction.y *= -1 +func _invert_y(): + _velocity.y *= -1 + +func has_collided(): + pass + #if $Raycasts/North.is_colliding() or $Raycasts/South.is_colliding(): + #_invert_y() + #print("y") + #if $Raycasts/West.is_colliding() or $Raycasts/East.is_colliding(): + #_invert_x() + #print("x") diff --git a/balls/basic/ball_basic.tscn b/balls/basic/ball_basic.tscn index fc7498e..5a1fadb 100644 --- a/balls/basic/ball_basic.tscn +++ b/balls/basic/ball_basic.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=4 format=3 uid="uid://sl3jbnopo0yh"] +[gd_scene load_steps=8 format=3 uid="uid://sl3jbnopo0yh"] [ext_resource type="PackedScene" uid="uid://d2r0ip6xy3a2" path="res://balls/ball.tscn" id="1_ffml1"] [ext_resource type="Texture2D" uid="uid://cxi0fqy5sajh5" path="res://balls/basic/basic.webp" id="2_4dnuc"] @@ -6,10 +6,50 @@ [sub_resource type="RectangleShape2D" id="RectangleShape2D_isusp"] size = Vector2(50, 50) +[sub_resource type="RectangleShape2D" id="RectangleShape2D_qicwc"] +size = Vector2(46, 2) + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_ugdte"] +size = Vector2(46, 2) + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_2tu06"] +size = Vector2(2, 46) + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_0vqpn"] +size = Vector2(2, 46) + [node name="Ball" instance=ExtResource("1_ffml1")] +collision_mask = 14 [node name="CollisionShape2D" type="CollisionShape2D" parent="." index="0"] shape = SubResource("RectangleShape2D_isusp") [node name="Sprite2D" type="Sprite2D" parent="." index="1"] texture = ExtResource("2_4dnuc") + +[node name="Raycasts" type="Node2D" parent="." index="2"] +visible = false + +[node name="North" type="ShapeCast2D" parent="Raycasts" index="0"] +shape = SubResource("RectangleShape2D_qicwc") +target_position = Vector2(0, -25) +collision_mask = 10 +collide_with_areas = true + +[node name="South" type="ShapeCast2D" parent="Raycasts" index="1"] +shape = SubResource("RectangleShape2D_ugdte") +target_position = Vector2(0, 25) +collision_mask = 10 +collide_with_areas = true + +[node name="West" type="ShapeCast2D" parent="Raycasts" index="2"] +shape = SubResource("RectangleShape2D_2tu06") +target_position = Vector2(-25, 0) +collision_mask = 10 +collide_with_areas = true + +[node name="East" type="ShapeCast2D" parent="Raycasts" index="3"] +shape = SubResource("RectangleShape2D_0vqpn") +target_position = Vector2(25, 0) +collision_mask = 10 +collide_with_areas = true diff --git a/blocks/block.gd b/blocks/block.gd new file mode 100644 index 0000000..87067d0 --- /dev/null +++ b/blocks/block.gd @@ -0,0 +1,19 @@ +extends Sprite2D +class_name Block + +@export var health: int = 1 + +func _ready(): + _update_text() + +func _update_text(): + $Label.text = "%s" % health + +func take_damage(damage): + health -= damage + _update_text() + if health < 0: + die() + +func die(): + self.queue_free() diff --git a/blocks/block.tscn b/blocks/block.tscn index 9be5cd9..1e5b435 100644 --- a/blocks/block.tscn +++ b/blocks/block.tscn @@ -1,3 +1,35 @@ -[gd_scene format=3 uid="uid://c1i1fp3xbp6ly"] +[gd_scene load_steps=5 format=3 uid="uid://c1i1fp3xbp6ly"] + +[ext_resource type="Texture2D" uid="uid://c7ejgh4t0c7cw" path="res://blocks/block.webp" id="1_lqtqu"] +[ext_resource type="Script" path="res://blocks/block.gd" id="2_jqrjk"] +[ext_resource type="FontFile" uid="uid://dxgnofummhosw" path="res://fonts/play_bold.ttf" id="3_gmjev"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_46e0q"] +size = Vector2(120, 50) [node name="Block" type="Sprite2D"] +texture = ExtResource("1_lqtqu") +script = ExtResource("2_jqrjk") + +[node name="Area2D" type="StaticBody2D" parent="."] +collision_layer = 8 +collision_mask = 4 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"] +shape = SubResource("RectangleShape2D_46e0q") + +[node name="Label" type="Label" parent="."] +anchors_preset = 8 +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +offset_left = -71.0 +offset_top = -48.0 +offset_right = -48.0 +offset_bottom = -1.0 +grow_horizontal = 2 +grow_vertical = 2 +theme_override_colors/font_color = Color(0, 0, 0, 1) +theme_override_fonts/font = ExtResource("3_gmjev") +theme_override_font_sizes/font_size = 40 diff --git a/project.godot b/project.godot index f0fc98e..3bb66ca 100644 --- a/project.godot +++ b/project.godot @@ -26,6 +26,8 @@ window/stretch/mode="viewport" 2d_physics/layer_2="boundary" 2d_physics/layer_3="ball" 2d_physics/layer_4="block" +2d_physics/stretch/mode="viewport" +2d_physics/size/viewport_height=1080 [rendering] diff --git a/scenes/level.tscn b/scenes/level.tscn index ce61fe9..df0e5ba 100644 --- a/scenes/level.tscn +++ b/scenes/level.tscn @@ -5,7 +5,7 @@ [ext_resource type="PackedScene" uid="uid://sl3jbnopo0yh" path="res://balls/basic/ball_basic.tscn" id="3_14yi2"] [ext_resource type="Script" path="res://scenes/world_boundary.gd" id="3_w4mye"] [ext_resource type="PackedScene" uid="uid://cbygje4vi2rkv" path="res://boundaries/world_boundary.tscn" id="4_455ct"] -[ext_resource type="Script" path="res://scenes/boundary.gd" id="4_r1kkq"] +[ext_resource type="PackedScene" uid="uid://c1i1fp3xbp6ly" path="res://blocks/block.tscn" id="7_7fnx3"] [sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_ybmo2"] normal = Vector2(0, 1) @@ -18,7 +18,7 @@ distance = -510.0 normal = Vector2(1, 0) distance = -750.0 -[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_ktw6h"] +[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_0wr4g"] normal = Vector2(-1, 0) distance = -750.0 @@ -54,7 +54,7 @@ horizontal_alignment = 1 [node name="GameContent" type="Node2D" parent="."] -[node name="WorldBoundary" type="Area2D" parent="GameContent"] +[node name="WorldBoundary" type="StaticBody2D" parent="GameContent"] position = Vector2(1140, 540) collision_layer = 2 collision_mask = 4 @@ -70,10 +70,8 @@ shape = SubResource("WorldBoundaryShape2D_k0bca") shape = SubResource("WorldBoundaryShape2D_4nfnd") boundary_invert_axis = 0 -[node name="Right" type="CollisionShape2D" parent="GameContent/WorldBoundary"] -shape = SubResource("WorldBoundaryShape2D_ktw6h") -debug_color = Color(0.878431, 0, 0.415686, 0.419608) -script = ExtResource("4_r1kkq") +[node name="Right" parent="GameContent/WorldBoundary" instance=ExtResource("4_455ct")] +shape = SubResource("WorldBoundaryShape2D_0wr4g") boundary_invert_axis = 0 [node name="Balls" type="Node2D" parent="GameContent"] @@ -83,80 +81,7 @@ position = Vector2(791, 656) motion_mode = 0 floor_stop_on_slope = true -[node name="Ball2" parent="GameContent/Balls" instance=ExtResource("3_14yi2")] -position = Vector2(1049, 563) -motion_mode = 0 -floor_stop_on_slope = true +[node name="Blocks" type="Node2D" parent="GameContent"] -[node name="Ball3" parent="GameContent/Balls" instance=ExtResource("3_14yi2")] -position = Vector2(1382, 687) -motion_mode = 0 -floor_stop_on_slope = true - -[node name="Ball4" parent="GameContent/Balls" instance=ExtResource("3_14yi2")] -position = Vector2(1289, 856) -motion_mode = 0 -floor_stop_on_slope = true - -[node name="Ball5" parent="GameContent/Balls" instance=ExtResource("3_14yi2")] -position = Vector2(619, 256) -motion_mode = 0 -floor_stop_on_slope = true - -[node name="Ball6" parent="GameContent/Balls" instance=ExtResource("3_14yi2")] -position = Vector2(663, 857) -motion_mode = 0 -floor_stop_on_slope = true - -[node name="Ball7" parent="GameContent/Balls" instance=ExtResource("3_14yi2")] -position = Vector2(859, 252) -motion_mode = 0 -floor_stop_on_slope = true - -[node name="Ball8" parent="GameContent/Balls" instance=ExtResource("3_14yi2")] -position = Vector2(958, 792) -motion_mode = 0 -floor_stop_on_slope = true - -[node name="Ball9" parent="GameContent/Balls" instance=ExtResource("3_14yi2")] -position = Vector2(1418, 412) -motion_mode = 0 -floor_stop_on_slope = true - -[node name="Ball10" parent="GameContent/Balls" instance=ExtResource("3_14yi2")] -position = Vector2(1677, 692) -motion_mode = 0 -floor_stop_on_slope = true - -[node name="Ball11" parent="GameContent/Balls" instance=ExtResource("3_14yi2")] -position = Vector2(1610, 877) -motion_mode = 0 -floor_stop_on_slope = true - -[node name="Ball12" parent="GameContent/Balls" instance=ExtResource("3_14yi2")] -position = Vector2(1394, 185) -motion_mode = 0 -floor_stop_on_slope = true - -[node name="Ball13" parent="GameContent/Balls" instance=ExtResource("3_14yi2")] -position = Vector2(1285, 546) -motion_mode = 0 -floor_stop_on_slope = true - -[node name="Ball14" parent="GameContent/Balls" instance=ExtResource("3_14yi2")] -position = Vector2(1078, 172) -motion_mode = 0 -floor_stop_on_slope = true - -[node name="Ball15" parent="GameContent/Balls" instance=ExtResource("3_14yi2")] -position = Vector2(1562, 537) -motion_mode = 0 -floor_stop_on_slope = true - -[node name="Ball16" parent="GameContent/Balls" instance=ExtResource("3_14yi2")] -position = Vector2(1507, 299) -motion_mode = 0 -floor_stop_on_slope = true - -[connection signal="body_entered" from="GameContent/WorldBoundary" to="GameContent/WorldBoundary" method="_on_ball_entered"] -[connection signal="body_shape_entered" from="GameContent/WorldBoundary" to="GameContent/WorldBoundary" method="_on_ball_shape_entered"] +[node name="Block" parent="GameContent/Blocks" instance=ExtResource("7_7fnx3")] +position = Vector2(907, 752) diff --git a/scenes/world_boundary.gd b/scenes/world_boundary.gd index 26778a0..866d51d 100644 --- a/scenes/world_boundary.gd +++ b/scenes/world_boundary.gd @@ -1,9 +1,10 @@ -extends Area2D +extends StaticBody2D -func _on_ball_shape_entered(body_rid, body, body_shape_index, local_shape_index): - print("FUCK YES") - match self.shape_owner_get_owner(local_shape_index).boundary_invert_axis: - 0: # x - body.invert_x() - 1: # y - body.invert_y() +#func _on_ball_shape_entered(body_rid, body, body_shape_index, local_shape_index): + #print("FUCK YES") + #body.has_collided() + #match self.shape_owner_get_owner(local_shape_index).boundary_invert_axis: + #0: # x + #body.invert_x() + #1: # y + #body.invert_y()