Adds red button support

This commit is contained in:
txrpe 2024-10-05 22:09:04 +02:00
parent 1a4878f32e
commit 9cccf190ad
4 changed files with 121 additions and 21 deletions

View File

@ -1,14 +1,20 @@
extends StaticBody2D
extends Node2D
@export var input_name = ""
@export var color_mix:Color = Color(1,1,1)
const ENEMY = preload("res://enemy.tscn")
const DISTANCE = 40
var red_button = false
var should_move = false
func _unhandled_input(event: InputEvent) -> void:
if(event.is_action_pressed(input_name)):
print_debug(input_name)
$AnimatedSprite2D.play("pressed")
var children = get_children(false)
var was_valid_input = false
if(red_button):
get_parent().red_button_press()
for a in children:
if(a.has_method("button_pressed")):
a.button_pressed()
@ -17,14 +23,23 @@ func _unhandled_input(event: InputEvent) -> void:
if get_parent().has_method("bad_press"):
get_parent().bad_press()
func get_has_enemy():
return get_children().any(func (a:Node):return a.has_method("button_pressed"))
func _on_spawn_tick_timeout() -> void:
spawn_enemy()
func set_red_button(is_red: bool):
red_button = is_red
if is_red:
$AnimatedSprite2D.play("red")
else:
$AnimatedSprite2D.play("default")
#func _process(delta: float) -> void:
#$AnimatedSprite2D.modulate = color_mix
func spawn_enemy():
if get_children().any(func (a:Node):return a.has_method("button_pressed")):
if get_has_enemy():
return
elif(randi_range(0,1)==1):
if(randi_range(0,1)==1):
var enemy = ENEMY.instantiate()
print_debug(randf_range(0.0, 2.0) * PI)
add_child(enemy)
@ -35,6 +50,15 @@ func spawn_enemy():
func enemy_attacked():
#TODO: check for collisions
if red_button:
get_parent().change_red_button()
return
move_button()
func move_button():
should_move = true
global_position = get_random_position()
@ -47,3 +71,16 @@ func get_random_position():
var x = randi_range(area2d.global_position.x + area2d.shape.get_rect().position.x, area2d.global_position.x + area2d.shape.get_rect().end.x)
var y = randi_range(area2d.global_position.y + area2d.shape.get_rect().position.y, area2d.global_position.y + area2d.shape.get_rect().end.y)
return Vector2(x,y)
var should_really_move = false
func _on_area_2d_area_entered(area: Area2D) -> void:
if should_really_move:
move_button()
if should_move:
move_button()
func _on_area_2d_area_exited(area: Area2D) -> void:
if should_move:
should_really_move = true
should_move = false

View File

