dynamic instantiation of bullets, bullet factory, multiple triggers, teleporting crates

This commit is contained in:
OddlyTimbot 2026-06-29 20:58:57 -04:00
parent d5fc80ff7b
commit b4bae4bb14
13 changed files with 290 additions and 55 deletions

View File

@ -15,6 +15,19 @@ run/main_scene="uid://ch6sfehmsskcs"
config/features=PackedStringArray("4.6", "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":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":90,"key_label":0,"unicode":122,"location":0,"echo":false,"script":null)
]
}
[physics]
3d/physics_engine="Jolt Physics"

15
scenes/bullet.tscn Normal file
View File

@ -0,0 +1,15 @@
[gd_scene format=3 uid="uid://bp51pocvj7p3x"]
[ext_resource type="Script" uid="uid://bvy43aky6y3er" path="res://scripts/bullet.gd" id="1_mkf8s"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_y25gk"]
size = Vector2(10, 4)
[node name="Bullet" type="Area2D" unique_id=397024757]
script = ExtResource("1_mkf8s")
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1862785605]
shape = SubResource("RectangleShape2D_y25gk")
debug_color = Color(0.92602646, 0.20405883, 0.46076977, 0.41960785)
[connection signal="body_entered" from="." to="." method="_on_body_entered"]

View File

@ -4,7 +4,7 @@
[sub_resource type="RectangleShape2D" id="RectangleShape2D_uwrxv"]
[node name="Crate" type="RigidBody2D" unique_id=1609374030]
[node name="Crate" type="RigidBody2D" unique_id=1609374030 groups=["shootable"]]
rotation = -0.68311554
script = ExtResource("1_b66cd")
metadata/_edit_group_ = true

View File

