From 205e8d43b50e1611f0f0131d5661dbe9802dde74 Mon Sep 17 00:00:00 2001 From: Darius Date: Mon, 13 Jan 2025 20:58:53 -0500 Subject: [PATCH] working bullet factory --- dariusgodotproject/project.godot | 9 ++++++- dariusgodotproject/scenes/bullet.tscn | 15 ++++++++++++ dariusgodotproject/scenes/crate.tscn | 2 +- dariusgodotproject/scenes/game.tscn | 11 ++++++++- dariusgodotproject/scenes/player.tscn | 8 ++++++- dariusgodotproject/scripts/bullet.gd | 24 +++++++++++++++++++ .../scripts/charactercontroller.gd | 20 +++++++++++++++- dariusgodotproject/scripts/gamecontroller.gd | 5 ++++ dariusgodotproject/scripts/scenemanager.gd | 22 +++++++++++++++++ 9 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 dariusgodotproject/scenes/bullet.tscn create mode 100644 dariusgodotproject/scripts/bullet.gd create mode 100644 dariusgodotproject/scripts/scenemanager.gd diff --git a/dariusgodotproject/project.godot b/dariusgodotproject/project.godot index fd9c5ef..1ff3555 100644 --- a/dariusgodotproject/project.godot +++ b/dariusgodotproject/project.godot @@ -26,11 +26,13 @@ folder_colors={ right={ "deadzone": 0.5, "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":68,"key_label":0,"unicode":100,"location":0,"echo":false,"script":null) +, 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":4194321,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) ] } left={ "deadzone": 0.5, -"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":65,"key_label":0,"unicode":97,"location":0,"echo":false,"script":null) +"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":4194319,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +, 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":65,"key_label":0,"unicode":97,"location":0,"echo":false,"script":null) ] } jump={ @@ -43,3 +45,8 @@ shove={ "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":88,"key_label":0,"unicode":120,"location":0,"echo":false,"script":null) ] } +shoot={ +"deadzone": 0.5, +"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":90,"key_label":0,"unicode":122,"location":0,"echo":false,"script":null) +] +} diff --git a/dariusgodotproject/scenes/bullet.tscn b/dariusgodotproject/scenes/bullet.tscn new file mode 100644 index 0000000..738f469 --- /dev/null +++ b/dariusgodotproject/scenes/bullet.tscn @@ -0,0 +1,15 @@ +[gd_scene load_steps=3 format=3 uid="uid://dwfife67i1fmr"] + +[ext_resource type="Script" path="res://scripts/bullet.gd" id="1_qivxv"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_dp02a"] +size = Vector2(16, 4) + +[node name="Area2D" type="Area2D"] +script = ExtResource("1_qivxv") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_dp02a") +debug_color = Color(1, 1, 0, 0.419608) + +[connection signal="body_entered" from="." to="." method="_on_body_entered"] diff --git a/dariusgodotproject/scenes/crate.tscn b/dariusgodotproject/scenes/crate.tscn index f3949b9..4c5322f 100644 --- a/dariusgodotproject/scenes/crate.tscn +++ b/dariusgodotproject/scenes/crate.tscn @@ -2,7 +2,7 @@ [sub_resource type="RectangleShape2D" id="RectangleShape2D_ixlbc"] -[node name="RigidBody2D" type="RigidBody2D"] +[node name="RigidBody2D" type="RigidBody2D" groups=["destructable"]] metadata/_edit_group_ = true [node name="CollisionShape2D" type="CollisionShape2D" parent="."] diff --git a/dariusgodotproject/scenes/game.tscn b/dariusgodotproject/scenes/game.tscn index 15f574d..b8c0e98 100644 --- a/dariusgodotproject/scenes/game.tscn +++ b/dariusgodotproject/scenes/game.tscn @@ -1,9 +1,11 @@ -[gd_scene load_steps=8 format=3 uid="uid://dcmyx5g6dksl8"] +[gd_scene load_steps=10 format=3 uid="uid://dcmyx5g6dksl8"] [ext_resource type="PackedScene" uid="uid://d1ej2kiy7jcpv" path="res://scenes/crate.tscn" id="1_uivx3"] [ext_resource type="Script" path="res://scripts/gamecontroller.gd" id="1_vhl00"] [ext_resource type="Script" path="res://scripts/trigger.gd" id="3_2qbah"] [ext_resource type="PackedScene" uid="uid://b75mow511wmmb" path="res://scenes/player.tscn" id="3_8h5jv"] +[ext_resource type="PackedScene" uid="uid://dwfife67i1fmr" path="res://scenes/bullet.tscn" id="5_7yfvg"] +[ext_resource type="Script" path="res://scripts/scenemanager.gd" id="6_n2kp0"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_1cyeq"] size = Vector2(101, 20) @@ -57,5 +59,12 @@ metadata/_edit_group_ = true shape = SubResource("CircleShape2D_mfh1j") debug_color = Color(0.825765, 0.00158581, 0.954354, 0.42) +[node name="Area2D2" parent="." instance=ExtResource("5_7yfvg")] +position = Vector2(309, 222) + +[node name="SceneManager" type="Node" parent="."] +unique_name_in_owner = true +script = ExtResource("6_n2kp0") + [connection signal="areaTrigger" from="Area2D" to="." method="_on_trigger"] [connection signal="body_entered" from="Area2D" to="Area2D" method="_on_body_entered"] diff --git a/dariusgodotproject/scenes/player.tscn b/dariusgodotproject/scenes/player.tscn index 3f521d5..280b5cf 100644 --- a/dariusgodotproject/scenes/player.tscn +++ b/dariusgodotproject/scenes/player.tscn @@ -4,7 +4,7 @@ [sub_resource type="CircleShape2D" id="CircleShape2D_i8a0m"] -[node name="CharacterBody2D" type="CharacterBody2D"] +[node name="CharacterBody2D" type="CharacterBody2D" groups=["player"]] script = ExtResource("1_racyk") metadata/_edit_group_ = true @@ -17,3 +17,9 @@ target_position = Vector2(25, 0) [node name="LeftRay" type="RayCast2D" parent="."] target_position = Vector2(-25, 0) + +[node name="RightTarget" type="Node2D" parent="."] +position = Vector2(10, -5) + +[node name="LeftTarget" type="Node2D" parent="."] +position = Vector2(-10, -5) diff --git a/dariusgodotproject/scripts/bullet.gd b/dariusgodotproject/scripts/bullet.gd new file mode 100644 index 0000000..428759a --- /dev/null +++ b/dariusgodotproject/scripts/bullet.gd @@ -0,0 +1,24 @@ +class_name Bullet extends Area2D + +var speed = 700 +signal hit + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta: float) -> void: + pass + +func _physics_process(delta: float) -> void: + position += transform.x * speed * delta +func setSpeed(value): + speed = value + + +func _on_body_entered(body: Node2D) -> void: + print("Bullet hits something") + if not body.is_in_group("player"): + hit.emit(body) diff --git a/dariusgodotproject/scripts/charactercontroller.gd b/dariusgodotproject/scripts/charactercontroller.gd index 29ce88e..9197110 100644 --- a/dariusgodotproject/scripts/charactercontroller.gd +++ b/dariusgodotproject/scripts/charactercontroller.gd @@ -14,6 +14,12 @@ var pushRightEnabled = false var pushLeftEnabled = false @export var PUSH_FORCE = 700 +# Bullet Attack Variables +@onready var right_target: Node2D = $RightTarget +@onready var left_target: Node2D = $LeftTarget + + + func _physics_process(delta: float) -> void: # Add the gravity. if not is_on_floor(): @@ -29,7 +35,19 @@ func _physics_process(delta: float) -> void: pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 2) if pushLeftEnabled && faceLeft == true: pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 2) - + # Handle Shooting + if Input.is_action_just_pressed("shoot"): + #print("I want to shoot") + #make a new bullet + var myBullet = %SceneManager.bulletFactory() + + #var myBullet = bullet.instantiate() + owner.add_child(myBullet) + myBullet.transform = right_target.global_transform + if faceLeft == true: + myBullet.transform = left_target.global_transform + myBullet.setSpeed(-700) + # 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("left", "right") diff --git a/dariusgodotproject/scripts/gamecontroller.gd b/dariusgodotproject/scripts/gamecontroller.gd index 571ff7a..172a29f 100644 --- a/dariusgodotproject/scripts/gamecontroller.gd +++ b/dariusgodotproject/scripts/gamecontroller.gd @@ -33,3 +33,8 @@ func _on_trigger(effect: Variant, body) -> void: if boxTotal <=0: print("YOU WON!!") get_tree().reload_current_scene() + +func bulletHit(body): + print("Game controller knows bullet hit something") + if body.is_in_group("destructable"): + body.queue_free() diff --git a/dariusgodotproject/scripts/scenemanager.gd b/dariusgodotproject/scripts/scenemanager.gd new file mode 100644 index 0000000..7e72561 --- /dev/null +++ b/dariusgodotproject/scripts/scenemanager.gd @@ -0,0 +1,22 @@ +extends Node +@onready var game: Node2D = $".." + +var bullet = preload("res://scenes/bullet.tscn") +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta: float) -> void: + pass + +func bulletFactory(): + print("Factory will make a bullet") + var myBullet = bullet.instantiate() + myBullet.connect("hit", onBulletHit) + return myBullet + +func onBulletHit(body): + print("Scene manager knows bullet hit") + game.bulletHit(body)