Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/UntoldEngine/ECS/Components.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public class AnimationComponent: Component {
var currentAnimation: AnimationClip?
public var animationsFilenames: [URL] = []
var pause: Bool = false
var playbackSpeed: Float = 1.0
var currentTime: Float = 0.0
public required init() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public protocol NodeAnimations: NodeProtocol {
func setAnimations(resource: String, name: String) -> Self
func changeAnimation(name: String, withPause pause: Bool) -> Self
func setAnimationPlaybackSpeed(speed: Float) -> Self
}

public extension NodeAnimations {
Expand All @@ -22,4 +23,9 @@ public extension NodeAnimations {
UntoldEngine.changeAnimation(entityId: entityID, name: name, withPause: pause)
return self
}

func setAnimationPlaybackSpeed(speed: Float) -> Self {
UntoldEngine.setAnimationPlaybackSpeed(entityId: entityID, speed: speed)
return self
}
}
6 changes: 6 additions & 0 deletions Sources/UntoldEngine/Scenes/SceneSerializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ struct EntityData: Codable {
var axisOfRotations: simd_float3 = .zero
var scale: simd_float3 = .one
var animations: [URL] = []
var animationPlaybackSpeed: Float = 1.0
var mass: Float = .init(1.0)
var lightData: LightData? = nil
var cameraData: CameraData? = nil
Expand Down Expand Up @@ -263,6 +264,7 @@ public func serializeScene() -> SceneData {
// Animation properties
if let animationComponent = scene.get(component: AnimationComponent.self, for: entityId) {
entityData.animations = animationComponent.animationsFilenames
entityData.animationPlaybackSpeed = animationComponent.playbackSpeed
}

entityData.hasAnimationComponent = hasComponent(entityId: entityId, componentType: AnimationComponent.self)
Expand Down Expand Up @@ -648,6 +650,7 @@ public func deserializeScene(sceneData: SceneData, meshLoadingMode: MeshLoadingM
}
if let animationComponent = scene.get(component: AnimationComponent.self, for: entityId) {
animationComponent.animationsFilenames = sceneDataEntity.animations
animationComponent.playbackSpeed = sceneDataEntity.animationPlaybackSpeed
}
}
case .asyncDefault:
Expand All @@ -667,6 +670,7 @@ public func deserializeScene(sceneData: SceneData, meshLoadingMode: MeshLoadingM
}
if let animationComponent = scene.get(component: AnimationComponent.self, for: entityId) {
animationComponent.animationsFilenames = sceneDataEntity.animations
animationComponent.playbackSpeed = sceneDataEntity.animationPlaybackSpeed
}
}
} else {
Expand All @@ -693,6 +697,7 @@ public func deserializeScene(sceneData: SceneData, meshLoadingMode: MeshLoadingM
}
if let animationComponent = scene.get(component: AnimationComponent.self, for: entityId) {
animationComponent.animationsFilenames = sceneDataEntity.animations
animationComponent.playbackSpeed = sceneDataEntity.animationPlaybackSpeed
}
}
case .asyncDefault:
Expand All @@ -713,6 +718,7 @@ public func deserializeScene(sceneData: SceneData, meshLoadingMode: MeshLoadingM
}
if let animationComponent = scene.get(component: AnimationComponent.self, for: entityId) {
animationComponent.animationsFilenames = sceneDataEntity.animations
animationComponent.playbackSpeed = sceneDataEntity.animationPlaybackSpeed
}
}
} else {
Expand Down
20 changes: 19 additions & 1 deletion Sources/UntoldEngine/Systems/AnimationSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private func updateAnimationSystem(deltaTime: Float) {
continue
}

animationComponent.currentTime += deltaTime
animationComponent.currentTime += deltaTime * animationComponent.playbackSpeed

guard let animationClip = animationComponent.currentAnimation else { return }

Expand Down Expand Up @@ -114,6 +114,24 @@ public func changeAnimation(entityId: EntityID, name: String, withPause: Bool =
animationComponent.pause = withPause
}

public func setAnimationPlaybackSpeed(entityId: EntityID, speed: Float) {
guard let animationComponent = scene.get(component: AnimationComponent.self, for: entityId) else {
handleError(.noAnimationComponent, entityId)
return
}

animationComponent.playbackSpeed = max(0.0, speed)
}

public func getAnimationPlaybackSpeed(entityId: EntityID) -> Float {
guard let animationComponent = scene.get(component: AnimationComponent.self, for: entityId) else {
handleError(.noAnimationComponent, entityId)
return 1.0
}

return animationComponent.playbackSpeed
}

public func getAllAnimationClips(entityId: EntityID) -> [String] {
guard let animationComponent = scene.get(component: AnimationComponent.self, for: entityId) else {
return []
Expand Down
66 changes: 66 additions & 0 deletions Sources/UntoldEngine/Systems/CameraSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,72 @@ public func cameraFollowLocal(entityId: EntityID,
updateCameraViewMatrix(entityId: entityId)
}

public func cameraFollowDeadZone(entityId: EntityID,
targetEntity: EntityID,
offset: simd_float3 = simd_float3(0, 0, 0),
deadZoneExtents: simd_float3,
smoothFactor: Float = 0.0,
deltaTime: Float = 0.0)
{
guard let cameraComponent = scene.get(component: CameraComponent.self, for: entityId) else {
return
}
guard let targetTransform = scene.get(component: LocalTransformComponent.self, for: targetEntity) else {
return
}

let cameraPosition = cameraComponent.localPosition
let targetPosition = targetTransform.position + offset
let toTarget = targetPosition - cameraPosition

let xAxis = rightDirectionVector(from: cameraComponent.rotation)
let yAxis = upDirectionVector(from: cameraComponent.rotation)
let zAxis = forwardDirectionVector(from: cameraComponent.rotation)

let localTarget = simd_float3(
simd_dot(toTarget, xAxis),
simd_dot(toTarget, yAxis),
simd_dot(toTarget, zAxis)
)

let extents = simd_abs(deadZoneExtents)
var correctionLocal = simd_float3(0, 0, 0)

if localTarget.x > extents.x {
correctionLocal.x = localTarget.x - extents.x
} else if localTarget.x < -extents.x {
correctionLocal.x = localTarget.x + extents.x
}

if localTarget.y > extents.y {
correctionLocal.y = localTarget.y - extents.y
} else if localTarget.y < -extents.y {
correctionLocal.y = localTarget.y + extents.y
}

if localTarget.z > extents.z {
correctionLocal.z = localTarget.z - extents.z
} else if localTarget.z < -extents.z {
correctionLocal.z = localTarget.z + extents.z
}

if length(correctionLocal) <= 0.0001 {
return
}

let correctionWorld = xAxis * correctionLocal.x + yAxis * correctionLocal.y + zAxis * correctionLocal.z
let desiredPosition = cameraPosition + correctionWorld

if smoothFactor > 0, deltaTime > 0 {
let t = min(smoothFactor * deltaTime, 1.0)
cameraComponent.localPosition = cameraPosition + (desiredPosition - cameraPosition) * t
} else {
cameraComponent.localPosition = desiredPosition
}

updateCameraViewMatrix(entityId: entityId)
}

public func cameraOrbitTarget(entityId: EntityID,
centerEntity: EntityID,
radius: Float,
Expand Down
7 changes: 7 additions & 0 deletions Sources/UntoldEngine/Systems/InputSystem+Keyboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

public struct KeyState {
public var wPressed = false, aPressed = false, sPressed = false, dPressed = false
public var jPressed = false, kPressed = false, lPressed = false
public var qPressed = false, ePressed = false
public var spacePressed = false, shiftPressed = false, ctrlPressed = false
public var altPressed = false
Expand Down Expand Up @@ -82,6 +83,9 @@ public extension InputSystem {
case kVK_ANSI_Q: keyState.qPressed = true
case kVK_ANSI_E: keyState.ePressed = true
case kVK_ANSI_Space: keyState.spacePressed = true
case kVK_ANSI_J: keyState.jPressed = true
case kVK_ANSI_K: keyState.kPressed = true
case kVK_ANSI_L: keyState.lPressed = true
// case kVK_ANSI_G: print("G pressed")
default: break
}
Expand All @@ -97,6 +101,9 @@ public extension InputSystem {
case kVK_ANSI_S: keyState.sPressed = false
case kVK_ANSI_Q: keyState.qPressed = false
case kVK_ANSI_E: keyState.ePressed = false
case kVK_ANSI_J: keyState.jPressed = false
case kVK_ANSI_K: keyState.kPressed = false
case kVK_ANSI_L: keyState.lPressed = false
case kVK_ANSI_Space: keyState.spacePressed = false
// case kVK_ANSI_G: print("G released")
default: break
Expand Down
2 changes: 1 addition & 1 deletion Sources/UntoldEngine/Systems/InputSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public final class InputSystem {
public let kVK_ANSI_Q: UInt16 = 12, kVK_ANSI_E: UInt16 = 14
public let kVK_ANSI_1: UInt16 = 18, kVK_ANSI_2: UInt16 = 19
public let kVK_ANSI_G: UInt16 = 5, kVK_ANSI_X: UInt16 = 7, kVK_ANSI_Y: UInt16 = 16, kVK_ANSI_Z: UInt16 = 6
public let kVK_ANSI_Space: UInt16 = 49
public let kVK_ANSI_Space: UInt16 = 49, kVK_ANSI_J: UInt16 = 38, kVK_ANSI_K: UInt16 = 40

public var keyState = KeyState()
public var gamePadState = GamePadState()
Expand Down
Loading
Loading