Skip to content

Commit abe214d

Browse files
committed
2 parents 7699414 + 548e924 commit abe214d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+9543
-42389
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
export.cfg
77
export_presets.cfg
88
*.import
9+
*.vscode
910

1011
# Imported translations (automatically generated from CSV files)
1112
*.translation
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
@tool
22
extends EditorPlugin
33

4-
54
func _enter_tree():
6-
add_custom_type("LPCAnimatedSprite2D", "Node2D", preload("LPCAnimatedSprite2D.gd"), null)
7-
5+
add_custom_type("LPCAnimatedSprite", "AnimatedSprite2D", preload("LPCAnimatedSprite2D.gd"), preload("icon2d.png"))
86

97
func _exit_tree():
10-
remove_custom_type("LPCAnimatedSprite2D")
8+
remove_custom_type("LPCAnimatedSprite")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://ksojhc2w7qtn
Lines changed: 141 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,154 @@
11
@tool
22
@icon("res://addons/LPCAnimatedSprite/icon2d.png")
3-
class_name LPCAnimatedSprite2D extends Node2D
3+
class_name LPCAnimatedSprite2D extends AnimatedSprite2D
44

5+
var _spritesheets_path: String
6+
var _atlas_cache = {}
57

6-
@export var SpriteSheets:Array[LPCSpriteSheet]
7-
@export var DefaultAnimation:LPCEnum.LPCAnimation = LPCEnum.LPCAnimation.IDLE_DOWN
8+
@export_dir var spritesheets_path: String:
9+
set(value):
10+
if _spritesheets_path != value:
11+
_spritesheets_path = value
12+
_refresh_sprites()
13+
get:
14+
return _spritesheets_path
815

9-
@export var Sprite2DTextureFilter:CanvasItem.TextureFilter = CanvasItem.TEXTURE_FILTER_NEAREST
16+
@export var animation_data: LPCAnimationDataBase:
17+
set(value):
18+
animation_data = value
19+
_refresh_sprites()
20+
notify_property_list_changed()
21+
get:
22+
return animation_data
1023

11-
var lastOffset : float = 1.0
12-
var AnimationNames : Array
13-
var LPC_base : LPCBase
24+
var animation_textures = {}
25+
var current_animation: String = ""
26+
var direction: String = "south"
1427

15-
func _ready():
16-
if Engine.is_editor_hint() == false:
17-
_instantiate()
28+
func _refresh_sprites():
29+
if not animation_data:
30+
return
31+
_setup_animation_properties()
32+
_load_spritesheets()
33+
_setup_sprite_frames()
34+
35+
func _load_spritesheets():
36+
_clear_unused_textures()
37+
_load_required_textures()
38+
39+
func _clear_unused_textures():
40+
for anim_name in animation_textures.keys():
41+
if not anim_name in animation_data.available_animations:
42+
animation_textures.erase(anim_name)
43+
44+
func _load_required_textures():
45+
for anim_name in animation_data.required_spritesheets:
46+
var spritesheet = animation_data.required_spritesheets[anim_name]
47+
var texture_path = _spritesheets_path.path_join(spritesheet + ".png")
48+
var texture = load(texture_path)
49+
if texture:
50+
animation_textures[anim_name] = texture
51+
else:
52+
push_error("Failed to load spritesheet: %s" % texture_path)
53+
54+
func _setup_animation_properties():
55+
if not sprite_frames:
56+
sprite_frames = SpriteFrames.new()
57+
58+
if animation_data.available_animations.size() > 0:
59+
current_animation = animation_data.available_animations[0]
60+
direction = "south"
61+
62+
func _setup_sprite_frames():
63+
if not animation_data or not sprite_frames:
64+
return
1865