@ -1,10 +1,11 @@
[gd_scene load_steps=8 format=3 uid="uid://bhvihrt8dipll"]
[gd_scene load_steps=9 format=3 uid="uid://bhvihrt8dipll"]
[ext_resource type="Script" path="res://button.gd" id="1_fkyus"]
[ext_resource type="Texture2D" uid="uid://q85jhjrqsrxk" path="res://Buttons/Idle.png" id="2_01nlj"]
[ext_resource type="Texture2D" uid="uid://blatwufdrwqml" path="res://Buttons/PressedIn1.png" id="3_180hi"]
[ext_resource type="Texture2D" uid="uid://4c7qtuk0exng" path="res://Buttons/FullPressed.png" id="4_5m7c4"]
[ext_resource type="Texture2D" uid="uid://ncd65m8iq7l0" path="res://Buttons/PressedOut.png" id="5_3xqo8"]
[ext_resource type="Texture2D" uid="uid://chckt1ppwsjq" path="res://Buttons/FORBIDDEN.png" id="6_p5821"]
[sub_resource type="SpriteFrames" id="SpriteFrames_leuex"]
animations = [{
@ -32,12 +33,20 @@ animations = [{
"loop": false,
"name": &"pressed",
"speed": 15.0
}, {
"frames": [{
"duration": 1.0,
"texture": ExtResource("6_p5821")
}],
"loop": true,
"name": &"red",
"speed": 5.0
}]
[sub_resource type="CircleShape2D" id="CircleShape2D_gc3ws"]
radius = 30.0666
[sub_resource type="CircleShape2D" id="CircleShape2D_l0l4p"]
radius = 10.4089
[node name="Button" type="StaticBody2D"]
[node name="Button" type="Node2D"]
script = ExtResource("1_fkyus")
metadata/_edit_group_ = true
@ -46,12 +55,14 @@ scale = Vector2(1.38, 1.316)
sprite_frames = SubResource("SpriteFrames_leuex")
animation = &"pressed"
[node name="SpawnTick" type="Timer" parent="."]
wait_time = 0.439
autostart = true
[node name="Area2D" type="Area2D" parent="."]
scale = Vector2(1.21306, 1.21306)
collision_layer = 2
collision_mask = 2
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
scale = Vector2(1.56, 1.56)
shape = SubResource("CircleShape2D_gc3ws")
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
scale = Vector2(4.74125, 4.84)
shape = SubResource("CircleShape2D_l0l4p")
[connection signal="timeout" from="SpawnTick" to="." method="_on_spawn_tick_timeout"]
[connection signal="area_entered" from="Area2D" to="." method="_on_area_2d_area_entered"]
[connection signal="area_exited" from="Area2D" to="." method="_on_area_2d_area_exited"]

44
game.gd
View File

@ -3,19 +3,59 @@ extends Node2D
const MAX_HEALTH = 100.0
const RECOVERY_RATE = 10.0
const BAD_PRESS_DAMAGE = 20.0
const MAX_ENEMIES = 3
const INIT_COLOR = Color(1,0,0,0)
var health = MAX_HEALTH
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("ui_accept"):
$SpawnTimer.paused = false
func _ready() -> void:
change_red_button()
func _process(delta: float) -> void:
health = minf(health + RECOVERY_RATE * delta ,MAX_HEALTH)
var new_color = Color(INIT_COLOR)
new_color.a = 1 - health / MAX_HEALTH
%DamageColorOverlay.color = new_color
if health < 5:
$SpawnTimer.paused =true
func bad_press():
health = maxf(health - BAD_PRESS_DAMAGE, 0)
if(health == 0):
print_debug("GAME OVER")
func red_button_press():
health = 0
func change_red_button():
var buttons = get_tree().get_nodes_in_group("button_group")
var index = -1
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.set_red_button(false)
index = buttons.find(old_red_button)
var r = randi_range(0, buttons.size()-1)
while (r == index):
r= randi_range(0, buttons.size()-1)
buttons[r].set_red_button(true)
func _on_spawn_timer_timeout() -> void:
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

View File

@ -4,9 +4,10 @@
[ext_resource type="PackedScene" uid="uid://bhvihrt8dipll" path="res://button.tscn" id="1_j4pve"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_sesmb"]
size = Vector2(997, 471)
size = Vector2(469, 405.155)
[node name="Game" type="Node2D"]
process_mode = 3
script = ExtResource("1_bhcvq")
[node name="CanvasLayer" type="CanvasLayer" parent="."]
@ -28,23 +29,34 @@ color = Color(0.623529, 0.14902, 0.184314, 0.592157)
[node name="A" parent="." groups=["button_group"] instance=ExtResource("1_j4pve")]
position = Vector2(872, 515)
input_name = "A"
color_mix = Color(1.01075e-06, 0.583986, 0.337423, 1)
[node name="Y" parent="." groups=["button_group"] instance=ExtResource("1_j4pve")]
position = Vector2(872, 330)
input_name = "Y"
color_mix = Color(0.917, 1, 0.17, 1)
[node name="X" parent="." groups=["button_group"] instance=ExtResource("1_j4pve")]
position = Vector2(777, 422)
input_name = "X"
color_mix = Color(0, 0.424825, 0.812361, 1)
[node name="B" parent="." instance=ExtResource("1_j4pve")]
[node name="B" parent="." groups=["button_group"] instance=ExtResource("1_j4pve")]
position = Vector2(954, 422)
input_name = "B"
color_mix = Color(0.735073, 0.468616, 3.85046e-07, 1)
[node name="SpawnArea" type="Area2D" parent="."]
scale = Vector2(1, 1.02189)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="SpawnArea"]
position = Vector2(568.5, 359.5)
position = Vector2(832.5, 392.423)
shape = SubResource("RectangleShape2D_sesmb")
debug_color = Color(0, 0.6, 0.701961, 0.14902)
[node name="SpawnTimer" type="Timer" parent="."]
wait_time = 0.5
autostart = true
[connection signal="timeout" from="SpawnTimer" to="." method="_on_spawn_timer_timeout"]