From 309f5f9941c787682d38d6c57c6707b608d9a9d1 Mon Sep 17 00:00:00 2001 From: Jdevadas Date: Mon, 11 Aug 2025 21:08:57 -0400 Subject: [PATCH] raycasts, melee attack,factory for bullets , spawn points, dynamic instatiation --- project.godot | 17 ++++++++- scenes/bullet.tscn | 15 ++++++++ scenes/crate.tscn | 14 +++++++ scenes/game.tscn | 54 +++++++++++---------------- scenes/player.tscn | 25 +++++++++++++ scenes/trigger.tscn | 2 +- scripts/bullet.gd | 18 +++++++++ scripts/crate.gd | 1 + scripts/gamecontroller.gd | 5 +++ scripts/player.gd | 77 +++++++++++++++++++++++++++++++++------ scripts/scene_manager.gd | 30 +++++++++++++++ 11 files changed, 210 insertions(+), 48 deletions(-) create mode 100644 scenes/bullet.tscn create mode 100644 scenes/crate.tscn create mode 100644 scenes/player.tscn create mode 100644 scripts/bullet.gd create mode 100644 scripts/crate.gd create mode 100644 scripts/scene_manager.gd diff --git a/project.godot b/project.godot index 6f7bb8d..6dd0652 100644 --- a/project.godot +++ b/project.godot @@ -11,6 +11,19 @@ config_version=5 [application] config/name="JulyGame" -run/main_scene="uid://doctwh8lc63y5" -config/features=PackedStringArray("4.4", "Forward Plus") +run/main_scene="res://scenes/game.tscn" +config/features=PackedStringArray("4.3", "Forward Plus") config/icon="res://icon.svg" + +[input] + +shove={ +"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) +] +} +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":88,"key_label":0,"unicode":120,"location":0,"echo":false,"script":null) +] +} diff --git a/scenes/bullet.tscn b/scenes/bullet.tscn new file mode 100644 index 0000000..3709dc5 --- /dev/null +++ b/scenes/bullet.tscn @@ -0,0 +1,15 @@ +[gd_scene load_steps=3 format=3 uid="uid://lg8k51aicde4"] + +[ext_resource type="Script" path="res://scripts/bullet.gd" id="1_bpcfw"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_oprgb"] +size = Vector2(8, 4) + +[node name="Bullet" type="Area2D"] +script = ExtResource("1_bpcfw") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_oprgb") +debug_color = Color(0.866207, 0.326527, 0.373638, 0.42) + +[connection signal="body_entered" from="." to="." method="_on_body_entered"] diff --git a/scenes/crate.tscn b/scenes/crate.tscn new file mode 100644 index 0000000..36d799f --- /dev/null +++ b/scenes/crate.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=3 format=3 uid="uid://d0mw3eh00s3kr"] + +[ext_resource type="Script" path="res://scripts/crate.gd" id="1_j2btj"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_uwrxv"] + +[node name="Crate" type="RigidBody2D" groups=["pushables", "shootables"]] +rotation = 0.602841 +script = ExtResource("1_j2btj") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_uwrxv") +debug_color = Color(0.820447, 0.391315, 0.259417, 0.42) diff --git a/scenes/game.tscn b/scenes/game.tscn index 7d4ab91..ed4817e 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -1,21 +1,24 @@ -[gd_scene load_steps=8 format=3 uid="uid://doctwh8lc63y5"] +[gd_scene load_steps=9 format=3 uid="uid://doctwh8lc63y5"] -[ext_resource type="Script" uid="uid://cpua8fr5bby78" path="res://scripts/gamecontroller.gd" id="1_lnu2h"] -[ext_resource type="Script" uid="uid://cnwga6l2btdbk" path="res://scripts/player.gd" id="1_uwrxv"] +[ext_resource type="Script" path="res://scripts/gamecontroller.gd" id="1_lnu2h"] +[ext_resource type="PackedScene" uid="uid://b5p1mlv4c1olv" path="res://scenes/player.tscn" id="2_5tj0f"] +[ext_resource type="Script" path="res://scripts/scene_manager.gd" id="2_w84c1"] [ext_resource type="PackedScene" uid="uid://6n3305pqp43a" path="res://scenes/trigger.tscn" id="2_yqjtg"] +[ext_resource type="PackedScene" uid="uid://d0mw3eh00s3kr" path="res://scenes/crate.tscn" id="4_syc2o"] +[ext_resource type="PackedScene" uid="uid://lg8k51aicde4" path="res://scenes/bullet.tscn" id="5_3trti"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_8cj0n"] size = Vector2(52, 20) -[sub_resource type="RectangleShape2D" id="RectangleShape2D_uwrxv"] - [sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_8cj0n"] -[sub_resource type="CircleShape2D" id="CircleShape2D_8cj0n"] - [node name="game" type="Node2D"] script = ExtResource("1_lnu2h") +[node name="SceneManager" type="Node2D" parent="."] +unique_name_in_owner = true +script = ExtResource("2_w84c1") + [node name="StaticBody2D" type="StaticBody2D" parent="."] position = Vector2(580, 413) metadata/_edit_group_ = true @@ -23,24 +26,6 @@ metadata/_edit_group_ = true [node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"] shape = SubResource("RectangleShape2D_8cj0n") -[node name="RigidBody2D" type="RigidBody2D" parent="."] -position = Vector2(594, 335) -rotation = 0.767945 -metadata/_edit_group_ = true - -[node name="CollisionShape2D" type="CollisionShape2D" parent="RigidBody2D"] -shape = SubResource("RectangleShape2D_uwrxv") -debug_color = Color(0.820447, 0.391315, 0.259417, 0.42) - -[node name="RigidBody2D2" type="RigidBody2D" parent="."] -position = Vector2(577, 256) -rotation = 0.602841 -metadata/_edit_group_ = true - -[node name="CollisionShape2D" type="CollisionShape2D" parent="RigidBody2D2"] -shape = SubResource("RectangleShape2D_uwrxv") -debug_color = Color(0.820447, 0.391315, 0.259417, 0.42) - [node name="ground" type="StaticBody2D" parent="."] position = Vector2(583, 553) metadata/_edit_group_ = true @@ -48,16 +33,19 @@ metadata/_edit_group_ = true [node name="CollisionShape2D" type="CollisionShape2D" parent="ground"] shape = SubResource("WorldBoundaryShape2D_8cj0n") -[node name="CharacterBody2D" type="CharacterBody2D" parent="."] -position = Vector2(580, 392) -script = ExtResource("1_uwrxv") -metadata/_edit_group_ = true - -[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"] -shape = SubResource("CircleShape2D_8cj0n") -debug_color = Color(0.256799, 0.626646, 0.294192, 0.42) +[node name="Player" parent="." instance=ExtResource("2_5tj0f")] +position = Vector2(580, 368) [node name="Area2D" parent="." instance=ExtResource("2_yqjtg")] position = Vector2(872, 521) +[node name="Crate" parent="." instance=ExtResource("4_syc2o")] +position = Vector2(592, 261) + +[node name="trigger" parent="." instance=ExtResource("2_yqjtg")] +position = Vector2(389, 519) + +[node name="Bullet" parent="." instance=ExtResource("5_3trti")] +position = Vector2(433, 332) + [connection signal="areaTriggerSignal" from="Area2D" to="." method="_on_trigger"] diff --git a/scenes/player.tscn b/scenes/player.tscn new file mode 100644 index 0000000..cf6fe2d --- /dev/null +++ b/scenes/player.tscn @@ -0,0 +1,25 @@ +[gd_scene load_steps=3 format=3 uid="uid://b5p1mlv4c1olv"] + +[ext_resource type="Script" path="res://scripts/player.gd" id="1_bqfhv"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_8cj0n"] + +[node name="Player" type="CharacterBody2D"] +script = ExtResource("1_bqfhv") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("CircleShape2D_8cj0n") +debug_color = Color(0.256799, 0.626646, 0.294192, 0.42) + +[node name="RightCast" type="RayCast2D" parent="."] +target_position = Vector2(21, 0) + +[node name="LeftCast" type="RayCast2D" parent="."] +target_position = Vector2(-20, 0) + +[node name="RightSpawn" type="Node2D" parent="."] +position = Vector2(12, -7) + +[node name="LeftSpawn" type="Node2D" parent="."] +position = Vector2(-12, -7) diff --git a/scenes/trigger.tscn b/scenes/trigger.tscn index 74a77e0..dfc5b68 100644 --- a/scenes/trigger.tscn +++ b/scenes/trigger.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=3 format=3 uid="uid://6n3305pqp43a"] -[ext_resource type="Script" uid="uid://c57chsew3r87y" path="res://scripts/trigger.gd" id="1_du5ex"] +[ext_resource type="Script" path="res://scripts/trigger.gd" id="1_du5ex"] [sub_resource type="CircleShape2D" id="CircleShape2D_uwrxv"] diff --git a/scripts/bullet.gd b/scripts/bullet.gd new file mode 100644 index 0000000..75b77cc --- /dev/null +++ b/scripts/bullet.gd @@ -0,0 +1,18 @@ +class_name Bullet extends Area2D + + +var speed:float = 700 +signal bulletDamageSignal(body,bullet) + +func setSpeed(value:float): + speed = value + +func _physics_process(delta: float) -> void: + position += transform.x * speed * delta + + +func _on_body_entered(body: Node2D) -> void: + + if body.is_in_group("shootables"): + + bulletDamageSignal.emit(body,self) diff --git a/scripts/crate.gd b/scripts/crate.gd new file mode 100644 index 0000000..7f42195 --- /dev/null +++ b/scripts/crate.gd @@ -0,0 +1 @@ +class_name Crate extends RigidBody2D diff --git a/scripts/gamecontroller.gd b/scripts/gamecontroller.gd index 3ea31da..5c87a97 100644 --- a/scripts/gamecontroller.gd +++ b/scripts/gamecontroller.gd @@ -15,3 +15,8 @@ func _on_trigger(effect: Variant, body:Variant) -> void: print("GC sees trigger "+effect) if not body is Player: body.queue_free() + +func bulletDamage(body, bullet): + print("gc knows about bullet hit") + body.queue_free() + #apply_central_impulse(Vector2(shoveDirection,0)*700) diff --git a/scripts/player.gd b/scripts/player.gd index 64400c1..3f29bdd 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -1,29 +1,82 @@ class_name Player extends CharacterBody2D +@onready var right_cast: RayCast2D = $RightCast +@onready var left_cast: RayCast2D = $LeftCast +@onready var right_spawn: Node2D = $RightSpawn +@onready var left_spawn: Node2D = $LeftSpawn const SPEED = 300.0 const JUMP_VELOCITY = -400.0 +var direction +enum FaceDirection{LEFT, RIGHT} +var facing:FaceDirection = FaceDirection.RIGHT -func _physics_process(delta): +var pushTarget +var pushEnabled := false + + + +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") + + handle_input() + handle_movement(delta) + move_and_slide() + handle_collisions() + +func handle_movement(delta:float) -> void: + if direction: velocity.x = direction * SPEED else: velocity.x = move_toward(velocity.x, 0, SPEED) + if not is_on_floor(): + velocity += get_gravity() * delta - move_and_slide() +func handle_input(): + if Input.is_action_just_pressed("ui_accept") and is_on_floor(): + velocity.y = JUMP_VELOCITY + direction = Input.get_axis("ui_left", "ui_right") + if direction <0: + facing =FaceDirection.LEFT + if direction > 0: + facing = FaceDirection.RIGHT + if Input.is_action_just_pressed("shove") && pushEnabled: + var shoveDirection:int + match facing: + FaceDirection.RIGHT: + #this is where tabbing does'nt work + shoveDirection = 1 + FaceDirection.LEFT: + shoveDirection = -1 + pushTarget.apply_central_impulse(Vector2(shoveDirection,0)*700) + if Input.is_action_just_pressed("shoot"): + + match facing: + FaceDirection.RIGHT: + + %SceneManager.makeBullet(right_spawn.global_transform,700) + FaceDirection.LEFT: + %SceneManager.makeBullet(left_spawn.global_transform,-700) +func handle_collisions(): 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() * 100) + # i? + if right_cast.is_colliding() && facing == FaceDirection.RIGHT: + + var collider = right_cast.get_collider() + if collider is Node && collider is RigidBody2D && collider.is_in_group("pushables"): + pushTarget = collider + pushEnabled = true + + if left_cast.is_colliding() && facing == FaceDirection.LEFT: + + var collider = left_cast.get_collider() + if collider is Node && collider is RigidBody2D && collider.is_in_group("pushables"): + pushTarget = collider + pushEnabled = true + if not right_cast.is_colliding() && not left_cast.is_colliding(): + pushEnabled = false diff --git a/scripts/scene_manager.gd b/scripts/scene_manager.gd new file mode 100644 index 0000000..e4e7792 --- /dev/null +++ b/scripts/scene_manager.gd @@ -0,0 +1,30 @@ +class_name SceneManager extends Node2D +var bulletArray = [] +var totalAllowedBullets = 7 +var bullet = preload("res://scenes/bullet.tscn") +@onready var game: Node2D = $".." + +# 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 +# makes and recycles bullets +func bulletFactory(): + var myBullet:Bullet + if bulletArray.size() < totalAllowedBullets: + myBullet = bullet.instantiate() + myBullet.bulletDamageSignal.connect(game.bulletDamage) + owner.add_child(myBullet) + else: + myBullet = bulletArray.pop_back() + bulletArray.push_front(myBullet) + return myBullet + +func makeBullet(position, speed): + var someBullet = bulletFactory() + someBullet.setSpeed(speed) + someBullet.transform = position