|
1 | 1 | @tool |
2 | | -extends Node |
| 2 | +class_name LPCAnimatedSprite2D extends Node2D |
3 | 3 |
|
4 | | -class_name LPCAnimatedSprite2D |
5 | 4 |
|
6 | 5 | @export var SpriteSheets:Array[LPCSpriteSheet] |
7 | 6 | @export var DefaultAnimation:LPCEnum.LPCAnimation = LPCEnum.LPCAnimation.IDLE_DOWN |
8 | | -@export var NodeType:LPCEnum.ESpriteNodeType = LPCEnum.ESpriteNodeType.Sprite_2D |
9 | 7 |
|
10 | | -@export_group("2D Properties") |
11 | 8 | @export var Sprite2DTextureFilter:CanvasItem.TextureFilter = CanvasItem.TEXTURE_FILTER_NEAREST |
12 | 9 |
|
13 | | -@export_group("3D Properties") |
14 | | -const DEFAULT_3D_PIXEL_SIZE:float = 0.01 |
15 | | -@export var Sprite3DScale:float = 1 |
16 | | -@export var Sprite3DBillboard:BaseMaterial3D.BillboardMode = BaseMaterial3D.BILLBOARD_DISABLED |
17 | | -@export var Sprite3DTextureFilter:BaseMaterial3D.TextureFilter = BaseMaterial3D.TEXTURE_FILTER_NEAREST |
| 10 | +var lastOffset : float = 1.0 |
| 11 | +var AnimationNames : Array |
| 12 | +var LPC_base : LPCBase |
18 | 13 |
|
19 | | -var lastOffset:float = 1.0 |
20 | | - |
21 | | -var AnimationNames:Array |
22 | 14 | func _ready(): |
23 | 15 | if Engine.is_editor_hint() == false: |
24 | | - LoadAnimations() |
| 16 | + _instantiate() |
| 17 | + |
| 18 | +func _enter_tree(): |
| 19 | + if Engine.is_editor_hint(): |
| 20 | + _instantiate() |
| 21 | + |
| 22 | +func _instantiate() -> void : |
| 23 | + if not LPC_base: |
| 24 | + LPC_base = LPCBase.new() |
| 25 | + LPC_base.LoadAnimations(self) |
25 | 26 |
|
26 | 27 | func play(animation: LPCEnum.LPCAnimation, fps: float = 5.0): |
27 | 28 | var sprites = get_children() |
28 | | - for sprite in sprites: |
29 | | - if sprite.sprite_frames.has_animation(AnimationNames[animation]): |
30 | | - sprite.visible = true |
31 | | - sprite.sprite_frames.set_animation_speed(AnimationNames[animation], fps) |
32 | | - sprite.play(AnimationNames[animation]) |
33 | | - else: |
34 | | - sprite.visible = false |
| 29 | + LPC_base.play(animation, sprites, AnimationNames, fps) |
35 | 30 |
|
36 | 31 | func _notification(what): |
37 | 32 | if what == NOTIFICATION_EDITOR_POST_SAVE: |
38 | | - call_deferred("LoadAnimations") |
39 | | - |
40 | | -func _enter_tree(): |
41 | | - if Engine.is_editor_hint(): |
42 | | - LoadAnimations() |
43 | | - |
44 | | -func LoadAnimations(): |
45 | | - AnimationNames = LPCEnum.LPCAnimation.keys() |
46 | | - var children = get_children(); |
47 | | - for child in children: |
48 | | - remove_child(child) |
49 | | - |
50 | | - for spriteSheet in SpriteSheets: |
51 | | - if spriteSheet == null: |
52 | | - push_warning("There are LPCSpriteSheets that are <empty> in the LPCAnimatedSprite2D panel") |
53 | | - continue |
54 | | - |
55 | | - var animatedSprite = CreateAnimatedSprite() |
56 | | - var spriteFrames = CreateSpritesFrames(spriteSheet) |
57 | | - animatedSprite.frames = spriteFrames |
58 | | - add_child(animatedSprite) |
59 | | - if spriteSheet.Name == null || spriteSheet.Name == "": |
60 | | - animatedSprite.name = "no_name" |
61 | | - else: |
62 | | - animatedSprite.name = spriteSheet.Name |
63 | | - animatedSprite.owner = get_tree().edited_scene_root |
64 | | - play(DefaultAnimation) |
65 | | - |
66 | | -func CreateAnimatedSprite(): |
67 | | - match NodeType: |
68 | | - LPCEnum.ESpriteNodeType.Sprite_3D: |
69 | | - var animatedSprite = AnimatedSprite3D.new() |
70 | | - animatedSprite.pixel_size = Sprite3DScale * DEFAULT_3D_PIXEL_SIZE |
71 | | - animatedSprite.texture_filter = Sprite3DTextureFilter |
72 | | - animatedSprite.billboard = Sprite3DBillboard |
73 | | - animatedSprite.sorting_offset = lastOffset |
74 | | - lastOffset += 1.0 |
75 | | - return animatedSprite |
76 | | - LPCEnum.ESpriteNodeType.Sprite_2D: |
77 | | - var animatedSprite = AnimatedSprite2D.new() |
78 | | - animatedSprite.texture_filter = Sprite2DTextureFilter |
79 | | - return animatedSprite |
80 | | - |
81 | | -func CreateSpritesFrames(spriteSheet:LPCSpriteSheet): |
82 | | - var spriteFrames = SpriteFrames.new() |
83 | | - spriteFrames.remove_animation("default") |
84 | | - |
85 | | - for animationData in spriteSheet.AnimationData(): |
86 | | - AddAnimation(spriteSheet, spriteFrames, animationData) |
87 | | - return spriteFrames |
| 33 | + call_deferred("_instantiate") |
88 | 34 |
|
89 | | -func AddAnimation(spriteSheet:LPCSpriteSheet, spriteFrames:SpriteFrames, animationData:LPCAnimationData): |
90 | | - if spriteSheet == null || spriteSheet.SpriteSheet == null: |
91 | | - return |
92 | | - |
93 | | - if spriteFrames.has_animation(animationData.Name): |
94 | | - spriteFrames.remove_animation(animationData.Name) |
95 | | - |
96 | | - spriteFrames.add_animation(animationData.Name) |
97 | | - var frameStart = animationData.FrameCount -1 if animationData.Reverse else 0 |
98 | | - var frameEnd = -1 if animationData.Reverse else animationData.FrameCount |
99 | | - var reversed = -1 if animationData.Reverse else 1 |
100 | | - for frame in range(frameStart, frameEnd , reversed): |
101 | | - var atlasTexture = AtlasTexture.new() |
102 | | - atlasTexture.atlas = spriteSheet.SpriteSheet |
103 | | - atlasTexture.region = spriteSheet.GetSpriteRect(animationData, frame) |
104 | | - spriteFrames.add_frame(animationData.Name, atlasTexture, 0.5) |
105 | | - spriteFrames.set_animation_loop(animationData.Name, animationData.Loop) |
106 | | - return spriteFrames |
| 35 | +func CreateAnimatedSprite(): |
| 36 | + var animatedSprite = AnimatedSprite2D.new() |
| 37 | + animatedSprite.texture_filter = Sprite2DTextureFilter |
| 38 | + return animatedSprite |
0 commit comments