working bullet factory
This commit is contained in:
1 parent
6f55d9442c
commit
8adfc2b674
9 files changed
+107
-3
No files matched your search
@@ -0,0 +1,23 @@
|
||||
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("hit")
|
||||
if not body.is_in_group("player"):
|
||||
hit.emit(body)
|
||||
@@ -27,3 +27,8 @@ func _on_trigger_alert(body):
|
||||
if totalBoxes <=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()
|
||||
@@ -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
|
||||
var bullet = preload("res://scenes/bullet.tscn")
|
||||
|
||||
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
|
||||
|
||||
|
||||
@@ -41,6 +47,18 @@ func _physics_process(delta):
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
|
||||
# shoot
|
||||
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
# make bullet
|
||||
var myBullet = %SceneManager.bulletFactory()
|
||||
owner.add_child(myBullet)
|
||||
myBullet.transform = right_target.global_transform
|
||||
if faceleft:
|
||||
myBullet.transform = left_target.global_transform
|
||||
myBullet.setSpeed(-700)
|
||||
|
||||
move_and_slide()
|
||||
if right_ray.is_colliding():
|
||||
if not faceleft:
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
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)
|
||||
Reference in new issue
Block a user