channel-switcher/games/crafting/crafting.gd

82 lines
2.2 KiB
GDScript3
Raw Permalink Normal View History

2025-05-12 17:10:37 +10:00
extends Node
2025-05-16 22:13:34 +10:00
signal game_win
signal game_lose
2025-05-12 17:10:37 +10:00
@export var keys : Array[String]
@export var time_limit : float = 20
@export var pixel_count : int = 16
2025-05-12 17:10:37 +10:00
@export_group("Node References")
@export var pixel_grid : GridContainer
2025-05-17 14:21:38 +10:00
@export var timer_progress : TextureProgressBar
2025-05-12 17:10:37 +10:00
var pixels : Array[Control]
var current_time : float = 25
2025-05-12 17:10:37 +10:00
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
create_pixels()
current_time = time_limit
pass # Replace with function body.
func create_pixels():
for i in range(pixel_count):
2025-05-12 17:10:37 +10:00
var newPixel = load("res://games/crafting/pixel.tscn").instantiate()
var randomKey = randi_range(0,keys.size()-1)
newPixel.key = keys[randomKey]
pixels.append(newPixel)
pixel_grid.add_child(newPixel)
func enter_letter(letter : String):
var found : bool = false
var options : Array[Control]
for pixel in pixels:
if (pixel.key == letter and !pixel.activated):
found = true
options.append(pixel)
if(found):
var randomPixel : int = randi_range(0, options.size()-1)
options[randomPixel].activate()
else:
for i in range(3):
var randomPixel : int = randi_range(0,pixels.size()-1)
pixels[randomPixel].deactivate()
if count_activated():
game_win.emit()
2025-05-12 17:10:37 +10:00
func count_activated() -> bool:
var count : int = 0
for pixel in pixels:
if pixel.activated:
count += 1
if count == pixel_count:
2025-05-12 17:10:37 +10:00
return true
else:
return false
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
current_time -= delta
timer_progress.value = current_time/time_limit
if current_time <= 0:
game_lose.emit()
2025-05-12 17:10:37 +10:00
#loser
if Input.is_action_just_pressed("crafting_e"):
enter_letter("E")
elif Input.is_action_just_pressed("crafting_q"):
enter_letter("Q")
elif Input.is_action_just_pressed("crafting_r"):
enter_letter("R")
elif Input.is_action_just_pressed("crafting_t"):
enter_letter("T")
elif Input.is_action_just_pressed("crafting_y"):
enter_letter("Y")
elif Input.is_action_just_pressed("crafting_u"):
enter_letter("U")
elif Input.is_action_just_pressed("crafting_f"):
enter_letter("F")
elif Input.is_action_just_pressed("crafting_m"):
enter_letter("M")