From 8a78da855cb656c7e3fdd1af688b737efcc30c90 Mon Sep 17 00:00:00 2001 From: AnicaCihla Date: Mon, 9 Jun 2025 21:21:45 -0400 Subject: [PATCH] bullets, bullet factory, shove attack, scenemanager --- Scenes/Player.tscn | 25 ++++++++++++++++++ Scenes/bullet.tscn | 14 ++++++++++ Scenes/game.tscn | 34 +++++++++++++----------- Scripts/character_body_2d.gd | 51 ++++++++++++++++++++++++++++++++++++ Scripts/scene_manager.gd | 30 +++++++++++++++++++++ Scripts/scene_manager.gd.uid | 1 + bullet.gd | 13 +++++++++ bullet.gd.uid | 1 + project.godot | 13 +++++++++ 9 files changed, 166 insertions(+), 16 deletions(-) create mode 100644 Scenes/Player.tscn create mode 100644 Scenes/bullet.tscn create mode 100644 Scripts/scene_manager.gd create mode 100644 Scripts/scene_manager.gd.uid create mode 100644 bullet.gd create mode 100644 bullet.gd.uid diff --git a/Scenes/Player.tscn b/Scenes/Player.tscn new file mode 100644 index 0000000..a220c81 --- /dev/null +++ b/Scenes/Player.tscn @@ -0,0 +1,25 @@ +[gd_scene load_steps=3 format=3 uid="uid://bu8e4iyw8pc03"] + +[ext_resource type="Script" uid="uid://dsryf6gxqcm1k" path="res://Scripts/character_body_2d.gd" id="1_0y7nr"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_2poj3"] + +[node name="CharacterBody2D" type="CharacterBody2D"] +script = ExtResource("1_0y7nr") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("CircleShape2D_2poj3") +debug_color = Color(0.613557, 0.363647, 0.991688, 0.42) + +[node name="RightCast" type="RayCast2D" parent="."] +target_position = Vector2(16, 0) + +[node name="LeftCast" type="RayCast2D" parent="."] +target_position = Vector2(-16, 0) + +[node name="RightSpawn" type="Node2D" parent="."] +position = Vector2(17, -8) + +[node name="LeftSpawn" type="Node2D" parent="."] +position = Vector2(-14, -9) diff --git a/Scenes/bullet.tscn b/Scenes/bullet.tscn new file mode 100644 index 0000000..afdd82d --- /dev/null +++ b/Scenes/bullet.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=3 format=3 uid="uid://cm8o5vpn4a00"] + +[ext_resource type="Script" uid="uid://q1ok5ukbtibm" path="res://bullet.gd" id="1_xjght"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_oduel"] +size = Vector2(14, 4) + +[node name="Bullet" type="Area2D"] +script = ExtResource("1_xjght") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_oduel") +debug_color = Color(0.995859, 0, 0.197667, 0.42) diff --git a/Scenes/game.tscn b/Scenes/game.tscn index 98606f3..6dd9251 100644 --- a/Scenes/game.tscn +++ b/Scenes/game.tscn @@ -1,35 +1,31 @@ -[gd_scene load_steps=7 format=3 uid="uid://c6mxse0hqjro7"] +[gd_scene load_steps=8 format=3 uid="uid://c6mxse0hqjro7"] -[ext_resource type="Script" uid="uid://dsryf6gxqcm1k" path="res://Scripts/character_body_2d.gd" id="1_ebmjs"] [ext_resource type="Script" uid="uid://tno7aku0yyke" path="res://Scripts/gamecontroller.gd" id="1_wrm1d"] +[ext_resource type="Script" uid="uid://bs4frobn6kxne" path="res://Scripts/scene_manager.gd" id="2_i6g32"] [ext_resource type="PackedScene" uid="uid://cd7fv3pwgr880" path="res://Scenes/crate.tscn" id="3_3dryh"] +[ext_resource type="PackedScene" uid="uid://bu8e4iyw8pc03" path="res://Scenes/Player.tscn" id="4_3dryh"] [ext_resource type="PackedScene" uid="uid://b5d8himq5sttt" path="res://Scenes/trigger.tscn" id="4_wowpa"] +[ext_resource type="PackedScene" uid="uid://cm8o5vpn4a00" path="res://Scenes/bullet.tscn" id="5_wowpa"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_2poj3"] size = Vector2(50, 20) -[sub_resource type="CircleShape2D" id="CircleShape2D_2poj3"] - [node name="Game" type="Node2D"] script = ExtResource("1_wrm1d") +[node name="SceneManager" type="Node2D" parent="."] +unique_name_in_owner = true +script = ExtResource("2_i6g32") + [node name="StaticBody2D" type="StaticBody2D" parent="."] position = Vector2(595, 337) -scale = Vector2(1.50487, 1.09333) +scale = Vector2(4.146, 0.985995) metadata/_edit_group_ = true [node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"] +scale = Vector2(1, 1) shape = SubResource("RectangleShape2D_2poj3") -[node name="CharacterBody2D" type="CharacterBody2D" parent="."] -position = Vector2(576, 303) -script = ExtResource("1_ebmjs") -metadata/_edit_group_ = true - -[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"] -shape = SubResource("CircleShape2D_2poj3") -debug_color = Color(0.613557, 0.363647, 0.991688, 0.42) - [node name="RigidBody2D3" parent="." instance=ExtResource("3_3dryh")] position = Vector2(596, 262) @@ -40,11 +36,17 @@ position = Vector2(616, 222) position = Vector2(574, 222) [node name="Area2D2" parent="." instance=ExtResource("4_wowpa")] -position = Vector2(511, 397) +position = Vector2(507, 414) effect = "destroy" [node name="Area2D3" parent="." instance=ExtResource("4_wowpa")] -position = Vector2(684, 400) +position = Vector2(679, 430) + +[node name="CharacterBody2D" parent="." instance=ExtResource("4_3dryh")] +position = Vector2(614, 314) + +[node name="Bullet" parent="." instance=ExtResource("5_wowpa")] +position = Vector2(520, 265) [connection signal="areatrigger" from="Area2D2" to="." method="_on_areatrigger"] [connection signal="areatrigger" from="Area2D3" to="." method="_on_areatrigger"] diff --git a/Scripts/character_body_2d.gd b/Scripts/character_body_2d.gd index efc6bf7..90fa538 100644 --- a/Scripts/character_body_2d.gd +++ b/Scripts/character_body_2d.gd @@ -4,6 +4,17 @@ extends CharacterBody2D const SPEED = 300.0 const JUMP_VELOCITY = -400.0 +@onready var right_cast: RayCast2D = $RightCast +@onready var left_cast: RayCast2D = $LeftCast +@onready var right_spawn: Node2D = $RightSpawn +@onready var left_spawn: Node2D = $LeftSpawn + + +enum FaceDirection{LEFT, RIGHT} +var facing:FaceDirection = FaceDirection.RIGHT + +var pushTarget +var pushEnabled = false func _physics_process(delta: float) -> void: # Add the gravity. @@ -13,12 +24,32 @@ func _physics_process(delta: float) -> void: # Handle jump. if Input.is_action_just_pressed("ui_accept") and is_on_floor(): velocity.y = JUMP_VELOCITY + + if Input.is_action_just_pressed("shove") && pushEnabled: + print("shove pressed") + if facing == FaceDirection.RIGHT: + pushTarget.apply_central_impulse(Vector2(1,0)*700) + pushEnabled = false + if facing == FaceDirection.LEFT: + pushTarget.apply_central_impulse(Vector2(-1,0)*700) + pushEnabled = false + + if Input.is_action_just_pressed("shoot"): + print("shoot a bullet") + if facing == FaceDirection.RIGHT: + %SceneManager.makeBullet(right_spawn.global_transform, 700) + if facing == FaceDirection.LEFT: + %SceneManager.makeBullet(left_spawn.global_transform, -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("ui_left", "ui_right") if direction: velocity.x = direction * SPEED + if direction <0: + facing = FaceDirection.LEFT + if direction >0: + facing = FaceDirection.RIGHT else: velocity.x = move_toward(velocity.x, 0, SPEED) @@ -27,3 +58,23 @@ func _physics_process(delta: float) -> void: var c = get_slide_collision(i) if c.get_collider() is RigidBody2D: c.get_collider().apply_central_impulse(-c.get_normal() * 100) + + if right_cast.is_colliding() && facing==FaceDirection.RIGHT: + #get the thing I am colliding with + var collider = right_cast.get_collider() + if collider is Node && collider is RigidBody2D: + print("I can shove this to the right") + pushTarget = collider + pushEnabled = true + if not right_cast.is_colliding() && not left_cast.is_colliding(): + pushEnabled = false + if left_cast.is_colliding() && facing==FaceDirection.LEFT: + var collider = left_cast.get_collider() + if collider is Node && collider is RigidBody2D: + print("I can shove this to the left") + pushTarget = collider + pushEnabled = true + if not left_cast.is_colliding() && not right_cast.is_colliding(): + pushEnabled = false + + diff --git a/Scripts/scene_manager.gd b/Scripts/scene_manager.gd new file mode 100644 index 0000000..f0d7ba2 --- /dev/null +++ b/Scripts/scene_manager.gd @@ -0,0 +1,30 @@ +extends Node2D + +var bullet = preload("res://Scenes/bullet.tscn") +var bulletArray =[] +var totalAllowedBullets = 7 + +#makes bullets +func bulletFactory(): + var mybullet + if bulletArray.size() < totalAllowedBullets: + #make a new bullet + mybullet = bullet.instantiate() + owner.add_child(mybullet) + else: + #recycle bullet + mybullet = bulletArray.pop_back() + + bulletArray.push_front(mybullet) + return mybullet + +#order desk +func makeBullet(position, speed): + print("Scenemanger orders a bullet") + #ask the factory for a bullet + var mybullet = bulletFactory() + #set the speed for the bullet + mybullet.setSpeed(speed) + #position the bullet + mybullet.transform = position + diff --git a/Scripts/scene_manager.gd.uid b/Scripts/scene_manager.gd.uid new file mode 100644 index 0000000..4c684ac --- /dev/null +++ b/Scripts/scene_manager.gd.uid @@ -0,0 +1 @@ +uid://bs4frobn6kxne diff --git a/bullet.gd b/bullet.gd new file mode 100644 index 0000000..5a91ee5 --- /dev/null +++ b/bullet.gd @@ -0,0 +1,13 @@ +class_name Bullet extends Area2D +var speed = 700 + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + pass # Replace with function body.func setSpeed(value): + +func setSpeed(value): + speed = value + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _physics_process(delta: float) -> void: + position += transform.x * speed * delta diff --git a/bullet.gd.uid b/bullet.gd.uid new file mode 100644 index 0000000..dddcdd1 --- /dev/null +++ b/bullet.gd.uid @@ -0,0 +1 @@ +uid://q1ok5ukbtibm diff --git a/project.godot b/project.godot index 11b351c..12c02d6 100644 --- a/project.godot +++ b/project.godot @@ -21,3 +21,16 @@ folder_colors={ "res://Scenes/": "pink", "res://Scripts/": "orange" } + +[input] + +shove={ +"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":88,"key_label":0,"unicode":120,"location":0,"echo":false,"script":null) +] +} +shoot={ +"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":89,"key_label":0,"unicode":121,"location":0,"echo":false,"script":null) +] +}