@ -1,16 +1,14 @@
[gd_scene format=3 uid="uid://ch6sfehmsskcs"]
[ext_resource type="Script" uid="uid://dtv434qd1rfmu" path="res://scripts/game.gd" id="1_lnu2h"]
[ext_resource type="Script" uid="uid://g4wclr5vnxk6" path="res://scripts/trigger.gd" id="1_uwrxv"]
[ext_resource type="Script" uid="uid://b5c00ybml8rj" path="res://scripts/scene_manager.gd" id="2_lbhrr"]
[ext_resource type="Script" uid="uid://b1obldvd2etx7" path="res://scripts/player.gd" id="2_yqjtg"]
[ext_resource type="PackedScene" uid="uid://byl4bdrupii3k" path="res://scenes/player.tscn" id="4_iywne"]
[ext_resource type="PackedScene" uid="uid://b6cmg0k8qjoxi" path="res://scenes/crate.tscn" id="5_iywne"]
[ext_resource type="PackedScene" uid="uid://b3hgbbmb0nohj" path="res://scenes/trigger.tscn" id="5_p57ef"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_8cj0n"]
[sub_resource type="CircleShape2D" id="CircleShape2D_8cj0n"]
[sub_resource type="CircleShape2D" id="CircleShape2D_uwrxv"]
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_iywne"]
[node name="Game" type="Node2D" unique_id=981419409]
script = ExtResource("1_lnu2h")
@ -18,58 +16,74 @@ script = ExtResource("1_lnu2h")
[node name="SceneManager" type="Node2D" parent="." unique_id=323246552]
script = ExtResource("2_lbhrr")
[node name="StaticBody2D" type="StaticBody2D" parent="." unique_id=1863997109]
position = Vector2(555, 409)
[node name="Player" parent="." unique_id=2145223914 instance=ExtResource("4_iywne")]
position = Vector2(490, 380)
[node name="Crates" type="Node2D" parent="." unique_id=207771395]
[node name="Crate" parent="Crates" unique_id=1609374030 instance=ExtResource("5_iywne")]
position = Vector2(951, 332.99997)
[node name="Crate3" parent="Crates" unique_id=249514132 instance=ExtResource("5_iywne")]
position = Vector2(731, 340)
[node name="Crate2" parent="Crates" unique_id=1603366376 instance=ExtResource("5_iywne")]
position = Vector2(558, 368)
[node name="Walls" type="Node2D" parent="." unique_id=80334518]
[node name="StaticBody2D" type="StaticBody2D" parent="Walls" unique_id=1863997109]
position = Vector2(553.00006, 401)
scale = Vector2(3.4000025, 1)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D" unique_id=1910670137]
[node name="CollisionShape2D" type="CollisionShape2D" parent="Walls/StaticBody2D" unique_id=1910670137]
shape = SubResource("RectangleShape2D_8cj0n")
[node name="StaticBody2D3" type="StaticBody2D" parent="." unique_id=2089762008]
[node name="StaticBody2D4" type="StaticBody2D" parent="Walls" unique_id=823918434]
position = Vector2(485.00006, 409)
scale = Vector2(3.4000025, 1)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="Walls/StaticBody2D4" unique_id=1243761990]
shape = SubResource("RectangleShape2D_8cj0n")
[node name="StaticBody2D3" type="StaticBody2D" parent="Walls" unique_id=2089762008]
position = Vector2(734.00006, 377)
scale = Vector2(3.4000025, 1)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D3" unique_id=892222577]
[node name="CollisionShape2D" type="CollisionShape2D" parent="Walls/StaticBody2D3" unique_id=892222577]
shape = SubResource("RectangleShape2D_8cj0n")
[node name="StaticBody2D2" type="StaticBody2D" parent="." unique_id=2038269484]
[node name="StaticBody2D2" type="StaticBody2D" parent="Walls" unique_id=2038269484]
position = Vector2(681.00006, 398)
rotation = -0.7375178
scale = Vector2(3.4000025, 1)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D2" unique_id=1636593491]
[node name="CollisionShape2D" type="CollisionShape2D" parent="Walls/StaticBody2D2" unique_id=1636593491]
shape = SubResource("RectangleShape2D_8cj0n")
[node name="Trigger" type="Area2D" parent="." unique_id=1742156972]
[node name="StaticBody2D5" type="StaticBody2D" parent="Walls" unique_id=564687476]
[node name="CollisionShape2D" type="CollisionShape2D" parent="Walls/StaticBody2D5" unique_id=1147447008]
position = Vector2(0, 588)
shape = SubResource("WorldBoundaryShape2D_iywne")
[node name="Triggers" type="Node2D" parent="." unique_id=1195236992]
[node name="Trigger" parent="Triggers" unique_id=229441825 instance=ExtResource("5_p57ef")]
position = Vector2(621.00006, 465.00006)
scale = Vector2(4.2, 4.2)
script = ExtResource("1_uwrxv")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="Trigger" unique_id=1581030951]
shape = SubResource("CircleShape2D_8cj0n")
debug_color = Color(0.3285461, 0.6189337, 0.22834331, 0.41960785)
[node name="Trigger2" parent="Triggers" unique_id=1474950249 instance=ExtResource("5_p57ef")]
position = Vector2(817, 445)
intent = "fan"
[node name="Player" type="CharacterBody2D" parent="." unique_id=1939613595]
position = Vector2(535.00006, 379.00003)
scale = Vector2(1.4799998, 1.4799998)
script = ExtResource("2_yqjtg")
metadata/_edit_group_ = true
[node name="Trigger3" parent="Triggers" unique_id=205623468 instance=ExtResource("5_p57ef")]
position = Vector2(953.99994, 442.99997)
intent = "teleport"
[node name="CollisionShape2D" type="CollisionShape2D" parent="Player" unique_id=312922488]
shape = SubResource("CircleShape2D_uwrxv")
debug_color = Color(0.39731503, 0.48776567, 0.95015794, 0.41960785)
[node name="Crates" type="Node2D" parent="." unique_id=207771395]
[node name="Crate" parent="Crates" unique_id=1609374030 instance=ExtResource("5_iywne")]
position = Vector2(625, 345.99997)
[node name="Crate2" parent="Crates" unique_id=1603366376 instance=ExtResource("5_iywne")]
position = Vector2(736, 347.99997)
[connection signal="areaTriggerSignal" from="Trigger" to="." method="_on_trigger"]
[connection signal="body_entered" from="Trigger" to="Trigger" method="_on_body_entered"]
[connection signal="areaTriggerSignal" from="Triggers/Trigger" to="." method="_on_trigger"]
[connection signal="areaTriggerSignal" from="Triggers/Trigger2" to="." method="_on_trigger"]
[connection signal="areaTriggerSignal" from="Triggers/Trigger3" to="." method="_on_trigger"]

26
scenes/player.tscn Normal file
View File

