diff --git a/Examples/2048 Game/SwiftUI2048.xcodeproj/project.pbxproj b/Examples/2048 Game/SwiftUI2048.xcodeproj/project.pbxproj new file mode 100755 index 0000000..b32c81b --- /dev/null +++ b/Examples/2048 Game/SwiftUI2048.xcodeproj/project.pbxproj @@ -0,0 +1,407 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 50; + objects = { + +/* Begin PBXBuildFile section */ + FA132EFA22A94D9400BD9743 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = FA132EF922A94D9400BD9743 /* MainMenu.xib */; }; + FA633E1F22A77D1C00DE6288 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA633E1E22A77D1C00DE6288 /* AppDelegate.swift */; }; + FA633E2522A77D1D00DE6288 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FA633E2422A77D1D00DE6288 /* Assets.xcassets */; }; + FA633E2822A77D1D00DE6288 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FA633E2722A77D1D00DE6288 /* Preview Assets.xcassets */; }; + FA633E2B22A77D1D00DE6288 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FA633E2922A77D1D00DE6288 /* LaunchScreen.storyboard */; }; + FA633E3422A77D5700DE6288 /* BlockView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA633E3322A77D5700DE6288 /* BlockView.swift */; }; + FA633E3722A7928500DE6288 /* BlockMatrix.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA633E3622A7928400DE6288 /* BlockMatrix.swift */; }; + FA633E3922A79AFA00DE6288 /* GameLogic.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA633E3822A79AFA00DE6288 /* GameLogic.swift */; }; + FA633E3B22A79B1200DE6288 /* GameView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA633E3A22A79B1200DE6288 /* GameView.swift */; }; + FA633E3D22A79B3C00DE6288 /* BlockGridView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA633E3C22A79B3C00DE6288 /* BlockGridView.swift */; }; + FA633E3F22A7BDBB00DE6288 /* IdentifiedBlock.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA633E3E22A7BDBB00DE6288 /* IdentifiedBlock.swift */; }; + FA633E4622A800AB00DE6288 /* Screenshot.png in Resources */ = {isa = PBXBuildFile; fileRef = FA633E4522A800AB00DE6288 /* Screenshot.png */; }; + FA633E4822A8019D00DE6288 /* LICENSE in Resources */ = {isa = PBXBuildFile; fileRef = FA633E4722A8019D00DE6288 /* LICENSE */; }; + FA6F2B2122A81832009C2D3B /* FunctionalUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA6F2B2022A81832009C2D3B /* FunctionalUtils.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + FA132EF822A90EFD00BD9743 /* SwiftUI2048.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = SwiftUI2048.entitlements; sourceTree = ""; }; + FA132EF922A94D9400BD9743 /* MainMenu.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = MainMenu.xib; sourceTree = ""; }; + FA633E1B22A77D1C00DE6288 /* 2048.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = 2048.app; sourceTree = BUILT_PRODUCTS_DIR; }; + FA633E1E22A77D1C00DE6288 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + FA633E2422A77D1D00DE6288 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + FA633E2722A77D1D00DE6288 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; + FA633E2A22A77D1D00DE6288 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; + FA633E2C22A77D1D00DE6288 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + FA633E3322A77D5700DE6288 /* BlockView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlockView.swift; sourceTree = ""; }; + FA633E3622A7928400DE6288 /* BlockMatrix.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlockMatrix.swift; sourceTree = ""; }; + FA633E3822A79AFA00DE6288 /* GameLogic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameLogic.swift; sourceTree = ""; }; + FA633E3A22A79B1200DE6288 /* GameView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameView.swift; sourceTree = ""; }; + FA633E3C22A79B3C00DE6288 /* BlockGridView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlockGridView.swift; sourceTree = ""; }; + FA633E3E22A7BDBB00DE6288 /* IdentifiedBlock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IdentifiedBlock.swift; sourceTree = ""; }; + FA633E4222A7FFD000DE6288 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; + FA633E4522A800AB00DE6288 /* Screenshot.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Screenshot.png; sourceTree = ""; }; + FA633E4722A8019D00DE6288 /* LICENSE */ = {isa = PBXFileReference; lastKnownFileType = text; path = LICENSE; sourceTree = ""; }; + FA6F2B2022A81832009C2D3B /* FunctionalUtils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FunctionalUtils.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + FA633E1822A77D1C00DE6288 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + FA633E1222A77D1C00DE6288 = { + isa = PBXGroup; + children = ( + FA633E4722A8019D00DE6288 /* LICENSE */, + FA633E4222A7FFD000DE6288 /* README.md */, + FA633E4522A800AB00DE6288 /* Screenshot.png */, + FA633E1D22A77D1C00DE6288 /* SwiftUI2048 */, + FA633E1C22A77D1C00DE6288 /* Products */, + ); + sourceTree = ""; + }; + FA633E1C22A77D1C00DE6288 /* Products */ = { + isa = PBXGroup; + children = ( + FA633E1B22A77D1C00DE6288 /* 2048.app */, + ); + name = Products; + sourceTree = ""; + }; + FA633E1D22A77D1C00DE6288 /* SwiftUI2048 */ = { + isa = PBXGroup; + children = ( + FA132EF822A90EFD00BD9743 /* SwiftUI2048.entitlements */, + FA6F2B2022A81832009C2D3B /* FunctionalUtils.swift */, + FA633E3522A7926200DE6288 /* Models */, + FA633E3222A77D3400DE6288 /* Views */, + FA633E1E22A77D1C00DE6288 /* AppDelegate.swift */, + FA633E2422A77D1D00DE6288 /* Assets.xcassets */, + FA633E2922A77D1D00DE6288 /* LaunchScreen.storyboard */, + FA132EF922A94D9400BD9743 /* MainMenu.xib */, + FA633E2C22A77D1D00DE6288 /* Info.plist */, + FA633E2622A77D1D00DE6288 /* Preview Content */, + ); + path = SwiftUI2048; + sourceTree = ""; + }; + FA633E2622A77D1D00DE6288 /* Preview Content */ = { + isa = PBXGroup; + children = ( + FA633E2722A77D1D00DE6288 /* Preview Assets.xcassets */, + ); + path = "Preview Content"; + sourceTree = ""; + }; + FA633E3222A77D3400DE6288 /* Views */ = { + isa = PBXGroup; + children = ( + FA633E3322A77D5700DE6288 /* BlockView.swift */, + FA633E3C22A79B3C00DE6288 /* BlockGridView.swift */, + FA633E3A22A79B1200DE6288 /* GameView.swift */, + ); + path = Views; + sourceTree = ""; + }; + FA633E3522A7926200DE6288 /* Models */ = { + isa = PBXGroup; + children = ( + FA633E3622A7928400DE6288 /* BlockMatrix.swift */, + FA633E3822A79AFA00DE6288 /* GameLogic.swift */, + FA633E3E22A7BDBB00DE6288 /* IdentifiedBlock.swift */, + ); + path = Models; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + FA633E1A22A77D1C00DE6288 /* 2048 */ = { + isa = PBXNativeTarget; + buildConfigurationList = FA633E2F22A77D1D00DE6288 /* Build configuration list for PBXNativeTarget "2048" */; + buildPhases = ( + FA633E1722A77D1C00DE6288 /* Sources */, + FA633E1822A77D1C00DE6288 /* Frameworks */, + FA633E1922A77D1C00DE6288 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = 2048; + productName = SwiftUI2048; + productReference = FA633E1B22A77D1C00DE6288 /* 2048.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + FA633E1322A77D1C00DE6288 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 1100; + LastUpgradeCheck = 1100; + ORGANIZATIONNAME = Cyandev; + TargetAttributes = { + FA633E1A22A77D1C00DE6288 = { + CreatedOnToolsVersion = 11.0; + }; + }; + }; + buildConfigurationList = FA633E1622A77D1C00DE6288 /* Build configuration list for PBXProject "SwiftUI2048" */; + compatibilityVersion = "Xcode 9.3"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = FA633E1222A77D1C00DE6288; + productRefGroup = FA633E1C22A77D1C00DE6288 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + FA633E1A22A77D1C00DE6288 /* 2048 */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + FA633E1922A77D1C00DE6288 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + FA633E2B22A77D1D00DE6288 /* LaunchScreen.storyboard in Resources */, + FA633E2822A77D1D00DE6288 /* Preview Assets.xcassets in Resources */, + FA633E2522A77D1D00DE6288 /* Assets.xcassets in Resources */, + FA633E4622A800AB00DE6288 /* Screenshot.png in Resources */, + FA633E4822A8019D00DE6288 /* LICENSE in Resources */, + FA132EFA22A94D9400BD9743 /* MainMenu.xib in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + FA633E1722A77D1C00DE6288 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + FA633E3F22A7BDBB00DE6288 /* IdentifiedBlock.swift in Sources */, + FA6F2B2122A81832009C2D3B /* FunctionalUtils.swift in Sources */, + FA633E3922A79AFA00DE6288 /* GameLogic.swift in Sources */, + FA633E3422A77D5700DE6288 /* BlockView.swift in Sources */, + FA633E3D22A79B3C00DE6288 /* BlockGridView.swift in Sources */, + FA633E1F22A77D1C00DE6288 /* AppDelegate.swift in Sources */, + FA633E3722A7928500DE6288 /* BlockMatrix.swift in Sources */, + FA633E3B22A79B1200DE6288 /* GameView.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + FA633E2922A77D1D00DE6288 /* LaunchScreen.storyboard */ = { + isa = PBXVariantGroup; + children = ( + FA633E2A22A77D1D00DE6288 /* Base */, + ); + name = LaunchScreen.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + FA633E2D22A77D1D00DE6288 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 13.0; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + FA633E2E22A77D1D00DE6288 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 13.0; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + FA633E3022A77D1D00DE6288 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_ENTITLEMENTS = SwiftUI2048/SwiftUI2048.entitlements; + "CODE_SIGN_IDENTITY[sdk=*]" = ""; + CODE_SIGN_STYLE = Automatic; + DERIVE_UIKITFORMAC_PRODUCT_BUNDLE_IDENTIFIER = YES; + DEVELOPMENT_ASSET_PATHS = "SwiftUI2048/Preview\\ Content"; + DEVELOPMENT_TEAM = 9ZS3K56XF5; + ENABLE_PREVIEWS = YES; + INFOPLIST_FILE = SwiftUI2048/Info.plist; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.cyandev.SwiftUI2048; + PRODUCT_NAME = "$(TARGET_NAME)"; + SUPPORTS_UIKITFORMAC = YES; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + FA633E3122A77D1D00DE6288 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_ENTITLEMENTS = SwiftUI2048/SwiftUI2048.entitlements; + "CODE_SIGN_IDENTITY[sdk=*]" = ""; + CODE_SIGN_STYLE = Automatic; + DERIVE_UIKITFORMAC_PRODUCT_BUNDLE_IDENTIFIER = YES; + DEVELOPMENT_ASSET_PATHS = "SwiftUI2048/Preview\\ Content"; + DEVELOPMENT_TEAM = 9ZS3K56XF5; + ENABLE_PREVIEWS = YES; + INFOPLIST_FILE = SwiftUI2048/Info.plist; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.cyandev.SwiftUI2048; + PRODUCT_NAME = "$(TARGET_NAME)"; + SUPPORTS_UIKITFORMAC = YES; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + FA633E1622A77D1C00DE6288 /* Build configuration list for PBXProject "SwiftUI2048" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + FA633E2D22A77D1D00DE6288 /* Debug */, + FA633E2E22A77D1D00DE6288 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + FA633E2F22A77D1D00DE6288 /* Build configuration list for PBXNativeTarget "2048" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + FA633E3022A77D1D00DE6288 /* Debug */, + FA633E3122A77D1D00DE6288 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = FA633E1322A77D1C00DE6288 /* Project object */; +} diff --git a/Examples/2048 Game/SwiftUI2048.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Examples/2048 Game/SwiftUI2048.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100755 index 0000000..df46ccf --- /dev/null +++ b/Examples/2048 Game/SwiftUI2048.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Examples/2048 Game/SwiftUI2048.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Examples/2048 Game/SwiftUI2048.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100755 index 0000000..18d9810 --- /dev/null +++ b/Examples/2048 Game/SwiftUI2048.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/Examples/2048 Game/SwiftUI2048/AppDelegate.swift b/Examples/2048 Game/SwiftUI2048/AppDelegate.swift new file mode 100755 index 0000000..781b8fa --- /dev/null +++ b/Examples/2048 Game/SwiftUI2048/AppDelegate.swift @@ -0,0 +1,58 @@ +// +// AppDelegate.swift +// SwiftUI2048 +// +// Created by Hongyu on 6/5/19. +// Copyright © 2019 Cyandev. All rights reserved. +// + +import UIKit +import SwiftUI + +@UIApplicationMain +class AppDelegate: UIResponder, UIApplicationDelegate { + + var gameLogic: GameLogic! + var window: UIWindow? + + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { + + gameLogic = GameLogic() + + window = UIWindow(frame: UIScreen.main.bounds) + window!.rootViewController = UIHostingController(rootView: + GameView().environmentObject(gameLogic) + ) + window!.makeKeyAndVisible() + + self.becomeFirstResponder() + + return true + } + + func applicationWillTerminate(_ application: UIApplication) { + // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. + } + + @objc func newGame(_ sender: AnyObject?) { + gameLogic.newGame() + } + + override func buildCommands(with builder: UICommandBuilder) { + builder.remove(menu: .edit) + builder.remove(menu: .format) + builder.remove(menu: .view) + + builder.replaceChildren(ofMenu: .file) { oldChildren in + var newChildren = oldChildren + let newGameItem = UIMutableKeyCommand(input: "N", + modifierFlags: .command, + action: #selector(newGame(_:))) + newGameItem.title = "New Game" + newChildren.insert(newGameItem, at: 0) + return newChildren + } + } + +} + diff --git a/Examples/2048 Game/SwiftUI2048/Assets.xcassets/AppIcon.appiconset/Contents.json b/Examples/2048 Game/SwiftUI2048/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100755 index 0000000..d8db8d6 --- /dev/null +++ b/Examples/2048 Game/SwiftUI2048/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,98 @@ +{ + "images" : [ + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "3x" + }, + { + "idiom" : "ipad", + "size" : "20x20", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "29x29", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "40x40", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "76x76", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "76x76", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "83.5x83.5", + "scale" : "2x" + }, + { + "idiom" : "ios-marketing", + "size" : "1024x1024", + "scale" : "1x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/Examples/2048 Game/SwiftUI2048/Assets.xcassets/Contents.json b/Examples/2048 Game/SwiftUI2048/Assets.xcassets/Contents.json new file mode 100755 index 0000000..da4a164 --- /dev/null +++ b/Examples/2048 Game/SwiftUI2048/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/Examples/2048 Game/SwiftUI2048/Base.lproj/LaunchScreen.storyboard b/Examples/2048 Game/SwiftUI2048/Base.lproj/LaunchScreen.storyboard new file mode 100755 index 0000000..8ca4785 --- /dev/null +++ b/Examples/2048 Game/SwiftUI2048/Base.lproj/LaunchScreen.storyboard @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Examples/2048 Game/SwiftUI2048/FunctionalUtils.swift b/Examples/2048 Game/SwiftUI2048/FunctionalUtils.swift new file mode 100755 index 0000000..c2ffdf4 --- /dev/null +++ b/Examples/2048 Game/SwiftUI2048/FunctionalUtils.swift @@ -0,0 +1,13 @@ +// +// FunctionalUtils.swift +// SwiftUI2048 +// +// Created by Hongyu on 6/5/19. +// Copyright © 2019 Cyandev. All rights reserved. +// + +import Foundation + +func bind(_ x: T, _ closure: (T) -> U) -> U { + return closure(x) +} diff --git a/Examples/2048 Game/SwiftUI2048/Info.plist b/Examples/2048 Game/SwiftUI2048/Info.plist new file mode 100755 index 0000000..bd0f804 --- /dev/null +++ b/Examples/2048 Game/SwiftUI2048/Info.plist @@ -0,0 +1,45 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleDisplayName + 2048 + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + $(PRODUCT_BUNDLE_PACKAGE_TYPE) + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + LSRequiresIPhoneOS + + UILaunchStoryboardName + LaunchScreen + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + UISupportedInterfaceOrientations~ipad + + UIInterfaceOrientationPortrait + UIInterfaceOrientationPortraitUpsideDown + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + + diff --git a/Examples/2048 Game/SwiftUI2048/MainMenu.xib b/Examples/2048 Game/SwiftUI2048/MainMenu.xib new file mode 100755 index 0000000..2bc8a16 --- /dev/null +++ b/Examples/2048 Game/SwiftUI2048/MainMenu.xib @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/Examples/2048 Game/SwiftUI2048/Models/BlockMatrix.swift b/Examples/2048 Game/SwiftUI2048/Models/BlockMatrix.swift new file mode 100755 index 0000000..e4d3c62 --- /dev/null +++ b/Examples/2048 Game/SwiftUI2048/Models/BlockMatrix.swift @@ -0,0 +1,127 @@ +// +// BlockMatrix.swift +// SwiftUI2048 +// +// Created by Hongyu on 6/5/19. +// Copyright © 2019 Cyandev. All rights reserved. +// + +import Foundation + +protocol Block { + + associatedtype Value + + var number: Value { get set } + +} + +struct IndexedBlock { + + let index: (Int, Int) + let item: T? + +} + +struct BlockMatrix : CustomDebugStringConvertible where T: Block { + + typealias Index = (Int, Int) + + fileprivate var matrix: [[T?]] + + init() { + matrix = [[T?]]() + for _ in 0..<4 { + var row = [T?]() + for _ in 0..<4 { + row.append(nil) + } + matrix.append(row) + } + } + + var debugDescription: String { + matrix.map { row -> String in + row.map { + if $0 == nil { + return " " + } else { + return String(describing: $0!.number) + } + }.joined(separator: "\t") + }.joined(separator: "\n") + } + + var flatten: [IndexedBlock] { + return self.matrix.enumerated().flatMap { (y: Int, element: [T?]) in + element.enumerated().map { (x: Int, element: T?) in + return IndexedBlock(index: (x, y), item: element) + } + } + } + + subscript(index: Self.Index) -> T? { + guard isIndexValid(index) else { + return nil + } + + return matrix[index.1][index.0] + } + + /// Move the block to specific location and leave the original location blank. + /// - Parameter from: Source location + /// - Parameter to: Destination location + mutating func move(from: Self.Index, to: Self.Index) { + guard isIndexValid(from) && isIndexValid(to) else { + // TODO: Throw an error? + return + } + + guard let source = self[from] else { + return + } + + matrix[to.1][to.0] = source + matrix[from.1][from.0] = nil + } + + /// Move the block to specific location, change its value and leave the original location blank. + /// - Parameter from: Source location + /// - Parameter to: Destination location + /// - Parameter newValue: The new value + mutating func move(from: Self.Index, to: Self.Index, with newValue: T.Value) { + guard isIndexValid(from) && isIndexValid(to) else { + // TODO: Throw an error? + return + } + + guard var source = self[from] else { + return + } + + source.number = newValue + + matrix[to.1][to.0] = source + matrix[from.1][from.0] = nil + } + + /// Place a block to specific location. + /// - Parameter block: The block to place + /// - Parameter to: Destination location + mutating func place(_ block: T?, to: Self.Index) { + matrix[to.1][to.0] = block + } + + fileprivate func isIndexValid(_ index: Self.Index) -> Bool { + guard index.0 >= 0 && index.0 < 4 else { + return false + } + + guard index.1 >= 0 && index.1 < 4 else { + return false + } + + return true + } + +} diff --git a/Examples/2048 Game/SwiftUI2048/Models/GameLogic.swift b/Examples/2048 Game/SwiftUI2048/Models/GameLogic.swift new file mode 100755 index 0000000..1b1995d --- /dev/null +++ b/Examples/2048 Game/SwiftUI2048/Models/GameLogic.swift @@ -0,0 +1,176 @@ +// +// GameLogic.swift +// SwiftUI2048 +// +// Created by Hongyu on 6/5/19. +// Copyright © 2019 Cyandev. All rights reserved. +// + +import Foundation +import SwiftUI +import Combine + +final class GameLogic : BindableObject { + + enum Direction { + case left + case right + case up + case down + } + + typealias BlockMatrixType = BlockMatrix + + let didChange = PassthroughSubject() + + fileprivate var _blockMatrix: BlockMatrixType! + var blockMatrix: BlockMatrixType { + return _blockMatrix + } + + fileprivate var _globalID = 0 + fileprivate var newGlobalID: Int { + _globalID += 1 + return _globalID + } + + init() { + newGame() + } + + func newGame() { + _blockMatrix = BlockMatrixType() + generateNewBlocks() + + didChange.send(self) + } + + func move(_ direction: Direction) { + defer { + didChange.send(self) + } + + var moved = false + + let axis = direction == .left || direction == .right + for row in 0..<4 { + var rowSnapshot = [IdentifiedBlock?]() + var compactRow = [IdentifiedBlock]() + for col in 0..<4 { + // Transpose if necessary. + if let block = _blockMatrix[axis ? (col, row) : (row, col)] { + rowSnapshot.append(block) + compactRow.append(block) + } + rowSnapshot.append(nil) + } + + merge(blocks: &compactRow, reverse: direction == .down || direction == .right) + + var newRow = [IdentifiedBlock?]() + compactRow.forEach { newRow.append($0) } + if compactRow.count < 4 { + for _ in 0..<(4 - compactRow.count) { + if direction == .left || direction == .up { + newRow.append(nil) + } else { + newRow.insert(nil, at: 0) + } + } + } + + newRow.enumerated().forEach { + if rowSnapshot[$0]?.number != $1?.number { + moved = true + } + _blockMatrix.place($1, to: axis ? ($0, row) : (row, $0)) + } + } + + if moved { + generateNewBlocks() + } + } + + fileprivate func merge(blocks: inout [IdentifiedBlock], reverse: Bool) { + if reverse { + blocks = blocks.reversed() + } + + blocks = blocks + .map { (false, $0) } + .reduce([(Bool, IdentifiedBlock)]()) { acc, item in + if acc.last?.0 == false && acc.last?.1.number == item.1.number { + var accPrefix = Array(acc.dropLast()) + var mergedBlock = item.1 + mergedBlock.number *= 2 + accPrefix.append((true, mergedBlock)) + return accPrefix + } else { + var accTmp = acc + accTmp.append((false, item.1)) + return accTmp + } + } + .map { $0.1 } + + if reverse { + blocks = blocks.reversed() + } + } + + @discardableResult fileprivate func generateNewBlocks() -> Bool { + var blankLocations = [BlockMatrixType.Index]() + for rowIndex in 0..<4 { + for colIndex in 0..<4 { + let index = (colIndex, rowIndex) + if _blockMatrix[index] == nil { + blankLocations.append(index) + } + } + } + + guard blankLocations.count >= 2 else { + return false + } + + // Don't forget to sync data. + defer { + didChange.send(self) + } + + // Place the first block. + var placeLocIndex = Int.random(in: 0.. ()) { +// var indices = (0..<4).map { $0 } +// if reversed { +// indices = indices.reversed() +// } +// +// for row in indices { +// for col in indices { +// if mode == .rowByRow { +// action((col, row)) +// } else { +// action((row, col)) // transpose +// } +// } +// } +// } + +} diff --git a/Examples/2048 Game/SwiftUI2048/Models/IdentifiedBlock.swift b/Examples/2048 Game/SwiftUI2048/Models/IdentifiedBlock.swift new file mode 100755 index 0000000..71d0f35 --- /dev/null +++ b/Examples/2048 Game/SwiftUI2048/Models/IdentifiedBlock.swift @@ -0,0 +1,16 @@ +// +// IdentifiedBlock.swift +// SwiftUI2048 +// +// Created by Hongyu on 6/5/19. +// Copyright © 2019 Cyandev. All rights reserved. +// + +import Foundation + +struct IdentifiedBlock: Block { + + let id: Int + var number: Int + +} diff --git a/Examples/2048 Game/SwiftUI2048/Preview Content/Preview Assets.xcassets/Contents.json b/Examples/2048 Game/SwiftUI2048/Preview Content/Preview Assets.xcassets/Contents.json new file mode 100755 index 0000000..da4a164 --- /dev/null +++ b/Examples/2048 Game/SwiftUI2048/Preview Content/Preview Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/Examples/2048 Game/SwiftUI2048/SwiftUI2048.entitlements b/Examples/2048 Game/SwiftUI2048/SwiftUI2048.entitlements new file mode 100755 index 0000000..ee95ab7 --- /dev/null +++ b/Examples/2048 Game/SwiftUI2048/SwiftUI2048.entitlements @@ -0,0 +1,10 @@ + + + + + com.apple.security.app-sandbox + + com.apple.security.network.client + + + diff --git a/Examples/2048 Game/SwiftUI2048/Views/BlockGridView.swift b/Examples/2048 Game/SwiftUI2048/Views/BlockGridView.swift new file mode 100755 index 0000000..50be395 --- /dev/null +++ b/Examples/2048 Game/SwiftUI2048/Views/BlockGridView.swift @@ -0,0 +1,116 @@ +// +// BlockGridView.swift +// SwiftUI2048 +// +// Created by Hongyu on 6/5/19. +// Copyright © 2019 Cyandev. All rights reserved. +// + +import SwiftUI + +fileprivate struct IdentifiableIndexedBlock : Identifiable { + + typealias ID = String + typealias IdentifiedValue = IndexedBlock + + static var uniqueBlankId = 0 + + let indexedBlock: IndexedBlock + + var id: Self.ID { + if let id = indexedBlock.item?.id { + return "\(id)" + } + + // TODO: (Refactor) Don't mix two types of block views. + IdentifiableIndexedBlock.uniqueBlankId += 1 + return "Blank_\(IdentifiableIndexedBlock.uniqueBlankId)" + } + + var identifiedValue: Self.IdentifiedValue { + return indexedBlock + } + +} + +extension AnyTransition { + + static func blockAppear(from: Edge) -> AnyTransition { + return .asymmetric( + insertion: AnyTransition.opacity + .combined(with: .move(edge: from)), + removal: .identity) + } + +} + +struct BlockGridView : View { + + typealias SupportingMatrix = BlockMatrix + + let matrix: Self.SupportingMatrix + let blockEnterEdge: Edge + + func createBlock(_ block: IdentifiedBlock?) -> some View { + if let block = block { + return BlockView(number: block.number) + } + return BlockView.blank() + } + + // FIXME: This is existed as a workaround for a Swift compiler bug. + func zIndex(_ block: IdentifiedBlock?) -> Double { + if block == nil { + return 1 + } + return 1000 + } + + var body: some View { + ZStack { + ForEach( + self.matrix.flatten.map { IdentifiableIndexedBlock(indexedBlock: $0) } + ) { block in + self.createBlock(block.item) + .frame(width: 65, height: 65, alignment: .center) + .position(x: CGFloat(block.index.0) * (65 + 12) + 32.5 + 12, + y: CGFloat(block.index.1) * (65 + 12) + 32.5 + 12) + .zIndex(self.zIndex(block.item)) + .transition(.blockAppear(from: self.blockEnterEdge)) + .animation(block.item == nil ? nil : .spring(mass: 1, stiffness: 400, damping: 56, initialVelocity: 0)) + } + } + .frame(width: 320, height: 320, alignment: .center) + .background( + Rectangle() + .fill(Color(red:0.72, green:0.66, blue:0.63, opacity:1.00)) + ) + .clipped() + .cornerRadius(6) + } + +} + +#if DEBUG +struct BlockGridView_Previews : PreviewProvider { + + static var matrix: BlockGridView.SupportingMatrix { + var _matrix = BlockGridView.SupportingMatrix() + _matrix.place(IdentifiedBlock(id: 1, number: 2), to: (2, 0)) + _matrix.place(IdentifiedBlock(id: 2, number: 2), to: (3, 0)) + _matrix.place(IdentifiedBlock(id: 3, number: 8), to: (1, 1)) + _matrix.place(IdentifiedBlock(id: 4, number: 4), to: (2, 1)) + _matrix.place(IdentifiedBlock(id: 5, number: 512), to: (3, 3)) + _matrix.place(IdentifiedBlock(id: 6, number: 1024), to: (2, 3)) + _matrix.place(IdentifiedBlock(id: 7, number: 16), to: (0, 3)) + _matrix.place(IdentifiedBlock(id: 8, number: 8), to: (1, 3)) + return _matrix + } + + static var previews: some View { + BlockGridView(matrix: matrix, blockEnterEdge: .top) + .previewLayout(.sizeThatFits) + } + +} +#endif diff --git a/Examples/2048 Game/SwiftUI2048/Views/BlockView.swift b/Examples/2048 Game/SwiftUI2048/Views/BlockView.swift new file mode 100755 index 0000000..27ad7f3 --- /dev/null +++ b/Examples/2048 Game/SwiftUI2048/Views/BlockView.swift @@ -0,0 +1,118 @@ +// +// BlockView.swift +// SwiftUI2048 +// +// Created by Hongyu on 6/5/19. +// Copyright © 2019 Cyandev. All rights reserved. +// + +import SwiftUI + +struct BlockView : View { + + fileprivate let colorScheme: [(Color, Color)] = [ + // 2 + (Color(red:0.91, green:0.87, blue:0.83, opacity:1.00), Color(red:0.42, green:0.39, blue:0.35, opacity:1.00)), + // 4 + (Color(red:0.90, green:0.86, blue:0.76, opacity:1.00), Color(red:0.42, green:0.39, blue:0.35, opacity:1.00)), + // 8 + (Color(red:0.93, green:0.67, blue:0.46, opacity:1.00), Color.white), + // 16 + (Color(red:0.94, green:0.57, blue:0.38, opacity:1.00), Color.white), + // 32 + (Color(red:0.95, green:0.46, blue:0.33, opacity:1.00), Color.white), + // 64 + (Color(red:0.94, green:0.35, blue:0.23, opacity:1.00), Color.white), + // 128 + (Color(red:0.91, green:0.78, blue:0.43, opacity:1.00), Color.white), + // 256 + (Color(red:0.91, green:0.78, blue:0.37, opacity:1.00), Color.white), + // 512 + (Color(red:0.90, green:0.77, blue:0.31, opacity:1.00), Color.white), + // 1024 + (Color(red:0.91, green:0.75, blue:0.24, opacity:1.00), Color.white), + // 2048 + (Color(red:0.91, green:0.74, blue:0.18, opacity:1.00), Color.white), + ] + + fileprivate let number: Int? + + init(number: Int) { + self.number = number + } + + fileprivate init() { + self.number = nil + } + + static func blank() -> Self { + return self.init() + } + + fileprivate var numberText: String { + guard let number = number else { + return "" + } + return String(number) + } + + fileprivate var fontSize: CGFloat { + let textLength = numberText.count + if textLength < 3 { + return 32 + } else if textLength < 4 { + return 18 + } else { + return 12 + } + } + + fileprivate var colorPair: (Color, Color) { + guard let number = number else { + return (Color(red:0.78, green:0.73, blue:0.68, opacity:1.00), Color.black) + } + let index = Int(log2(Double(number))) - 1 + if index < 0 || index >= colorScheme.count { + fatalError("No color for such number") + } + return colorScheme[index] + } + + // MARK: Body + + var body: some View { + ZStack { + Rectangle().fill(colorPair.0) + + Text(numberText) + .font(Font.system(size: fontSize).bold()) + .color(colorPair.1) + .id(numberText) + .transition(AnyTransition.scale(scale: 0.5, anchor: .center).combined(with: .opacity)) + .animation(.fluidSpring()) + } + .clipped() + .cornerRadius(6) + } + +} + +// MARK: - Previews + +#if DEBUG +struct BlockView_Previews : PreviewProvider { + + static var previews: some View { + Group { + ForEach((1...11).map { Int(pow(2, Double($0))) }) { i in + BlockView(number: i) + .previewLayout(.sizeThatFits) + } + + BlockView.blank() + .previewLayout(.sizeThatFits) + } + } + +} +#endif diff --git a/Examples/2048 Game/SwiftUI2048/Views/GameView.swift b/Examples/2048 Game/SwiftUI2048/Views/GameView.swift new file mode 100755 index 0000000..851bd80 --- /dev/null +++ b/Examples/2048 Game/SwiftUI2048/Views/GameView.swift @@ -0,0 +1,127 @@ +// +// GameView.swift +// SwiftUI2048 +// +// Created by Hongyu on 6/5/19. +// Copyright © 2019 Cyandev. All rights reserved. +// + +import SwiftUI + +extension Edge { + + static func from(_ from: GameLogic.Direction) -> Self { + switch from { + case .down: + return .top + case .up: + return .bottom + case .left: + return .trailing + case .right: + return .leading + } + } + +} + +struct GameView : View { + + @State var gestureStartLocation: CGPoint = .zero + @State var lastGestureDirection: GameLogic.Direction = .up + @EnvironmentObject var gameLogic: GameLogic + + fileprivate struct LayoutTraits { + let bannerOffset: CGSize + let containerAlignment: Alignment + } + + fileprivate func layoutTraits(`for` proxy: GeometryProxy) -> LayoutTraits { + let landscape = proxy.size.width > proxy.size.height + + return LayoutTraits( + bannerOffset: landscape + ? .init(width: proxy.safeAreaInsets.leading + 32, height: 0) + : .init(width: 0, height: proxy.safeAreaInsets.top + 32), + containerAlignment: landscape ? .leading : .top + ) + } + + var gesture: some Gesture { + let threshold: CGFloat = 44 + let drag = DragGesture() + .onChanged { v in + guard self.gestureStartLocation != v.startLocation else { return } + + withTransaction(Transaction()) { + self.gestureStartLocation = v.startLocation + + if v.translation.width > threshold { + // Move right + self.gameLogic.move(.right) + self.lastGestureDirection = .right + } else if v.translation.width < -threshold { + // Move left + self.gameLogic.move(.left) + self.lastGestureDirection = .left + } else if v.translation.height > threshold { + // Move down + self.gameLogic.move(.down) + self.lastGestureDirection = .down + } else if v.translation.height < -threshold { + // Move up + self.gameLogic.move(.up) + self.lastGestureDirection = .up + } else { + // Direction cannot be deduced, reset gesture state. + self.gestureStartLocation = .zero + } + } + + // After the scene is updated, reset the last gesture direction + // to make sure the animation is right when user starts a new + // game. + OperationQueue.main.addOperation { + self.lastGestureDirection = .up + } + } + return drag + } + + var body: some View { + GeometryReader { proxy in + bind(self.layoutTraits(for: proxy)) { layoutTraits in + ZStack(alignment: layoutTraits.containerAlignment) { + Text("2048") + .font(Font.system(size: 48).weight(.black)) + .color(Color(red:0.47, green:0.43, blue:0.40, opacity:1.00)) + .offset(layoutTraits.bannerOffset) + + ZStack(alignment: .top) { + BlockGridView(matrix: self.gameLogic.blockMatrix, + blockEnterEdge: .from(self.lastGestureDirection)) + .gesture(self.gesture) + } + .frame(width: proxy.size.width, height: proxy.size.height, alignment: .center) + } + .frame(width: proxy.size.width, height: proxy.size.height, alignment: .center) + .background( + Rectangle().fill(Color(red:0.96, green:0.94, blue:0.90, opacity:1.00)) + ) + } + } + .edgesIgnoringSafeArea(.all) + } + +} + +#if DEBUG +struct GameView_Previews : PreviewProvider { + + static var previews: some View { + GameView() + .environmentObject(GameLogic()) + } + +} +#endif diff --git a/README.md b/README.md index 26a6e76..3e84261 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Include Layout, UI, Animations, Gestures, Draw and Data. See projects files in ` - [Creating And Combining Views](#creating-and-combining-views) - [Building Lists And Navigation](#building-lists-and-navigation) - [Handling User Input](#handling-user-input) +- [2048 Game](#2048-game) - [Composing Complex Interfaces](#composing-complex-interfaces) - [Working With UIControls](#working-with-uicontrols) - [Example To-Do App](#example-to-do-app) @@ -36,6 +37,10 @@ Include Layout, UI, Animations, Gestures, Draw and Data. See projects files in ` +#### 2048 Game + + + #### Composing Complex Interfaces @@ -83,5 +88,5 @@ Include Layout, UI, Animations, Gestures, Draw and Data. See projects files in ` #### Authors -Thanks for [Jinxiansen](https://github.com/Jinxiansen), [ra1028](https://github.com/ra1028), [timdonnelly](https://github.com/timdonnelly), [TwoLivesLeft](https://github.com/TwoLivesLeft) & [devxoul](https://github.com/devxoul) for examples project. +Thanks for [Jinxiansen](https://github.com/Jinxiansen), [ra1028](https://github.com/ra1028), [timdonnelly](https://github.com/timdonnelly), [TwoLivesLeft](https://github.com/TwoLivesLeft), [devxoul](https://github.com/devxoul), [cmtrounce](https://github.com/cmtrounce), [unixzii](https://github.com/unixzii) for examples project. diff --git a/Resources/2048game.png b/Resources/2048game.png new file mode 100755 index 0000000..1a803fc Binary files /dev/null and b/Resources/2048game.png differ