diff --git a/januaryproject/project.godot b/januaryproject/project.godot index f18a662..3e030a8 100644 --- a/januaryproject/project.godot +++ b/januaryproject/project.godot @@ -43,3 +43,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/januaryproject/scenes/bullet.tscn b/januaryproject/scenes/bullet.tscn new file mode 100644 index 0000000..b9fad73 --- /dev/null +++ b/januaryproject/scenes/bullet.tscn @@ -0,0 +1,15 @@ +[gd_scene load_steps=3 format=3 uid="uid://ckc5gbyie17tm"] + +[ext_resource type="Script" path="res://scripts/bullet.gd" id="1_fxxmp"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_rws30"] +size = Vector2(10, 4) + +[node name="Area2D" type="Area2D"] +script = ExtResource("1_fxxmp") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_rws30") +debug_color = Color(0.892283, 0.284139, 0.402358, 0.42) + +[connection signal="body_entered" from="." to="." method="_on_body_entered"] diff --git a/januaryproject/scenes/crate.tscn b/januaryproject/scenes/crate.tscn index 3cd1c0f..4622fa3 100644 --- a/januaryproject/scenes/crate.tscn +++ b/januaryproject/scenes/crate.tscn @@ -2,7 +2,7 @@ [sub_resource type="RectangleShape2D" id="RectangleShape2D_jqoba"] -[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/januaryproject/scenes/game.tscn b/januaryproject/scenes/game.tscn index 64f0010..f149201 100644 --- a/januaryproject/scenes/game.tscn +++ b/januaryproject/scenes/game.tscn @@ -1,9 +1,11 @@ -[gd_scene load_steps=8 format=3 uid="uid://r8jj43l4t6is"] +[gd_scene load_steps=10 format=3 uid="uid://r8jj43l4t6is"] [ext_resource type="Script" path="res://scripts/gamecontroller.gd" id="1_8i1rq"] [ext_resource type="PackedScene" uid="uid://di3tq4f0xmdqc" path="res://scenes/crate.tscn" id="1_tfj4n"] [ext_resource type="Script" path="res://scripts/trigger.gd" id="3_hm0br"] [ext_resource type="PackedScene" uid="uid://dcbk086hhrn7y" path="res://scenes/player.tscn" id="3_oy8j2"] +[ext_resource type="PackedScene" uid="uid://ckc5gbyie17tm" path="res://scenes/bullet.tscn" id="5_omx4l"] +[ext_resource type="Script" path="res://scripts/scene_manager.gd" id="6_1kka5"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_jk5qv"] size = Vector2(66, 20) @@ -56,5 +58,12 @@ metadata/_edit_group_ = true shape = SubResource("CircleShape2D_8j7te") debug_color = Color(0.827271, 0.252084, 0.761388, 0.42) +[node name="Area2D2" parent="." instance=ExtResource("5_omx4l")] +position = Vector2(178, 316) + +[node name="SceneManager" type="Node" parent="."] +unique_name_in_owner = true +script = ExtResource("6_1kka5") + [connection signal="areaTrigger" from="Area2D" to="." method="_on_trigger"] [connection signal="body_entered" from="Area2D" to="Area2D" method="_on_body_entered"] diff --git a/januaryproject/scenes/player.tscn b/januaryproject/scenes/player.tscn index 510036f..a84c7a9 100644 --- a/januaryproject/scenes/player.tscn +++ b/januaryproject/scenes/player.tscn @@ -4,7 +4,7 @@ [sub_resource type="CircleShape2D" id="CircleShape2D_qrixx"] -[node name="CharacterBody2D" type="CharacterBody2D"] +[node name="CharacterBody2D" type="CharacterBody2D" groups=["player"]] script = ExtResource("1_6alft") metadata/_edit_group_ = true @@ -17,3 +17,9 @@ target_position = Vector2(18, 0) [node name="LeftRay" type="RayCast2D" parent="."] target_position = Vector2(-19, 0) + +[node name="RightTarget" type="Node2D" parent="."] +position = Vector2(10, -5) + +[node name="LeftTarget" type="Node2D" parent="."] +position = Vector2(-10, -5) diff --git a/januaryproject/scripts/bullet.gd b/januaryproject/scripts/bullet.gd new file mode 100644 index 0000000..d64553c --- /dev/null +++ b/januaryproject/scripts/bullet.gd @@ -0,0 +1,25 @@ +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 hit sumtin") + if not body.is_in_group("player"): + hit.emit(body) diff --git a/januaryproject/scripts/charactercontroller.gd b/januaryproject/scripts/charactercontroller.gd index cbfe68b..d27ddf2 100644 --- a/januaryproject/scripts/charactercontroller.gd +++ b/januaryproject/scripts/charactercontroller.gd @@ -14,6 +14,11 @@ 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 +34,18 @@ 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"): + # 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/januaryproject/scripts/gamecontroller.gd b/januaryproject/scripts/gamecontroller.gd index 08542f1..5b61119 100644 --- a/januaryproject/scripts/gamecontroller.gd +++ b/januaryproject/scripts/gamecontroller.gd @@ -32,4 +32,9 @@ 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/januaryproject/scripts/scene_manager.gd b/januaryproject/scripts/scene_manager.gd new file mode 100644 index 0000000..7b4895a --- /dev/null +++ b/januaryproject/scripts/scene_manager.gd @@ -0,0 +1,23 @@ +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)