@ -0,0 +1,26 @@
[gd_scene format=3 uid="uid://byl4bdrupii3k"]
[ext_resource type="Script" uid="uid://b1obldvd2etx7" path="res://scripts/player.gd" id="1_3vyb7"]
[sub_resource type="CircleShape2D" id="CircleShape2D_uwrxv"]
[node name="Player" type="CharacterBody2D" unique_id=2145223914]
scale = Vector2(1.4799998, 1.4799998)
script = ExtResource("1_3vyb7")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=775715684]
shape = SubResource("CircleShape2D_uwrxv")
debug_color = Color(0.39731503, 0.48776567, 0.95015794, 0.41960785)
[node name="RightCast" type="RayCast2D" parent="." unique_id=967990879]
target_position = Vector2(16.891893, 0)
[node name="LeftCast" type="RayCast2D" parent="." unique_id=926020029]
target_position = Vector2(-17.56757, -0.67567575)
[node name="RightSpawn" type="Marker2D" parent="." unique_id=72314368]
position = Vector2(12.837839, -8.1081085)
[node name="LeftSpawn" type="Marker2D" parent="." unique_id=374152113]
position = Vector2(-12.837839, -8.108109)

16
scenes/trigger.tscn Normal file
View File

@ -0,0 +1,16 @@
[gd_scene format=3 uid="uid://b3hgbbmb0nohj"]
[ext_resource type="Script" uid="uid://g4wclr5vnxk6" path="res://scripts/trigger.gd" id="1_du5ex"]
[sub_resource type="CircleShape2D" id="CircleShape2D_8cj0n"]
[node name="Trigger" type="Area2D" unique_id=229441825]
scale = Vector2(4.2, 4.2)
script = ExtResource("1_du5ex")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1121443164]
shape = SubResource("CircleShape2D_8cj0n")
debug_color = Color(0.3285461, 0.6189337, 0.22834331, 0.41960785)
[connection signal="body_entered" from="." to="." method="_on_body_entered"]

13
scripts/bullet.gd Normal file
View File

@ -0,0 +1,13 @@
class_name Bullet extends Area2D
var speed:float = 700
signal bulletHitSignal(body, bullet)
func setSpeed(value)->void:
speed = value
func _process(delta: float) -> void:
position += transform.x * speed * delta
func _on_body_entered(body: Node2D) -> void:
if body is Crate:
bulletHitSignal.emit(body,self)

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

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

View File

@ -1 +1,13 @@
class_name Crate extends RigidBody2D
var teleport_target:Vector2 = Vector2.ZERO
var should_teleport:bool = false
func teleport_to(position:Vector2):
teleport_target = position
should_teleport = true
func _integrate_forces(state: PhysicsDirectBodyState2D) -> void:
if should_teleport:
state.transform = Transform2D.IDENTITY.translated(teleport_target)
should_teleport = false

View File

@ -1,11 +1,14 @@
extends Node2D
class_name GameController extends Node2D
signal destroySignal(body)
signal fanTriggerSignal(body, fanDirection)
signal teleportTriggerSignal(body)
var numberOfCrates = 2
var timeAvailable = 5
var timeAvailable = 50
var timer = Timer.new()
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
get_window().grab_focus()
add_child(timer)
timer.wait_time = 1
timer.one_shot = false
@ -24,14 +27,25 @@ func _process(delta: float) -> void:
pass
func _on_trigger(body: Variant, trigger: Variant) -> void:
func _on_trigger(body: Variant, trigger: Variant, intent:String) -> void:
print("GC knows trigger fired")
body.queue_free()
numberOfCrates -=1
if numberOfCrates == 0:
print("YOU WON!!!")
get_tree().reload_current_scene()
match intent:
"destroy":
destroySignal.emit(body)
"fan":
print("do a fan effect")
fanTriggerSignal.emit(body,Vector2(0,-1)*100*9)
"teleport":
print("do a teleport")
teleportTriggerSignal.emit(body)
func crateTotal(howmany):
numberOfCrates = howmany
print("GC knows about crates: "+str(numberOfCrates))
if numberOfCrates == 0:
print("YOU WON!!!")
get_tree().reload_current_scene()
func bulletDamage(body, _bullet)->void:
if body.is_in_group("shootable"):
destroySignal.emit(body)

View File