19-
func _enter_tree():
20-
if Engine.is_editor_hint():
21-
_instantiate()
66+
sprite_frames.clear_all()
67+
_atlas_cache.clear()
68+
69+
for anim_name in animation_data.available_animations:
70+
for dir in animation_data.available_directions[anim_name].keys():
71+
var anim_key = anim_name + "_" + dir
72+
73+
sprite_frames.add_animation(anim_key)
74+
sprite_frames.set_animation_loop(anim_key, animation_data.animation_loops[anim_name])
75+
sprite_frames.set_animation_speed(anim_key, animation_data.animation_speeds[anim_name])
76+
77+
var texture = animation_textures.get(anim_name)
78+
if not texture:
79+
push_warning("No texture for animation: %s" % anim_name)
80+
continue
81+
82+
var frame_count = animation_data.animation_frame_counts[anim_name]
83+
var frame_size = animation_data.frame_sizes[anim_name]
84+
var animation_rows = animation_data.animation_rows[anim_name]
85+
var direction_offset = animation_data.available_directions[anim_name][dir]
86+
var custom_frames = animation_data.custom_frames.get(anim_name, null)
87+
var base_frame_size = animation_data.base_animation_size
88+
var frame_start = animation_data.initial_sprite_indices[anim_name]
89+
90+
if not _atlas_cache.has(anim_key):
91+
_atlas_cache[anim_key] = []
92+
93+
if custom_frames:
94+
_setup_custom_frames(anim_key, texture, custom_frames, animation_rows, base_frame_size, direction_offset, frame_size)
95+
else:
96+
_setup_standard_frames(anim_key, texture, frame_count, frame_start, animation_rows, base_frame_size, direction_offset, frame_size)
97+
98+
_play_current_animation()
2299

23-
func _instantiate() -> void :
24-
if not LPC_base:
25-
LPC_base = LPCBase.new()
26-
LPC_base.LoadAnimations(self)
100+
func _setup_custom_frames(anim_key, texture, custom_frames, animation_rows, base_frame_size, direction_offset, frame_size):
101+
for i in range(custom_frames.size()):
102+
var frame_idx = custom_frames[i]
103+
var atlas = _get_or_create_atlas(anim_key, i)
104+
atlas.atlas = texture
105+
atlas.region = Rect2(frame_idx * frame_size, animation_rows * base_frame_size + (direction_offset * frame_size), frame_size, frame_size)
106+
sprite_frames.add_frame(anim_key, atlas)
27107

28-
func play(animation: LPCEnum.LPCAnimation, fps: float = 5.0):
29-
var sprites = get_children()
30-
LPC_base.play(animation, sprites, AnimationNames, fps)
108+
func _setup_standard_frames(anim_key, texture, frame_count, frame_start, animation_rows, base_frame_size, direction_offset, frame_size):
109+
for i in range(frame_count):
110+
var atlas = _get_or_create_atlas(anim_key, i)
111+
atlas.atlas = texture
112+
atlas.region = Rect2((i + frame_start) * frame_size, animation_rows * base_frame_size + (direction_offset * frame_size), frame_size, frame_size)
113+
sprite_frames.add_frame(anim_key, atlas)
114+
115+
func _get_or_create_atlas(anim_key, frame_idx):
116+
if _atlas_cache[anim_key].size() <= frame_idx:
117+
_atlas_cache[anim_key].resize(frame_idx + 1)
118+
119+
if not _atlas_cache[anim_key][frame_idx]:
120+
_atlas_cache[anim_key][frame_idx] = AtlasTexture.new()
121+
122+
return _atlas_cache[anim_key][frame_idx]
123+
124+
func _play_current_animation():
125+
if current_animation and direction:
126+
var anim_key = current_animation + "_" + direction
127+
if sprite_frames.has_animation(anim_key):
128+
play(anim_key)
129+
130+
func play_animation(anim_name: String = "idle", dir: String = "south"):
131+
if not animation_data:
132+
return
133+
134+
if not anim_name in animation_data.available_animations:
135+
return
136+
137+
if not dir in animation_data.available_directions[anim_name]:
138+
dir = animation_data.available_directions[anim_name].keys()[0]
139+
140+
current_animation = anim_name
141+
direction = dir
142+
143+
_play_current_animation()
144+
145+
func _ready():
146+
if not sprite_frames:
147+
set_sprite_frames(SpriteFrames.new())
148+
if not animation_data:
149+
animation_data = LPCAnimationData.new()
150+
_refresh_sprites()
31151

32152
func _notification(what):
33153
if what == NOTIFICATION_EDITOR_POST_SAVE:
34-
call_deferred("_instantiate")
35-
36-
func CreateAnimatedSprite():
37-
var animatedSprite = AnimatedSprite2D.new()
38-
animatedSprite.texture_filter = Sprite2DTextureFilter
39-
return animatedSprite
154+
_setup_sprite_frames()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://dn724gbsh1jky

addons/LPCAnimatedSprite/LPCAnimatedSprite3D.gd

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)