This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SpriteKit | |
| class UpdateService { | |
| /// Handle on current SKScene | |
| let scene : Level | |
| /// Array of callbacks to be called each time the updateLoopRuns | |
| var updateListeners : Array<((CFTimeInterval) -> ())> = [] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// If cache is established | |
| if self.updateListeners.count > 0 { | |
| for listener in self.updateListeners { | |
| listener(currentTime) | |
| } | |
| } | |
| /// Rebuild the cache | |
| else { | |
| self.layers[WorldLayer.World.rawValue].enumerateChildNodesWithName("Character") { | |
| node, stop in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func boundScaleToWindow(scale:CGFloat) -> CGFloat { | |
| /// Right now, this is hard coded against a 2800x1880 image sitting in a 2048x1536 window whos center is 0.5/0.5 | |
| if(scale > 1.3) { | |
| return 1.3 | |
| } | |
| if(scale < 0.9) { | |
| return 0.9 | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func boundScaleToWindow(var scale:CGFloat) -> CGFloat { | |
| /// Right now, this is hard coded against a 2800x1880 image sitting in a 2048x1536 window whos center is 0.5/0.5 | |
| if(scale > 1.3) { | |
| scale = 1.3 | |
| } | |
| if(scale < 0.9) { | |
| scale = 0.9 | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Our for loop inside of the wheel() function | |
| for (index, unit) in enumerate(self.units) { | |
| var vector = self.formation!.getVectorAtPosition(index) | |
| // Translate unit to new point. Basically, subtract rotation center offset from units position | |
| vector = CGVectorMake(vector.dx - self.translateX, vector.dy - self.translateY) | |
| // Rotate the matrix at units position | |
| var newX = vector.dx * cosRadian - vector.dy * sinRadian | |
| var newY = vector.dx * sinRadian + vector.dy * cosRadian |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func updateWithTime(currentTime:NSTimeInterval) { | |
| // Local boolean. Next step would be to use a state machine. | |
| if(self.wheeling) { | |
| self.wheel() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This would be the public method callled to start a wheel | |
| func wheelToAngle(angle:CGFloat) { | |
| self.wheelAngle = angle | |
| self.wheeling = true | |
| } | |
| // This would run each game loop to perform the wheel | |
| func wheel() { | |
| // Hard code the speed of the turn. Each frame, move 0.003 radians until we reach target. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func findClosestUnitToPosition(destination:CGPoint, var positionedUnits:Array<CharacterNode>) -> CharacterNode { | |
| var closestUnit : CharacterNode? = nil | |
| var closestDistance : CGFloat? = nil | |
| // Loop over all of our units. | |
| for(i, unit) in enumerate(self.units) { | |
| // If the unit hasn't been moved already. | |
| if(!contains(positionedUnits, unit)) { | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func moveIntoFormation() { | |
| // New unit array. Starts empty, and once we assign a unit to a position, | |
| // we'll add to this array. | |
| var positionedUnits : Array<CharacterNode> = [] | |
| // Instead of looping over units, loop over unit count | |
| for var index = 0; index < self.units.count; ++index { | |
| // Get the vector from the formation class for that position. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func moveIntoFormation() { | |
| // Loop over each unit in our Squad. | |
| for (i, unit) in enumerate(self.units) { | |
| // Get the vector from the formation class for that position. | |
| // Just a simple accessor to the array. | |
| let vector = self.formation!.getVectorAtPosition(i) | |
| // Hard coded rotation by radian of 0.4. |