Wow, geniuses. I used to be a genius too, and now I’m writing toys for the iPhone. Last week Apple and Google excited me a lot, trying to deprive applications and revenues. I had to sweat and make 7 games in 7 days on Swift 3.0. At the same time, I did not look into the old Obj-C code of the old games - it is so much easier to make applications in a new language.
Before, when I was a genius, every time I had to re-write the sort function (as an element of the game) and then be proud of it. God, what an idiot I was. However, not much I have changed. But the tool has changed. See what sorting now looks like in Swift 3.0.
let vtxSorted = vtx.sorted(by:{ $0.yPosition > $1.yPosition })
And that's it! VTX vertex array sorted by Y coordinate ... Yes, why is this necessary? But why ... Sorting by coordinate is needed to build a Voronoi diagram, about which there were quite a few articles on Habré. ')
Voronoi diagram
You all know about convex-hull and how to build it. In Russian, this is called the Voronoi diagram. Let me remind you for pkhpeshnikov that this is a grid equidistant from all points scattered on the plane in disarray. Replace the plane with the iPhone screen, dots with bold white pixels and get this picture.
Nice and immediately want to write some game. So I did back in 2009, when you, the reader, did not even know about Ruby, who instead of Anna Karenina now lies on the tracks.
Yes, and let him lie, and we fly away into the distance - that's when I made the game on OpenGL and did not earn anything on it. Maybe I received $ 50 in 5 years, but this is unlikely.
But the game is cool. You poke your finger at the screen and you are nervous that you do not have time to remove all the cells from the board. What else is needed for happiness? Well, maybe a little candy and jam. Moreover, in the prime of life.
And so I decided to remake the game in a new way. Just a bright picture with a hot topic about elections in the US came across the Internet, which I made as an application icon. He replaced the points with Clintonicha and Trump-pam-pam, the grid with the States - and rushed to program in pure Swift.
Ambush with opengl
Why OpenGL? To draw textured triangles. Pouring pure color is in the garden. Kindergarten. We will have only hardcore!
Aaaa! Nope I'm not talking about the defender of Zenith. There is no Swift API for calling OpenGL functions! Estimate, peppers! Okay, let's play with cheeks and try metal. This is a fashionable library, which Apple made instead of OpenGL, with similar functionality and interface, but supposedly working faster on native hardware.
Ambush with Metal
This library does not work on the simulator! Atas! When Jobs was not like this. Hold me 100 people! Three thousand devils. Life seems to have been wasted. And so sad, with a shattered dream, I wandered under the sudden Balkan rain and listened to the rye of the local guys. However, the girl laughed even louder. Suddenly, as a green-eyed taxi checkered, a thought struck me - no need for libraries, kind man! I will be able to draw a triangle with a texture myself, on a clean UIKit, on the Strigier, dear to my heart. And not even a triangle, but a whole polygon, which is even steeper.
How? But look at the code, the Russian language is powerless here.
var mask = CAShapeLayer() mask.frame = groundTrump.layer.bounds let path = CGMutablePath() for t in app.trumpTriangles { if t.flag == 0 { path.closeSubpath() // path.move(to: tp) // } else { path.addLine(to: tp) // } } mask.path = path groundTrump.layer.mask = mask1
Yes, the mask steers and I know it. And now you know too. Go ahead, you need to write a grid generation algorithm. I love grids, my dissertation was about gridless methods (SPH). Here P - particles.
By the way, on github lie overseas algorithms for building the Voronoi diagram grid on Swift 3.0. Two whole. Unfortunately, the first of them does not take into account the rectangular border, the other works with error errors. I had to write the program myself, which was wonderful, the brains curled up to the side, clockwise. It is clockwise that I go around each vertex separately and the entire array of points in general. The Fletcher method did not use, sorted the points by the corner and fried stupidly O (N ^ 2). At 50 points, this does not matter, although on the iPhone 4S it noticeably slows down. But who has the iPhone 4S now? Even the editors of Habré do not use them.
What else? The game is not posted to the store, maybe I'll do it tomorrow, but I suggest you take a look at the video of the gameplay and admire my amazing work in the comments.