summaryrefslogtreecommitdiff
path: root/extractor/action_format.gd
diff options
context:
space:
mode:
Diffstat (limited to 'extractor/action_format.gd')
-rw-r--r--extractor/action_format.gd182
1 files changed, 182 insertions, 0 deletions
diff --git a/extractor/action_format.gd b/extractor/action_format.gd
index d61c590..25a794e 100644
--- a/extractor/action_format.gd
+++ b/extractor/action_format.gd
@@ -82,6 +82,188 @@ static func from_bytes(bytes: ByteStream) -> ActionFormat:
return action_format
+func convert(name: String, sprites_path: String) -> Node2D:
+ var node := Node2D.new()
+ node.name = name
+ node.set_script(load("res://extractor/actions.gd"))
+
+ var animation_player := AnimationPlayer.new()
+ animation_player.name = "AnimationPlayer"
+ animation_player.unique_name_in_owner = true
+ node.add_child(animation_player)
+ animation_player.owner = node
+
+ var sprite_layers := CanvasGroup.new()
+ sprite_layers.name = "SpriteLayers"
+ sprite_layers.unique_name_in_owner = true
+
+ node.add_child(sprite_layers)
+ sprite_layers.owner = node
+
+ var track_properties = [
+ "animation",
+ "frame",
+ "speed_scale",
+ "position",
+ "self_modulate",
+ "scale",
+ "rotation_degrees",
+ "flip_h",
+ "visible",
+ ]
+
+ var sprite_frames := SpriteFrames.new()
+ #sprite_frames.add_animation("default")
+ for img_file_path in DirAccess.get_files_at(sprites_path):
+ if img_file_path.ends_with(".png"):
+ sprite_frames.add_frame("default", load("%s/%s" % [sprites_path, img_file_path]))
+
+ var animation_library := AnimationLibrary.new()
+
+ # get max number of sprite layers for all actions
+ var action_sprite_layers_max_count = actions.reduce(func(accum, action: ActionData):
+ return max(accum, action.motions.reduce(func(accum2, motion: Motion):
+ return max(accum2, motion.sprite_layer_count)
+ , 0))
+ , 0)
+
+ # add Nodes for each sprite layer
+ for sprite_layer_idx in action_sprite_layers_max_count:
+ var sprite = AnimatedSprite2D.new()
+ sprite.centered = false # 必要!!
+ sprite.texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST
+ sprite.sprite_frames = sprite_frames
+ sprite.name = str(sprite_layer_idx).pad_zeros(3)
+ sprite_layers.add_child(sprite)
+ sprite.owner = node
+
+ for action_idx in actions.size():
+ var action: ActionData = actions[action_idx]
+ var frame_timing_base := ((frame_times[action_idx] * 24) / 1000)
+
+ # TODO: check why this is necessary
+ if sprites_path.contains("cursors") and action_idx == 0:
+ frame_timing_base = ((frame_times[action_idx] * 24 * 2) / 1000)
+
+ # add animation for each action
+ var animation := Animation.new()
+ animation.loop_mode = Animation.LOOP_LINEAR
+ animation.length = frame_timing_base * action.motion_count
+ animation_library.add_animation(str(action_idx).pad_zeros(3), animation)
+
+ # get max number of sprite layers for current action motions
+ var motion_sprite_layers_max_count = action.motions.reduce(func(accum, motion: Motion):
+ return max(accum, motion.sprite_layer_count)
+ , 0)
+
+ # add animation tracks for each sprite layer
+ for sprite_layer_idx in motion_sprite_layers_max_count:
+ var sprite := sprite_layers.get_child(sprite_layer_idx)
+ for property_idx in track_properties.size():
+ var track_idx = (sprite_layer_idx * track_properties.size()) + property_idx
+ animation.add_track(Animation.TYPE_VALUE, track_idx)
+ animation.value_track_set_update_mode(track_idx, Animation.UPDATE_DISCRETE)
+ animation.track_set_path(
+ track_idx,
+ "%s:%s" % ["SpriteLayers/" + sprite.name, track_properties[property_idx]]
+ )
+
+ for i in range(motion_sprite_layers_max_count, action_sprite_layers_max_count):
+ var sprite := sprite_layers.get_child(i)
+ var track_idx = animation.add_track(Animation.TYPE_VALUE)
+ animation.track_set_path(
+ track_idx,
+ "%s:visible" % ["SpriteLayers/" + sprite.name]
+ )
+ animation.track_insert_key(track_idx, 0.0, false)
+
+ # add animation tracks
+ for motion_idx in action.motions.size():
+ var motion: Motion = action.motions[motion_idx]
+
+ # TODO: no animations to speak of available ? sprite_index = -1
+ #if motion.event_id == -1:
+ #continue
+
+ var timing = motion_idx * frame_timing_base
+ var visible_key = 0
+
+ # add visible = false animation tracks to other sprite_layers
+ for i in motion_sprite_layers_max_count:
+ var track_idx = i * track_properties.size() + track_properties.find("visible")
+ visible_key = animation.track_insert_key(track_idx, timing, false)
+
+ for sprite_layer_idx in motion.sprite_layers.size():
+ var layer: SpriteLayer = motion.sprite_layers[sprite_layer_idx]
+
+ if layer.sprite_index == -1:
+ continue
+
+ var track_base_idx = sprite_layer_idx * track_properties.size()
+
+ animation.track_insert_key(
+ track_base_idx + track_properties.find("animation"),
+ timing,
+ "default"
+ )
+
+ animation.track_insert_key(
+ track_base_idx + track_properties.find("frame"),
+ timing,
+ layer.sprite_index
+ )
+
+ animation.track_insert_key(
+ track_base_idx + track_properties.find("speed_scale"),
+ timing,
+ 1.0
+ )
+
+ var layer_image := sprite_frames.get_frame_texture("default", layer.sprite_index)
+ var position: Vector2 = layer.get_position() - ceil(layer_image.get_size() / 2) # for fixing half pixel drawing
+ var rotated = layer_image.get_size().rotated(deg_to_rad(layer.rotation_degrees))
+ var distance = layer_image.get_size() - rotated
+ animation.track_insert_key(
+ track_base_idx + track_properties.find("position"),
+ timing,
+ position + (distance / 2)
+ )
+
+ animation.track_insert_key(
+ track_base_idx + track_properties.find("self_modulate"),
+ timing,
+ layer.get_color()
+ )
+
+ animation.track_insert_key(
+ track_base_idx + track_properties.find("scale"),
+ timing,
+ layer.get_scale()
+ )
+
+ animation.track_insert_key(
+ track_base_idx + track_properties.find("rotation_degrees"),
+ timing,
+ layer.rotation_degrees
+ )
+
+ animation.track_insert_key(
+ track_base_idx + track_properties.find("flip_h"),
+ timing,
+ layer.flip_h
+ )
+
+ animation.track_set_key_value(
+ track_base_idx + track_properties.find("visible"),
+ visible_key,
+ true
+ )
+
+ animation_player.add_animation_library("", animation_library)
+
+ return node
+
+
class ActionData:
## Byte Type: u32 [br]
## Byte Length: 4