working bullet factory
This commit is contained in:
1 parent
7bac7283dd
commit
29acef878e
9 files changed
+109
-5
No files matched your search
@@ -0,0 +1,25 @@
|
||||
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 hit sumtin")
|
||||
if not body.is_in_group("player"):
|
||||
hit.emit(body)
|
||||
@@ -14,6 +14,11 @@ 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 +34,18 @@ 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"):
|
||||
# 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")
|
||||
|
||||
@@ -32,4 +32,9 @@ 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,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