Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Sistema de Rewind para godot 4+
- Como usar : crie um node2d coloque ele como filho do node que deseja aplicar o efeito ,exemplo : o objeto player e coloque o script abaixo nele.Não esqueça de lincar as coisas no inspector :
- # RewindComponent.gd
- extends Node
- @export var target: Node2D # CharacterBody2D (jogador)
- @export var sprite: Sprite2D # Sprite2D filho
- @export var max_history: int = 300
- @export var record_rate: int = 1
- @export var rewind_step_time: float = 0.016 # ~60 fps
- @export var rewind_stop_offset: int = 30 # frames a parar antes do início
- @onready var anim_player: AnimationPlayer = target.get_node("anin_player") if target else null
- var state_history: Array[Dictionary] = []
- var is_recording: bool = false
- var is_rewinding: bool = false
- var frame_counter: int = 0
- var rewind_timer: float = 0.0
- var rewind_index: int = -1
- func _ready() -> void:
- if target == null:
- target = get_parent() as Node2D
- if target == null:
- push_error("Target (Node2D) não encontrado!")
- queue_free()
- return
- if sprite == null:
- sprite = target.get_node("Sprite") as Sprite2D
- if sprite == null:
- push_error("Sprite2D não encontrado!")
- queue_free()
- return
- anim_player = target.get_node("anin_player") as AnimationPlayer
- if anim_player == null:
- push_warning("AnimationPlayer 'anin_player' não encontrado!")
- func _physics_process(delta: float) -> void:
- if is_rewinding:
- rewind_timer += delta
- if rewind_timer >= rewind_step_time:
- rewind_timer -= rewind_step_time
- apply_rewind_step()
- return
- # Inputs (continuam funcionando normalmente)
- if Input.is_action_just_pressed("record_toggle"):
- if is_recording:
- stop_recording()
- else:
- start_recording()
- if Input.is_action_just_pressed("record_stop"):
- stop_recording()
- if Input.is_action_just_pressed("rewind") and not is_rewinding and not state_history.is_empty():
- trigger_rewind()
- if is_recording:
- frame_counter += 1
- if frame_counter % record_rate == 0:
- state_history.append({
- "position": target.global_position,
- "texture": sprite.texture,
- "frame": sprite.frame,
- "scale": sprite.scale,
- "hframes": sprite.hframes
- })
- if state_history.size() > max_history:
- state_history.remove_at(0)
- # Funções públicas para chamar de outros scripts
- func start_recording() -> void:
- if is_recording or is_rewinding:
- return # Evita iniciar gravação durante rewind ou já gravando
- state_history.clear() # Descarta rewind anterior
- frame_counter = 0
- state_history.append({
- "position": target.global_position,
- "texture": sprite.texture,
- "frame": sprite.frame,
- "scale": sprite.scale,
- "hframes": sprite.hframes
- })
- is_recording = true
- print("Gravação INICIADA via função - Histórico limpo | Posição inicial: ", target.global_position)
- func stop_recording() -> void:
- if not is_recording:
- return
- is_recording = false
- print("Gravação ENCERRADA via função")
- func trigger_rewind() -> void:
- if is_rewinding or state_history.is_empty():
- print("Rewind ignorado: já rebobinando ou sem histórico")
- return
- is_rewinding = true
- is_recording = false
- rewind_timer = 0.0
- rewind_index = state_history.size() - 1
- print("Rewind INICIADO via função - Total estados: ", state_history.size())
- func apply_rewind_step() -> void:
- if rewind_index < rewind_stop_offset:
- finish_rewind()
- return
- var state = state_history[rewind_index]
- target.global_position = state["position"]
- sprite.texture = state["texture"]
- sprite.frame = state["frame"]
- sprite.scale = state["scale"]
- sprite.hframes = state["hframes"]
- rewind_index -= 1
- func finish_rewind() -> void:
- is_rewinding = false
- if anim_player != null:
- var direcao = main.direcao_inicial_player
- if direcao == Vector2.RIGHT:
- anim_player.play("idle_right")
- elif direcao == Vector2.LEFT:
- anim_player.play("idle_left")
- elif direcao == Vector2.UP:
- anim_player.play("idle_up")
- elif direcao == Vector2.DOWN:
- anim_player.play("idle_down")
- else:
- anim_player.play("idle_right")
- print("Rewind FINALIZADO - Parou ", rewind_stop_offset, " frames antes | Animação: ", anim_player.current_animation)
- else:
- print("Rewind FINALIZADO - AnimationPlayer não encontrado")
Advertisement
Add Comment
Please, Sign In to add comment