godot-start/script/demo05_scene_node/hero.gd

91 lines
2.0 KiB
GDScript
Raw Permalink Normal View History

2021-10-03 01:02:45 +00:00
extends Sprite
2022-01-22 06:05:17 +00:00
const StringUtils = preload("res://zfoo/util/StringUtils.gd")
2021-10-03 01:02:45 +00:00
var gameWidth: int
var gameHeight: int
var spriteWidth: int
var spriteHeight: int
2021-10-03 14:43:19 +00:00
func _enter_tree():
2022-01-22 06:05:17 +00:00
windowPositionTest()
2021-10-04 08:49:12 +00:00
# positionTest()
2022-01-22 11:12:49 +00:00
# textureTest()
2021-10-04 08:49:12 +00:00
pass
2022-01-22 11:36:15 +00:00
func _physics_process(delta):
setupGameWindow()
setupSprite()
positionTopCenter()
pass
2022-01-22 06:05:17 +00:00
# 屏幕坐标系
func windowPositionTest():
2022-01-22 11:12:49 +00:00
# 设置游戏屏幕的位置
2022-01-22 11:36:15 +00:00
# OS.window_position = Vector2(100, 100)
2022-01-22 06:05:17 +00:00
# OS.window_fullscreen = true
2022-01-22 11:36:15 +00:00
print(StringUtils.format("屏幕大小[{}]", [OS.window_size]))
print(StringUtils.format("屏幕位置[{}]", [OS.window_position]))
2022-01-22 06:05:17 +00:00
pass
2021-10-04 08:49:12 +00:00
# 坐标点测试用例
func positionTest():
print(position)
print(global_position)
print(to_global(position))
print(to_local(global_position))
pass
2022-01-22 11:36:15 +00:00
func setupGameWindow():
gameWidth = OS.window_size.x
gameHeight = OS.window_size.y
2021-10-03 01:02:45 +00:00
func setupSprite():
spriteWidth = texture.get_width()
spriteHeight = texture.get_height()
func positionTopCenter() -> void:
2022-01-22 11:36:15 +00:00
#get_parent().position.x = 0
#get_parent().position.y = 0
#self.global_position
2021-10-03 01:02:45 +00:00
self.position.x = gameWidth / 2
self.position.y = spriteHeight / 2
func positionCenter() -> void:
2022-01-22 11:36:15 +00:00
self.global_position.x = gameWidth / 2
self.global_position.y = gameHeight / 2
2021-10-03 01:02:45 +00:00
func positionBottomCenter() -> void:
2022-01-22 11:36:15 +00:00
self.global_position.x = gameWidth / 2
self.global_position.y = gameHeight - spriteHeight / 2
2021-10-03 01:02:45 +00:00
func positionLeftCenter() -> void:
2022-01-22 11:36:15 +00:00
self.global_position.x = spriteWidth / 2
self.global_position.y = gameHeight / 2
2022-01-22 11:12:49 +00:00
# 纹理测试用例
func textureTest():
var texture = get_texture()
var image = texture.get_data()
print(offset)
print("texture type: ", texture)
print("height: ", texture.get_height())
print("width: ", texture.get_width())
print("Image data property: ", image.data)
# 无法直接编辑图片
image.shrink_x2()
print("new image data property",image.data)
#这样做会破坏项目的资源,使资源变得混乱
image.save_png("C:\\Users\\jaysunxiao\\Desktop\\newImage.png")
pass