├── .gitignore ├── LICENSE ├── README.md ├── ShellGame.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata └── ShellGame ├── AppDelegate.swift ├── Assets.xcassets └── AppIcon.appiconset │ ├── Contents.json │ ├── shell-game-1024.png │ ├── shell-game-120.png │ ├── shell-game-152.png │ ├── shell-game-167.png │ ├── shell-game-180.png │ └── shell-game-76.png ├── AudioPlayer.swift ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Extensions.swift ├── GameViewController.swift ├── Info.plist ├── Level.swift ├── Permutation.swift ├── RecordStorage.swift └── art.scnassets ├── Settings.json ├── game.scn ├── sounds ├── fail.mp3 └── success.wav └── textures ├── plastic_cup_green_diffuce.jpg ├── plastic_cup_opaque_bump.jpg └── plastic_cup_opaque_norm.jpg /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | xcuserdata 4 | *.ipa 5 | *.xcuserstate 6 | 7 | .DS_Store 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ARKit Shell Game 2 | 3 | Shell Game built with ARKit and SceneKit. Good sample to learn AR/3D basics. 4 | 5 | ![ARKit Shell Game Demo](https://user-images.githubusercontent.com/2081318/29219707-a1628db8-7eda-11e7-8836-fc50799b8309.gif) 6 | 7 | ## Requirements 8 | * XCode 9 9 | * Device running iOS, with an A9 chip or higher (iPhone 6S or newer, iPad Pro) 10 | 11 | ## How to play 12 | * Move device around to detect a plane 13 | * Tap to locate game at plane surface 14 | * Tap again to start game 15 | * (Cups will be randomly rotating) 16 | * Guess ball position by tap to a cup 17 | 18 | ## License 19 | ARKit Shell Game is available under the Apache License, Version 2.0. See the [LICENSE](./LICENSE) file for more info. 20 | -------------------------------------------------------------------------------- /ShellGame.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 8848E7E01F41A92B0084DD5A /* RecordStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8848E7DF1F41A92B0084DD5A /* RecordStorage.swift */; }; 11 | 8848E7E21F41AE190084DD5A /* AudioPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8848E7E11F41AE190084DD5A /* AudioPlayer.swift */; }; 12 | 9A9D8CFD1F2C584F00C6A628 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A9D8CFC1F2C584F00C6A628 /* AppDelegate.swift */; }; 13 | 9A9D8CFF1F2C584F00C6A628 /* art.scnassets in Resources */ = {isa = PBXBuildFile; fileRef = 9A9D8CFE1F2C584F00C6A628 /* art.scnassets */; }; 14 | 9A9D8D041F2C584F00C6A628 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9A9D8D021F2C584F00C6A628 /* Main.storyboard */; }; 15 | 9A9D8D061F2C584F00C6A628 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9A9D8D051F2C584F00C6A628 /* Assets.xcassets */; }; 16 | 9A9D8D091F2C584F00C6A628 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9A9D8D071F2C584F00C6A628 /* LaunchScreen.storyboard */; }; 17 | D11871001F2F6A0A00346E6E /* Permutation.swift in Sources */ = {isa = PBXBuildFile; fileRef = D11870FF1F2F6A0A00346E6E /* Permutation.swift */; }; 18 | D11871021F3042DE00346E6E /* GameViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D11871011F3042DE00346E6E /* GameViewController.swift */; }; 19 | D11871061F3352AD00346E6E /* Level.swift in Sources */ = {isa = PBXBuildFile; fileRef = D11871051F3352AD00346E6E /* Level.swift */; }; 20 | D18776171F346CC9001F3E40 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D18776161F346CC9001F3E40 /* Extensions.swift */; }; 21 | /* End PBXBuildFile section */ 22 | 23 | /* Begin PBXFileReference section */ 24 | 8848E7DF1F41A92B0084DD5A /* RecordStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecordStorage.swift; sourceTree = ""; }; 25 | 8848E7E11F41AE190084DD5A /* AudioPlayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioPlayer.swift; sourceTree = ""; }; 26 | 9A9D8CF91F2C584F00C6A628 /* ShellGame.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ShellGame.app; sourceTree = BUILT_PRODUCTS_DIR; }; 27 | 9A9D8CFC1F2C584F00C6A628 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 28 | 9A9D8CFE1F2C584F00C6A628 /* art.scnassets */ = {isa = PBXFileReference; lastKnownFileType = wrapper.scnassets; path = art.scnassets; sourceTree = ""; }; 29 | 9A9D8D031F2C584F00C6A628 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 30 | 9A9D8D051F2C584F00C6A628 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 31 | 9A9D8D081F2C584F00C6A628 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 32 | 9A9D8D0A1F2C584F00C6A628 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 33 | D11870FF1F2F6A0A00346E6E /* Permutation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Permutation.swift; sourceTree = ""; }; 34 | D11871011F3042DE00346E6E /* GameViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameViewController.swift; sourceTree = ""; }; 35 | D11871051F3352AD00346E6E /* Level.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Level.swift; sourceTree = ""; }; 36 | D18776161F346CC9001F3E40 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = ""; }; 37 | /* End PBXFileReference section */ 38 | 39 | /* Begin PBXFrameworksBuildPhase section */ 40 | 9A9D8CF61F2C584F00C6A628 /* Frameworks */ = { 41 | isa = PBXFrameworksBuildPhase; 42 | buildActionMask = 2147483647; 43 | files = ( 44 | ); 45 | runOnlyForDeploymentPostprocessing = 0; 46 | }; 47 | /* End PBXFrameworksBuildPhase section */ 48 | 49 | /* Begin PBXGroup section */ 50 | 9A9D8CF01F2C584F00C6A628 = { 51 | isa = PBXGroup; 52 | children = ( 53 | 9A9D8CFB1F2C584F00C6A628 /* ShellGame */, 54 | 9A9D8CFA1F2C584F00C6A628 /* Products */, 55 | ); 56 | sourceTree = ""; 57 | }; 58 | 9A9D8CFA1F2C584F00C6A628 /* Products */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | 9A9D8CF91F2C584F00C6A628 /* ShellGame.app */, 62 | ); 63 | name = Products; 64 | sourceTree = ""; 65 | }; 66 | 9A9D8CFB1F2C584F00C6A628 /* ShellGame */ = { 67 | isa = PBXGroup; 68 | children = ( 69 | 9A9D8CFC1F2C584F00C6A628 /* AppDelegate.swift */, 70 | D11871011F3042DE00346E6E /* GameViewController.swift */, 71 | D11870FF1F2F6A0A00346E6E /* Permutation.swift */, 72 | D11871051F3352AD00346E6E /* Level.swift */, 73 | D18776161F346CC9001F3E40 /* Extensions.swift */, 74 | 8848E7DF1F41A92B0084DD5A /* RecordStorage.swift */, 75 | 8848E7E11F41AE190084DD5A /* AudioPlayer.swift */, 76 | 9A9D8CFE1F2C584F00C6A628 /* art.scnassets */, 77 | 9A9D8D021F2C584F00C6A628 /* Main.storyboard */, 78 | 9A9D8D051F2C584F00C6A628 /* Assets.xcassets */, 79 | 9A9D8D071F2C584F00C6A628 /* LaunchScreen.storyboard */, 80 | 9A9D8D0A1F2C584F00C6A628 /* Info.plist */, 81 | ); 82 | path = ShellGame; 83 | sourceTree = ""; 84 | }; 85 | /* End PBXGroup section */ 86 | 87 | /* Begin PBXNativeTarget section */ 88 | 9A9D8CF81F2C584F00C6A628 /* ShellGame */ = { 89 | isa = PBXNativeTarget; 90 | buildConfigurationList = 9A9D8D0D1F2C584F00C6A628 /* Build configuration list for PBXNativeTarget "ShellGame" */; 91 | buildPhases = ( 92 | 9A9D8CF51F2C584F00C6A628 /* Sources */, 93 | 9A9D8CF61F2C584F00C6A628 /* Frameworks */, 94 | 9A9D8CF71F2C584F00C6A628 /* Resources */, 95 | ); 96 | buildRules = ( 97 | ); 98 | dependencies = ( 99 | ); 100 | name = ShellGame; 101 | productName = ShellGame; 102 | productReference = 9A9D8CF91F2C584F00C6A628 /* ShellGame.app */; 103 | productType = "com.apple.product-type.application"; 104 | }; 105 | /* End PBXNativeTarget section */ 106 | 107 | /* Begin PBXProject section */ 108 | 9A9D8CF11F2C584F00C6A628 /* Project object */ = { 109 | isa = PBXProject; 110 | attributes = { 111 | LastSwiftUpdateCheck = 0900; 112 | LastUpgradeCheck = 0900; 113 | ORGANIZATIONNAME = Handsome; 114 | TargetAttributes = { 115 | 9A9D8CF81F2C584F00C6A628 = { 116 | CreatedOnToolsVersion = 9.0; 117 | }; 118 | }; 119 | }; 120 | buildConfigurationList = 9A9D8CF41F2C584F00C6A628 /* Build configuration list for PBXProject "ShellGame" */; 121 | compatibilityVersion = "Xcode 8.0"; 122 | developmentRegion = en; 123 | hasScannedForEncodings = 0; 124 | knownRegions = ( 125 | en, 126 | Base, 127 | ); 128 | mainGroup = 9A9D8CF01F2C584F00C6A628; 129 | productRefGroup = 9A9D8CFA1F2C584F00C6A628 /* Products */; 130 | projectDirPath = ""; 131 | projectRoot = ""; 132 | targets = ( 133 | 9A9D8CF81F2C584F00C6A628 /* ShellGame */, 134 | ); 135 | }; 136 | /* End PBXProject section */ 137 | 138 | /* Begin PBXResourcesBuildPhase section */ 139 | 9A9D8CF71F2C584F00C6A628 /* Resources */ = { 140 | isa = PBXResourcesBuildPhase; 141 | buildActionMask = 2147483647; 142 | files = ( 143 | 9A9D8CFF1F2C584F00C6A628 /* art.scnassets in Resources */, 144 | 9A9D8D091F2C584F00C6A628 /* LaunchScreen.storyboard in Resources */, 145 | 9A9D8D061F2C584F00C6A628 /* Assets.xcassets in Resources */, 146 | 9A9D8D041F2C584F00C6A628 /* Main.storyboard in Resources */, 147 | ); 148 | runOnlyForDeploymentPostprocessing = 0; 149 | }; 150 | /* End PBXResourcesBuildPhase section */ 151 | 152 | /* Begin PBXSourcesBuildPhase section */ 153 | 9A9D8CF51F2C584F00C6A628 /* Sources */ = { 154 | isa = PBXSourcesBuildPhase; 155 | buildActionMask = 2147483647; 156 | files = ( 157 | D18776171F346CC9001F3E40 /* Extensions.swift in Sources */, 158 | D11871021F3042DE00346E6E /* GameViewController.swift in Sources */, 159 | 8848E7E01F41A92B0084DD5A /* RecordStorage.swift in Sources */, 160 | D11871001F2F6A0A00346E6E /* Permutation.swift in Sources */, 161 | D11871061F3352AD00346E6E /* Level.swift in Sources */, 162 | 9A9D8CFD1F2C584F00C6A628 /* AppDelegate.swift in Sources */, 163 | 8848E7E21F41AE190084DD5A /* AudioPlayer.swift in Sources */, 164 | ); 165 | runOnlyForDeploymentPostprocessing = 0; 166 | }; 167 | /* End PBXSourcesBuildPhase section */ 168 | 169 | /* Begin PBXVariantGroup section */ 170 | 9A9D8D021F2C584F00C6A628 /* Main.storyboard */ = { 171 | isa = PBXVariantGroup; 172 | children = ( 173 | 9A9D8D031F2C584F00C6A628 /* Base */, 174 | ); 175 | name = Main.storyboard; 176 | sourceTree = ""; 177 | }; 178 | 9A9D8D071F2C584F00C6A628 /* LaunchScreen.storyboard */ = { 179 | isa = PBXVariantGroup; 180 | children = ( 181 | 9A9D8D081F2C584F00C6A628 /* Base */, 182 | ); 183 | name = LaunchScreen.storyboard; 184 | sourceTree = ""; 185 | }; 186 | /* End PBXVariantGroup section */ 187 | 188 | /* Begin XCBuildConfiguration section */ 189 | 9A9D8D0B1F2C584F00C6A628 /* Debug */ = { 190 | isa = XCBuildConfiguration; 191 | buildSettings = { 192 | ALWAYS_SEARCH_USER_PATHS = NO; 193 | CLANG_ANALYZER_NONNULL = YES; 194 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 195 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 196 | CLANG_CXX_LIBRARY = "libc++"; 197 | CLANG_ENABLE_MODULES = YES; 198 | CLANG_ENABLE_OBJC_ARC = YES; 199 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 200 | CLANG_WARN_BOOL_CONVERSION = YES; 201 | CLANG_WARN_COMMA = YES; 202 | CLANG_WARN_CONSTANT_CONVERSION = YES; 203 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 204 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 205 | CLANG_WARN_EMPTY_BODY = YES; 206 | CLANG_WARN_ENUM_CONVERSION = YES; 207 | CLANG_WARN_INFINITE_RECURSION = YES; 208 | CLANG_WARN_INT_CONVERSION = YES; 209 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 210 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 211 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 212 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 213 | CLANG_WARN_STRICT_PROTOTYPES = YES; 214 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 215 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 216 | CLANG_WARN_UNREACHABLE_CODE = YES; 217 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 218 | CODE_SIGN_IDENTITY = "iPhone Developer"; 219 | COPY_PHASE_STRIP = NO; 220 | DEBUG_INFORMATION_FORMAT = dwarf; 221 | ENABLE_STRICT_OBJC_MSGSEND = YES; 222 | ENABLE_TESTABILITY = YES; 223 | GCC_C_LANGUAGE_STANDARD = gnu11; 224 | GCC_DYNAMIC_NO_PIC = NO; 225 | GCC_NO_COMMON_BLOCKS = YES; 226 | GCC_OPTIMIZATION_LEVEL = 0; 227 | GCC_PREPROCESSOR_DEFINITIONS = ( 228 | "DEBUG=1", 229 | "$(inherited)", 230 | ); 231 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 232 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 233 | GCC_WARN_UNDECLARED_SELECTOR = YES; 234 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 235 | GCC_WARN_UNUSED_FUNCTION = YES; 236 | GCC_WARN_UNUSED_VARIABLE = YES; 237 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 238 | MTL_ENABLE_DEBUG_INFO = YES; 239 | ONLY_ACTIVE_ARCH = YES; 240 | SDKROOT = iphoneos; 241 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 242 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 243 | }; 244 | name = Debug; 245 | }; 246 | 9A9D8D0C1F2C584F00C6A628 /* Release */ = { 247 | isa = XCBuildConfiguration; 248 | buildSettings = { 249 | ALWAYS_SEARCH_USER_PATHS = NO; 250 | CLANG_ANALYZER_NONNULL = YES; 251 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 252 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 253 | CLANG_CXX_LIBRARY = "libc++"; 254 | CLANG_ENABLE_MODULES = YES; 255 | CLANG_ENABLE_OBJC_ARC = YES; 256 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 257 | CLANG_WARN_BOOL_CONVERSION = YES; 258 | CLANG_WARN_COMMA = YES; 259 | CLANG_WARN_CONSTANT_CONVERSION = YES; 260 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 261 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 262 | CLANG_WARN_EMPTY_BODY = YES; 263 | CLANG_WARN_ENUM_CONVERSION = YES; 264 | CLANG_WARN_INFINITE_RECURSION = YES; 265 | CLANG_WARN_INT_CONVERSION = YES; 266 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 267 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 268 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 269 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 270 | CLANG_WARN_STRICT_PROTOTYPES = YES; 271 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 272 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 273 | CLANG_WARN_UNREACHABLE_CODE = YES; 274 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 275 | CODE_SIGN_IDENTITY = "iPhone Developer"; 276 | COPY_PHASE_STRIP = NO; 277 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 278 | ENABLE_NS_ASSERTIONS = NO; 279 | ENABLE_STRICT_OBJC_MSGSEND = YES; 280 | GCC_C_LANGUAGE_STANDARD = gnu11; 281 | GCC_NO_COMMON_BLOCKS = YES; 282 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 283 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 284 | GCC_WARN_UNDECLARED_SELECTOR = YES; 285 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 286 | GCC_WARN_UNUSED_FUNCTION = YES; 287 | GCC_WARN_UNUSED_VARIABLE = YES; 288 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 289 | MTL_ENABLE_DEBUG_INFO = NO; 290 | SDKROOT = iphoneos; 291 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 292 | VALIDATE_PRODUCT = YES; 293 | }; 294 | name = Release; 295 | }; 296 | 9A9D8D0E1F2C584F00C6A628 /* Debug */ = { 297 | isa = XCBuildConfiguration; 298 | buildSettings = { 299 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 300 | DEVELOPMENT_TEAM = 6Y3UWZWJMU; 301 | INFOPLIST_FILE = ShellGame/Info.plist; 302 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 303 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 304 | OTHER_SWIFT_FLAGS = ""; 305 | PRODUCT_BUNDLE_IDENTIFIER = is.handsome.swift.ShellGame; 306 | PRODUCT_NAME = "$(TARGET_NAME)"; 307 | SWIFT_VERSION = 4.0; 308 | TARGETED_DEVICE_FAMILY = "1,2"; 309 | }; 310 | name = Debug; 311 | }; 312 | 9A9D8D0F1F2C584F00C6A628 /* Release */ = { 313 | isa = XCBuildConfiguration; 314 | buildSettings = { 315 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 316 | DEVELOPMENT_TEAM = 6Y3UWZWJMU; 317 | INFOPLIST_FILE = ShellGame/Info.plist; 318 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 319 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 320 | PRODUCT_BUNDLE_IDENTIFIER = is.handsome.swift.ShellGame; 321 | PRODUCT_NAME = "$(TARGET_NAME)"; 322 | SWIFT_VERSION = 4.0; 323 | TARGETED_DEVICE_FAMILY = "1,2"; 324 | }; 325 | name = Release; 326 | }; 327 | /* End XCBuildConfiguration section */ 328 | 329 | /* Begin XCConfigurationList section */ 330 | 9A9D8CF41F2C584F00C6A628 /* Build configuration list for PBXProject "ShellGame" */ = { 331 | isa = XCConfigurationList; 332 | buildConfigurations = ( 333 | 9A9D8D0B1F2C584F00C6A628 /* Debug */, 334 | 9A9D8D0C1F2C584F00C6A628 /* Release */, 335 | ); 336 | defaultConfigurationIsVisible = 0; 337 | defaultConfigurationName = Release; 338 | }; 339 | 9A9D8D0D1F2C584F00C6A628 /* Build configuration list for PBXNativeTarget "ShellGame" */ = { 340 | isa = XCConfigurationList; 341 | buildConfigurations = ( 342 | 9A9D8D0E1F2C584F00C6A628 /* Debug */, 343 | 9A9D8D0F1F2C584F00C6A628 /* Release */, 344 | ); 345 | defaultConfigurationIsVisible = 0; 346 | defaultConfigurationName = Release; 347 | }; 348 | /* End XCConfigurationList section */ 349 | }; 350 | rootObject = 9A9D8CF11F2C584F00C6A628 /* Project object */; 351 | } 352 | -------------------------------------------------------------------------------- /ShellGame.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /ShellGame/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2017 Handsome. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | ///////////////////////////////////////////////////////////////////////////// 17 | 18 | import UIKit 19 | 20 | @UIApplicationMain 21 | class AppDelegate: UIResponder, UIApplicationDelegate { 22 | 23 | var window: UIWindow? 24 | 25 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 26 | return true 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /ShellGame/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "size" : "60x60", 35 | "idiom" : "iphone", 36 | "filename" : "shell-game-120.png", 37 | "scale" : "2x" 38 | }, 39 | { 40 | "size" : "60x60", 41 | "idiom" : "iphone", 42 | "filename" : "shell-game-180.png", 43 | "scale" : "3x" 44 | }, 45 | { 46 | "idiom" : "ipad", 47 | "size" : "20x20", 48 | "scale" : "1x" 49 | }, 50 | { 51 | "idiom" : "ipad", 52 | "size" : "20x20", 53 | "scale" : "2x" 54 | }, 55 | { 56 | "idiom" : "ipad", 57 | "size" : "29x29", 58 | "scale" : "1x" 59 | }, 60 | { 61 | "idiom" : "ipad", 62 | "size" : "29x29", 63 | "scale" : "2x" 64 | }, 65 | { 66 | "idiom" : "ipad", 67 | "size" : "40x40", 68 | "scale" : "1x" 69 | }, 70 | { 71 | "idiom" : "ipad", 72 | "size" : "40x40", 73 | "scale" : "2x" 74 | }, 75 | { 76 | "size" : "76x76", 77 | "idiom" : "ipad", 78 | "filename" : "shell-game-76.png", 79 | "scale" : "1x" 80 | }, 81 | { 82 | "size" : "76x76", 83 | "idiom" : "ipad", 84 | "filename" : "shell-game-152.png", 85 | "scale" : "2x" 86 | }, 87 | { 88 | "size" : "83.5x83.5", 89 | "idiom" : "ipad", 90 | "filename" : "shell-game-167.png", 91 | "scale" : "2x" 92 | }, 93 | { 94 | "size" : "1024x1024", 95 | "idiom" : "ios-marketing", 96 | "filename" : "shell-game-1024.png", 97 | "scale" : "1x" 98 | } 99 | ], 100 | "info" : { 101 | "version" : 1, 102 | "author" : "xcode" 103 | } 104 | } -------------------------------------------------------------------------------- /ShellGame/Assets.xcassets/AppIcon.appiconset/shell-game-1024.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/handsomecode/arkit-shell-game/23fd73bf6bd7da3203abab1364b3f4c08c2f26c4/ShellGame/Assets.xcassets/AppIcon.appiconset/shell-game-1024.png -------------------------------------------------------------------------------- /ShellGame/Assets.xcassets/AppIcon.appiconset/shell-game-120.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/handsomecode/arkit-shell-game/23fd73bf6bd7da3203abab1364b3f4c08c2f26c4/ShellGame/Assets.xcassets/AppIcon.appiconset/shell-game-120.png -------------------------------------------------------------------------------- /ShellGame/Assets.xcassets/AppIcon.appiconset/shell-game-152.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/handsomecode/arkit-shell-game/23fd73bf6bd7da3203abab1364b3f4c08c2f26c4/ShellGame/Assets.xcassets/AppIcon.appiconset/shell-game-152.png -------------------------------------------------------------------------------- /ShellGame/Assets.xcassets/AppIcon.appiconset/shell-game-167.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/handsomecode/arkit-shell-game/23fd73bf6bd7da3203abab1364b3f4c08c2f26c4/ShellGame/Assets.xcassets/AppIcon.appiconset/shell-game-167.png -------------------------------------------------------------------------------- /ShellGame/Assets.xcassets/AppIcon.appiconset/shell-game-180.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/handsomecode/arkit-shell-game/23fd73bf6bd7da3203abab1364b3f4c08c2f26c4/ShellGame/Assets.xcassets/AppIcon.appiconset/shell-game-180.png -------------------------------------------------------------------------------- /ShellGame/Assets.xcassets/AppIcon.appiconset/shell-game-76.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/handsomecode/arkit-shell-game/23fd73bf6bd7da3203abab1364b3f4c08c2f26c4/ShellGame/Assets.xcassets/AppIcon.appiconset/shell-game-76.png -------------------------------------------------------------------------------- /ShellGame/AudioPlayer.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2017 Handsome. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | ///////////////////////////////////////////////////////////////////////////// 17 | 18 | import Foundation 19 | import SceneKit 20 | 21 | class AudioPlayer { 22 | 23 | enum Sound { 24 | case success 25 | case fail 26 | } 27 | 28 | static let shared = AudioPlayer() 29 | 30 | private lazy var successAudioSource: SCNAudioSource = { 31 | let sound = SCNAudioSource(fileNamed: "art.scnassets/sounds/success.wav")! 32 | sound.volume = 1 33 | sound.isPositional = true 34 | sound.shouldStream = false 35 | sound.load() 36 | return sound 37 | }() 38 | 39 | private lazy var failAudioSource: SCNAudioSource = { 40 | let sound = SCNAudioSource(fileNamed: "art.scnassets/sounds/fail.mp3")! 41 | sound.volume = 1 42 | sound.isPositional = true 43 | sound.shouldStream = false 44 | sound.load() 45 | return sound 46 | }() 47 | 48 | private init() {} 49 | 50 | func playSound(_ sound: Sound, on node: SCNNode) { 51 | let action = SCNAction.playAudio(audioSourceForSound(sound), 52 | waitForCompletion: false) 53 | node.runAction(action) 54 | } 55 | 56 | private func audioSourceForSound(_ sound: Sound) -> SCNAudioSource { 57 | switch sound { 58 | case .success: 59 | return successAudioSource 60 | case .fail: 61 | return failAudioSource 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /ShellGame/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /ShellGame/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 33 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /ShellGame/Extensions.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2017 Handsome. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | ///////////////////////////////////////////////////////////////////////////// 17 | 18 | import UIKit 19 | import SceneKit 20 | 21 | extension SCNVector3 { 22 | func length() -> Float { 23 | return sqrtf(x*x + y*y + z*z) 24 | } 25 | } 26 | 27 | extension Int { 28 | func sign() -> Int { 29 | return (self < 0 ? -1 : 1) 30 | } 31 | } 32 | 33 | extension Float { 34 | func sign() -> Float { 35 | return (self < 0 ? -1.0 : 1.0) 36 | } 37 | } 38 | 39 | extension Level { 40 | static func generate(number: Int) -> Level { 41 | 42 | func randomPermutation() -> Permutation { 43 | let random = arc4random_uniform(3) 44 | if random == 0 { 45 | return Permutation([1, 0, 2]) 46 | } else if random == 1 { 47 | return Permutation([0, 2, 1]) 48 | } else { 49 | return Permutation([2, 1, 0]) 50 | } 51 | } 52 | let speed = 0.5 + log(1 + Float(number*number) / 2) / 5 53 | return Level(steps: (0..() 56 | for from in 0.. Void) -> SCNAction { 72 | var executed = false 73 | return SCNAction.customAction(duration: 0.0) { node, _ in 74 | if !executed { 75 | block(node) 76 | executed = true 77 | } 78 | } 79 | } 80 | 81 | class func permutate(index: Int, with step: LevelStep, to distance: Float) -> SCNAction { 82 | let from = index 83 | let to = step.permutation[from] 84 | let toto = step.permutation[to] 85 | let halfDistance = distance * 0.5 86 | if to == from { 87 | return SCNAction.wait(duration: 0.0) 88 | } 89 | if toto == from { 90 | let prePivot = SCNAction.single() { node in 91 | let direction = Float((to - from).sign()) * cos(node.rotation.w).sign() 92 | let offset = direction * halfDistance * Float(abs(to - from)) 93 | node.pivot = SCNMatrix4MakeTranslation(offset, 0.0, 0.0) 94 | node.localTranslate(by: SCNVector3(offset, 0.0, 0.0)) 95 | } 96 | 97 | let pathLength = .pi * halfDistance * Float(abs(to - from)) 98 | let duration = TimeInterval(pathLength / step.speed) 99 | let rotationDirection = CGFloat(step.clockwise.contains(from) ? -1 : 1) 100 | let rotate = SCNAction.rotate(by: .pi * rotationDirection, around: SCNVector3(0, 1, 0), duration: duration) 101 | 102 | let postPivot = SCNAction.single() { node in 103 | node.localTranslate(by: SCNVector3(-node.pivot.m41, 0.0, 0.0)) 104 | node.pivot = SCNMatrix4Identity 105 | } 106 | 107 | return SCNAction.sequence([prePivot, rotate, postPivot]) 108 | } else { 109 | fatalError("unsupported permutation. to be added later") 110 | } 111 | } 112 | } 113 | 114 | extension UIView { 115 | func roundCorners(radius: CGFloat) { 116 | self.layer.cornerRadius = radius 117 | self.clipsToBounds = true 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /ShellGame/GameViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2017 Handsome. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | ///////////////////////////////////////////////////////////////////////////// 17 | 18 | import Foundation 19 | import ARKit 20 | 21 | enum GameState: Int { 22 | case position = 0 23 | case start 24 | case inAction 25 | case select 26 | case next 27 | } 28 | 29 | class GameViewContoller: UIViewController { 30 | @IBOutlet private weak var sceneView: ARSCNView! 31 | @IBOutlet private weak var scoreLabel: UILabel! 32 | @IBOutlet private weak var recordLabel: UILabel! 33 | @IBOutlet private weak var informationLabel: UILabel! 34 | @IBOutlet private weak var informationContainerView: UIView! 35 | @IBOutlet private weak var resultsPanelView: UIView! 36 | @IBOutlet private weak var informationCenterYConstraint: NSLayoutConstraint! 37 | 38 | private let cupsNumber = 3 39 | private var cupsPermutation = Permutation([0, 1, 2]) 40 | 41 | private var state: GameState = .position { 42 | didSet { 43 | handleGameState() 44 | } 45 | } 46 | 47 | private var record = 0 { 48 | didSet { 49 | DispatchQueue.main.async { 50 | self.recordLabel.text = " Record: \(self.record) " 51 | } 52 | } 53 | } 54 | 55 | private var levelNumber = 1 { 56 | didSet { 57 | score = score + (levelNumber - oldValue) * 10 58 | } 59 | } 60 | 61 | private var score = 0 { 62 | didSet { 63 | DispatchQueue.main.async { 64 | self.scoreLabel.text = " Score: \(self.score) " 65 | if self.record < self.score { 66 | self.record = self.score 67 | RecordStorage.shared.save(record: self.record) 68 | } 69 | } 70 | } 71 | } 72 | 73 | private lazy var scene: SCNScene = { 74 | guard let scene = SCNScene(named: "art.scnassets/game.scn") else { 75 | preconditionFailure("Game scene not found") 76 | } 77 | return scene 78 | }() 79 | 80 | private lazy var gameNode: SCNNode = { 81 | guard let node = scene.rootNode.childNode(withName: "game", recursively: true) else { 82 | preconditionFailure("Game node not found") 83 | } 84 | node.position = SCNVector3 (0, -0.1, -0.5) 85 | return node 86 | }() 87 | 88 | private lazy var ballNode: SCNNode = { 89 | guard let node = scene.rootNode.childNode(withName: "ball", recursively: true) else { 90 | preconditionFailure("Ball node not found") 91 | } 92 | return node 93 | }() 94 | 95 | private lazy var cupsNodes = findCupsNodes(in: gameNode) 96 | private lazy var indexOfCupWithBall = Int(arc4random_uniform(UInt32(cupsNumber))) 97 | 98 | override func viewDidLoad() { 99 | super.viewDidLoad() 100 | 101 | sceneView.scene = scene 102 | sceneView.delegate = self 103 | sceneView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tappedInSceneView))) 104 | 105 | resultsPanelView.isHidden = true 106 | informationContainerView.alpha = 0 107 | 108 | record = RecordStorage.shared.load() 109 | 110 | showMessage("Move device to find a plane", animated: true, duration: 2) 111 | showDebuggingIfNeeded() 112 | } 113 | 114 | override func viewWillAppear(_ animated: Bool) { 115 | super.viewWillAppear(animated) 116 | 117 | let configuration = ARWorldTrackingConfiguration() 118 | configuration.planeDetection = .horizontal 119 | sceneView.session.run(configuration) 120 | } 121 | 122 | override func viewDidLayoutSubviews() { 123 | super.viewDidLayoutSubviews() 124 | 125 | scoreLabel.roundCorners(radius: 4) 126 | recordLabel.roundCorners(radius: 4) 127 | informationContainerView.roundCorners(radius: 10) 128 | 129 | updatePositionOfInformationView() 130 | } 131 | 132 | override func viewWillDisappear(_ animated: Bool) { 133 | super.viewWillDisappear(animated) 134 | sceneView.session.pause() 135 | } 136 | 137 | private func handleGameState() { 138 | switch state { 139 | case .inAction: 140 | DispatchQueue.main.async { 141 | self.informationContainerView.alpha = 0.0 142 | } 143 | self.showMessage("") 144 | case .select: 145 | DispatchQueue.main.async { 146 | self.informationContainerView.alpha = 1.0 147 | } 148 | self.showMessage("Choose a cup") 149 | case .start: 150 | DispatchQueue.main.async { 151 | self.resultsPanelView.isHidden = false 152 | self.informationCenterYConstraint.constant = self.view.frame.height/3 153 | self.view.layoutIfNeeded() 154 | } 155 | self.showMessage("Tap to start", animated: true, duration: 0.3) 156 | case .next: 157 | DispatchQueue.main.async { 158 | self.informationContainerView.alpha = 1.0 159 | } 160 | self.showMessage("Tap to continue") 161 | case .position: fallthrough 162 | default: break 163 | } 164 | } 165 | 166 | @objc private func tappedInSceneView(recognizer: UIGestureRecognizer) { 167 | if state == .position { 168 | let touchLocation = recognizer.location(in: sceneView) 169 | let hitTestResult = sceneView.hitTest(touchLocation, types: .existingPlane) 170 | 171 | guard let hitResult = hitTestResult.first else { 172 | print("HitResult is empty") 173 | return 174 | } 175 | 176 | gameNode.transform = SCNMatrix4(hitResult.worldTransform) 177 | let camera = self.sceneView.pointOfView! 178 | gameNode.rotation = SCNVector4(0, 1, 0, camera.rotation.y) 179 | gameNode.isHidden = false 180 | 181 | let configuration = ARWorldTrackingConfiguration() 182 | sceneView.session.run(configuration) 183 | 184 | state = .start 185 | } else if state == .start { 186 | state = .inAction 187 | let elevation = 0.075 188 | let moveCupUp = SCNAction.moveBy(x: 0, y: CGFloat(elevation), z: 0, duration: 0.5) 189 | let ballStartPosition = cupsNodes[indexOfCupWithBall].convertPosition(SCNVector3(0, 0, 0), to: gameNode) 190 | let moveBall = SCNAction.move(to: ballStartPosition, duration: 0.5) 191 | let moveCupDown = SCNAction.moveBy(x: 0, y: -CGFloat(elevation), z: 0, duration: 0.5) 192 | 193 | cupsNodes[indexOfCupWithBall].runAction(SCNAction.sequence([moveCupUp, 194 | SCNAction.wait(duration: moveBall.duration), 195 | moveCupDown])) { 196 | self.run() 197 | } 198 | ballNode.runAction(SCNAction.sequence([SCNAction.wait(duration: moveCupUp.duration), 199 | moveBall])) 200 | 201 | } else if state == .select { 202 | let touchLocation = recognizer.location(in: sceneView) 203 | let hitTestResult = sceneView.hitTest(touchLocation, options: [:]) 204 | 205 | guard let hitResult = hitTestResult.first else { 206 | print("HitResult is empty") 207 | return 208 | } 209 | 210 | if let cup = hitResult.node.parent, let selectedCup = cupsNodes.index(of: cup) { 211 | let actualBallPosition = cupsPermutation[indexOfCupWithBall] 212 | let selectedBallPosition = cupsPermutation[selectedCup] 213 | print ("actualBallPosition: \(actualBallPosition). selectedBallPosition: \(selectedBallPosition)") 214 | ballNode.position = cupsNodes[indexOfCupWithBall].convertPosition(SCNVector3(0, 0, 0), to: self.gameNode) 215 | ballNode.isHidden = false 216 | let cupWithBall = cupsNodes[indexOfCupWithBall] 217 | if selectedBallPosition == actualBallPosition { 218 | state = .inAction 219 | AudioPlayer.shared.playSound(.success, on: sceneView.scene.rootNode) 220 | let moveCupUp = SCNAction.moveBy(x: 0, y: CGFloat(0.075), z: 0, duration: 0.5) 221 | cupWithBall.runAction(moveCupUp) { 222 | self.levelNumber = self.levelNumber + 1 223 | self.state = .next 224 | } 225 | } else { 226 | self.score = max(0, self.score - 5) 227 | AudioPlayer.shared.playSound(.fail, on: sceneView.scene.rootNode) 228 | } 229 | } 230 | } else if state == .next { 231 | state = .inAction 232 | let cupWithBall = cupsNodes[indexOfCupWithBall] 233 | let moveCupDown = SCNAction.moveBy(x: 0, y: -CGFloat(0.075), z: 0, duration: 0.5) 234 | cupWithBall.runAction(moveCupDown) { 235 | self.run() 236 | } 237 | } 238 | } 239 | 240 | private func run() { 241 | ballNode.isHidden = true 242 | run(level: Level.generate(number: levelNumber)) { 243 | self.state = .select 244 | } 245 | } 246 | 247 | private func run(level: Level, completionHandler: @escaping () -> Void) { 248 | var stepToRun = 0 249 | 250 | var innerCompletionHandler = {} 251 | innerCompletionHandler = { 252 | stepToRun = stepToRun + 1 253 | if stepToRun < level.steps.count { 254 | self.run(levelStep: level.steps[stepToRun], completionHandler: innerCompletionHandler) 255 | } else { 256 | completionHandler() 257 | } 258 | } 259 | run(levelStep: level.steps[stepToRun], completionHandler: innerCompletionHandler) 260 | } 261 | 262 | private func run(levelStep: LevelStep, completionHandler: @escaping() -> Void){ 263 | var completionCounter = 0 264 | let proxyCompletionHandler = { 265 | completionCounter = completionCounter + 1 266 | if completionCounter == self.cupsNodes.count { 267 | self.cupsPermutation = Permutation.mult(self.cupsPermutation, levelStep.permutation) 268 | completionHandler() 269 | } 270 | } 271 | 272 | let invertedCupsPermutation = Permutation.invert(cupsPermutation) 273 | for index in 0.. [SCNNode] { 305 | var cupsNodes = [SCNNode]() 306 | for i in 0.. 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleDisplayName 8 | ShellGame 9 | CFBundleExecutable 10 | $(EXECUTABLE_NAME) 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | $(PRODUCT_NAME) 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | NSCameraUsageDescription 26 | This application will use the camera for Augmented Reality. 27 | UILaunchStoryboardName 28 | LaunchScreen 29 | UIMainStoryboardFile 30 | Main 31 | UIRequiredDeviceCapabilities 32 | 33 | armv7 34 | 35 | UIStatusBarHidden 36 | 37 | UISupportedInterfaceOrientations 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationLandscapeLeft 41 | UIInterfaceOrientationLandscapeRight 42 | 43 | UISupportedInterfaceOrientations~ipad 44 | 45 | UIInterfaceOrientationPortrait 46 | UIInterfaceOrientationPortraitUpsideDown 47 | UIInterfaceOrientationLandscapeLeft 48 | UIInterfaceOrientationLandscapeRight 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /ShellGame/Level.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2017 Handsome. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | ///////////////////////////////////////////////////////////////////////////// 17 | 18 | import Foundation 19 | 20 | struct LevelStep { 21 | typealias Speed = Float 22 | 23 | let permutation: Permutation 24 | let speed: Speed 25 | let clockwise: Set 26 | 27 | init(permutation: Permutation, speed: Speed = 1.0, clockwise: Set = Set()) { 28 | self.permutation = permutation 29 | self.speed = speed 30 | self.clockwise = clockwise 31 | } 32 | } 33 | 34 | struct Level { 35 | let steps: [LevelStep] 36 | } 37 | -------------------------------------------------------------------------------- /ShellGame/Permutation.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2017 Handsome. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | ///////////////////////////////////////////////////////////////////////////// 17 | 18 | import Foundation 19 | 20 | /** 21 | Represents algebraic Permutation - https://en.wikipedia.org/wiki/Permutation 22 | */ 23 | class Permutation: NSObject { 24 | private let array: [Int] 25 | 26 | var count: Int { 27 | return array.count 28 | } 29 | 30 | subscript(index: Int) -> Int { 31 | get { 32 | return array[index] 33 | } 34 | } 35 | 36 | init (_ permutation: [Int]) { 37 | array = permutation 38 | } 39 | 40 | /** 41 | Return the product of two permutations. 42 | 43 | Note: operation is not commutative. For example: 44 | - mult([1, 0, 2], [2, 1, 0]) = [1, 2, 0] 45 | - mult([2, 1, 0], [1, 0, 2]) = [2, 0, 1] 46 | 47 | - parameter a: The multiplicand, or left operand of permutation multiplication. 48 | - parameter b: The multiplier, or right operand of permutation multiplication. 49 | */ 50 | static func mult(_ a: Permutation, _ b: Permutation) -> Permutation { 51 | if a.count != b.count { 52 | fatalError("multiplication of unequal sized permutations are not supported") 53 | } 54 | var resultArray = Array(0.. Permutation { 71 | var result = [Int](repeating: -1, count: a.count) 72 | for (index, value) in a.array.enumerated() { 73 | result[value] = index 74 | } 75 | return Permutation(result) 76 | } 77 | 78 | override var description : String { 79 | var result = "Permutation(" 80 | for index in 0.. \(array[index])" 82 | if index < array.count - 1 { 83 | result += "; " 84 | } 85 | } 86 | return result + ")" 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /ShellGame/RecordStorage.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2017 Handsome. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | ///////////////////////////////////////////////////////////////////////////// 17 | 18 | import Foundation 19 | 20 | class RecordStorage { 21 | 22 | static let shared = RecordStorage() 23 | 24 | private let recordKey = "record" 25 | private lazy var userDefaults = UserDefaults.standard 26 | 27 | private init(){} 28 | 29 | func save(record: Int) { 30 | userDefaults.set(record, forKey: recordKey) 31 | userDefaults.synchronize() 32 | } 33 | 34 | func load() -> Int { 35 | guard let recordString = userDefaults.string(forKey: recordKey), 36 | let record = Int(recordString) else { 37 | return 0 38 | } 39 | return record 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /ShellGame/art.scnassets/Settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "interleaveGeometrySources" : true, 3 | "forceYUp" : true, 4 | "preferCompressedTextures" : true 5 | } -------------------------------------------------------------------------------- /ShellGame/art.scnassets/game.scn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/handsomecode/arkit-shell-game/23fd73bf6bd7da3203abab1364b3f4c08c2f26c4/ShellGame/art.scnassets/game.scn -------------------------------------------------------------------------------- /ShellGame/art.scnassets/sounds/fail.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/handsomecode/arkit-shell-game/23fd73bf6bd7da3203abab1364b3f4c08c2f26c4/ShellGame/art.scnassets/sounds/fail.mp3 -------------------------------------------------------------------------------- /ShellGame/art.scnassets/sounds/success.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/handsomecode/arkit-shell-game/23fd73bf6bd7da3203abab1364b3f4c08c2f26c4/ShellGame/art.scnassets/sounds/success.wav -------------------------------------------------------------------------------- /ShellGame/art.scnassets/textures/plastic_cup_green_diffuce.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/handsomecode/arkit-shell-game/23fd73bf6bd7da3203abab1364b3f4c08c2f26c4/ShellGame/art.scnassets/textures/plastic_cup_green_diffuce.jpg -------------------------------------------------------------------------------- /ShellGame/art.scnassets/textures/plastic_cup_opaque_bump.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/handsomecode/arkit-shell-game/23fd73bf6bd7da3203abab1364b3f4c08c2f26c4/ShellGame/art.scnassets/textures/plastic_cup_opaque_bump.jpg -------------------------------------------------------------------------------- /ShellGame/art.scnassets/textures/plastic_cup_opaque_norm.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/handsomecode/arkit-shell-game/23fd73bf6bd7da3203abab1364b3f4c08c2f26c4/ShellGame/art.scnassets/textures/plastic_cup_opaque_norm.jpg --------------------------------------------------------------------------------