From 2f646169fb17938f2586df121308092a63796418 Mon Sep 17 00:00:00 2001 From: Taevas Date: Sun, 4 May 2025 00:36:12 +0200 Subject: [PATCH] initial ring map support (#2) --- elements/player.tscn | 2 ++ gui/map.tscn | 66 ++++++++++++++++++++++++++++++++++++++++++++ index.tscn | 14 +++++++++- levels/level.gd | 3 ++ project.godot | 7 +++++ 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 gui/map.tscn diff --git a/elements/player.tscn b/elements/player.tscn index c377c79..74c84c6 100644 --- a/elements/player.tscn +++ b/elements/player.tscn @@ -11,8 +11,10 @@ var velocity := 0.0: if value != velocity: velocity_change.emit(value) velocity = value +@onready var ball_position: Vector3 = $Sphere.position func _physics_process(_delta: float) -> void: + ball_position = $Sphere.position $SpotLightMain.position = $Sphere.position + Vector3(0, 5, 0) $SpotLightTop.position = $Sphere.position $SpotLightLeft.position = $Sphere.position diff --git a/gui/map.tscn b/gui/map.tscn new file mode 100644 index 0000000..28a0ad2 --- /dev/null +++ b/gui/map.tscn @@ -0,0 +1,66 @@ +[gd_scene load_steps=2 format=3 uid="uid://bcxbw6wd54ksv"] + +[sub_resource type="GDScript" id="GDScript_r4thc"] +script/source = "extends Control + +func _ready() -> void: + $TemplateRing.hide() + +func display_rings(player: Node3D, rings: Array[Ring]) -> void: + var old_rings_display := $Rings.get_children() + var difference := len(rings) - len(old_rings_display) + + # Too many displayed, free them + for i in -difference: + old_rings_display[-(1 + i)].queue_free() + # Not enough displayed, create some + for i in difference: + var new_child := $TemplateRing.duplicate() + new_child.name = \"Ring\" + str(i + 1) + new_child.show() + $Rings.add_child(new_child) + + var rings_display := $Rings.get_children() + var center: Vector2i = get_viewport().size / 2 + for i in len(rings): + rings_display[i].color = rings[i].material.albedo_color + rings_display[i].rotation = rings[i].rotation.z + rings_display[i].position.x = center.x + rings[i].position.x - player.ball_position.x + rings_display[i].position.y = center.y - rings[i].position.y + player.ball_position.y + + if rings[i].collected: + rings_display[i].scale = $TemplateRing.scale * 2 * min(4, max(1, rings[i].scale.x)) + else: + rings_display[i].scale = $TemplateRing.scale * 3 * min(4, max(1, rings[i].scale.x)) +" + +[node name="Map" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +script = SubResource("GDScript_r4thc") + +[node name="ColorRect" type="ColorRect" parent="."] +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +color = Color(1, 0, 1, 0.0392157) + +[node name="Rings" type="Control" parent="."] +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="TemplateRing" type="ColorRect" parent="."] +layout_mode = 0 +offset_right = 2.0 +offset_bottom = 2.0 diff --git a/index.tscn b/index.tscn index 6c6a276..c17011e 100644 --- a/index.tscn +++ b/index.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=7 format=3 uid="uid://ccgnnif026wb4"] +[gd_scene load_steps=8 format=3 uid="uid://ccgnnif026wb4"] [ext_resource type="PackedScene" uid="uid://xd3nsiglcdfc" path="res://gui/timer.tscn" id="1_356j3"] [ext_resource type="PackedScene" uid="uid://dw0xl8644x166" path="res://gui/gravity.tscn" id="1_ir7so"] [ext_resource type="PackedScene" uid="uid://cckeamgkt8bqo" path="res://gui/speed.tscn" id="2_2gn6w"] +[ext_resource type="PackedScene" uid="uid://bcxbw6wd54ksv" path="res://gui/map.tscn" id="2_d1yoc"] [ext_resource type="PackedScene" uid="uid://cn55m5dqo3m6u" path="res://gui/rings.tscn" id="3_mbj17"] [ext_resource type="PackedScene" uid="uid://dkxtwpcy4moyo" path="res://menus/pause_menu.tscn" id="4_3bfj3"] @@ -19,6 +20,7 @@ var current_level_scene: PackedScene const res_main_menu = preload(\"res://menus/main/main_menu.tscn\") @onready var gui_gravity := $GUI/Gravity +@onready var gui_map := $GUI/Map @onready var gui_timer := $GUI/TopLeft/Timer @onready var gui_speed := $GUI/TopRight/Speed @onready var gui_rings := $GUI/BottomRight/Rings @@ -130,8 +132,14 @@ func fullscreen_game() -> void: func _process(_delta: float) -> void: if is_instance_valid(current_level): gui_speed.ball_velocity = current_level.velocity + if Input.is_action_pressed(\"display_map\"): + gui_map.display_rings(current_level.player, current_level.rings) func _input(_event: InputEvent) -> void: + if Input.is_action_just_released(\"display_map\"): + gui_map.hide() + if Input.is_action_just_pressed(\"display_map\"): + gui_map.show() if Input.is_action_just_pressed(\"restart_level\"): restart_level() @@ -154,6 +162,10 @@ mouse_filter = 2 [node name="Gravity" parent="GUI" instance=ExtResource("1_ir7so")] layout_mode = 1 +[node name="Map" parent="GUI" instance=ExtResource("2_d1yoc")] +visible = false +layout_mode = 1 + [node name="TopLeft" type="MarginContainer" parent="GUI"] layout_mode = 1 offset_right = 160.0 diff --git a/levels/level.gd b/levels/level.gd index b823630..ede459a 100644 --- a/levels/level.gd +++ b/levels/level.gd @@ -12,6 +12,7 @@ const gravity_strength_strong = gravity_strength_normal * 3 ## Lowercase, spaceless name of the level var id: String var thumbnail: CompressedTexture2D +var player: Node3D var music: Music var rings: Array[Ring] = [] @@ -39,6 +40,8 @@ func _ready() -> void: for child in children: if is_instance_of(child, Music): music = child + if child.name == "Player" and is_instance_of(child, Node3D): + player = child assert(is_instance_valid(music), self.name + " has no music!") var rings_node = get_node("Rings") diff --git a/project.godot b/project.godot index 011b267..3a00814 100644 --- a/project.godot +++ b/project.godot @@ -78,6 +78,13 @@ restart_level={ , Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":2,"pressure":0.0,"pressed":false,"script":null) ] } +display_map={ +"deadzone": 0.2, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194326,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":9,"pressure":0.0,"pressed":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":10,"pressure":0.0,"pressed":false,"script":null) +] +} [physics]