I was trying to move from appkit to swiftUI. As a learning project I am building a cellular automata style project based on Pattersons Worms.. I am trying something similar to the EA game Worms? for the Commodore 64. There is a video on YouTube of the game running, but I'm not allowed to link it here.
The problem I have is that the animation is driven by a ruleset. When the automata hits a configuration that is not in the ruleset it is supposed to stop and ask the user. For each step the model returns either the next move, or nil to indicate the user need to make a choice that will be sent back to the model to be added to the ruleset.
My current approach, and I might be following the wrong path, is a ZStack where the bottom level is the grid, the middle level is the established worm segments and the top level is either the animation of the next worm segment or the user chooser to choose the segment. I've only implemented the animation of the next worm segment. The idea is that when the model adds a segment that it first animated at the top level and then displayed by the middle level. Then the top level animates the next segment. I was animating the trim on the segment to draw the line. If the current move is nil, then the middle level draws the segment. If current move has a value, the animation draw it, and then on completion sets the current move to nil so that the bottom level draw it.
The problem I ran into was resetting the animation to draw the next segment. I've tried two approaches. in one the completion resets the animation boolean variable, but I need a manual step to set the next stage of the animation. The other uses the completion to set the next step, but it the animation doesn't run for the step and the display is an always a step behind. I'm not sure how to both update the move and reset the animation at the same time.
I have uploaded a simplified version without the full grid and simplified model to GitHub (https://github.com/thomasrdean/AnimationTest). Is there any other way to reset the animation the than the completion so I can use the completion to retrieve the next step from the model?
Have you tried using the animation(_:value:)
+ onChange(of:initial:_:)
modifier ?
You'll be able to apply the given animation to this view when the specified value changes and use onChange to enqueue a new update to the value.
You should checkout Animating views and transitions tutorial. It's a great starting to learn about SwiftUI animations.