Skip to content

Commit

Permalink
feat(block, card): 添加震动、改变边框颜色、改变文字颜色的功能
Browse files Browse the repository at this point in the history
注意:Card 振动的振幅和时长继承自 Block,Card 本身的 ShakeTimer 初始 Wait Time 并不会被用到
  • Loading branch information
cutekibry committed Feb 8, 2024
1 parent 01a3cb9 commit 7be6967
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 27 deletions.
53 changes: 35 additions & 18 deletions objects/block/block.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "_"
Expand Down Expand Up @@ -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")
32 changes: 26 additions & 6 deletions objects/block/block.tscn
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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"]
Expand Down
43 changes: 40 additions & 3 deletions objects/card/card.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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():
Expand All @@ -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")
5 changes: 5 additions & 0 deletions objects/card/card.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

0 comments on commit 7be6967

Please sign in to comment.