From 39e15201b6ac5d73e106546fe771ed5cbdb9862f Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Tue, 14 Dec 2021 21:53:45 +0100 Subject: [PATCH] add CardValue --- CardValue.gd | 9 ++++ Deck.gd | 116 ++++++++++++++++++++-------------------------- HanafudaCard.gd | 25 ++++++---- HanafudaCard.tscn | 2 - Main.gd | 17 ++++--- Main.tscn | 1 - project.godot | 6 +++ 7 files changed, 93 insertions(+), 83 deletions(-) create mode 100644 CardValue.gd diff --git a/CardValue.gd b/CardValue.gd new file mode 100644 index 0000000..0bc64cb --- /dev/null +++ b/CardValue.gd @@ -0,0 +1,9 @@ +class_name CardValue +extends Node + +var month: int +var type: int + +func _init(month: int, type: int): + self.month = month + self.type = type diff --git a/Deck.gd b/Deck.gd index e41bd55..a4a9a88 100644 --- a/Deck.gd +++ b/Deck.gd @@ -1,83 +1,74 @@ extends Node2D -var HanafudaCard = preload("res://HanafudaCard.tscn") +const CardValue = preload("res://CardValue.gd") onready var cards = [ - HanafudaCard.instance().init_card(Enums.Month.JANUARY, Enums.Type.LIGHT), - HanafudaCard.instance().init_card(Enums.Month.JANUARY, Enums.Type.RIBBON), - HanafudaCard.instance().init_card(Enums.Month.JANUARY, Enums.Type.SCRAP_1), - HanafudaCard.instance().init_card(Enums.Month.JANUARY, Enums.Type.SCRAP_2), + CardValue.new(Enums.Month.JANUARY, Enums.Type.LIGHT), + CardValue.new(Enums.Month.JANUARY, Enums.Type.RIBBON), + CardValue.new(Enums.Month.JANUARY, Enums.Type.SCRAP_1), + CardValue.new(Enums.Month.JANUARY, Enums.Type.SCRAP_2), - HanafudaCard.instance().init_card(Enums.Month.FEBRUARY, Enums.Type.ANIMAL), - HanafudaCard.instance().init_card(Enums.Month.FEBRUARY, Enums.Type.RIBBON), - HanafudaCard.instance().init_card(Enums.Month.FEBRUARY, Enums.Type.SCRAP_1), - HanafudaCard.instance().init_card(Enums.Month.FEBRUARY, Enums.Type.SCRAP_2), + CardValue.new(Enums.Month.FEBRUARY, Enums.Type.ANIMAL), + CardValue.new(Enums.Month.FEBRUARY, Enums.Type.RIBBON), + CardValue.new(Enums.Month.FEBRUARY, Enums.Type.SCRAP_1), + CardValue.new(Enums.Month.FEBRUARY, Enums.Type.SCRAP_2), - HanafudaCard.instance().init_card(Enums.Month.MARCH, Enums.Type.LIGHT), - HanafudaCard.instance().init_card(Enums.Month.MARCH, Enums.Type.RIBBON), - HanafudaCard.instance().init_card(Enums.Month.MARCH, Enums.Type.SCRAP_1), - HanafudaCard.instance().init_card(Enums.Month.MARCH, Enums.Type.SCRAP_2), + CardValue.new(Enums.Month.MARCH, Enums.Type.LIGHT), + CardValue.new(Enums.Month.MARCH, Enums.Type.RIBBON), + CardValue.new(Enums.Month.MARCH, Enums.Type.SCRAP_1), + CardValue.new(Enums.Month.MARCH, Enums.Type.SCRAP_2), - HanafudaCard.instance().init_card(Enums.Month.APRIL, Enums.Type.ANIMAL), - HanafudaCard.instance().init_card(Enums.Month.APRIL, Enums.Type.RIBBON), - HanafudaCard.instance().init_card(Enums.Month.APRIL, Enums.Type.SCRAP_1), - HanafudaCard.instance().init_card(Enums.Month.APRIL, Enums.Type.SCRAP_2), + CardValue.new(Enums.Month.APRIL, Enums.Type.ANIMAL), + CardValue.new(Enums.Month.APRIL, Enums.Type.RIBBON), + CardValue.new(Enums.Month.APRIL, Enums.Type.SCRAP_1), + CardValue.new(Enums.Month.APRIL, Enums.Type.SCRAP_2), - HanafudaCard.instance().init_card(Enums.Month.MAY, Enums.Type.ANIMAL), - HanafudaCard.instance().init_card(Enums.Month.MAY, Enums.Type.RIBBON), - HanafudaCard.instance().init_card(Enums.Month.MAY, Enums.Type.SCRAP_1), - HanafudaCard.instance().init_card(Enums.Month.MAY, Enums.Type.SCRAP_2), + CardValue.new(Enums.Month.MAY, Enums.Type.ANIMAL), + CardValue.new(Enums.Month.MAY, Enums.Type.RIBBON), + CardValue.new(Enums.Month.MAY, Enums.Type.SCRAP_1), + CardValue.new(Enums.Month.MAY, Enums.Type.SCRAP_2), - HanafudaCard.instance().init_card(Enums.Month.JUNE, Enums.Type.ANIMAL), - HanafudaCard.instance().init_card(Enums.Month.JUNE, Enums.Type.RIBBON), - HanafudaCard.instance().init_card(Enums.Month.JUNE, Enums.Type.SCRAP_1), - HanafudaCard.instance().init_card(Enums.Month.JUNE, Enums.Type.SCRAP_2), + CardValue.new(Enums.Month.JUNE, Enums.Type.ANIMAL), + CardValue.new(Enums.Month.JUNE, Enums.Type.RIBBON), + CardValue.new(Enums.Month.JUNE, Enums.Type.SCRAP_1), + CardValue.new(Enums.Month.JUNE, Enums.Type.SCRAP_2), - HanafudaCard.instance().init_card(Enums.Month.JULY, Enums.Type.ANIMAL), - HanafudaCard.instance().init_card(Enums.Month.JULY, Enums.Type.RIBBON), - HanafudaCard.instance().init_card(Enums.Month.JULY, Enums.Type.SCRAP_1), - HanafudaCard.instance().init_card(Enums.Month.JULY, Enums.Type.SCRAP_2), + CardValue.new(Enums.Month.JULY, Enums.Type.ANIMAL), + CardValue.new(Enums.Month.JULY, Enums.Type.RIBBON), + CardValue.new(Enums.Month.JULY, Enums.Type.SCRAP_1), + CardValue.new(Enums.Month.JULY, Enums.Type.SCRAP_2), - HanafudaCard.instance().init_card(Enums.Month.AUGUST, Enums.Type.LIGHT), - HanafudaCard.instance().init_card(Enums.Month.AUGUST, Enums.Type.ANIMAL), - HanafudaCard.instance().init_card(Enums.Month.AUGUST, Enums.Type.SCRAP_1), - HanafudaCard.instance().init_card(Enums.Month.AUGUST, Enums.Type.SCRAP_2), + CardValue.new(Enums.Month.AUGUST, Enums.Type.LIGHT), + CardValue.new(Enums.Month.AUGUST, Enums.Type.ANIMAL), + CardValue.new(Enums.Month.AUGUST, Enums.Type.SCRAP_1), + CardValue.new(Enums.Month.AUGUST, Enums.Type.SCRAP_2), - HanafudaCard.instance().init_card(Enums.Month.SEPTEMBER, Enums.Type.ANIMAL), - HanafudaCard.instance().init_card(Enums.Month.SEPTEMBER, Enums.Type.RIBBON), - HanafudaCard.instance().init_card(Enums.Month.SEPTEMBER, Enums.Type.SCRAP_1), - HanafudaCard.instance().init_card(Enums.Month.SEPTEMBER, Enums.Type.SCRAP_2), + CardValue.new(Enums.Month.SEPTEMBER, Enums.Type.ANIMAL), + CardValue.new(Enums.Month.SEPTEMBER, Enums.Type.RIBBON), + CardValue.new(Enums.Month.SEPTEMBER, Enums.Type.SCRAP_1), + CardValue.new(Enums.Month.SEPTEMBER, Enums.Type.SCRAP_2), - HanafudaCard.instance().init_card(Enums.Month.OCTOBER, Enums.Type.ANIMAL), - HanafudaCard.instance().init_card(Enums.Month.OCTOBER, Enums.Type.RIBBON), - HanafudaCard.instance().init_card(Enums.Month.OCTOBER, Enums.Type.SCRAP_1), - HanafudaCard.instance().init_card(Enums.Month.OCTOBER, Enums.Type.SCRAP_2), + CardValue.new(Enums.Month.OCTOBER, Enums.Type.ANIMAL), + CardValue.new(Enums.Month.OCTOBER, Enums.Type.RIBBON), + CardValue.new(Enums.Month.OCTOBER, Enums.Type.SCRAP_1), + CardValue.new(Enums.Month.OCTOBER, Enums.Type.SCRAP_2), - HanafudaCard.instance().init_card(Enums.Month.NOVEMBER, Enums.Type.LIGHT), - HanafudaCard.instance().init_card(Enums.Month.NOVEMBER, Enums.Type.ANIMAL), - HanafudaCard.instance().init_card(Enums.Month.NOVEMBER, Enums.Type.RIBBON), - HanafudaCard.instance().init_card(Enums.Month.NOVEMBER, Enums.Type.SCRAP_1), + CardValue.new(Enums.Month.NOVEMBER, Enums.Type.LIGHT), + CardValue.new(Enums.Month.NOVEMBER, Enums.Type.ANIMAL), + CardValue.new(Enums.Month.NOVEMBER, Enums.Type.RIBBON), + CardValue.new(Enums.Month.NOVEMBER, Enums.Type.SCRAP_1), - HanafudaCard.instance().init_card(Enums.Month.DECEMBER, Enums.Type.LIGHT), - HanafudaCard.instance().init_card(Enums.Month.DECEMBER, Enums.Type.SCRAP_1), - HanafudaCard.instance().init_card(Enums.Month.DECEMBER, Enums.Type.SCRAP_2), - HanafudaCard.instance().init_card(Enums.Month.DECEMBER, Enums.Type.SCRAP_3), + CardValue.new(Enums.Month.DECEMBER, Enums.Type.LIGHT), + CardValue.new(Enums.Month.DECEMBER, Enums.Type.SCRAP_1), + CardValue.new(Enums.Month.DECEMBER, Enums.Type.SCRAP_2), + CardValue.new(Enums.Month.DECEMBER, Enums.Type.SCRAP_3), ] var card_order = [] var next_to_draw = 0 -# Called when the node enters the scene tree for the first time. func _ready() -> void: init_card_order() - for c in cards: - add_child(c) - c.hide() - - - var card: HanafudaCard = cards[0] - card.show() - card.reveal(false) func init_card_order() -> void: card_order.resize(cards.size()) @@ -86,14 +77,9 @@ func init_card_order() -> void: card_order.shuffle() next_to_draw = 0 -func draw_card() -> int: +func draw_card() -> CardValue: if next_to_draw >= card_order.size(): - return -1 + return null var card_id = card_order[next_to_draw] next_to_draw += 1 return card_id - -func card(index: int) -> HanafudaCard: - if index == -1: - printerr("Invalid card index") - return cards[index] diff --git a/HanafudaCard.gd b/HanafudaCard.gd index aab743a..770031e 100644 --- a/HanafudaCard.gd +++ b/HanafudaCard.gd @@ -1,27 +1,30 @@ tool -extends Node2D class_name HanafudaCard +extends Node2D + +const CardValue = preload("res://CardValue.gd") export var revealed: bool = true setget reveal -export(Enums.Month) var month: int -export(Enums.Type) var type: int +export(Enums.Month) var month: int setget _set_card_month +export(Enums.Type) var type: int setget _set_card_type +onready var value = CardValue.new(Enums.Month.JANUARY, Enums.Type.LIGHT) onready var _is_ready := true # Called when the node enters the scene tree for the first time. func _ready() -> void: + var texture_path := card_texture() $Frontside.texture = load(texture_path) reveal(revealed) -func init_card(month_: int, type_: int) -> HanafudaCard: - month = month_ - type = type_ +func init_card(value_: CardValue) -> HanafudaCard: + value = value_ return self func card_texture() -> String: var m := "" - match month: + match value.month: Enums.Month.JANUARY: m = "January" Enums.Month.FEBRUARY: m = "February" Enums.Month.MARCH: m = "March" @@ -36,7 +39,7 @@ func card_texture() -> String: Enums.Month.DECEMBER: m = "December" var t := "" - match type: + match value.type: Enums.Type.LIGHT: t = "Hikari" Enums.Type.ANIMAL: t = "Tane" Enums.Type.RIBBON: t = "Tanzaku" @@ -59,3 +62,9 @@ func reveal(new_reveal: bool) -> void: else: $Frontside.hide() $Backside.show() + +func _set_card_month(new_month: int) -> void: + value.month = new_month + +func _set_card_type(new_type: int) -> void: + value.type = new_type diff --git a/HanafudaCard.tscn b/HanafudaCard.tscn index 18b8fa3..023fa44 100644 --- a/HanafudaCard.tscn +++ b/HanafudaCard.tscn @@ -7,8 +7,6 @@ [node name="HanafudaCard" type="Node2D"] script = ExtResource( 1 ) -month = 6 -type = 3 [node name="Frontside" type="Sprite" parent="."] texture = ExtResource( 2 ) diff --git a/Main.gd b/Main.gd index 473a69b..9f8deec 100644 --- a/Main.gd +++ b/Main.gd @@ -1,5 +1,7 @@ extends Node +onready var HanafudaCard = preload("res://HanafudaCard.tscn") + onready var deck = $Deck onready var tween = $Tween @@ -8,18 +10,19 @@ func _ready(): func oya(): tween.start() - var card_1 = $Deck.card($Deck.draw_card()) - card_1.show() + var card_1 = HanafudaCard.instance().init_card($Deck.draw_card()) + add_child(card_1) +# card_1.show() tween.interpolate_property(card_1, "position", Vector2(0, 0), Vector2(100, 100), 3, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT) - var card_2 = $Deck.card($Deck.draw_card()) - card_2.show() - tween.interpolate_property(card_2, "position", - Vector2(0, 0), Vector2(-100, 100), 0.4, - Tween.TRANS_LINEAR, Tween.EASE_IN_OUT) +# var card_2 = $Deck.card($Deck.draw_card()) +# card_2.show() +# tween.interpolate_property(card_2, "position", +# Vector2(0, 0), Vector2(-100, 100), 0.4, +# Tween.TRANS_LINEAR, Tween.EASE_IN_OUT) yield(tween, "tween_all_completed") diff --git a/Main.tscn b/Main.tscn index 10393bf..47f8457 100644 --- a/Main.tscn +++ b/Main.tscn @@ -18,7 +18,6 @@ __meta__ = { [node name="HanafudaCard" parent="." instance=ExtResource( 1 )] visible = false position = Vector2( 205, 447 ) -month = 2 [node name="Deck" type="Node2D" parent="."] position = Vector2( 293, 412 ) diff --git a/project.godot b/project.godot index 259cc44..69b62d7 100644 --- a/project.godot +++ b/project.godot @@ -9,12 +9,18 @@ config_version=4 _global_script_classes=[ { +"base": "Node", +"class": "CardValue", +"language": "GDScript", +"path": "res://CardValue.gd" +}, { "base": "Node2D", "class": "HanafudaCard", "language": "GDScript", "path": "res://HanafudaCard.gd" } ] _global_script_class_icons={ +"CardValue": "", "HanafudaCard": "" }