ld56/game.gd

127 lines
3.0 KiB
GDScript3
Raw Normal View History

extends Node2D
const MAX_HEALTH = 100.0
const RECOVERY_RATE = 10.0
const BAD_PRESS_DAMAGE = 20.0
2024-10-06 17:38:16 +00:00
const MAX_ENEMIES = 4
2024-10-06 22:33:18 +00:00
@export var INIT_COLOR:Color = Color(1,0,0,0)
var health = MAX_HEALTH
2024-10-06 22:33:18 +00:00
var spawn_interval = 1.4
var interval_step = 0.05
var max_out = 0.7
var player_score = 0
var first_time = true
2024-10-06 17:38:16 +00:00
var game_active = false
2024-10-06 22:33:18 +00:00
var needs_restart = false
2024-10-05 20:09:04 +00:00
func _unhandled_input(event: InputEvent) -> void:
2024-10-06 22:33:18 +00:00
if needs_restart && !game_active:
if event.is_action_pressed("A"):
start_game()
2024-10-06 17:38:16 +00:00
2024-10-05 20:09:04 +00:00
func _ready() -> void:
2024-10-06 22:33:18 +00:00
%Dialog.start_dialog_1()
func start_game():
2024-10-06 22:33:18 +00:00
$SpawnTimer.paused = false
$SpawnTimer.start()
2024-10-06 22:33:18 +00:00
health = MAX_HEALTH
player_score = 0
draw_score()
%Dialog.visible = false
2024-10-05 20:09:04 +00:00
change_red_button()
first_time = false
$SpawnTimer.wait_time = spawn_interval
for button in get_tree().get_nodes_in_group("button_group"):
button.enemy_killed.connect(on_enemy_killed)
2024-10-06 22:33:18 +00:00
game_active = true
call_deferred("set_game_active",)
$Intro4Bar.stop()
$"8bart1".play()
2024-10-05 20:09:04 +00:00
2024-10-06 22:33:18 +00:00
func set_game_active():
game_active = true
func game_over():
game_active = false
$SpawnTimer.paused = true
%Dialog/Text.text = "GAME OVER, TRY AGAIN ?"
%Dialog.visible = true
$"8bart1".stop()
$Intro4Bar.play()
needs_restart = true
func _process(delta: float) -> void:
health = minf(health + RECOVERY_RATE * delta ,MAX_HEALTH)
if(spawn_interval > max_out):
spawn_interval -= interval_step*delta
var new_color = Color(INIT_COLOR)
new_color.a = 1 - health / MAX_HEALTH
%DamageColorOverlay.color = new_color
2024-10-05 20:09:04 +00:00
if health < 5:
2024-10-06 22:33:18 +00:00
game_over()
2024-10-05 20:09:04 +00:00
func bad_press():
health = maxf(health - BAD_PRESS_DAMAGE, 0)
spawn_interval -= interval_step
2024-10-06 17:38:16 +00:00
$Camera2D.apply_shake()
func ate():
health = maxf(health - BAD_PRESS_DAMAGE, 0)
spawn_interval += interval_step
2024-10-06 17:38:16 +00:00
$Camera2D.apply_shake()
2024-10-05 20:09:04 +00:00
func red_button_press():
health = 0
func change_red_button():
var buttons = get_tree().get_nodes_in_group("button_group")
var old_red_button_index = -1
2024-10-05 20:09:04 +00:00
if buttons.filter(func (n: Node): return n.red_button).size() > 0:
var old_red_button = buttons.filter(func (n: Node): return n.red_button)[0]
old_red_button_index = buttons.find(old_red_button)
2024-10-05 20:09:04 +00:00
var r = randi_range(0, buttons.size()-1)
while (r == old_red_button_index || buttons[r].get_has_enemy()):
2024-10-05 20:09:04 +00:00
r= randi_range(0, buttons.size()-1)
if(!first_time):
buttons[old_red_button_index].red_out()
buttons[r].red_in()
else:
buttons[r].set_red_button(true)
2024-10-05 20:09:04 +00:00
func _on_spawn_timer_timeout() -> void:
$SpawnTimer.wait_time = spawn_interval
2024-10-05 20:09:04 +00:00
var buttons = get_tree().get_nodes_in_group("button_group")
var enemies_to_spawn = randi_range(1, MAX_ENEMIES)
while (enemies_to_spawn > 0):
var button = buttons[randi_range(0, buttons.size()-1)]
if button.has_method("get_has_enemy"):
if button.get_has_enemy():
enemies_to_spawn -=1
continue
button.spawn_enemy()
enemies_to_spawn -=1
return
func on_enemy_killed():
player_score+=1
2024-10-06 22:33:18 +00:00
draw_score()
2024-10-06 22:33:18 +00:00
func draw_score():
$CanvasLayer/Label.text = str(player_score)
func _on_dialog_dialogue_over() -> void:
start_game()
2024-10-06 22:33:18 +00:00
func _on_bart_1_finished() -> void:
$"8bart1".stop()
$Intense.play()