working bullet factory

This commit is contained in:
Darius committed 2025-01-13 20:58:53 -05:00
1 parent 80a771f3a9
commit 205e8d43b5
9 files changed
+111 -5

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
class_name Bullet extends Area2D
var speed = 700
signal hit
# 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
func _physics_process(delta: float) -> void:
position += transform.x * speed * delta
func setSpeed(value):
speed = value
func _on_body_entered(body: Node2D) -> void:
print("Bullet hits something")
if not body.is_in_group("player"):
hit.emit(body)
@@ -14,6 +14,12 @@ var pushRightEnabled = false
var pushLeftEnabled = false
@export var PUSH_FORCE = 700
# Bullet Attack Variables
@onready var right_target: Node2D = $RightTarget
@onready var left_target: Node2D = $LeftTarget
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
@@ -29,7 +35,19 @@ func _physics_process(delta: float) -> void:
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 2)
if pushLeftEnabled && faceLeft == true:
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 2)
# Handle Shooting
if Input.is_action_just_pressed("shoot"):
#print("I want to shoot")
#make a new bullet
var myBullet = %SceneManager.bulletFactory()
#var myBullet = bullet.instantiate()
owner.add_child(myBullet)
myBullet.transform = right_target.global_transform
if faceLeft == true:
myBullet.transform = left_target.global_transform
myBullet.setSpeed(-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("left", "right")
@@ -33,3 +33,8 @@ func _on_trigger(effect: Variant, body) -> void:
if boxTotal <=0:
print("YOU WON!!")
get_tree().reload_current_scene()
func bulletHit(body):
print("Game controller knows bullet hit something")
if body.is_in_group("destructable"):
body.queue_free()
@@ -0,0 +1,22 @@
extends Node
@onready var game: Node2D = $".."
var bullet = preload("res://scenes/bullet.tscn")
# 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
func bulletFactory():
print("Factory will make a bullet")
var myBullet = bullet.instantiate()
myBullet.connect("hit", onBulletHit)
return myBullet
func onBulletHit(body):
print("Scene manager knows bullet hit")
game.bulletHit(body)