From 5007d7aa2a523a7350ab933654125de0bb11d553 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Mon, 6 Dec 2021 23:10:39 +0100 Subject: [PATCH] add card Deck --- Deck.gd | 70 +++++++++++++++++++++++++++++++++++++++++++++ Enums.gd | 40 ++++++++++++++++++++++++++ HanafudaCard.gd | 73 ++++++++++++++++++++++++----------------------- HanafudaCard.tscn | 5 ++-- Main.gd | 16 +++++++++++ Main.tscn | 10 +++++-- project.godot | 14 +++++++++ 7 files changed, 189 insertions(+), 39 deletions(-) create mode 100644 Deck.gd create mode 100644 Enums.gd create mode 100644 Main.gd diff --git a/Deck.gd b/Deck.gd new file mode 100644 index 0000000..aee01bb --- /dev/null +++ b/Deck.gd @@ -0,0 +1,70 @@ +extends Node + +var HanafudaCard = preload("res://HanafudaCard.tscn") + +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), + + 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), + + 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), + + 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), + + 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), + + 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), + + 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), + + 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), + + 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), + + 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), + + 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), + + 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), +] + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + for c in cards: + add_child(c) diff --git a/Enums.gd b/Enums.gd new file mode 100644 index 0000000..645963c --- /dev/null +++ b/Enums.gd @@ -0,0 +1,40 @@ +extends Node + +enum Month { + JANUARY, + FEBRUARY, + MARCH, + APRIL, + MAY, + JUNE, + JULY, + AUGUST, + SEPTEMBER, + OCTOBER, + NOVEMBER, + DECEMBER, +} + +enum Flower { + PINE, + PLUM_BLOSSOM, + CHERRY_BLOSSOM, + WISTERIA, + IRIS, + PEONY, + BUSH_CLOVER, + GRASS, + CHRYSANTHEMUM, + MAPLE, + WILLOW, + PAULOWNIA, +} + +enum Type { + LIGHT, + ANIMAL, + RIBBON, + SCRAP_1, + SCRAP_2, + SCRAP_3, +} diff --git a/HanafudaCard.gd b/HanafudaCard.gd index c2955d4..cfac459 100644 --- a/HanafudaCard.gd +++ b/HanafudaCard.gd @@ -1,49 +1,52 @@ tool extends Node2D - -enum Month { - JANUARY, - FEBRUARY, - MARCH, - APRIL, - MAY, - JUNE, - JULY, - AUGUST, - SEPTEMBER, - OCTOBER, - NOVEMBER, - DECEMBER, -} - -enum Flower { - PINE, - PLUM_BLOSSOM, - CHERRY_BLOSSOM, - WISTERIA, - IRIS, - PEONY, - BUSH_CLOVER, - GRASS, - CHRYSANTHEMUM, - MAPLE, - WILLOW, - PAULOWNIA, -} +class_name HanafudaCard export var revealed: bool = true setget reveal +export(Enums.Month) var month: int +export(Enums.Type) var type: int onready var _is_ready := true # Called when the node enters the scene tree for the first time. -func _ready(): +func _ready() -> void: + var texture_path := card_texture() + $Frontside.texture = load(texture_path) reveal(revealed) -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta): - pass +func init_card(month_: int, type_: int) -> void: + month = month_ + type = type_ -func reveal(new_reveal: bool): +func card_texture() -> String: + var m := "" + match month: + Enums.Month.JANUARY: m = "January" + Enums.Month.FEBRUARY: m = "February" + Enums.Month.MARCH: m = "March" + Enums.Month.APRIL: m = "April" + Enums.Month.MAY: m = "May" + Enums.Month.JUNE: m = "June" + Enums.Month.JULY: m = "July" + Enums.Month.AUGUST: m = "August" + Enums.Month.SEPTEMBER: m = "September" + Enums.Month.OCTOBER: m = "October" + Enums.Month.NOVEMBER: m = "November" + Enums.Month.DECEMBER: m = "December" + + var t := "" + match type: + Enums.Type.LIGHT: t = "Hikari" + Enums.Type.ANIMAL: t = "Tane" + Enums.Type.RIBBON: t = "Tanzaku" + Enums.Type.SCRAP_1: t = "Kasu_1" + Enums.Type.SCRAP_2: t = "Kasu_2" + Enums.Type.SCRAP_3: t = "Kasu_3" + + var texture_path := "res://assets/png/Hanafuda_{month}_{type}.png".format({"month": m, "type": t}) + return texture_path + +func reveal(new_reveal: bool) -> void: revealed = new_reveal if not _is_ready: diff --git a/HanafudaCard.tscn b/HanafudaCard.tscn index 78e1223..de2d77d 100644 --- a/HanafudaCard.tscn +++ b/HanafudaCard.tscn @@ -7,13 +7,14 @@ [node name="HanafudaCard" type="Node2D"] script = ExtResource( 1 ) -revealed = false +month = 6 +type = 3 [node name="Frontside" type="Sprite" parent="."] +visible = false texture = ExtResource( 2 ) [node name="Backside" type="Node2D" parent="."] -visible = false [node name="TextureRect" type="TextureRect" parent="Backside"] margin_left = -110.0 diff --git a/Main.gd b/Main.gd new file mode 100644 index 0000000..1eccaec --- /dev/null +++ b/Main.gd @@ -0,0 +1,16 @@ +extends Node + + +# Declare member variables here. Examples: +# var a = 2 +# var b = "text" + + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta): +# pass diff --git a/Main.tscn b/Main.tscn index 63a79ce..39390ab 100644 --- a/Main.tscn +++ b/Main.tscn @@ -1,8 +1,11 @@ -[gd_scene load_steps=2 format=2] +[gd_scene load_steps=4 format=2] [ext_resource path="res://HanafudaCard.tscn" type="PackedScene" id=1] +[ext_resource path="res://Main.gd" type="Script" id=2] +[ext_resource path="res://Deck.gd" type="Script" id=3] [node name="Node" type="Node"] +script = ExtResource( 2 ) [node name="Background" type="ColorRect" parent="."] anchor_right = 1.0 @@ -14,4 +17,7 @@ __meta__ = { [node name="HanafudaCard" parent="." instance=ExtResource( 1 )] position = Vector2( 205, 447 ) -revealed = true +month = 2 + +[node name="Deck" type="Node" parent="."] +script = ExtResource( 3 ) diff --git a/project.godot b/project.godot index 78467f5..259cc44 100644 --- a/project.godot +++ b/project.godot @@ -8,12 +8,26 @@ config_version=4 +_global_script_classes=[ { +"base": "Node2D", +"class": "HanafudaCard", +"language": "GDScript", +"path": "res://HanafudaCard.gd" +} ] +_global_script_class_icons={ +"HanafudaCard": "" +} + [application] config/name="Hanafuda" run/main_scene="res://Main.tscn" config/icon="res://icon.png" +[autoload] + +Enums="*res://Enums.gd" + [display] window/size/width=600