add theme, player simple attack
This commit is contained in:
39
scripts/components/brick_throw.gd
Normal file
39
scripts/components/brick_throw.gd
Normal file
@@ -0,0 +1,39 @@
|
||||
class_name BrickThrowComponent
|
||||
extends Node
|
||||
|
||||
@export var brick_scene: PackedScene
|
||||
@export var fire_rate: float = 1.0
|
||||
@export var player_controller: PlayerController
|
||||
@export var timer: Timer
|
||||
|
||||
var can_throw: bool = true
|
||||
|
||||
func _ready() -> void:
|
||||
setup_timer()
|
||||
can_throw = true
|
||||
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
if event.is_action_pressed("attack") and can_throw:
|
||||
throw_brick()
|
||||
|
||||
func setup_timer() -> void:
|
||||
timer.wait_time = fire_rate
|
||||
timer.one_shot = false
|
||||
timer.autostart = false
|
||||
timer.timeout.connect(on_timer_timeout)
|
||||
|
||||
|
||||
func on_timer_timeout() -> void:
|
||||
can_throw = true
|
||||
|
||||
|
||||
func throw_brick() -> void:
|
||||
var brick_instance: Node2D = brick_scene.instantiate()
|
||||
var brick: BulletComponent = brick_instance.get_node("BulletComponent")
|
||||
brick_instance.position = player_controller.position
|
||||
brick.direction = Vector2.RIGHT if player_controller.velocity.x >= 0.0 else Vector2.LEFT
|
||||
get_tree().current_scene.add_child(brick_instance)
|
||||
|
||||
can_throw = false
|
||||
timer.start()
|
@@ -1,7 +1,6 @@
|
||||
class_name BulletComponent
|
||||
extends Node
|
||||
|
||||
|
||||
@export var root: Node2D
|
||||
@export var direction: Vector2 = Vector2.RIGHT
|
||||
@export var speed: float = 10.0
|
||||
@@ -10,23 +9,27 @@ extends Node
|
||||
@export var life_time: float = 5.0
|
||||
@export var timer: Timer
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
root = get_parent()
|
||||
visibility_notifier.screen_exited.connect(_on_screen_exited)
|
||||
area2d.body_entered.connect(on_area2d_body_entered)
|
||||
|
||||
|
||||
timer.wait_time = life_time
|
||||
timer.timeout.connect(on_timer_timeout)
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
root.position += direction * speed * delta
|
||||
|
||||
|
||||
|
||||
func _on_screen_exited() -> void:
|
||||
root.queue_free()
|
||||
|
||||
|
||||
|
||||
func on_area2d_body_entered(_body: Node2D) -> void:
|
||||
root.queue_free()
|
||||
|
||||
|
||||
|
||||
|
||||
func on_timer_timeout() -> void:
|
||||
root.queue_free()
|
Reference in New Issue
Block a user