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

This commit is contained in:
OddlyTimbot committed 2026-06-29 20:58:57 -04:00
1 parent d5fc80ff7b
commit b4bae4bb14
13 files changed
+290 -55

No files matched your search

+13
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
View File
@@ -0,0 +1 @@
uid://bvy43aky6y3er
+12
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
+23 -9
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)
+61 -3
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)
+51 -1
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)
+6 -3
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)