-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
4 changed files
with
11,315 additions
and
11,301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,38 @@ | ||
@tool | ||
extends Node | ||
class_name LPCAnimatedSprite2D extends Node2D | ||
|
||
class_name LPCAnimatedSprite2D | ||
|
||
@export var SpriteSheets:Array[LPCSpriteSheet] | ||
@export var DefaultAnimation:LPCEnum.LPCAnimation = LPCEnum.LPCAnimation.IDLE_DOWN | ||
@export var NodeType:LPCEnum.ESpriteNodeType = LPCEnum.ESpriteNodeType.Sprite_2D | ||
|
||
@export_group("2D Properties") | ||
@export var Sprite2DTextureFilter:CanvasItem.TextureFilter = CanvasItem.TEXTURE_FILTER_NEAREST | ||
|
||
@export_group("3D Properties") | ||
const DEFAULT_3D_PIXEL_SIZE:float = 0.01 | ||
@export var Sprite3DScale:float = 1 | ||
@export var Sprite3DBillboard:BaseMaterial3D.BillboardMode = BaseMaterial3D.BILLBOARD_DISABLED | ||
@export var Sprite3DTextureFilter:BaseMaterial3D.TextureFilter = BaseMaterial3D.TEXTURE_FILTER_NEAREST | ||
var lastOffset : float = 1.0 | ||
var AnimationNames : Array | ||
var LPC_base : LPCBase | ||
|
||
var lastOffset:float = 1.0 | ||
|
||
var AnimationNames:Array | ||
func _ready(): | ||
if Engine.is_editor_hint() == false: | ||
LoadAnimations() | ||
_instantiate() | ||
|
||
func _enter_tree(): | ||
if Engine.is_editor_hint(): | ||
_instantiate() | ||
|
||
func _instantiate() -> void : | ||
if not LPC_base: | ||
LPC_base = LPCBase.new() | ||
LPC_base.LoadAnimations(self) | ||
|
||
func play(animation: LPCEnum.LPCAnimation, fps: float = 5.0): | ||
var sprites = get_children() | ||
for sprite in sprites: | ||
if sprite.sprite_frames.has_animation(AnimationNames[animation]): | ||
sprite.visible = true | ||
sprite.sprite_frames.set_animation_speed(AnimationNames[animation], fps) | ||
sprite.play(AnimationNames[animation]) | ||
else: | ||
sprite.visible = false | ||
LPC_base.play(animation, sprites, AnimationNames, fps) | ||
|
||
func _notification(what): | ||
if what == NOTIFICATION_EDITOR_POST_SAVE: | ||
call_deferred("LoadAnimations") | ||
|
||
func _enter_tree(): | ||
if Engine.is_editor_hint(): | ||
LoadAnimations() | ||
|
||
func LoadAnimations(): | ||
AnimationNames = LPCEnum.LPCAnimation.keys() | ||
var children = get_children(); | ||
for child in children: | ||
remove_child(child) | ||
|
||
for spriteSheet in SpriteSheets: | ||
if spriteSheet == null: | ||
push_warning("There are LPCSpriteSheets that are <empty> in the LPCAnimatedSprite2D panel") | ||
continue | ||
|
||
var animatedSprite = CreateAnimatedSprite() | ||
var spriteFrames = CreateSpritesFrames(spriteSheet) | ||
animatedSprite.frames = spriteFrames | ||
add_child(animatedSprite) | ||
if spriteSheet.Name == null || spriteSheet.Name == "": | ||
animatedSprite.name = "no_name" | ||
else: | ||
animatedSprite.name = spriteSheet.Name | ||
animatedSprite.owner = get_tree().edited_scene_root | ||
play(DefaultAnimation) | ||
|
||
func CreateAnimatedSprite(): | ||
match NodeType: | ||
LPCEnum.ESpriteNodeType.Sprite_3D: | ||
var animatedSprite = AnimatedSprite3D.new() | ||
animatedSprite.pixel_size = Sprite3DScale * DEFAULT_3D_PIXEL_SIZE | ||
animatedSprite.texture_filter = Sprite3DTextureFilter | ||
animatedSprite.billboard = Sprite3DBillboard | ||
animatedSprite.sorting_offset = lastOffset | ||
lastOffset += 1.0 | ||
return animatedSprite | ||
LPCEnum.ESpriteNodeType.Sprite_2D: | ||
var animatedSprite = AnimatedSprite2D.new() | ||
animatedSprite.texture_filter = Sprite2DTextureFilter | ||
return animatedSprite | ||
|
||
func CreateSpritesFrames(spriteSheet:LPCSpriteSheet): | ||
var spriteFrames = SpriteFrames.new() | ||
spriteFrames.remove_animation("default") | ||
|
||
for animationData in spriteSheet.AnimationData(): | ||
AddAnimation(spriteSheet, spriteFrames, animationData) | ||
return spriteFrames | ||
call_deferred("_instantiate") | ||
|
||
func AddAnimation(spriteSheet:LPCSpriteSheet, spriteFrames:SpriteFrames, animationData:LPCAnimationData): | ||
if spriteSheet == null || spriteSheet.SpriteSheet == null: | ||
return | ||
|
||
if spriteFrames.has_animation(animationData.Name): | ||
spriteFrames.remove_animation(animationData.Name) | ||
|
||
spriteFrames.add_animation(animationData.Name) | ||
var frameStart = animationData.FrameCount -1 if animationData.Reverse else 0 | ||
var frameEnd = -1 if animationData.Reverse else animationData.FrameCount | ||
var reversed = -1 if animationData.Reverse else 1 | ||
for frame in range(frameStart, frameEnd , reversed): | ||
var atlasTexture = AtlasTexture.new() | ||
atlasTexture.atlas = spriteSheet.SpriteSheet | ||
atlasTexture.region = spriteSheet.GetSpriteRect(animationData, frame) | ||
spriteFrames.add_frame(animationData.Name, atlasTexture, 0.5) | ||
spriteFrames.set_animation_loop(animationData.Name, animationData.Loop) | ||
return spriteFrames | ||
func CreateAnimatedSprite(): | ||
var animatedSprite = AnimatedSprite2D.new() | ||
animatedSprite.texture_filter = Sprite2DTextureFilter | ||
return animatedSprite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
@tool | ||
class_name LPCAnimatedSprite3D extends Node3D | ||
|
||
@export var SpriteSheets:Array[LPCSpriteSheet] | ||
@export var DefaultAnimation:LPCEnum.LPCAnimation = LPCEnum.LPCAnimation.IDLE_DOWN | ||
|
||
const DEFAULT_3D_PIXEL_SIZE:float = 0.01 | ||
@export var Sprite3DScale:float = 1 | ||
@export var Sprite3DBillboard:BaseMaterial3D.BillboardMode = BaseMaterial3D.BILLBOARD_DISABLED | ||
@export var Sprite3DTextureFilter:BaseMaterial3D.TextureFilter = BaseMaterial3D.TEXTURE_FILTER_NEAREST | ||
|
||
var lastOffset:float = 1.0 | ||
var AnimationNames:Array | ||
var LPC_base : LPCBase | ||
|
||
func _ready(): | ||
if Engine.is_editor_hint() == false: | ||
_instantiate() | ||
|
||
func _enter_tree(): | ||
if Engine.is_editor_hint(): | ||
_instantiate() | ||
|
||
func _instantiate() -> void : | ||
if not LPC_base: | ||
LPC_base = LPCBase.new() | ||
LPC_base.LoadAnimations(self) | ||
|
||
func play(animation: LPCEnum.LPCAnimation, fps: float = 5.0): | ||
var sprites = get_children() | ||
LPC_base.play(animation, sprites, AnimationNames, fps) | ||
|
||
func _notification(what): | ||
if what == NOTIFICATION_EDITOR_POST_SAVE: | ||
call_deferred("_instantiate") | ||
|
||
func CreateAnimatedSprite(): | ||
var animatedSprite = AnimatedSprite3D.new() | ||
animatedSprite.pixel_size = Sprite3DScale * DEFAULT_3D_PIXEL_SIZE | ||
animatedSprite.texture_filter = Sprite3DTextureFilter | ||
animatedSprite.billboard = Sprite3DBillboard | ||
animatedSprite.sorting_offset = lastOffset | ||
lastOffset += 1.0 | ||
return animatedSprite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
class_name LPCBase extends Node | ||
|
||
func play(animation: LPCEnum.LPCAnimation, sprites: Array, animation_names: Array, fps: float = 5.0) -> void: | ||
for sprite in sprites: | ||
if sprite.sprite_frames.has_animation(animation_names[animation]): | ||
sprite.visible = true | ||
sprite.sprite_frames.set_animation_speed(animation_names[animation], fps) | ||
sprite.play(animation_names[animation]) | ||
else: | ||
sprite.visible = false | ||
|
||
func LoadAnimations(main_node : Node) -> void: | ||
main_node.AnimationNames = LPCEnum.LPCAnimation.keys() | ||
var children = main_node.get_children(); | ||
for child in children: | ||
main_node.remove_child(child) | ||
|
||
for spriteSheet in main_node.SpriteSheets: | ||
if spriteSheet == null: | ||
push_warning("There are LPCSpriteSheets that are <empty> in the LPCAnimatedSprite2D panel") | ||
continue | ||
|
||
var animatedSprite = main_node.CreateAnimatedSprite() | ||
var spriteFrames = CreateSpritesFrames(spriteSheet) | ||
animatedSprite.frames = spriteFrames | ||
main_node.add_child(animatedSprite) | ||
if spriteSheet.Name == null || spriteSheet.Name == "": | ||
animatedSprite.name = "no_name" | ||
else: | ||
animatedSprite.name = spriteSheet.Name | ||
animatedSprite.owner = main_node.get_tree().edited_scene_root | ||
main_node.play(main_node.DefaultAnimation) | ||
|
||
func CreateSpritesFrames(spriteSheet:LPCSpriteSheet): | ||
var spriteFrames = SpriteFrames.new() | ||
spriteFrames.remove_animation("default") | ||
|
||
for animationData in spriteSheet.AnimationData(): | ||
AddAnimation(spriteSheet, spriteFrames, animationData) | ||
return spriteFrames | ||
|
||
func AddAnimation(spriteSheet:LPCSpriteSheet, spriteFrames:SpriteFrames, animationData:LPCAnimationData): | ||
if spriteSheet == null || spriteSheet.SpriteSheet == null: | ||
return | ||
|
||
if spriteFrames.has_animation(animationData.Name): | ||
spriteFrames.remove_animation(animationData.Name) | ||
|
||
spriteFrames.add_animation(animationData.Name) | ||
var frameStart = animationData.FrameCount -1 if animationData.Reverse else 0 | ||
var frameEnd = -1 if animationData.Reverse else animationData.FrameCount | ||
var reversed = -1 if animationData.Reverse else 1 | ||
for frame in range(frameStart, frameEnd , reversed): | ||
var atlasTexture = AtlasTexture.new() | ||
atlasTexture.atlas = spriteSheet.SpriteSheet | ||
atlasTexture.region = spriteSheet.GetSpriteRect(animationData, frame) | ||
spriteFrames.add_frame(animationData.Name, atlasTexture, 0.5) | ||
spriteFrames.set_animation_loop(animationData.Name, animationData.Loop) | ||
return spriteFrames |
Oops, something went wrong.