Redo basic DiceController with new generic ItemController

This commit is contained in:
CactiChameleon9 2022-08-17 21:01:52 +01:00
parent ea2dde50e3
commit a2185e081c
6 changed files with 53 additions and 130 deletions

View File

@ -12,6 +12,8 @@ onready var target_position : Vector2 = position
var moving : bool = false var moving : bool = false
var cards = [] setget ,get_cards var cards = [] setget ,get_cards
var dice = []
func get_cards(): func get_cards():
for child in get_children(): for child in get_children():

View File

@ -1,31 +0,0 @@
tool
extends Control
const dice_image_string = "res://Assets/Dice/Dice%s.png"
const selected_shader = preload("res://UI/RainbowOutline.tres")
export (int, 0, 6) var dice_value : int = 0
export var selected : bool setget _set_selected
func _set_selected(new_value):
selected = new_value
if selected:
self.material = selected_shader
else:
self.material = null
func _physics_process(delta):
self.texture = load(dice_image_string % dice_value)
func _ready():
randomize()
if dice_value == 0:
self.dice_value = int(round(rand_range(0.5, 6.49999999)))
_physics_process(0)

View File

@ -1,14 +0,0 @@
[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"]
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 )

26
UI/DiceContainer.tscn Normal file
View File

@ -0,0 +1,26 @@
[gd_scene load_steps=4 format=2]
[ext_resource path="res://AutoGridContainer - Full Version/AutoGridContainer.tscn" type="PackedScene" id=1]
[ext_resource path="res://UI/ItemContainer.gd" type="Script" id=2]
[ext_resource path="res://UI/DiceView.tscn" type="PackedScene" id=3]
[node name="DiceContainer" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
script = ExtResource( 2 )
item_view_scene = ExtResource( 3 )
child_holder = NodePath("Margin/AutoGrid")
character_property = "dice"
view_property = "dice_value"
[node name="Margin" type="MarginContainer" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 30.0
margin_top = 30.0
margin_right = -30.0
margin_bottom = -30.0
[node name="AutoGrid" parent="Margin" instance=ExtResource( 1 )]
margin_right = 1220.0
margin_bottom = 660.0

View File

@ -1,76 +1,22 @@
tool
extends Control extends Control
signal dice_selected (dice_value) const dice_image_string = "res://Assets/Dice/Dice%s.png"
signal scene_finished const selected_shader = preload("res://UI/RainbowOutline.tres")
signal scene_failed
var active : bool = false export (int, 0, 6) var dice_value : int = 0
export var selected : bool setget set_selected
const dice = preload("res://UI/Dice.tscn")
var selected : bool = false
var selected_dice = null
var current_dice = []
func _physics_process(delta): func set_selected(new_value):
selected = new_value
# no keyboard input if not selected if selected:
if not selected: self.material = selected_shader
selected_dice = null else:
return self.material = null
# skip turn if the list is empty
if len(current_dice) == 0: func _physics_process(_delta):
selected = false self.texture = load(dice_image_string % dice_value)
return
# if selected dice is null, add a value
if not selected_dice:
selected_dice = 0
# TODO: maybe support actual dicrectional selection
# move the selection forward or backward the list depending on input
if (Input.is_action_just_pressed("ui_up") or
Input.is_action_just_pressed("ui_left")):
current_dice[selected_dice].selected = false
selected_dice += 1
if selected_dice >= len(current_dice):
selected_dice = 0
if (Input.is_action_just_pressed("ui_down") or
Input.is_action_just_pressed("ui_right")):
current_dice[selected_dice].selected = false
selected_dice -= 1
if selected_dice < 0:
selected_dice = len(current_dice) -1
# enable the selected shader
current_dice[selected_dice].selected = true
#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)
current_dice[selected_dice].queue_free()
current_dice.remove(selected_dice)
selected_dice = null
selected = false
func roll_dice(specific_value : int = 0):
# make a new dice instance and add it to the grid container
var new_dice = dice.instance()
$Margin/AutoGrid.add_child(new_dice)
# if a specifc dice choosen, make new dice that type
if specific_value in [1, 2, 3, 4, 5, 6]:
new_dice.dice_value = specific_value
# add the current dice to the list of dice
current_dice.append(new_dice)

View File

@ -1,21 +1,15 @@
[gd_scene load_steps=3 format=2] [gd_scene load_steps=3 format=2]
[ext_resource path="res://AutoGridContainer - Full Version/AutoGridContainer.tscn" type="PackedScene" id=1] [ext_resource path="res://Assets/Dice/Dice6.png" type="Texture" id=1]
[ext_resource path="res://UI/DiceView.gd" type="Script" id=2] [ext_resource path="res://UI/DiceView.gd" type="Script" id=2]
[node name="DiceView" type="Control"] [node name="DiceView" type="TextureRect"]
anchor_right = 1.0 margin_right = 100.0
anchor_bottom = 1.0 margin_bottom = 100.0
size_flags_horizontal = 3
size_flags_vertical = 3
texture = ExtResource( 1 )
expand = true
stretch_mode = 6
script = ExtResource( 2 ) script = ExtResource( 2 )
dice_value = 6
[node name="Margin" type="MarginContainer" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 30.0
margin_top = 30.0
margin_right = -30.0
margin_bottom = -30.0
[node name="AutoGrid" parent="Margin" instance=ExtResource( 1 )]
margin_right = 1220.0
margin_bottom = 660.0