@ -1,9 +1,22 @@
extends CharacterBody2D
class_name Player extends CharacterBody2D
signal pushSignal(body, direction)
signal shootSignal(markerPosition, speed)
@onready var right_cast: RayCast2D = $RightCast
@onready var left_cast: RayCast2D = $LeftCast
@onready var right_spawn: Marker2D = $RightSpawn
@onready var left_spawn: Marker2D = $LeftSpawn
var pushRightEnabled = false
var pushLeftEnabled = false
var pushTarget
var pushTimer = 0
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
enum FaceDirection {RIGHT,LEFT}
var facing:FaceDirection = FaceDirection.RIGHT
func _physics_process(delta: float) -> void:
# Add the gravity.
@ -13,19 +26,64 @@ func _physics_process(delta: float) -> void:
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Handle shove.
if Input.is_action_just_pressed("shove"):
print("I want to shove")
pushTimer = Time.get_ticks_msec()
if Input.is_action_just_released("shove"):
var pushForce = Time.get_ticks_msec() - pushTimer
if pushForce>4000:
pushForce=4000
if pushRightEnabled and facing==FaceDirection.RIGHT:
#deliver the boom
pushSignal.emit(pushTarget, Vector2(1,0)*100*(pushForce/100))
if pushLeftEnabled and facing==FaceDirection.LEFT:
pushSignal.emit(pushTarget, Vector2(-1,0)*100*(pushForce/100))
#Handle Shoot
if Input.is_action_just_pressed("shoot"):
print("Ima shoot a bullet")
#marker postion, speed
match facing:
FaceDirection.RIGHT:
shootSignal.emit(right_spawn.global_transform, 800)
FaceDirection.LEFT:
shootSignal.emit(left_spawn.global_transform, 800)
# 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.RIGHT
else:
facing = FaceDirection.LEFT
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
if right_cast.is_colliding():
print("Right raycast collision")
var collider = right_cast.get_collider()
if collider is Node:
pushRightEnabled = true
pushTarget = collider
else:
pushRightEnabled = false
if left_cast.is_colliding():
print("Left raycast collision")
var collider = left_cast.get_collider()
if collider is Node:
pushLeftEnabled = true
pushTarget = collider
else:
pushLeftEnabled = false
#a loop
for i in get_slide_collision_count():
var c = get_slide_collision(i)
#is this a bonkable thing?
if c.get_collider() is RigidBody2D:
c.get_collider().apply_central_impulse(-c.get_normal() * 100)
#c.get_collider().apply_central_impulse(-c.get_normal() * 100)
pushSignal.emit(c.get_collider(), -c.get_normal() * 100)

View File

@ -1,10 +1,22 @@
extends Node2D
@onready var game: Node2D = $".."
@onready var game: GameController = $".."
@onready var crates: Node2D = $"../Crates"
@onready var player: Player = $"../Player"
var bullet = preload("res://scenes/bullet.tscn")
var bulletArray:Array[Bullet] = []
var totalAllowedBullets:int = 7
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
#signals from the view
player.pushSignal.connect(pushTarget)
player.shootSignal.connect(makeBullet)
#signals from the GameController
game.destroySignal.connect(destroy)
game.fanTriggerSignal.connect(pushTarget)
game.teleportTriggerSignal.connect(teleportTarget)
buildLevel()
@ -14,10 +26,48 @@ func _process(delta: float) -> void:
func buildLevel()->void:
#tell GC how many crates...
updateCrates()
func updateCrates()->void:
var totalCrates = 0
if crates:
for obj in crates.get_children():
if obj is Crate:
if not obj.tree_exited.is_connected(updateCrates):
obj.tree_exited.connect(updateCrates)
totalCrates +=1
game.crateTotal(totalCrates)
func pushTarget(body,pushDirection)->void:
if body is RigidBody2D:
body.apply_central_impulse(pushDirection)
func destroy(body)->void:
body.queue_free()
func teleportTarget(body)->void:
if body is Crate:
var viewport_size = get_viewport().get_visible_rect().size
var random_x = randf_range(0, viewport_size.x)
var random_y = randf_range(0, viewport_size.y)
var newPosition = Vector2(random_x,random_y)
body.teleport_to(newPosition)
func bulletFactory()->Bullet:
var myBullet:Bullet
#how many bullets have been produced?
if bulletArray.size() <= totalAllowedBullets:
#make a new bullet
myBullet = bullet.instantiate()
add_sibling(myBullet)
if not myBullet.bulletHitSignal.is_connected(game.bulletDamage):
myBullet.bulletHitSignal.connect(game.bulletDamage)
else:
myBullet = bulletArray.pop_back()
bulletArray.push_front(myBullet)
return myBullet
func makeBullet(spawnPosition, speed)->void:
print("make bullet")
var myBullet:Bullet = bulletFactory()
myBullet.transform = spawnPosition
myBullet.setSpeed(speed)

View File

@ -1,5 +1,8 @@
extends Area2D
signal areaTriggerSignal(body, trigger)
class_name Trigger extends Area2D
@export var intent = "destroy"
signal areaTriggerSignal(body, trigger, intent)
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
@ -13,4 +16,4 @@ func _process(delta: float) -> void:
func _on_body_entered(body: Node2D) -> void:
print("Something entered the trigger!")
areaTriggerSignal.emit(body, self)
areaTriggerSignal.emit(body, self, intent)