diff --git a/objects/card/card.gd b/objects/card/card.gd index 72b76c1..9f050b7 100644 --- a/objects/card/card.gd +++ b/objects/card/card.gd @@ -25,6 +25,7 @@ var is_shaking := false func _ready(): origin_global_position = global_position last_global_position = global_position + $HighlightSprite.visible = false func _on_mouse_release(): #prints(entered_area.name, is_card_entered, entered_area.occupied) @@ -97,6 +98,7 @@ func _process(_delta: float) -> void: var progress = $ShakeTimer.time_left / $ShakeTimer.wait_time * 2 * PI offset = int(sin(progress) * shake_amount) $CardBackSprite.position.x = offset + $HighlightSprite.position.x = offset $Word.position.x = offset func on_card_entered(area: Block) -> void: @@ -116,8 +118,17 @@ func on_card_base_exited() -> void: is_card_base_entered -= 1 #print(is_card_entered) -func set_word(e: String) -> void: - $Word.set_word(e) +func set_word(value: String) -> void: + $Word.set_word(value) + var card_type := "card-%s" % ExprValidator.get_char_type_as_str(get_word()).to_lower() + if ImageLib.PALETTE.has(card_type): + ImageLib.update_animation( + $CardBackSprite, 1, 3, 1, "res://objects/card/card%d.png", + ImageLib.PALETTE["lightblue"], ImageLib.PALETTE[card_type] + ) + print($CardBackSprite.animation) + print($CardBackSprite.sprite_frames.animations) + func get_word() -> String: return $Word.get_word() @@ -135,22 +146,22 @@ func shake(is_letter_red: bool, amount: float, duration: float) -> void: if is_letter_red: $Word.set_color(ImageLib.PALETTE["red"]) else: - $CardBackSprite.animation = "default" + $HighlightSprite.visible = false + func _on_mouse_entered(): - $CardBackSprite.animation = "highlighted" + $HighlightSprite.visible = true Input.set_default_cursor_shape(Input.CURSOR_POINTING_HAND) func _on_mouse_exited(): - $CardBackSprite.animation = "default" + $HighlightSprite.visible = false Input.set_default_cursor_shape(Input.CURSOR_ARROW) func set_color(value: Color) -> void: $Word.set_color(value) - func set_victory(v: bool): if v: $CollisionShape2D.set_deferred("disabled", true) diff --git a/objects/card/card.tscn b/objects/card/card.tscn index 531bdc5..02533d7 100644 --- a/objects/card/card.tscn +++ b/objects/card/card.tscn @@ -5,11 +5,9 @@ [ext_resource type="Texture2D" uid="uid://cfbqh1k0etr6g" path="res://objects/card/card1.png" id="3_o4f0v"] [ext_resource type="Texture2D" uid="uid://c8qjbksyn1bqt" path="res://objects/card/card2.png" id="4_c30j1"] [ext_resource type="Texture2D" uid="uid://bevrfq1hhyqwe" path="res://objects/card/card3.png" id="5_w81n3"] -[ext_resource type="Texture2D" uid="uid://cmcc5xrpkrd0f" path="res://objects/card/card_highlighted.png" id="6_po2i6"] +[ext_resource type="Texture2D" uid="uid://pmapbo480in1" path="res://objects/card/card_highlight_border.png" id="7_kix4t"] [ext_resource type="AudioStream" uid="uid://cj4v8ehypq3sk" path="res://objects/card/put_down.wav" id="7_u4ylf"] -[sub_resource type="RectangleShape2D" id="RectangleShape2D_gu6l6"] - [sub_resource type="SpriteFrames" id="SpriteFrames_fo0r0"] animations = [{ "frames": [{ @@ -25,34 +23,27 @@ animations = [{ "loop": true, "name": &"default", "speed": 5.0 -}, { -"frames": [{ -"duration": 1.0, -"texture": ExtResource("6_po2i6") -}], -"loop": true, -"name": &"highlighted", -"speed": 5.0 }] +[sub_resource type="RectangleShape2D" id="RectangleShape2D_gu6l6"] + [node name="Card" type="Area2D"] z_index = 50 script = ExtResource("1_0bviv") [node name="Word" parent="." instance=ExtResource("1_ns36n")] z_index = 1 -frame_progress = 0.111549 -text_id = 1 -color = Color(0, 0, 0, 1) + +[node name="CardBackSprite" type="AnimatedSprite2D" parent="."] +sprite_frames = SubResource("SpriteFrames_fo0r0") + +[node name="HighlightSprite" type="Sprite2D" parent="."] +texture = ExtResource("7_kix4t") [node name="CollisionShape2D" type="CollisionShape2D" parent="."] z_index = 15 shape = SubResource("RectangleShape2D_gu6l6") -[node name="CardBackSprite" type="AnimatedSprite2D" parent="."] -sprite_frames = SubResource("SpriteFrames_fo0r0") -autoplay = "default" - [node name="SFXPutDown" type="AudioStreamPlayer" parent="."] stream = ExtResource("7_u4ylf") volume_db = 13.117 diff --git a/objects/card/card_highlight_border.png b/objects/card/card_highlight_border.png new file mode 100644 index 0000000..2f7f195 Binary files /dev/null and b/objects/card/card_highlight_border.png differ diff --git a/objects/card_base/card_base_highlighted.png.import b/objects/card/card_highlight_border.png.import similarity index 65% rename from objects/card_base/card_base_highlighted.png.import rename to objects/card/card_highlight_border.png.import index 5c5565a..ba37e56 100644 --- a/objects/card_base/card_base_highlighted.png.import +++ b/objects/card/card_highlight_border.png.import @@ -2,16 +2,16 @@ importer="texture" type="CompressedTexture2D" -uid="uid://cs7vfmsrhpomj" -path="res://.godot/imported/card_base_highlighted.png-50d4af98046c8a87d73d61ca6664f89e.ctex" +uid="uid://pmapbo480in1" +path="res://.godot/imported/card_highlight_border.png-c0b130850c2d1dcdc5af25ea7e1dafdc.ctex" metadata={ "vram_texture": false } [deps] -source_file="res://objects/card_base/card_base_highlighted.png" -dest_files=["res://.godot/imported/card_base_highlighted.png-50d4af98046c8a87d73d61ca6664f89e.ctex"] +source_file="res://objects/card/card_highlight_border.png" +dest_files=["res://.godot/imported/card_highlight_border.png-c0b130850c2d1dcdc5af25ea7e1dafdc.ctex"] [params] diff --git a/objects/card/card_highlighted.png b/objects/card/card_highlighted.png deleted file mode 100644 index cac2f73..0000000 Binary files a/objects/card/card_highlighted.png and /dev/null differ diff --git a/objects/card_base/card_base.gd b/objects/card_base/card_base.gd index f137c1b..5cf3b4e 100644 --- a/objects/card_base/card_base.gd +++ b/objects/card_base/card_base.gd @@ -11,6 +11,9 @@ signal card_put var fade_flag := false var mouse_on := false +enum { DEFAULT, HIGHLIGHT, DISABLED} +var available_stat := DEFAULT + var card_count = 0 var new_card_node: Card @@ -25,11 +28,11 @@ func set_card_count(value): card_count = value if card_count > 0: if mouse_on: - $AnimatedSprite2D.animation = "highlighted" + available_stat = HIGHLIGHT else: - $AnimatedSprite2D.animation = "default" + available_stat = DEFAULT else: - $AnimatedSprite2D.animation = "disabled" + available_stat = DISABLED update_card_count_label() @@ -74,6 +77,11 @@ func _input_event(_viewport: Object, event: InputEvent, _shape_idx: int) -> void func set_word(e: String) -> void: $Word.set_word(e) + var card_type := "card-%s" % ExprValidator.get_char_type_as_str(e).to_lower() + if ImageLib.PALETTE.has(card_type): + ImageLib.update_animation($CardBaseSprite, 1, 1, 1, "res://objects/card_base/card_base%d.png", + ImageLib.PALETTE["lightblue"], ImageLib.PALETTE[card_type]) + func get_word() -> String: return $Word.get_word() @@ -98,18 +106,23 @@ func _process(_delta) -> void: if fade_flag: var offset = $FadeTimer.time_left / $FadeTimer.wait_time offset = (1 - pow(offset, 1.5)) * FADE_MOVE_AMOUNT - $AnimatedSprite2D.position.y = offset + $CardBaseSprite.position.y = offset + $HighlightSprite.position.y = offset + $DisabledSprite.position.y = offset $Word.position.y = offset + + $HighlightSprite.visible = (available_stat == HIGHLIGHT) + $DisabledSprite.visible = (available_stat == DISABLED) func _on_mouse_entered(): mouse_on = true - if $AnimatedSprite2D.animation == "default": - $AnimatedSprite2D.animation = "highlighted" + if available_stat == DEFAULT: + available_stat = HIGHLIGHT Input.set_default_cursor_shape(Input.CURSOR_POINTING_HAND) func _on_mouse_exited(): mouse_on = false - if $AnimatedSprite2D.animation == "highlighted": - $AnimatedSprite2D.animation = "default" + if available_stat == HIGHLIGHT: + available_stat = DEFAULT Input.set_default_cursor_shape(Input.CURSOR_ARROW) diff --git a/objects/card_base/card_base.tscn b/objects/card_base/card_base.tscn index 1bdbd1b..e02c82e 100644 --- a/objects/card_base/card_base.tscn +++ b/objects/card_base/card_base.tscn @@ -5,7 +5,7 @@ [ext_resource type="FontFile" uid="uid://1000owdyvwfg" path="res://fonts/unifont-15.1.04.otf" id="3_0qykx"] [ext_resource type="Texture2D" uid="uid://b5d2rfms7vyfi" path="res://objects/card_base/card_base1.png" id="3_h7i1h"] [ext_resource type="Texture2D" uid="uid://bkjbrw1ppac5q" path="res://objects/card_base/card_base_disabled.png" id="4_0unxr"] -[ext_resource type="Texture2D" uid="uid://cs7vfmsrhpomj" path="res://objects/card_base/card_base_highlighted.png" id="5_tkdj2"] +[ext_resource type="Texture2D" uid="uid://bkuwo3g8pyuyi" path="res://objects/card_base/card_base_highlight_border.png" id="5_r41oe"] [ext_resource type="AudioStream" uid="uid://0sefo32v1jne" path="res://objects/card_base/pick_up.wav" id="7_0c7kt"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_2cmwb"] @@ -28,14 +28,6 @@ animations = [{ "loop": true, "name": &"disabled", "speed": 5.0 -}, { -"frames": [{ -"duration": 1.0, -"texture": ExtResource("5_tkdj2") -}], -"loop": true, -"name": &"highlighted", -"speed": 5.0 }] [node name="CardBase" type="Area2D"] @@ -45,10 +37,20 @@ script = ExtResource("1_k5qn1") shape = SubResource("RectangleShape2D_2cmwb") debug_color = Color(0.65098, 0.431373, 0.756863, 0.419608) +[node name="CardBaseSprite" type="AnimatedSprite2D" parent="."] +sprite_frames = SubResource("SpriteFrames_bhb3x") + [node name="Word" parent="." instance=ExtResource("2_jvsp3")] -z_index = 1 frame_progress = 0.953211 +[node name="HighlightSprite" type="Sprite2D" parent="."] +visible = false +texture = ExtResource("5_r41oe") + +[node name="DisabledSprite" type="Sprite2D" parent="."] +visible = false +texture = ExtResource("4_0unxr") + [node name="Label" type="Label" parent="."] offset_left = -13.0 offset_top = 17.0 @@ -59,9 +61,6 @@ text = "0" horizontal_alignment = 1 vertical_alignment = 1 -[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."] -sprite_frames = SubResource("SpriteFrames_bhb3x") - [node name="Cards" type="Node" parent="."] [node name="FadeTimer" type="Timer" parent="."] diff --git a/objects/card_base/card_base_highlight_border.png b/objects/card_base/card_base_highlight_border.png new file mode 100644 index 0000000..19ad568 Binary files /dev/null and b/objects/card_base/card_base_highlight_border.png differ diff --git a/objects/card_base/card_base_highlight_border.png.import b/objects/card_base/card_base_highlight_border.png.import new file mode 100644 index 0000000..5de750e --- /dev/null +++ b/objects/card_base/card_base_highlight_border.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://bkuwo3g8pyuyi" +path="res://.godot/imported/card_base_highlight_border.png-02641e6f6e23a694917134adadde948a.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://objects/card_base/card_base_highlight_border.png" +dest_files=["res://.godot/imported/card_base_highlight_border.png-02641e6f6e23a694917134adadde948a.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/objects/card_base/card_base_highlighted.png b/objects/card_base/card_base_highlighted.png deleted file mode 100644 index 6213968..0000000 Binary files a/objects/card_base/card_base_highlighted.png and /dev/null differ diff --git a/objects/word/word.gd b/objects/word/word.gd index 321d3ba..74ba764 100644 --- a/objects/word/word.gd +++ b/objects/word/word.gd @@ -30,12 +30,7 @@ func set_text_id(value: int) -> void: update_animation() func update_animation() -> void: - var animation_id = str(text_id) + color.to_html() - if not sprite_frames.has_animation(animation_id): - sprite_frames.add_animation(animation_id) - for i in range(3): - sprite_frames.add_frame(animation_id, load_image(i * STEP + text_id + 1, color)) - animation = animation_id + ImageLib.update_animation(self, text_id + 1, 3, STEP, "res://objects/word/sprites/sprite%d.png", Color.BLACK, color) @@ -53,11 +48,6 @@ func get_word() -> String: func _ready(): update_animation() - - - -func _process(_delta): - update_animation() func set_color(value: Color) -> void: color = value @@ -72,9 +62,3 @@ func set_victory(v: bool) -> void: set_color(ImageLib.PALETTE["golden"]) else: set_color(ImageLib.PALETTE["default"]) - -func load_image(h: int, new_color: Color): - var image := load("res://objects/word/sprites/sprite" + str(h) + ".png") - if new_color != Color.BLACK: - return ImageLib.change_color(image, Color.BLACK, new_color) - return image diff --git a/objects/word/word.tscn b/objects/word/word.tscn index 9fa61db..3678ecc 100644 --- a/objects/word/word.tscn +++ b/objects/word/word.tscn @@ -1,35 +1,19 @@ -[gd_scene load_steps=6 format=3 uid="uid://cvx7wowcbfo0r"] +[gd_scene load_steps=4 format=3 uid="uid://cvx7wowcbfo0r"] [ext_resource type="Texture2D" uid="uid://cyyu6tsje4x72" path="res://objects/word/sprites/sprite3.png" id="1_ariam"] [ext_resource type="Script" path="res://objects/word/word.gd" id="1_lkxlh"] -[ext_resource type="Texture2D" uid="uid://bqtqgiv5etyny" path="res://objects/word/sprites/sprite28.png" id="2_l4ops"] -[ext_resource type="Texture2D" uid="uid://dua7hylyf4ws6" path="res://objects/word/sprites/sprite53.png" id="3_2kshj"] [sub_resource type="SpriteFrames" id="SpriteFrames_3n85j"] animations = [{ "frames": [{ "duration": 1.0, "texture": ExtResource("1_ariam") -}, { -"duration": 1.0, -"texture": ExtResource("2_l4ops") -}, { -"duration": 1.0, -"texture": ExtResource("3_2kshj") }], "loop": true, "name": &"default", -"speed": 3.0 -}, { -"frames": [], -"loop": true, -"name": &"new_animation", "speed": 5.0 }] [node name="Word" type="AnimatedSprite2D"] sprite_frames = SubResource("SpriteFrames_3n85j") -autoplay = "default" script = ExtResource("1_lkxlh") -text_id = null -color = null diff --git a/scripts/expr_validator.gd b/scripts/expr_validator.gd index 5db6550..bdf841a 100644 --- a/scripts/expr_validator.gd +++ b/scripts/expr_validator.gd @@ -48,7 +48,7 @@ const IS_PAIR_VALID := [ [1, 1, 1, 1, 0, 0] ] -enum {OP, COMP, BRAC_L, BRAC_R, VAR, CONST} +enum {OP, COMP, BRACL, BRACR, VAR, CONST} static func get_char_type(ch: String) -> int: @@ -57,16 +57,40 @@ static func get_char_type(ch: String) -> int: elif ch == "<" or ch == "=" or ch == ">": return COMP elif ch == "(": - return BRAC_L + return BRACL elif ch == ")": - return BRAC_R + return BRACR elif ch == "0" or ch == "1": return CONST - else: + elif is_alpha(ch): return VAR + else: + push_error("get_char_type(%s) is undefined" % ch) + return -1 + +static func get_char_type_enum_name(value: int) -> String: + if value == OP: + return "OP" + elif value == COMP: + return "COMP" + elif value == BRACL: + return "BRACL" + elif value == BRACR: + return "BRACR" + elif value == VAR: + return "VAR" + elif value == CONST: + return "CONST" + else: + push_error("get_char_type_enum_name(%d) is undefined" % value) + return "ERR" +static func get_char_type_as_str(ch: String) -> String: + return get_char_type_enum_name(get_char_type(ch)) + + static func has_implict_prod(string: String) -> bool: - return get_char_type(string[0]) in [BRAC_R, VAR, CONST] and get_char_type(string[1]) in [BRAC_L, VAR, CONST] + return get_char_type(string[0]) in [BRACR, VAR, CONST] and get_char_type(string[1]) in [BRACL, VAR, CONST] static func get_priority(ch: String) -> int: if ch == "*": @@ -175,9 +199,9 @@ static func calculate_value(expr: String, var_values: Dictionary) -> bool: ## ## 合法返回 [],否则返回不合法的下标。 static func check_valid(expr: String) -> Array: - if get_char_type(expr[0]) != BRAC_L and get_char_type(expr[0]) not in [VAR, CONST]: + if get_char_type(expr[0]) != BRACL and get_char_type(expr[0]) not in [VAR, CONST]: return [0] - if get_char_type(expr[len(expr) - 1]) != BRAC_R and get_char_type(expr[len(expr) - 1]) not in [VAR, CONST]: + if get_char_type(expr[len(expr) - 1]) != BRACR and get_char_type(expr[len(expr) - 1]) not in [VAR, CONST]: return [len(expr) - 1] for i in range(len(expr) - 1): if not IS_PAIR_VALID[get_char_type(expr[i])][get_char_type(expr[i + 1])]: diff --git a/scripts/image_lib.gd b/scripts/image_lib.gd index b0366c3..dc9224e 100644 --- a/scripts/image_lib.gd +++ b/scripts/image_lib.gd @@ -7,6 +7,10 @@ const PALETTE = { "orange": Color("#ffb463"), "card-op": Color("#ffb463"), + "skyblue": Color("#c2fdff"), + "card-bracl": Color("#c2fdff"), + "card-bracr": Color("#c2fdff"), + "lightblue": Color("#8eacf3"), "card-var": Color("#8eacf3"), @@ -19,7 +23,7 @@ const PALETTE = { "default": Color.BLACK, } -static func change_color(image: Texture, old_color: Color, new_color: Color) -> Texture: +static func change_color(image: Texture2D, old_color: Color, new_color: Color) -> Texture2D: var new_texture = image.get_image() for x in range(new_texture.get_width()): for y in range(new_texture.get_height()): @@ -27,3 +31,20 @@ static func change_color(image: Texture, old_color: Color, new_color: Color) -> if color == old_color: new_texture.set_pixel(x, y, new_color) return ImageTexture.create_from_image(new_texture) + +static func update_animation( + sprite: AnimatedSprite2D, + start: int, + n: int, + step: int, + path_pattern: String, + old_color: Color, + new_color: Color +) -> void: + var animation_id = "%d_%d_%d_%s" % [start, n, step, new_color.to_html()] + if not sprite.sprite_frames.has_animation(animation_id): + sprite.sprite_frames.add_animation(animation_id) + for i in range(n): + var image = load(path_pattern % (i * step + start)) + sprite.sprite_frames.add_frame(animation_id, ImageLib.change_color(image, old_color, new_color)) + sprite.play(animation_id)