Add bullets #4

Open
opened 2026-05-04 20:52:07 +00:00 by OddlyTimbot · 0 comments
Owner

Give the player the ability to shoot bullets. This will involve making a Bullet that extends Area2D.
Bullets will be made in the SceneManager, which will use an object pool to control the total number of bullets.

First the bullet code:

class_name Bullet extends Area2D

var speed:float = 700
@onready var bullet_graphic = $BulletGraphic
signal bulletDamageSignal(area, bullet)
# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	position += transform.x * speed * delta

func setSpeed(value)->void:
	speed = value
	if speed<0:
		bullet_graphic.flip_h = true
	else:
		bullet_graphic.flip_h = false


func _on_area_entered(area):
	bulletDamageSignal.emit(area, self)

In the SceneManager, an array will be used to control and recycle bullets.
var bullet = preload("res://scenes/bullet.tscn")

And then:

var bulletArray = []
var totalAllowedBullets = 7

Create a function that can be called by the player, that requests the SceneManager to create a bullet and put it in the world.

func bulletFactory()->Bullet:
	var myBullet:Bullet
	if bulletArray.size() < totalAllowedBullets:
		#make a new bullet
		myBullet = bullet.instantiate()
		myBullet.bulletDamageSignal.connect(Gamecontroller.bulletDamage)
		owner.add_child(myBullet)
	else:
		myBullet = bulletArray.pop_back()
		
	bulletArray.push_front(myBullet)
	return myBullet
	
func makeBullet(_bulletPosition, _bulletSpeed)->void:
	print("make a bullet, put it in the world")
	var myBullet = bulletFactory()
	myBullet.transform = _bulletPosition
	myBullet.setSpeed(_bulletSpeed)

Now from the Player, the bullet can be requested. In the handleInput method:

if Input.is_action_just_pressed("shoot"):
		print("Ima shoot")
		match facing:
			FaceDirection.RIGHT:
				print("shoot right")
				%SceneManager.makeBullet(right_spawn.global_transform, 700)
			FaceDirection.LEFT:
				print("shoot left")
				%SceneManager.makeBullet(left_spawn.global_transform, -700)
Give the player the ability to shoot bullets. This will involve making a Bullet that extends Area2D. Bullets will be made in the SceneManager, which will use an object pool to control the total number of bullets. First the bullet code: ``` class_name Bullet extends Area2D var speed:float = 700 @onready var bullet_graphic = $BulletGraphic signal bulletDamageSignal(area, bullet) # Called when the node enters the scene tree for the first time. func _ready(): pass # Replace with function body. # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta): position += transform.x * speed * delta func setSpeed(value)->void: speed = value if speed<0: bullet_graphic.flip_h = true else: bullet_graphic.flip_h = false func _on_area_entered(area): bulletDamageSignal.emit(area, self) ``` In the SceneManager, an array will be used to control and recycle bullets. `var bullet = preload("res://scenes/bullet.tscn")` And then: ``` var bulletArray = [] var totalAllowedBullets = 7 ``` Create a function that can be called by the player, that requests the SceneManager to create a bullet and put it in the world. ``` func bulletFactory()->Bullet: var myBullet:Bullet if bulletArray.size() < totalAllowedBullets: #make a new bullet myBullet = bullet.instantiate() myBullet.bulletDamageSignal.connect(Gamecontroller.bulletDamage) owner.add_child(myBullet) else: myBullet = bulletArray.pop_back() bulletArray.push_front(myBullet) return myBullet func makeBullet(_bulletPosition, _bulletSpeed)->void: print("make a bullet, put it in the world") var myBullet = bulletFactory() myBullet.transform = _bulletPosition myBullet.setSpeed(_bulletSpeed) ``` Now from the Player, the bullet can be requested. In the `handleInput` method: ``` if Input.is_action_just_pressed("shoot"): print("Ima shoot") match facing: FaceDirection.RIGHT: print("shoot right") %SceneManager.makeBullet(right_spawn.global_transform, 700) FaceDirection.LEFT: print("shoot left") %SceneManager.makeBullet(left_spawn.global_transform, -700) ```
OddlyTimbot added the
enhancement
label 2026-05-04 21:35:33 +00:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: OddlyTimbot/AprilGameExample#4
No description provided.