Skip to content

Commit

Permalink
Merge pull request #39 from ligen131/refactor-20240213-huge-refactor
Browse files Browse the repository at this point in the history
重大重构
  • Loading branch information
cutekibry authored Feb 13, 2024
2 parents 5a9aa5a + d6fb9ed commit 01e536d
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 247 deletions.
21 changes: 11 additions & 10 deletions levels/base_level/base_level.gd
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,17 @@ func init(_chap_id: int, _lvl_id: int) -> void:

var pos := WIDTH / 2 - sep * len(question) / 2 + 12 + 7
for i in range(len(question)):
var ch = question[i]
var ch: String = question[i]

var new_block = BlockScn.instantiate()
var new_block: Block = BlockScn.instantiate()

new_block.quest_pos = i
if ch != "." and ch != "_":
new_block.occupied = true
new_block.is_fixed = true
new_block.set_word(ch)
new_block.set_block_type("CONST")
else:
new_block.set_word("_")
new_block.is_fixed = false
if ch == "_":
req_pos.append(i)
new_block.set_block_type("GOLDEN")
Expand All @@ -128,14 +128,16 @@ func init(_chap_id: int, _lvl_id: int) -> void:
pos += sep

$Blocks.add_child(new_block)

new_block.occupied_card_changed.connect(_on_block_occupied_card_changed)

expr = question
# print(expr)


pos = WIDTH / 2 - CARDS_SEP * len(choices) / 2 + 16
for ch in choices:
var new_card_base = CardBaseScn.instantiate()
var new_card_base: CardBase = CardBaseScn.instantiate()

new_card_base.set_word(ch)

Expand All @@ -144,7 +146,6 @@ func init(_chap_id: int, _lvl_id: int) -> void:
pos += CARDS_SEP

$CardBases.add_child(new_card_base)
new_card_base.card_put.connect(_on_card_put)


func _ready():
Expand All @@ -164,13 +165,13 @@ func stage_clear() -> void:
$HUDs/NextLevelButton.start_fade()


func _on_card_put() -> void:
func _on_block_occupied_card_changed(_node) -> void:
var block_array = []
for block : Block in $Blocks.get_children():
if not block.occupied:
# print(block.quest_pos, " is not occupied")
if block.is_empty():
#print(block.quest_pos, " is not occupied")
return
expr[block.quest_pos] = block.occupied_word
expr[block.quest_pos] = block.get_word()
block_array.append(block)

# prints("# expr: ", expr)
Expand Down
112 changes: 67 additions & 45 deletions objects/block/block.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,57 @@ extends Area2D
class_name Block


signal occupied_card_changed(block: Block) ## 当被占用的 Card 发生改变后发出。


const SHAKE_AMOUNT := 3.0 ## 振动时的振幅(单位为像素)。


var occupied := false ## 当 Block 上有 Card 或 Block 字符固定时,occupied 为 true。
var occupied_word := "_" ## Block 上的字符,若没有字符则为 _ 。
var occupied_card: Card = null
var is_fixed : bool ## 是否为固定的(即在表达式中已初始化了的)。
var occupied_card: Card = null ## 占用的 Card 对象。

var quest_pos := -1 ## 在表达式中对应字符位置的下标。
var is_shaking := false ## 是否正在振动。


## 是否为空槽。
func is_empty() -> bool:
return not self.is_fixed and self.occupied_card == null

func _on_area_entered(area: Card):
if not occupied:
area.on_card_entered(self)

func _on_area_exited(area: Card):
if not occupied:
area.on_card_exited()

func set_word(value: String) -> void:
## 设置固定的字符为 value。
##
## 应当只在 is_fixed 为 true 时被调用。
func set_word(value: String) -> void:
assert(self.is_fixed, "void Block::set_word(value: String) should only be called when is_fixed is true.")
$Word.set_word(value)
if value != "_" and value != ".":
occupied = true
occupied_word = value
else:
occupied = false
occupied_word = "_"

func set_card(c: Card) -> void:
occupied_card = c
occupied_word = c.get_word()

## 设置占用的 Card 为 card。
##
## 应当只在 is_fixed 为 false 时被调用。
func set_card(card: Card) -> void:
assert(not self.is_fixed, "void Block::set_card(card: Card) should only be called when is_fixed is false.")
if self.occupied_card != card:
self.occupied_card = card
occupied_card_changed.emit(self)


## 获取当前字符。
##
## 当 is_fixed 为 true 时,从子节点 Word 返回固定的字符。
##
## 当 is_fixed 为 false 时,返回占用的 Card 的字符。
func get_word() -> String:
if self.is_fixed:
return $Word.get_word()
else:
if self.occupied_card == null:
return " "
else:
return self.occupied_card.get_word()


func set_block_type(value: String) -> void:
if value == "GOLDEN":
$GoalFrameSprite.set_visible(true)
Expand All @@ -46,49 +64,53 @@ func set_block_type(value: String) -> void:
else:
$GoalFrameSprite.set_visible(false)
$PitSprite.set_visible(false)


func get_word() -> String:
return $Word.get_word()


func set_victory(v: bool) -> void:
if v and not self.is_fixed and self.occupied_card != null:
self.occupied_card.set_victory(v)


## 设置字符颜色为 value。
func set_color(value: Color) -> void:
if self.is_fixed:
$Word.set_color(value)
else:
if self.occupied_card != null:
self.occupied_card.set_color(value)

func _process(_delta):
var offset := 0
if is_shaking:
var progress = $ShakeTimer.time_left / $ShakeTimer.wait_time * 2 * PI
offset = int(sin(progress) * SHAKE_AMOUNT)
$GoalFrameSprite.position.x = offset
$PitSprite.position.x = offset
$Word.position.x = offset


## 开始震动。
## is_frame_red 表示是否将框改为红色。
func shake(is_frame_red: bool) -> void:
# 开启震动
$ShakeTimer.start()
is_shaking = true
self.is_shaking = true

# 使附着的卡牌也震动
if occupied_card != null:
occupied_card.shake(not is_frame_red, SHAKE_AMOUNT, $ShakeTimer.wait_time) # 若框不红,则里面的字要红
if self.occupied_card != null:
self.occupied_card.shake(not is_frame_red, SHAKE_AMOUNT, $ShakeTimer.wait_time) # 若框不红,则里面的字要红
elif not is_frame_red:
$Word.set_color(ImageLib.PALETTE["red"])

if is_frame_red:
$GoalFrameSprite.animation = "red"


func _process(_delta):
var offset := 0
if is_shaking:
var progress = (1 - $ShakeTimer.time_left / $ShakeTimer.wait_time) * 2 * PI
offset = int(sin(progress) * SHAKE_AMOUNT)
$GoalFrameSprite.position.x = offset
$PitSprite.position.x = offset
$Word.position.x = offset



func _on_shake_timer_timeout() -> void:
is_shaking = false
self.is_shaking = false
$GoalFrameSprite.animation = "default"
$Word.set_color(ImageLib.PALETTE["default"])

func set_victory(v: bool) -> void:
if v and occupied_card != null:
occupied_card.set_victory(v)

func set_color(value: Color) -> void:
if occupied_card != null:
occupied_card.set_color(value)
else:
$Word.set_color(value)
2 changes: 1 addition & 1 deletion objects/block/block.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[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)
size = Vector2(22, 22)

[sub_resource type="SpriteFrames" id="SpriteFrames_3dme2"]
animations = [{
Expand Down
Loading

0 comments on commit 01e536d

Please sign in to comment.