From c3f5a6116e70e16b635e69da71fb4422e5f3e7e3 Mon Sep 17 00:00:00 2001 From: AnicaCihla Date: Mon, 14 Apr 2025 20:56:26 -0400 Subject: [PATCH] Week two! Went off-script a bit... --- project.godot | 13 ++++ scenes/ball.tscn | 14 ++++ scenes/bullet.tscn | 14 ++++ scenes/node_2d.tscn | 77 +++++++++--------- scenes/player.tscn | 24 ++++++ scripts/ball.gd | 1 + scripts/ball.gd.uid | 1 + scripts/bullet.gd | 24 ++++++ scripts/bullet.gd.uid | 1 + scripts/character_body_2d.gd | 31 -------- scripts/player.gd | 78 +++++++++++++++++++ ...character_body_2d.gd.uid => player.gd.uid} | 0 scripts/scene_manager.gd | 15 ++++ scripts/scene_manager.gd.uid | 1 + 14 files changed, 223 insertions(+), 71 deletions(-) create mode 100644 scenes/ball.tscn create mode 100644 scenes/bullet.tscn create mode 100644 scenes/player.tscn create mode 100644 scripts/ball.gd create mode 100644 scripts/ball.gd.uid create mode 100644 scripts/bullet.gd create mode 100644 scripts/bullet.gd.uid delete mode 100644 scripts/character_body_2d.gd create mode 100644 scripts/player.gd rename scripts/{character_body_2d.gd.uid => player.gd.uid} (100%) create mode 100644 scripts/scene_manager.gd create mode 100644 scripts/scene_manager.gd.uid diff --git a/project.godot b/project.godot index e5af9b0..8598ec9 100644 --- a/project.godot +++ b/project.godot @@ -14,3 +14,16 @@ config/name="learning" run/main_scene="res://scenes/node_2d.tscn" config/features=PackedStringArray("4.4", "Forward Plus") config/icon="res://icon.svg" + +[input] + +attract={ +"deadzone": 0.2, +"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":2,"canceled":false,"pressed":false,"double_click":false,"script":null) +] +} +shoot={ +"deadzone": 0.2, +"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"canceled":false,"pressed":false,"double_click":false,"script":null) +] +} diff --git a/scenes/ball.tscn b/scenes/ball.tscn new file mode 100644 index 0000000..f52cefa --- /dev/null +++ b/scenes/ball.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=3 format=3 uid="uid://cdyodb8q2mkwd"] + +[ext_resource type="Script" uid="uid://doqrmlk7jvot7" path="res://scripts/ball.gd" id="1_7s4qf"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_rf3rd"] +radius = 24.0 + +[node name="RigidBody2D" type="RigidBody2D"] +script = ExtResource("1_7s4qf") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("CircleShape2D_rf3rd") +debug_color = Color(0.809626, 0.453417, 0.723759, 0.42) diff --git a/scenes/bullet.tscn b/scenes/bullet.tscn new file mode 100644 index 0000000..5575380 --- /dev/null +++ b/scenes/bullet.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=3 format=3 uid="uid://ohcwdeyx018t"] + +[ext_resource type="Script" uid="uid://dkr6ap7argaf3" path="res://scripts/bullet.gd" id="1_mkf8s"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_y25gk"] + +[node name="Area2D" type="Area2D"] +script = ExtResource("1_mkf8s") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("CircleShape2D_y25gk") +debug_color = Color(0.557768, 0.541597, 0.413157, 0.42) + +[connection signal="body_entered" from="." to="." method="_on_body_entered"] diff --git a/scenes/node_2d.tscn b/scenes/node_2d.tscn index b459b5a..e3bf287 100644 --- a/scenes/node_2d.tscn +++ b/scenes/node_2d.tscn @@ -1,19 +1,14 @@ [gd_scene load_steps=10 format=3 uid="uid://f0wep3w30dh8"] [ext_resource type="Script" uid="uid://ct2t7c0om0cdh" path="res://scripts/gamecontroller.gd" id="1_4dvvg"] -[ext_resource type="Script" uid="uid://cmrdtps7txgxx" path="res://scripts/character_body_2d.gd" id="1_6e84v"] +[ext_resource type="Script" uid="uid://dnx7g8ts5f4uc" path="res://scripts/scene_manager.gd" id="2_4vge2"] +[ext_resource type="PackedScene" uid="uid://6vl0c70v1gqs" path="res://scenes/player.tscn" id="2_rnjyy"] +[ext_resource type="PackedScene" uid="uid://cdyodb8q2mkwd" path="res://scenes/ball.tscn" id="3_04qyp"] [ext_resource type="PackedScene" uid="uid://dxj2q0disdlb5" path="res://scenes/area_2d.tscn" id="3_rnjyy"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_rf3rd"] size = Vector2(433.759, 40) -[sub_resource type="CircleShape2D" id="CircleShape2D_rf3rd"] -radius = 24.0 - -[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_rf3rd"] -radius = 21.0 -height = 78.0 - [sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_6e84v"] [sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_4dvvg"] @@ -22,63 +17,65 @@ normal = Vector2(0.999872, -0.015998) [sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_rnjyy"] normal = Vector2(-0.999909, -0.0135123) -[node name="Node2D" type="Node2D"] +[node name="Game" type="Node2D"] script = ExtResource("1_4dvvg") -[node name="StaticBody2D" type="StaticBody2D" parent="."] -position = Vector2(370, 393) +[node name="SceneManager" type="Node" parent="."] +unique_name_in_owner = true +script = ExtResource("2_4vge2") + +[node name="level" type="Node2D" parent="."] + +[node name="StaticBody2D" type="StaticBody2D" parent="level"] +position = Vector2(347, 337) +rotation = 0.253073 metadata/_edit_group_ = true -[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"] -rotation = 0.359538 +[node name="CollisionShape2D" type="CollisionShape2D" parent="level/StaticBody2D"] shape = SubResource("RectangleShape2D_rf3rd") -[node name="RigidBody2D" type="RigidBody2D" parent="."] -position = Vector2(370, 273) +[node name="StaticBody2D2" type="StaticBody2D" parent="level"] +position = Vector2(713, 488) metadata/_edit_group_ = true -[node name="CollisionShape2D" type="CollisionShape2D" parent="RigidBody2D"] -shape = SubResource("CircleShape2D_rf3rd") -debug_color = Color(1, 0.234871, 0.652107, 0.42) - -[node name="StaticBody2D2" type="StaticBody2D" parent="."] -position = Vector2(679, 504) -metadata/_edit_group_ = true - -[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D2"] -rotation = -0.357792 +[node name="CollisionShape2D" type="CollisionShape2D" parent="level/StaticBody2D2"] shape = SubResource("RectangleShape2D_rf3rd") -[node name="CharacterBody2D" type="CharacterBody2D" parent="."] -position = Vector2(623, 366) -script = ExtResource("1_6e84v") -metadata/_edit_group_ = true - -[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"] -shape = SubResource("CapsuleShape2D_rf3rd") -debug_color = Color(0.232329, 0.752878, 1.15514e-06, 0.42) - -[node name="floor" type="StaticBody2D" parent="."] +[node name="floor" type="StaticBody2D" parent="level"] position = Vector2(558, 646) metadata/_edit_group_ = true -[node name="CollisionShape2D" type="CollisionShape2D" parent="floor"] +[node name="CollisionShape2D" type="CollisionShape2D" parent="level/floor"] shape = SubResource("WorldBoundaryShape2D_6e84v") -[node name="left wall" type="StaticBody2D" parent="."] +[node name="ceiling" type="StaticBody2D" parent="level"] +position = Vector2(569, -1) +rotation = 3.14159 +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="level/ceiling"] +shape = SubResource("WorldBoundaryShape2D_6e84v") + +[node name="left wall" type="StaticBody2D" parent="level"] position = Vector2(0, 387) metadata/_edit_group_ = true -[node name="CollisionShape2D" type="CollisionShape2D" parent="left wall"] +[node name="CollisionShape2D" type="CollisionShape2D" parent="level/left wall"] shape = SubResource("WorldBoundaryShape2D_4dvvg") -[node name="right wall" type="StaticBody2D" parent="."] +[node name="right wall" type="StaticBody2D" parent="level"] position = Vector2(1154, 345) metadata/_edit_group_ = true -[node name="CollisionShape2D" type="CollisionShape2D" parent="right wall"] +[node name="CollisionShape2D" type="CollisionShape2D" parent="level/right wall"] shape = SubResource("WorldBoundaryShape2D_rnjyy") +[node name="ball" parent="." instance=ExtResource("3_04qyp")] +position = Vector2(370, 273) + +[node name="CharacterBody2D" parent="." instance=ExtResource("2_rnjyy")] +position = Vector2(623, 366) + [node name="Area2D" parent="." instance=ExtResource("3_rnjyy")] position = Vector2(83, 587) diff --git a/scenes/player.tscn b/scenes/player.tscn new file mode 100644 index 0000000..f0836b4 --- /dev/null +++ b/scenes/player.tscn @@ -0,0 +1,24 @@ +[gd_scene load_steps=4 format=3 uid="uid://6vl0c70v1gqs"] + +[ext_resource type="Script" uid="uid://cmrdtps7txgxx" path="res://scripts/player.gd" id="1_3vyb7"] + +[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_rf3rd"] +radius = 21.0 +height = 78.0 + +[sub_resource type="GDScript" id="GDScript_3vyb7"] +script/source = "extends RayCast2D + + +" + +[node name="CharacterBody2D" type="CharacterBody2D"] +script = ExtResource("1_3vyb7") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("CapsuleShape2D_rf3rd") +debug_color = Color(0.232329, 0.752878, 1.15514e-06, 0.42) + +[node name="RayCast2D" type="RayCast2D" parent="."] +script = SubResource("GDScript_3vyb7") diff --git a/scripts/ball.gd b/scripts/ball.gd new file mode 100644 index 0000000..4509852 --- /dev/null +++ b/scripts/ball.gd @@ -0,0 +1 @@ +class_name Ball extends RigidBody2D diff --git a/scripts/ball.gd.uid b/scripts/ball.gd.uid new file mode 100644 index 0000000..6b9d699 --- /dev/null +++ b/scripts/ball.gd.uid @@ -0,0 +1 @@ +uid://doqrmlk7jvot7 diff --git a/scripts/bullet.gd b/scripts/bullet.gd new file mode 100644 index 0000000..4d0ce21 --- /dev/null +++ b/scripts/bullet.gd @@ -0,0 +1,24 @@ +class_name Bullet extends Area2D + +const BULLET_SCENE: PackedScene = preload("res://scenes/bullet.tscn") +@export var SPEED = 700 +var bearing: Vector2 = Vector2(1,0) + +signal bulletHit(body, bullet) + +static func create(pos: Vector2, bearing: Vector2) -> Bullet: + var instance = BULLET_SCENE.instantiate() + instance.position = pos + instance.bearing = bearing + return instance + +func _physics_process(delta: float) -> void: + position += bearing * SPEED * delta + if position.x < 0 || position.x > 1152: + bearing.x *= -1 + if position.y < 0 || position.y > 648: + bearing.y *= -1 + +func _on_body_entered(body: Node2D) -> void: + if not body is Player: + bulletHit.emit(body, self) diff --git a/scripts/bullet.gd.uid b/scripts/bullet.gd.uid new file mode 100644 index 0000000..4acd7af --- /dev/null +++ b/scripts/bullet.gd.uid @@ -0,0 +1 @@ +uid://dkr6ap7argaf3 diff --git a/scripts/character_body_2d.gd b/scripts/character_body_2d.gd deleted file mode 100644 index 1785209..0000000 --- a/scripts/character_body_2d.gd +++ /dev/null @@ -1,31 +0,0 @@ -extends CharacterBody2D - - -const SPEED = 300.0 -const JUMP_VELOCITY = -400.0 - -const PUSH = 100.0 - - -func _physics_process(delta: float) -> void: - # Add the gravity. - if not is_on_floor(): - velocity += get_gravity() * delta - - # Handle jump. - if Input.is_action_just_pressed("ui_accept") and is_on_floor(): - velocity.y = JUMP_VELOCITY - - # Get the input direction and handle the movement/deceleration. - # As good practice, you should replace UI actions with custom gameplay actions. - var direction := Input.get_axis("ui_left", "ui_right") - if direction: - velocity.x = direction * SPEED - else: - velocity.x = move_toward(velocity.x, 0, SPEED) - - move_and_slide() - for i in get_slide_collision_count(): - var c = get_slide_collision(i) - if c.get_collider() is RigidBody2D: - c.get_collider().apply_central_impulse(-c.get_normal() * PUSH) diff --git a/scripts/player.gd b/scripts/player.gd new file mode 100644 index 0000000..28c4354 --- /dev/null +++ b/scripts/player.gd @@ -0,0 +1,78 @@ +class_name Player extends CharacterBody2D + +@onready var ray_cast_2d: RayCast2D = $RayCast2D +@onready var game: GameController = $".." + +const SPEED = 300.0 +const JUMP_VELOCITY = -400.0 + +const PUSH = 100.0 + +enum InteractionStates {NONE, ATTRACT, REPEL} +var interactionState = InteractionStates.NONE + +func _physics_process(delta: float) -> void: + # Add the gravity. + if not is_on_floor(): + velocity += get_gravity() * delta + + handle_input() + + move_and_slide() + + handle_collisions() + +func handle_input() -> void: + # Handle jump. + if Input.is_action_just_pressed("ui_up") and is_on_floor(): + velocity.y = JUMP_VELOCITY + + # Get the input direction and handle the movement/deceleration. + # As good practice, you should replace UI actions with custom gameplay actions. + var direction := Input.get_axis("ui_left", "ui_right") + if direction: + velocity.x = direction * SPEED + else: + velocity.x = move_toward(velocity.x, 0, SPEED) + + # Follow mouse cursor with raycast. + var target = get_local_mouse_position() + ray_cast_2d.target_position = target + + # Handle attract/repel inputs, or reset to no interaction + if Input.is_action_just_pressed("attract"): + interactionState = InteractionStates.ATTRACT + #elif Input.is_action_just_pressed("repel"): + #interactionState = InteractionStates.REPEL + else: + interactionState = InteractionStates.NONE + + # Shoot bullets + if Input.is_action_just_pressed("shoot"): + var dir = (get_global_mouse_position() - global_position).normalized() + %SceneManager.makeBullet(global_position, dir) + +func update_movement() -> void: + pass + +func update_states() -> void: + pass + +func handle_collisions() -> void: + #for i in get_slide_collision_count(): + #var c = get_slide_collision(i) + #if c.get_collider() is RigidBody2D: + #c.get_collider().apply_central_impulse(-c.get_normal() * PUSH) + + if ray_cast_2d.is_colliding(): + var collider = ray_cast_2d.get_collider() + if collider is RigidBody2D: + var angle = collider.get_angle_to(to_local(position)) + var direction = Vector2.RIGHT.rotated(angle) + match interactionState: + InteractionStates.REPEL: + collider.apply_central_impulse(direction * PUSH) + InteractionStates.ATTRACT: + collider.apply_central_impulse((direction * PUSH) * -1) + InteractionStates.NONE: + pass diff --git a/scripts/character_body_2d.gd.uid b/scripts/player.gd.uid similarity index 100% rename from scripts/character_body_2d.gd.uid rename to scripts/player.gd.uid diff --git a/scripts/scene_manager.gd b/scripts/scene_manager.gd new file mode 100644 index 0000000..7e7500b --- /dev/null +++ b/scripts/scene_manager.gd @@ -0,0 +1,15 @@ +extends Node + +@onready var game: GameController = $".." +#var bullet = preload("res://scenes/bullet.tscn") + +func buildLevel() -> void: + pass + +func _ready() -> void: + buildLevel() + +func makeBullet(pos, dir) -> void: + print("pos: %s \n dir: %s" % [pos, dir]) + var bullet = Bullet.create(pos, dir) + game.add_child(bullet) diff --git a/scripts/scene_manager.gd.uid b/scripts/scene_manager.gd.uid new file mode 100644 index 0000000..7c02eaf --- /dev/null +++ b/scripts/scene_manager.gd.uid @@ -0,0 +1 @@ +uid://dnx7g8ts5f4uc