From 5bd9d2efb3316687f0689c8224e2dbacbc54bc4a Mon Sep 17 00:00:00 2001 From: CactiChameleon9 Date: Sun, 17 Jul 2022 15:53:56 +0100 Subject: [PATCH] Make selecting a dice do something to the CardView --- Scenes/BattleScene.gd | 5 +++ Scenes/BattleScene.tscn | 12 +++---- UI/Card.gd | 29 +++++++++++++++-- UI/CardView.gd | 71 +++++++++++++++++++++++++++++++++++++++++ UI/CardView.tscn | 29 +---------------- UI/Dice.gd | 12 ++++--- UI/Dice.tscn | 4 ++- UI/DiceView.gd | 3 ++ project.godot | 1 + 9 files changed, 122 insertions(+), 44 deletions(-) diff --git a/Scenes/BattleScene.gd b/Scenes/BattleScene.gd index bb32111..88833d5 100644 --- a/Scenes/BattleScene.gd +++ b/Scenes/BattleScene.gd @@ -13,6 +13,10 @@ func _ready(): $UI/CardView.selected = false self.player_to_move = false + $UI/CardView.draw_card() + $UI/CardView.draw_card() + $UI/CardView.draw_card() + $UI/DiceView.roll_dice() $UI/DiceView.roll_dice() $UI/DiceView.roll_dice() @@ -40,6 +44,7 @@ func _physics_process(delta): # select the card chooser if dice is selected if (Input.is_action_just_pressed("ui_accept") and $UI/DiceView.selected == true): + yield(get_tree().create_timer(0.1), "timeout") #TODO BAD WORKAROUND $UI/DiceView.selected = false $UI/CardView.selected = true diff --git a/Scenes/BattleScene.tscn b/Scenes/BattleScene.tscn index bef0014..cc533bc 100644 --- a/Scenes/BattleScene.tscn +++ b/Scenes/BattleScene.tscn @@ -33,14 +33,8 @@ 1/shape_offset = Vector2( 0, 0 ) 1/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 ) 1/shape_one_way = false -1/shape_one_way_margin = 1.0 -1/shapes = [ { -"autotile_coord": Vector2( 0, 0 ), -"one_way": false, -"one_way_margin": 1.0, -"shape": null, -"shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) -} ] +1/shape_one_way_margin = 0.0 +1/shapes = [ ] 1/z_index = 0 [node name="BattleScene" type="Node2D"] @@ -81,3 +75,5 @@ anchor_top = 0.509722 margin_left = 1.52588e-05 margin_top = -7.62939e-06 grow_horizontal = 0 + +[connection signal="dice_selected" from="UI/DiceView" to="UI/CardView" method="set_currently_holding_dice"] diff --git a/UI/Card.gd b/UI/Card.gd index 9835957..bbb367c 100644 --- a/UI/Card.gd +++ b/UI/Card.gd @@ -11,16 +11,38 @@ const TYPE_COLORS = [ Color("#a4a4a4"), # MOVEMENT ] +const dice_node = preload("res://UI/Dice.tscn") export (Resource) var card_info var input_dice = [] var addition_dice_amount : int setget _set_addition_dice +var hovering_dice setget _set_hovering_dice + + +func _set_hovering_dice(dice_value): + if hovering_dice == dice_value: + return + + hovering_dice = dice_value + + #remove the dice preview if not hovering + if dice_value == null: + var input_dice_children = $VBox/AutoGrid.get_node_from_grid("InputDice0").get_children() + if len(input_dice_children) <= 1: + return + var old_dice = input_dice_children[1] + $VBox/AutoGrid.get_node_from_grid("InputDice0").remove_child(old_dice) + return + + var new_dice = dice_node.instance() + new_dice.dice_value = dice_value + $VBox/AutoGrid.get_node_from_grid("InputDice0").add_child(new_dice) func _set_addition_dice(new_amount): addition_dice_amount = new_amount - $VBox/AutoGrid/InputDice0/Number.text = String(new_amount) + $VBox/AutoGrid.get_node_from_grid("InputDice0").get_child("Number").text = String(new_amount) func _ready(): @@ -45,9 +67,12 @@ func _ready(): self.addition_dice_amount = card_info.addition_amount -func dice_inputted(dice_number : int): +func dice_inputted(dice_number): # -- SINGLE DICE CHECKS -- + if dice_number == null: + return + #check if dice is within dice range if dice_number >= 1 and dice_number <= 6: return diff --git a/UI/CardView.gd b/UI/CardView.gd index d3286b9..6e41b99 100644 --- a/UI/CardView.gd +++ b/UI/CardView.gd @@ -1,3 +1,74 @@ extends Control +const card = preload("res://UI/Card.tscn") +const card_db_string = "res://Assets/CardDB/%s.tres" + + var selected : bool = false +var hovering_card = null + +var current_cards = [] + +var currently_holding_dice = null + + +func set_currently_holding_dice(dice_number : int): + currently_holding_dice = dice_number + print(dice_number) + + +func _physics_process(delta): + + # no keyboard input if not selected + if not selected: + hovering_card = null + return + + # if selected card is null, add a value + if not hovering_card: + hovering_card = 0 + + # move the selection forward or backward the list depending on input + if (Input.is_action_just_pressed("ui_down") or + Input.is_action_just_pressed("ui_right")): + + current_cards[hovering_card].hovering_dice = null + + hovering_card += 1 + if hovering_card >= len(current_cards): + hovering_card = 0 + + if (Input.is_action_just_pressed("ui_up") or + Input.is_action_just_pressed("ui_left")): + + current_cards[hovering_card].hovering_dice = null + + hovering_card -= 1 + if hovering_card < 0: + hovering_card = len(current_cards) -1 + + # show the dice over the card if hovering + current_cards[hovering_card].hovering_dice = currently_holding_dice + + #if the enter key is pressed, remove the selected card and emit the signal + if Input.is_action_just_pressed("ui_accept"): + current_cards[hovering_card].dice_inputted(currently_holding_dice) + + +func draw_card(specific_card : String = ""): + # make a new card instance and add it to the grid container + var new_card = card.instance() + $Margin/HBox.add_child(new_card) + + # check if a specific card data exists + var card_data_check = File.new() + var card_data_exists : bool = card_data_check.file_exists(card_db_string % specific_card) + + # if a specifc card choosen, make new card that type + if card_data_exists: + new_card.card_info = load(card_db_string % specific_card) + else: #no card choosen, pick default + new_card.card_info = load(card_db_string % "Default") + + # add the current card to the list of card + current_cards.append(new_card) diff --git a/UI/CardView.tscn b/UI/CardView.tscn index b24b763..91ca274 100644 --- a/UI/CardView.tscn +++ b/UI/CardView.tscn @@ -1,9 +1,5 @@ -[gd_scene load_steps=6 format=2] +[gd_scene load_steps=2 format=2] -[ext_resource path="res://UI/Card.tscn" type="PackedScene" id=1] -[ext_resource path="res://Assets/CardDB/Sprint.tres" type="Resource" id=2] -[ext_resource path="res://Assets/CardDB/Poisonous apple.tres" type="Resource" id=3] -[ext_resource path="res://Assets/CardDB/Broadsword.tres" type="Resource" id=4] [ext_resource path="res://UI/CardView.gd" type="Script" id=5] [node name="CardView" type="Control"] @@ -24,26 +20,3 @@ margin_right = 1220.0 margin_bottom = 660.0 rect_min_size = Vector2( 0, 300 ) custom_constants/separation = 50 - -[node name="Card1" parent="Margin/HBox" instance=ExtResource( 1 )] -anchor_right = 0.0 -anchor_bottom = 0.0 -margin_right = 373.0 -margin_bottom = 660.0 -card_info = ExtResource( 2 ) - -[node name="Card2" parent="Margin/HBox" instance=ExtResource( 1 )] -anchor_right = 0.0 -anchor_bottom = 0.0 -margin_left = 423.0 -margin_right = 796.0 -margin_bottom = 660.0 -card_info = ExtResource( 4 ) - -[node name="Card3" parent="Margin/HBox" instance=ExtResource( 1 )] -anchor_right = 0.0 -anchor_bottom = 0.0 -margin_left = 846.0 -margin_right = 1220.0 -margin_bottom = 660.0 -card_info = ExtResource( 3 ) diff --git a/UI/Dice.gd b/UI/Dice.gd index 9c72a47..5f32cab 100644 --- a/UI/Dice.gd +++ b/UI/Dice.gd @@ -4,7 +4,7 @@ extends Control const dice_image_string = "res://Assets/Dice/Dice%s.png" const selected_shader = preload("res://UI/RainbowOutline.tres") -export (int, 1, 6) var dice_value : int setget _set_dice_value +export (int, 0, 6) var dice_value : int = 0 export var selected : bool setget _set_selected @@ -16,14 +16,16 @@ func _set_selected(new_value): self.material = null -func _set_dice_value(new_value): - dice_value = new_value - self.texture = load(dice_image_string % new_value) +func _physics_process(delta): + self.texture = load(dice_image_string % dice_value) func _ready(): randomize() - self.dice_value = int(round(rand_range(0.5, 6.49999999))) + if dice_value == 0: + self.dice_value = int(round(rand_range(0.5, 6.49999999))) + + _physics_process(0) diff --git a/UI/Dice.tscn b/UI/Dice.tscn index a3550b8..b95c2e2 100644 --- a/UI/Dice.tscn +++ b/UI/Dice.tscn @@ -1,5 +1,6 @@ -[gd_scene load_steps=2 format=2] +[gd_scene load_steps=3 format=2] +[ext_resource path="res://Assets/Dice/Dice6.png" type="Texture" id=1] [ext_resource path="res://UI/Dice.gd" type="Script" id=2] [node name="Dice" type="TextureRect"] @@ -7,6 +8,7 @@ margin_right = 100.0 margin_bottom = 100.0 size_flags_horizontal = 3 size_flags_vertical = 3 +texture = ExtResource( 1 ) expand = true stretch_mode = 6 script = ExtResource( 2 ) diff --git a/UI/DiceView.gd b/UI/DiceView.gd index ab5f736..895d985 100644 --- a/UI/DiceView.gd +++ b/UI/DiceView.gd @@ -47,10 +47,13 @@ func _physics_process(delta): #if the enter key is pressed, remove the selected dice and emit the signal if Input.is_action_just_pressed("ui_accept"): emit_signal("dice_selected", current_dice[selected_dice].dice_value) + print(current_dice[selected_dice].dice_value) + print("current_dice[selected_dice].dice_value") current_dice[selected_dice].queue_free() current_dice.remove(selected_dice) selected_dice = null + selected = false func roll_dice(specific_value : int = 0): diff --git a/project.godot b/project.godot index c74e53e..e289f34 100644 --- a/project.godot +++ b/project.godot @@ -21,6 +21,7 @@ _global_script_class_icons={ [application] config/name="Quit Rolling Around" +run/main_scene="res://Scenes/BattleScene.tscn" config/icon="res://icon.png" [display]