From 7be69670e17ffae332a8d7ea0f529d16ddc5b553 Mon Sep 17 00:00:00 2001 From: cutekibry Date: Thu, 8 Feb 2024 20:24:19 +0800 Subject: [PATCH] =?UTF-8?q?feat(block,=20card):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E9=9C=87=E5=8A=A8=E3=80=81=E6=94=B9=E5=8F=98=E8=BE=B9=E6=A1=86?= =?UTF-8?q?=E9=A2=9C=E8=89=B2=E3=80=81=E6=94=B9=E5=8F=98=E6=96=87=E5=AD=97?= =?UTF-8?q?=E9=A2=9C=E8=89=B2=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 注意:Card 振动的振幅和时长继承自 Block,Card 本身的 ShakeTimer 初始 Wait Time 并不会被用到 --- objects/block/block.gd | 53 ++++++++++++++++++++++++++-------------- objects/block/block.tscn | 32 +++++++++++++++++++----- objects/card/card.gd | 43 +++++++++++++++++++++++++++++--- objects/card/card.tscn | 5 ++++ 4 files changed, 106 insertions(+), 27 deletions(-) diff --git a/objects/block/block.gd b/objects/block/block.gd index 997abc0..8fdd1f1 100644 --- a/objects/block/block.gd +++ b/objects/block/block.gd @@ -3,32 +3,31 @@ extends Area2D class_name Block -const SHAKE_AMOUNT := 4 +const SHAKE_AMOUNT := 3.0 ## 振动时的振幅(单位为像素)。 -var occupied := false -var occupied_word: String -var occupied_card: Card +var occupied := false ## 当 Block 上有 Card 或 Block 字符固定时,occupied 为 true。 +var occupied_word := "_" ## Block 上的字符,若没有字符则为 _ 。 +var occupied_card: Card = null + +var quest_pos := -1 ## 在表达式中对应字符位置的下标。 +var is_shaking := false ## 是否正在振动。 -var quest_pos := -1 -var is_shaking := false func _on_area_entered(area: Card): - #prints("Entered", self, name) if not occupied: area.on_card_entered(self) func _on_area_exited(area: Card): - #prints("Exited", self, name) if not occupied: area.on_card_exited() -func set_word(e: String) -> void: - $Word.set_word(e) - if e != "_" and e != ".": +func set_word(value: String) -> void: + $Word.set_word(value) + if value != "_" and value != ".": occupied = true - occupied_word = e + occupied_word = value else: occupied = false occupied_word = "_" @@ -63,15 +62,33 @@ func _process(_delta): $Word.position.x = offset -func shake(): +## 开始震动。 +## is_frame_red 表示是否将框改为红色。 +func shake(is_frame_red: bool) -> void: + # 开启震动 $ShakeTimer.start() is_shaking = true - -func _on_shake_timer_timeout(): + + # 使附着的卡牌也震动 + if occupied_card != null: + occupied_card.shake(not is_frame_red, SHAKE_AMOUNT, $ShakeTimer.wait_time) # 若框不红,则里面的字要红 + elif not is_frame_red: + $Word.set_color_from_name("red") + + if is_frame_red: + $GoalFrameSprite.animation = "red" + +func _on_shake_timer_timeout() -> void: is_shaking = false + $GoalFrameSprite.animation = "default" + $Word.set_color_from_name("default") -func set_victory(v: bool): - if occupied and occupied_card: +func set_victory(v: bool) -> void: + if v and occupied_card != null: occupied_card.set_victory(v) + +func set_color_from_name(name: String) -> void: + if occupied_card != null: + occupied_card.set_color_from_name(name) else: - $Word.set_victory(v) + $Word.set_color_from_name("golden") diff --git a/objects/block/block.tscn b/objects/block/block.tscn index 75f677a..89f6f66 100644 --- a/objects/block/block.tscn +++ b/objects/block/block.tscn @@ -1,13 +1,33 @@ -[gd_scene load_steps=6 format=3 uid="uid://ckjmjwywh78fe"] +[gd_scene load_steps=8 format=3 uid="uid://ckjmjwywh78fe"] [ext_resource type="Script" path="res://objects/block/block.gd" id="1_11h57"] [ext_resource type="PackedScene" uid="uid://cvx7wowcbfo0r" path="res://objects/word/word.tscn" id="2_pjs40"] [ext_resource type="Texture2D" uid="uid://dvj7bjepxeks0" path="res://objects/block/pit.png" id="3_ye3hk"] -[ext_resource type="Texture2D" uid="uid://cb71r50jw2a3i" path="res://objects/block/goal_frame2.png" id="4_v6rgm"] +[ext_resource type="Texture2D" uid="uid://cb71r50jw2a3i" path="res://objects/block/goal_frame.png" id="4_yvtjk"] +[ext_resource type="Texture2D" uid="uid://cvv532ui6p5gg" path="res://objects/block/goal_frame_red.png" id="5_et5vw"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_3if4e"] size = Vector2(16, 16) +[sub_resource type="SpriteFrames" id="SpriteFrames_3dme2"] +animations = [{ +"frames": [{ +"duration": 1.0, +"texture": ExtResource("4_yvtjk") +}], +"loop": true, +"name": &"default", +"speed": 5.0 +}, { +"frames": [{ +"duration": 1.0, +"texture": ExtResource("5_et5vw") +}], +"loop": true, +"name": &"red", +"speed": 5.0 +}] + [node name="Block" type="Area2D"] script = ExtResource("1_11h57") @@ -22,11 +42,11 @@ debug_color = Color(0.968627, 0, 0.470588, 0.419608) z_index = -1 texture = ExtResource("3_ye3hk") -[node name="GoalFrameSprite" type="Sprite2D" parent="."] -texture = ExtResource("4_v6rgm") - [node name="ShakeTimer" type="Timer" parent="."] -wait_time = 0.1 +wait_time = 0.2 + +[node name="GoalFrameSprite" type="AnimatedSprite2D" parent="."] +sprite_frames = SubResource("SpriteFrames_3dme2") [connection signal="area_entered" from="." to="." method="_on_area_entered"] [connection signal="area_exited" from="." to="." method="_on_area_exited"] diff --git a/objects/card/card.gd b/objects/card/card.gd index b3676ec..f9e8b64 100644 --- a/objects/card/card.gd +++ b/objects/card/card.gd @@ -16,7 +16,11 @@ var last_occupied_area: Block var is_card_base_entered = 0 var entered_card_base_global_position: Vector2 -var is_victory = false +var is_victory := false + +var shake_amount : float +var is_shaking := false + func _ready(): origin_global_position = global_position @@ -81,13 +85,19 @@ func _input_event(viewport: Object, event: InputEvent, shape_idx: int) -> void: reset_position() func _process(delta: float) -> void: - #prints(name, global_position) if not have_deal_on_mouse_release and not Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT): _on_mouse_release() if is_dragging: have_deal_on_mouse_release = false global_position = get_global_mouse_position().round() Input.set_default_cursor_shape(Input.CURSOR_DRAG) + + var offset := 0 + if is_shaking: + var progress = $ShakeTimer.time_left / $ShakeTimer.wait_time * 2 * PI + offset = int(sin(progress) * shake_amount) + $CardBackSprite.position.x = offset + $Word.position.x = offset func on_card_entered(area: Block) -> void: is_card_entered += 1 @@ -113,6 +123,20 @@ func get_word() -> String: return $Word.get_word() +func shake(is_letter_red: bool, amount: float, duration: float) -> void: + $ShakeTimer.wait_time = duration + shake_amount = amount + + # 开启震动 + $ShakeTimer.start() + is_shaking = true + + + if is_letter_red: + $Word.set_color_from_name("red") + else: + $CardBackSprite.animation = "default" + func _on_mouse_entered(): @@ -123,5 +147,18 @@ func _on_mouse_exited(): $CardBackSprite.animation = "default" Input.set_default_cursor_shape(Input.CURSOR_ARROW) +func set_color_from_name(name: String) -> void: + $Word.set_color_from_name(name) + +func set_color(value: Color) -> void: + $Word.set_color(value) + + func set_victory(v: bool): - $Word.set_victory(v) + if v: + $CollisionShape2D.set_deferred("disabled", true) + + +func _on_shake_timer_timeout(): + is_shaking = false + $Word.set_color_from_name("default") diff --git a/objects/card/card.tscn b/objects/card/card.tscn index 0c0571d..912f83d 100644 --- a/objects/card/card.tscn +++ b/objects/card/card.tscn @@ -55,5 +55,10 @@ autoplay = "default" stream = ExtResource("7_u4ylf") volume_db = 13.117 +[node name="ShakeTimer" type="Timer" parent="."] +wait_time = 100.0 +one_shot = true + [connection signal="mouse_entered" from="." to="." method="_on_mouse_entered"] [connection signal="mouse_exited" from="." to="." method="_on_mouse_exited"] +[connection signal="timeout" from="ShakeTimer" to="." method="_on_shake_timer_timeout"]