Transition
This commit is contained in:
@@ -8,46 +8,146 @@ enum InteractType {
|
||||
|
||||
# Movement speed in units per second
|
||||
@export var gravity: float = 24.8
|
||||
|
||||
@export var interactType:InteractType = InteractType.NONE
|
||||
@export_multiline var interactTexts:Array[String] = []
|
||||
@export var npcLookAtPlayer:bool = true
|
||||
@export var playerLookAtNPC:bool = true
|
||||
@export var interactCamera:Camera3D = null
|
||||
@export var cutscene:Cutscene = null
|
||||
|
||||
var nextTextIndex:int = 0
|
||||
var previousCamera:Camera3D = null
|
||||
var playerEnt:Player
|
||||
var conversationEnding:bool = false
|
||||
|
||||
func lookAtPlayer() -> void:
|
||||
if !playerEnt:
|
||||
return
|
||||
|
||||
# Get player position and NPC position
|
||||
var playerPos = playerEnt.global_transform.origin
|
||||
var npcPos = global_transform.origin
|
||||
|
||||
# Rotate the NPC to look at the player
|
||||
if npcLookAtPlayer:
|
||||
var npcDirection = (playerPos - npcPos).normalized()
|
||||
var npcRotation = Vector3(0, atan2(npcDirection.x, npcDirection.z), 0)
|
||||
global_transform.basis = Basis.from_euler(npcRotation)
|
||||
|
||||
# Rotate the player to look at the NPC
|
||||
if playerLookAtNPC:
|
||||
var playerDirection = (npcPos - playerPos).normalized()
|
||||
var playerRotation = Vector3(0, atan2(playerDirection.x, playerDirection.z), 0)
|
||||
playerEnt.global_transform.basis = Basis.from_euler(playerRotation)
|
||||
|
||||
func showTexts():
|
||||
TRANSITION.fadeOutStart.disconnect(showTexts)
|
||||
|
||||
# Any texts?
|
||||
if interactTexts.size() == 0:
|
||||
endTexts()
|
||||
return
|
||||
|
||||
# First text.
|
||||
UI.TEXTBOX.setText(interactTexts[nextTextIndex])
|
||||
UI.TEXTBOX.textboxClosing.connect(onTextboxClosing)
|
||||
|
||||
func endTexts():
|
||||
UI.TEXTBOX.textboxClosing.disconnect(onTextboxClosing)
|
||||
conversationEnding = true
|
||||
|
||||
# Do we fade out the camera?
|
||||
if interactCamera:
|
||||
TRANSITION.fade(TRANSITION.FadeType.FADE_OUT)
|
||||
TRANSITION.fadeOutEnd.connect(onFadeOutEnd)
|
||||
return
|
||||
|
||||
# Reset Camera?
|
||||
if previousCamera:
|
||||
previousCamera.current = true
|
||||
previousCamera = null
|
||||
|
||||
func _enter_tree() -> void:
|
||||
$InteractableArea.interactEvent.connect(_on_interact)
|
||||
$InteractableArea.interactEvent.connect(onInteract)
|
||||
|
||||
func _exit_tree() -> void:
|
||||
$InteractableArea.interactEvent.disconnect(_on_interact)
|
||||
$InteractableArea.interactEvent.disconnect(onInteract)
|
||||
UI.TEXTBOX.textboxClosing.disconnect(onTextboxClosing)
|
||||
TRANSITION.fadeOutEnd.disconnect(onFadeOutEnd)
|
||||
TRANSITION.fadeInEnd.disconnect(onFadeInEnd)
|
||||
|
||||
func _physics_process(delta):
|
||||
# Apply gravity if not on floor
|
||||
if !is_on_floor():
|
||||
velocity += PHYSICS.GRAVITY * delta
|
||||
|
||||
move_and_slide()
|
||||
|
||||
func _on_interact() -> void:
|
||||
func onInteract(playerEntity:Player) -> void:
|
||||
# Reset state
|
||||
previousCamera = null
|
||||
nextTextIndex = 0
|
||||
playerEnt = playerEntity
|
||||
conversationEnding = false
|
||||
|
||||
match interactType:
|
||||
InteractType.TEXTBOX:
|
||||
if interactTexts.size() == 0:
|
||||
PAUSE.cutscenePause()
|
||||
|
||||
# If a camera is set, switch to it, otherwise chat immediately.
|
||||
if !interactCamera:
|
||||
showTexts()
|
||||
return
|
||||
UI.TEXTBOX.setText(interactTexts[nextTextIndex])
|
||||
UI.TEXTBOX.textboxClosing.connect(onTextboxClosing)
|
||||
return
|
||||
|
||||
# Fade out.
|
||||
TRANSITION.fade(TRANSITION.FadeType.FADE_OUT)
|
||||
TRANSITION.fadeOutEnd.connect(onFadeOutEnd)
|
||||
|
||||
InteractType.CUTSCENE:
|
||||
if cutscene:
|
||||
cutscene.start()
|
||||
return
|
||||
if !cutscene:
|
||||
return
|
||||
cutscene.start()
|
||||
|
||||
_:
|
||||
return
|
||||
|
||||
|
||||
func onTextboxClosing() -> void:
|
||||
nextTextIndex += 1
|
||||
|
||||
# More text?
|
||||
if nextTextIndex < interactTexts.size():
|
||||
UI.TEXTBOX.setText(interactTexts[nextTextIndex])
|
||||
else:
|
||||
UI.TEXTBOX.textboxClosing.disconnect(onTextboxClosing)
|
||||
endTexts()
|
||||
|
||||
func onFadeOutEnd() -> void:
|
||||
# Begin fade back in.
|
||||
TRANSITION.fadeOutEnd.disconnect(onFadeOutEnd)
|
||||
TRANSITION.fade(TRANSITION.FadeType.FADE_IN)
|
||||
TRANSITION.fadeInEnd.connect(onFadeInEnd)
|
||||
|
||||
# Is the conversation ending?
|
||||
if conversationEnding:
|
||||
# Reset camera.
|
||||
if previousCamera:
|
||||
previousCamera.current = true
|
||||
previousCamera = null
|
||||
return
|
||||
|
||||
# No! We are starting the conversation, make the ents look at each other
|
||||
lookAtPlayer()
|
||||
|
||||
# Change camera
|
||||
previousCamera = get_viewport().get_camera_3d()
|
||||
interactCamera.current = true
|
||||
|
||||
func onFadeInEnd() -> void:
|
||||
TRANSITION.fadeInEnd.disconnect(onFadeInEnd)
|
||||
|
||||
# Did the conversation end?
|
||||
if conversationEnding:
|
||||
PAUSE.cutsceneResume()
|
||||
return
|
||||
|
||||
# Show texts after fade in.
|
||||
showTexts()
|
||||
|
0
scripts/entities/Player.gd
Normal file
0
scripts/entities/Player.gd
Normal file
@@ -1,6 +1,7 @@
|
||||
class_name PlayerInteraction extends Node
|
||||
|
||||
@export var interactableArea:Area3D
|
||||
@export var player:CharacterBody3D
|
||||
|
||||
func canInteract() -> bool:
|
||||
if PAUSE.isMovementPaused():
|
||||
@@ -26,4 +27,4 @@ func _process(delta: float) -> void:
|
||||
if !interactable:
|
||||
return
|
||||
|
||||
interactable.interactEvent.emit()
|
||||
interactable.interactEvent.emit(player)
|
||||
|
@@ -5,9 +5,6 @@ const SPEED = 8.0
|
||||
|
||||
@export var player:CharacterBody3D
|
||||
|
||||
func _ready() -> void:
|
||||
player = get_parent().get_parent()
|
||||
|
||||
func canMove() -> bool:
|
||||
if PAUSE.isMovementPaused():
|
||||
return false
|
||||
|
Reference in New Issue
Block a user