Samurai-Sculpt-Plus/logic.gd

142 lines
3.6 KiB
GDScript3
Raw Normal View History

2025-06-09 02:17:00 +10:00
extends Node2D
@export var code_len : int = 2
@export var ghost_tokens : int = 2
@export var allowed_time : float = 5
@export var starting_lives : int = 3
@export var icon_container: HBoxContainer
@export var icons : Array[Texture]
2025-06-09 14:47:46 +10:00
@export var progress_bar : TextureProgressBar
2025-06-09 02:17:00 +10:00
@export var scroll_container : ScrollContainer
2025-06-09 14:43:22 +10:00
@export var score_label : Label
@export var life_container : HBoxContainer
@export var life_texture : Texture
2025-06-09 15:30:49 +10:00
@export var round_label : Label
2025-06-09 02:17:00 +10:00
var current_time : float = 5
var score : int = 0
var lives : int = 3
var code : Array[int]
var code_rects : Array[TextureRect]
var gaming : bool = false
var codepos : int = 0 #current position in the code to check against
var test_break : float = 1.5
var target_scroll : float
# start scrolling at codepos == 11
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
#start_round()
2025-06-09 14:43:22 +10:00
draw_lives()
2025-06-09 02:17:00 +10:00
pass # Replace with function body.
2025-06-09 14:43:22 +10:00
func draw_lives():
for child in life_container.get_children():
child.queue_free()
for i in range(lives):
var new_tex = TextureRect.new()
new_tex.texture = life_texture
life_container.add_child(new_tex)
func lose_life():
lives -= 1
draw_lives()
if lives <= 0:
#end game
pass
2025-06-09 02:17:00 +10:00
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
target_scroll = (codepos*50) - 550
scroll_container.scroll_horizontal = lerpf(scroll_container.scroll_horizontal,target_scroll,0.05)
2025-06-09 14:43:22 +10:00
score_label.text = str(score)
2025-06-09 15:30:49 +10:00
round_label.text = str(code_len) + " of " + str(code_len+ghost_tokens)
if not gaming:
round_label.text = "Get Ready!"
2025-06-09 02:17:00 +10:00
if gaming:
current_time -= delta
progress_bar.value = (5-current_time)/allowed_time
2025-06-09 15:30:49 +10:00
2025-06-09 02:17:00 +10:00
else:
test_break -= delta
2025-06-09 15:30:49 +10:00
progress_bar.value -= delta/1.5
2025-06-09 02:17:00 +10:00
if test_break <= 0:
start_round()
2025-06-09 14:43:22 +10:00
2025-06-09 15:30:49 +10:00
progress_bar.modulate = Color(1,1,1,progress_bar.value)
2025-06-09 14:43:22 +10:00
if gaming and current_time <= 0:
lose_round()
2025-06-09 02:17:00 +10:00
if Input.is_action_just_pressed("up"):
process_input(0)
elif Input.is_action_just_pressed("right"):
process_input(1)
elif Input.is_action_just_pressed("down"):
process_input(2)
elif Input.is_action_just_pressed("left"):
process_input(3)
func start_round():
current_time = allowed_time
codepos = 0
generate_code()
gaming = true
#scroll_container.scroll_horizontal = 0
pass
func process_input(direction : int):
if(code[codepos] == direction):
#correct input
icon_container.get_child(codepos).modulate = Color(0,1,0)
2025-06-09 14:43:22 +10:00
score += codepos+1
2025-06-09 02:17:00 +10:00
codepos += 1
2025-06-09 14:43:22 +10:00
2025-06-09 02:17:00 +10:00
if(codepos == code_len):
win_round()
else:
#incorrect input
codepos = 0
#scroll_container.scroll_horizontal = 0
for i in range(code_len):
icon_container.get_child(i).modulate = Color(1,1,1)
func win_round():
2025-06-09 14:43:22 +10:00
#todo: bous for remaining time?
2025-06-09 02:17:00 +10:00
gaming = false
codepos = 0
test_break = 1.5 # todo: replace with gamer samurai animation stuff
if ghost_tokens > 0:
ghost_tokens -= 1
code_len += 1
2025-06-09 14:43:22 +10:00
2025-06-09 02:17:00 +10:00
else:
ghost_tokens = 2
code_len -= 1
2025-06-09 14:43:22 +10:00
score += 100
func lose_round():
gaming = false
codepos = 0
test_break = 1.5
lose_life()
2025-06-09 02:17:00 +10:00
func generate_code():
code.clear()
for child in icon_container.get_children():
child.queue_free()
for i in range(code_len):
var ran_dir : int = randi_range(0,3)
code.append(ran_dir)
var new_texture_rect : TextureRect = TextureRect.new()
new_texture_rect.texture = icons[ran_dir]
icon_container.add_child(new_texture_rect)
for i in range(ghost_tokens):
var new_texture_rect : TextureRect = TextureRect.new()
new_texture_rect.texture = icons[4]
new_texture_rect.modulate = Color(1,1,1,0.25)
icon_container.add_child(new_texture_rect)
print(code)