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)
```
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
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:
In the SceneManager, an array will be used to control and recycle bullets.
var bullet = preload("res://scenes/bullet.tscn")And then:
Create a function that can be called by the player, that requests the SceneManager to create a bullet and put it in the world.
Now from the Player, the bullet can be requested. In the
handleInputmethod: