raycasts, melee attack, factory for bullets, spawn points, dynamic instantiation

This commit is contained in:
OddlyTimbot 2025-08-11 21:04:36 -04:00
parent 2b2d2f08e5
commit b6d588fcfe
13 changed files with 202 additions and 42 deletions

View File

@ -14,3 +14,16 @@ config/name="JulyGame"
run/main_scene="uid://coiqh7p31ukig"
config/features=PackedStringArray("4.4", "Forward Plus")
config/icon="res://icon.svg"
[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":90,"key_label":0,"unicode":122,"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":88,"key_label":0,"unicode":120,"location":0,"echo":false,"script":null)
]
}

15
scenes/bullet.tscn Normal file
View File

@ -0,0 +1,15 @@
[gd_scene load_steps=3 format=3 uid="uid://dopt3unflv7l3"]
[ext_resource type="Script" uid="uid://bhh178vuo5bgo" path="res://scripts/bullet.gd" id="1_mkf8s"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_y25gk"]
size = Vector2(8, 4)
[node name="Bullet" type="Area2D"]
script = ExtResource("1_mkf8s")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_y25gk")
debug_color = Color(0.97707, 0.111833, 0.261075, 0.42)
[connection signal="body_entered" from="." to="." method="_on_body_entered"]

14
scenes/crate.tscn Normal file
View File

@ -0,0 +1,14 @@
[gd_scene load_steps=3 format=3 uid="uid://dlihpyr58tqqq"]
[ext_resource type="Script" uid="uid://c5gjsxgxmjfki" path="res://scripts/crate.gd" id="1_b66cd"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_uwrxv"]
[node name="Crate" type="RigidBody2D" groups=["pushables", "shootables"]]
rotation = -0.301036
script = ExtResource("1_b66cd")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_uwrxv")
debug_color = Color(0.817443, 0.400008, 0.177458, 0.42)

View File

@ -1,21 +1,24 @@
[gd_scene load_steps=8 format=3 uid="uid://coiqh7p31ukig"]
[gd_scene load_steps=9 format=3 uid="uid://coiqh7p31ukig"]
[ext_resource type="Script" uid="uid://bblqtronap0j4" path="res://scripts/gamecontroller.gd" id="1_lnu2h"]
[ext_resource type="Script" uid="uid://b7rahgxxw1yuw" path="res://scripts/player.gd" id="1_uwrxv"]
[ext_resource type="PackedScene" uid="uid://dlihpyr58tqqq" path="res://scenes/crate.tscn" id="2_lbhrr"]
[ext_resource type="PackedScene" uid="uid://boqg1nyudmkh4" path="res://scenes/player.tscn" id="2_lnu2h"]
[ext_resource type="Script" uid="uid://d1daji4xwuj03" path="res://scripts/scene_manager.gd" id="2_p57ef"]
[ext_resource type="PackedScene" uid="uid://csotsc2cycyia" path="res://scenes/trigger.tscn" id="2_yqjtg"]
[ext_resource type="PackedScene" uid="uid://dopt3unflv7l3" path="res://scenes/bullet.tscn" id="5_iywne"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_8cj0n"]
size = Vector2(44, 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_p57ef")
[node name="StaticBody2D" type="StaticBody2D" parent="."]
position = Vector2(567, 402)
metadata/_edit_group_ = true
@ -23,23 +26,8 @@ metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"]
shape = SubResource("RectangleShape2D_8cj0n")
[node name="RigidBody2D" type="RigidBody2D" parent="."]
position = Vector2(593, 331)
rotation = -0.349066
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="RigidBody2D"]
shape = SubResource("RectangleShape2D_uwrxv")
debug_color = Color(0.817443, 0.400008, 0.177458, 0.42)
[node name="RigidBody2D2" type="RigidBody2D" parent="."]
[node name="Crate" parent="." instance=ExtResource("2_lbhrr")]
position = Vector2(575, 221)
rotation = -0.301036
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="RigidBody2D2"]
shape = SubResource("RectangleShape2D_uwrxv")
debug_color = Color(0.817443, 0.400008, 0.177458, 0.42)
[node name="ground" type="StaticBody2D" parent="."]
position = Vector2(551, 565)
@ -48,17 +36,13 @@ metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="ground"]
shape = SubResource("WorldBoundaryShape2D_8cj0n")
[node name="CharacterBody2D" type="CharacterBody2D" parent="."]
[node name="Player" parent="." instance=ExtResource("2_lnu2h")]
position = Vector2(567, 380)
script = ExtResource("1_uwrxv")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"]
shape = SubResource("CircleShape2D_8cj0n")
debug_color = Color(0.172558, 0.632665, 0.34599, 0.42)
[node name="Area2D" parent="." instance=ExtResource("2_yqjtg")]
position = Vector2(601, 428)
scale = Vector2(1.36, 1.12)
position = Vector2(564, 436)
[node name="Bullet" parent="." instance=ExtResource("5_iywne")]
position = Vector2(351, 312)
[connection signal="areaTriggerSignal" from="Area2D" to="." method="_on_trigger"]

25
scenes/player.tscn Normal file
View File

@ -0,0 +1,25 @@
[gd_scene load_steps=3 format=3 uid="uid://boqg1nyudmkh4"]
[ext_resource type="Script" uid="uid://b7rahgxxw1yuw" path="res://scripts/player.gd" id="1_3vyb7"]
[sub_resource type="CircleShape2D" id="CircleShape2D_8cj0n"]
[node name="Player" type="CharacterBody2D"]
script = ExtResource("1_3vyb7")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_8cj0n")
debug_color = Color(0.172558, 0.632665, 0.34599, 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(13, -6)
[node name="LeftSpawn" type="Node2D" parent="."]
position = Vector2(-13, -7)

18
scripts/bullet.gd Normal file
View File

@ -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:
print("Bullet hitting")
if body.is_in_group("shootables"):
print("This is shootable")
bulletDamageSignal.emit(body, self)

1
scripts/bullet.gd.uid Normal file
View File

@ -0,0 +1 @@
uid://bhh178vuo5bgo

1
scripts/crate.gd Normal file
View File

@ -0,0 +1 @@
class_name Crate extends RigidBody2D

1
scripts/crate.gd.uid Normal file
View File

@ -0,0 +1 @@
uid://c5gjsxgxmjfki

View File

@ -16,3 +16,7 @@ func _on_trigger(effect: Variant, body: Variant) -> void:
if not body is Player:
body.queue_free()
func bulletDamage(body, bullet):
print("GC know about bullet hit")
body.queue_free()

View File

@ -1,29 +1,78 @@
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
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
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:
shoveDirection = 1
FaceDirection.LEFT:
shoveDirection = -1
pushTarget.apply_central_impulse(Vector2(shoveDirection,0)*700)
if Input.is_action_just_pressed("shoot"):
print("Ima shoot")
match facing:
FaceDirection.RIGHT:
print("shoot to right")
%SceneManager.makeBullet(right_spawn.global_transform, 700)
FaceDirection.LEFT:
%SceneManager.makeBullet(left_spawn.global_transform, -700)
move_and_slide()
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)
if right_cast.is_colliding() && facing==FaceDirection.RIGHT:
print("RC is colliding")
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

34
scripts/scene_manager.gd Normal file
View File

@ -0,0 +1,34 @@
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:
#make a new bullet
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)
# postition the bullet
someBullet.transform = position

View File

@ -0,0 +1 @@
uid://d1daji4xwuj03