Skip to content

Commit

Permalink
feat(word.gd): 对 Word 进行重要修改
Browse files Browse the repository at this point in the history
* 删除 Word 的 is_victory 属性
* 添加 Word 的 color 属性,使其可以任意更改颜色
* 将 Word 的 text_id 由“从 1 开始”改为“从 0 开始”
  • Loading branch information
cutekibry committed Feb 8, 2024
1 parent aa5a627 commit 1e41c38
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions objects/word/word.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extends AnimatedSprite2D


@export var text_id := 1
@export var is_victory = false
@export var color := Color.BLACK


const STEP := 30
Expand All @@ -18,60 +18,80 @@ const LETTER_NAME = ["_", "=", "P", "b", "D",
func get_letter_id(letter: String) -> int:
for i in range(len(LETTER_NAME)):
if LETTER_NAME[i] == letter:
return i + 1
return i
assert(false, "Letter " + letter + " not found")
return -1


## 内建,不要用。
func set_text_id(value: int) -> void:
assert(value >= 0, "text id < 0")
text_id = value

assert(value > 0, "text id <= 0")
var animation_id = str(text_id) + str(is_victory)
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, is_victory))
sprite_frames.add_frame(animation_id, load_image(i * STEP + text_id + 1, color))
animation = animation_id



## 更改字母为 text。
func set_word(text: String) -> void:
if text == "_" or text == ".":
set_text_id(1)
set_text_id(0)
else:
set_text_id(get_letter_id(text))

## 获得当前字母。
func get_word() -> String:
return LETTER_NAME[text_id - 1]
return LETTER_NAME[text_id]


func _ready():
set_text_id(text_id)
update_animation()



func _process(delta):
if animation == "default":
set_text_id(text_id)
update_animation()

func set_victory(v: bool):
if v != is_victory:
is_victory = v
set_text_id(text_id)
func set_color_from_name(name: String) -> void:
if name == "red":
set_color(Color("#DD4132"))
elif name == "golden":
set_color(Color("#F5DF4D"))
elif name == "default":
set_color(Color.BLACK)
else:
assert(false, "color %s not in list" % name)

func set_color(value: Color) -> void:
color = value
update_animation()

## 设置是否为关卡通过状态。
## 已弃用,请使用 set_color 或 set_color_from_name 代替。
## @deprecated
func set_victory(v: bool) -> void:
push_warning("void set_victory(v: bool) is deprecated.")
if v:
set_color_from_name("golden")
else:
set_color_from_name("default")

func load_image(h: int, is_victory: bool):
func load_image(h: int, new_color: Color):
var image := load("res://objects/word/sprites/sprite" + str(h) + ".png")
if is_victory:
if new_color != Color.BLACK:
var new_texture = image.get_image()
for x in range(new_texture.get_width()):
for y in range(new_texture.get_height()):
var color = new_texture.get_pixel(x, y)
if color == Color(0, 0, 0, 1): # 如果像素是黑色
new_texture.set_pixel(x, y, Color.hex(0xf5df4dff)) # 将其改为金黄色
if color == Color.BLACK: # 如果像素是黑色
new_texture.set_pixel(x, y, new_color) # 将其改为金黄色
return ImageTexture.create_from_image(new_texture)

return image

0 comments on commit 1e41c38

Please sign in to comment.