What we discussed in the comments:
Personally, I would change the dictionary to an array. As I said before:
Right now, since you are using a dictionary, what you've built can only handle 2 waves: a close wave, and a far wave. I'm thinking that you might want more than 2 waves? Maybe 5 close waves?
Here is what your code will look like after migrating from a Dictionary to an Array:
enum WaveType { case close, far }
class WaveNode: SKNode {}
func addWave() -> WaveNode {}
// I have taken the initiative to group these up into a tuple:
var waves = [(waveType: WaveType, waveNode: WaveNode, duration: TimeInterval)]()
// Here's what adding a wave to the list would look like:
waves.append((waveType: .far, waveNode: addWave(...), duration: 4))
// Here's an alternate but identical way to add a wave to your array:
waves += [(
waveType: .far,
waveNode: addWave(...),
duration: 4
)]
Still, this feels a little clunky as you say:
wondering how can I make addWave
function shorter ? As it [has a lot of parameters]
Specifically you state these parameters:
- position: CGPoint
- zPosition: CGFloat
- xScale: CGFloat
- direction: WaveDirection
I think it can be made shorter, and I propose this can be done in 3 steps.
Step 1
Let's edit your WaveNode
class to contain 2 new attributes: waveType
and duration
. Just as position, zPosition, xScale, and direction are all attributes of WaveNode
.
class WaveNode: SKNode {
// include these, as well as some default value
var duration: TimeInterval = 0
var waveType = .far
}
Step 2
Next, we will edit your addWave()
method. I have edited it in 2 ways:
- I added underscores
_
preceding each parameter tag to decrease length when calling the method.
- I added
waveType
and duration
as parameters to your method as seen below:
func addWave(_ position: CGPoint,_ zPosition: CGFloat,_ xScale: CGFloat,_ direction: WaveDirection,_ waveType: WaveType,_ duration: TimeInterval) -> WaveNode {
let wave = WaveNode()
// Keep all code that was in between
// Just add these 2 lines above the return line
wave.waveType = waveType
wave.duration = duration
return wave
}
Step 3
Adding waveType
and duration
as attributes to WaveNode
lets us not have to use an array of a tuple anymore. Instead, it will look like this:
var waves = [WaveNode]()
// And adding waves will be easy and much shorter:
waves.append(waveNode(...))
If you would like even shorter ways to rewrite this, I would be happy to oblige. Let me know what you think.