Skip to content

Commit d2cd1d8

Browse files
authored
Added "LPCBase" class with common functions from existing 2D and new 3D main class (#37)
* Added "LPCBase" class with common functions from existing 2D and new 3D main classes * Bug fixes and applying the changes to the example project * Replacing some removed functionality
1 parent 3f0261a commit d2cd1d8

File tree

4 files changed

+11315
-11301
lines changed

4 files changed

+11315
-11301
lines changed
Lines changed: 20 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,38 @@
11
@tool
2-
extends Node
2+
class_name LPCAnimatedSprite2D extends Node2D
33

4-
class_name LPCAnimatedSprite2D
54

65
@export var SpriteSheets:Array[LPCSpriteSheet]
76
@export var DefaultAnimation:LPCEnum.LPCAnimation = LPCEnum.LPCAnimation.IDLE_DOWN
8-
@export var NodeType:LPCEnum.ESpriteNodeType = LPCEnum.ESpriteNodeType.Sprite_2D
97

10-
@export_group("2D Properties")
118
@export var Sprite2DTextureFilter:CanvasItem.TextureFilter = CanvasItem.TEXTURE_FILTER_NEAREST
129

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
1813

19-
var lastOffset:float = 1.0
20-
21-
var AnimationNames:Array
2214
func _ready():
2315
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)
2526

2627
func play(animation: LPCEnum.LPCAnimation, fps: float = 5.0):
2728
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)
3530

3631
func _notification(what):
3732
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")
8834

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
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
@tool
2+
class_name LPCAnimatedSprite3D extends Node3D
3+
4+
@export var SpriteSheets:Array[LPCSpriteSheet]
5+
@export var DefaultAnimation:LPCEnum.LPCAnimation = LPCEnum.LPCAnimation.IDLE_DOWN
6+
7+
const DEFAULT_3D_PIXEL_SIZE:float = 0.01
8+
@export var Sprite3DScale:float = 1
9+
@export var Sprite3DBillboard:BaseMaterial3D.BillboardMode = BaseMaterial3D.BILLBOARD_DISABLED
10+
@export var Sprite3DTextureFilter:BaseMaterial3D.TextureFilter = BaseMaterial3D.TEXTURE_FILTER_NEAREST
11+
12+
var lastOffset:float = 1.0
13+
var AnimationNames:Array
14+
var LPC_base : LPCBase
15+
16+
func _ready():
17+
if Engine.is_editor_hint() == false:
18+
_instantiate()
19+
20+
func _enter_tree():
21+
if Engine.is_editor_hint():
22+
_instantiate()
23+
24+
func _instantiate() -> void :
25+
if not LPC_base:
26+
LPC_base = LPCBase.new()
27+
LPC_base.LoadAnimations(self)
28+
29+
func play(animation: LPCEnum.LPCAnimation, fps: float = 5.0):
30+
var sprites = get_children()
31+
LPC_base.play(animation, sprites, AnimationNames, fps)
32+
33+
func _notification(what):
34+
if what == NOTIFICATION_EDITOR_POST_SAVE:
35+
call_deferred("_instantiate")
36+
37+
func CreateAnimatedSprite():
38+
var animatedSprite = AnimatedSprite3D.new()
39+
animatedSprite.pixel_size = Sprite3DScale * DEFAULT_3D_PIXEL_SIZE
40+
animatedSprite.texture_filter = Sprite3DTextureFilter
41+
animatedSprite.billboard = Sprite3DBillboard
42+
animatedSprite.sorting_offset = lastOffset
43+
lastOffset += 1.0
44+
return animatedSprite
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class_name LPCBase extends Node
2+
3+
func play(animation: LPCEnum.LPCAnimation, sprites: Array, animation_names: Array, fps: float = 5.0) -> void:
4+
for sprite in sprites:
5+
if sprite.sprite_frames.has_animation(animation_names[animation]):
6+
sprite.visible = true
7+
sprite.sprite_frames.set_animation_speed(animation_names[animation], fps)
8+
sprite.play(animation_names[animation])
9+
else:
10+
sprite.visible = false
11+
12+
func LoadAnimations(main_node : Node) -> void:
13+
main_node.AnimationNames = LPCEnum.LPCAnimation.keys()
14+
var children = main_node.get_children();
15+
for child in children:
16+
main_node.remove_child(child)
17+
18+
for spriteSheet in main_node.SpriteSheets:
19+
if spriteSheet == null:
20+
push_warning("There are LPCSpriteSheets that are <empty> in the LPCAnimatedSprite2D panel")
21+
continue
22+
23+
var animatedSprite = main_node.CreateAnimatedSprite()
24+
var spriteFrames = CreateSpritesFrames(spriteSheet)
25+
animatedSprite.frames = spriteFrames
26+
main_node.add_child(animatedSprite)
27+
if spriteSheet.Name == null || spriteSheet.Name == "":
28+
animatedSprite.name = "no_name"
29+
else:
30+
animatedSprite.name = spriteSheet.Name
31+
animatedSprite.owner = main_node.get_tree().edited_scene_root
32+
main_node.play(main_node.DefaultAnimation)
33+
34+
func CreateSpritesFrames(spriteSheet:LPCSpriteSheet):
35+
var spriteFrames = SpriteFrames.new()
36+
spriteFrames.remove_animation("default")
37+
38+
for animationData in spriteSheet.AnimationData():
39+
AddAnimation(spriteSheet, spriteFrames, animationData)
40+
return spriteFrames
41+
42+
func AddAnimation(spriteSheet:LPCSpriteSheet, spriteFrames:SpriteFrames, animationData:LPCAnimationData):
43+
if spriteSheet == null || spriteSheet.SpriteSheet == null:
44+
return
45+
46+
if spriteFrames.has_animation(animationData.Name):
47+
spriteFrames.remove_animation(animationData.Name)
48+
49+
spriteFrames.add_animation(animationData.Name)
50+
var frameStart = animationData.FrameCount -1 if animationData.Reverse else 0
51+
var frameEnd = -1 if animationData.Reverse else animationData.FrameCount
52+
var reversed = -1 if animationData.Reverse else 1
53+
for frame in range(frameStart, frameEnd , reversed):
54+
var atlasTexture = AtlasTexture.new()
55+
atlasTexture.atlas = spriteSheet.SpriteSheet
56+
atlasTexture.region = spriteSheet.GetSpriteRect(animationData, frame)
57+
spriteFrames.add_frame(animationData.Name, atlasTexture, 0.5)
58+
spriteFrames.set_animation_loop(animationData.Name, animationData.Loop)
59+
return spriteFrames

0 commit comments

Comments
 (0)