You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -33,44 +35,57 @@ public final class KeyboardControlledForceComponent: OctopusComponent, OctopusUp
33
35
/// Change this to a different code to customize the keys.
34
36
publicvararrowLeft:UInt16=.arrowLeft
35
37
36
-
publicvarbaseMagnitude:CGFloat
37
-
publicvarmaximumMagnitude:CGFloat
38
-
publicvaracceleratedMagnitude:CGFloat=0
39
-
40
-
/// The amount to increase `acceleratedMagnitude` by, per update, while there is keyboard input. `acceleratedMagnitude` is reset to `baseMagnitude` when there is no keyboard input.
41
-
publicvaracceleration:CGFloat
42
-
43
-
publicvarhorizontalFactor:CGFloat=1
44
-
publicvarverticalFactor:CGFloat=1
38
+
publicvarmagnitude:AcceleratedValue<CGFloat>
39
+
40
+
publicvarhorizontalFactor:CGFloat
41
+
publicvarverticalFactor:CGFloat
45
42
46
43
/// Specifies a fixed or variable timestep for per-update changes.
47
44
///
48
45
/// For physics effects, a per-second timestep may be suitable.
49
-
publicvartimestep:TimeStep
46
+
publicvartimestep:TimeStep
50
47
51
48
/// - Parameters:
52
-
/// - baseMagnitude: The minimum magnitude to apply to the physics body on every update. Affected by `timestep`.
53
-
/// - maximumMagnitude: The maximum magnitude to allow after acceleration has been applied.
54
-
/// - acceleration: The amount to increase the magnitude by, per update, while there is keyboard input. The magnitude is reset to the `baseMagnitude` when there is no keyboard input. Affected by `timestep`.
55
-
/// - horizontalFactor: Multiply the X axis force by this factor. Default: `1`. To reverse the X axis, specify a negative value like `-1`. To disable the X axis, specify `0`.
56
-
/// - verticalFactor: Multiply the Y axis force by this factor. Default: `1`. To reverse the Y axis, specify a negative value like `-1`. To disable the Y axis, specify `0`.
49
+
/// - magnitude: The magnitude to apply to the physics body on every update. Affected by `timestep`.
50
+
/// - horizontalFactor: Multiply the X axis force by this factor. Default: `1.0`. To reverse the X axis, specify a negative value like `-1`. To disable the X axis, specify `0`.
51
+
/// - verticalFactor: Multiply the Y axis force by this factor. Default: `1.0`. To reverse the Y axis, specify a negative value like `-1`. To disable the Y axis, specify `0`.
57
52
/// - timestep: Specifies a fixed or variable timestep for per-update changes. Default: `.perSecond`
58
-
publicinit(baseMagnitude:CGFloat=600, // ÷ 60 = 10 per frame
59
-
maximumMagnitude:CGFloat=1200, // ÷ 60 = 20 per frame
60
-
acceleration:CGFloat=600,
61
-
horizontalFactor:CGFloat=1,
62
-
verticalFactor:CGFloat=1,
53
+
publicinit(magnitude:AcceleratedValue<CGFloat>,
54
+
horizontalFactor:CGFloat=1.0,
55
+
verticalFactor:CGFloat=1.0,
63
56
timestep:TimeStep=.perSecond)
64
57
{
65
-
self.baseMagnitude = baseMagnitude
66
-
self.maximumMagnitude = maximumMagnitude
67
-
self.acceleration = acceleration
58
+
self.magnitude = magnitude
68
59
self.horizontalFactor = horizontalFactor
69
60
self.verticalFactor = verticalFactor
70
61
self.timestep = timestep
71
62
super.init()
72
63
}
73
64
65
+
/// - Parameters:
66
+
/// - baseMagnitude: The minimum magnitude to apply to the physics body on every update. Affected by `timestep`.
67
+
/// - maximumMagnitude: The maximum magnitude to allow after acceleration has been applied.
68
+
/// - acceleration: The amount to increase the magnitude by, per update, while there is keyboard input. The magnitude is reset to the `baseMagnitude` when there is no keyboard input. Affected by `timestep`.
69
+
/// - horizontalFactor: Multiply the X axis force by this factor. Default: `1.0`. To reverse the X axis, specify a negative value like `-1`. To disable the X axis, specify `0`.
70
+
/// - verticalFactor: Multiply the Y axis force by this factor. Default: `1.0`. To reverse the Y axis, specify a negative value like `-1`. To disable the Y axis, specify `0`.
71
+
/// - timestep: Specifies a fixed or variable timestep for per-update changes. Default: `.perSecond`
72
+
publicconvenienceinit(baseMagnitude:CGFloat=600, // ÷ 60 = 10 per frame
73
+
maximumMagnitude:CGFloat=1200, // ÷ 60 = 20 per frame
publicrequiredinit?(coder aDecoder:NSCoder){fatalError("init(coder:) has not been implemented")}
75
90
76
91
@inlinable
@@ -81,15 +96,15 @@ public final class KeyboardControlledForceComponent: OctopusComponent, OctopusUp
81
96
!keyboardEventComponent.codesPressed.isEmpty,
82
97
let physicsBody =coComponent(PhysicsComponent.self)?.physicsBody ?? entityNode?.physicsBody
83
98
else{
84
-
acceleratedMagnitude = baseMagnitude // TODO: PERFORMANCE: Figure out a better way than setting this every update.
99
+
magnitude.reset() // TODO: PERFORMANCE: Figure out a better way than setting this every update.
85
100
return
86
101
}
87
102
88
103
// Did player press a directional arrow key?
89
104
// ❕ NOTE: Don't use `switch` or `else` because we want to process multiple keypresses, to generate diagonal forces and also cancel out opposing directions.
0 commit comments