mirror of
https://github.com/ivanvorobei/SwiftUI.git
synced 2026-03-29 21:52:10 +02:00
Add 2048 Game
This commit is contained in:
58
Examples/2048 Game/SwiftUI2048/AppDelegate.swift
Executable file
58
Examples/2048 Game/SwiftUI2048/AppDelegate.swift
Executable file
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
6
Examples/2048 Game/SwiftUI2048/Assets.xcassets/Contents.json
Executable file
6
Examples/2048 Game/SwiftUI2048/Assets.xcassets/Contents.json
Executable file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
26
Examples/2048 Game/SwiftUI2048/Base.lproj/LaunchScreen.storyboard
Executable file
26
Examples/2048 Game/SwiftUI2048/Base.lproj/LaunchScreen.storyboard
Executable file
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14810.11" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
|
||||
<device id="retina6_1" orientation="portrait" appearance="light"/>
|
||||
<dependencies>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14766.13"/>
|
||||
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
|
||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||
</dependencies>
|
||||
<scenes>
|
||||
<!--View Controller-->
|
||||
<scene sceneID="EHf-IW-A2E">
|
||||
<objects>
|
||||
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
|
||||
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
|
||||
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
<color key="backgroundColor" xcode11CocoaTouchSystemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
|
||||
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
|
||||
</view>
|
||||
</viewController>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
|
||||
</objects>
|
||||
<point key="canvasLocation" x="53" y="375"/>
|
||||
</scene>
|
||||
</scenes>
|
||||
</document>
|
||||
13
Examples/2048 Game/SwiftUI2048/FunctionalUtils.swift
Executable file
13
Examples/2048 Game/SwiftUI2048/FunctionalUtils.swift
Executable file
@@ -0,0 +1,13 @@
|
||||
//
|
||||
// FunctionalUtils.swift
|
||||
// SwiftUI2048
|
||||
//
|
||||
// Created by Hongyu on 6/5/19.
|
||||
// Copyright © 2019 Cyandev. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
func bind<T, U>(_ x: T, _ closure: (T) -> U) -> U {
|
||||
return closure(x)
|
||||
}
|
||||
45
Examples/2048 Game/SwiftUI2048/Info.plist
Executable file
45
Examples/2048 Game/SwiftUI2048/Info.plist
Executable file
@@ -0,0 +1,45 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
||||
<key>CFBundleDisplayName</key>
|
||||
<string>2048</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>$(PRODUCT_NAME)</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
<key>LSRequiresIPhoneOS</key>
|
||||
<true/>
|
||||
<key>UILaunchStoryboardName</key>
|
||||
<string>LaunchScreen</string>
|
||||
<key>UIRequiredDeviceCapabilities</key>
|
||||
<array>
|
||||
<string>armv7</string>
|
||||
</array>
|
||||
<key>UISupportedInterfaceOrientations</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||
</array>
|
||||
<key>UISupportedInterfaceOrientations~ipad</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
<string>UIInterfaceOrientationPortraitUpsideDown</string>
|
||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
20
Examples/2048 Game/SwiftUI2048/MainMenu.xib
Executable file
20
Examples/2048 Game/SwiftUI2048/MainMenu.xib
Executable file
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14810.11" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
|
||||
<device id="retina6_1" orientation="portrait" appearance="light"/>
|
||||
<dependencies>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14766.13"/>
|
||||
</dependencies>
|
||||
<objects>
|
||||
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="AppDelegate" customModule="_048"/>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
|
||||
<command title="New Game" input="n" id="TrP-zF-WNW">
|
||||
<keyModifierFlags key="modifierFlags" command="YES"/>
|
||||
<alternates>
|
||||
<commandAlternate title="a" actionName="newGame:" id="Fnd-9b-r2T"/>
|
||||
</alternates>
|
||||
<connections>
|
||||
<action selector="newGame:" destination="-2" id="AQs-UV-nU7"/>
|
||||
</connections>
|
||||
</command>
|
||||
</objects>
|
||||
</document>
|
||||
127
Examples/2048 Game/SwiftUI2048/Models/BlockMatrix.swift
Executable file
127
Examples/2048 Game/SwiftUI2048/Models/BlockMatrix.swift
Executable file
@@ -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<T> {
|
||||
|
||||
let index: (Int, Int)
|
||||
let item: T?
|
||||
|
||||
}
|
||||
|
||||
struct BlockMatrix<T> : 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<T>] {
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
176
Examples/2048 Game/SwiftUI2048/Models/GameLogic.swift
Executable file
176
Examples/2048 Game/SwiftUI2048/Models/GameLogic.swift
Executable file
@@ -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<IdentifiedBlock>
|
||||
|
||||
let didChange = PassthroughSubject<GameLogic, Never>()
|
||||
|
||||
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..<blankLocations.count)
|
||||
_blockMatrix.place(IdentifiedBlock(id: newGlobalID, number: 2), to: blankLocations[placeLocIndex])
|
||||
|
||||
// Place the second block.
|
||||
guard let lastLoc = blankLocations.last else {
|
||||
return false
|
||||
}
|
||||
blankLocations[placeLocIndex] = lastLoc
|
||||
placeLocIndex = Int.random(in: 0..<(blankLocations.count - 1))
|
||||
_blockMatrix.place(IdentifiedBlock(id: newGlobalID, number: 2), to: blankLocations[placeLocIndex])
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// fileprivate func forEachBlockIndices(mode: ForEachMode = .rowByRow,
|
||||
// reversed: Bool = false,
|
||||
// _ action: (BlockMatrixType.Index) -> ()) {
|
||||
// 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
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
16
Examples/2048 Game/SwiftUI2048/Models/IdentifiedBlock.swift
Executable file
16
Examples/2048 Game/SwiftUI2048/Models/IdentifiedBlock.swift
Executable file
@@ -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
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
10
Examples/2048 Game/SwiftUI2048/SwiftUI2048.entitlements
Executable file
10
Examples/2048 Game/SwiftUI2048/SwiftUI2048.entitlements
Executable file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>com.apple.security.app-sandbox</key>
|
||||
<true/>
|
||||
<key>com.apple.security.network.client</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
||||
116
Examples/2048 Game/SwiftUI2048/Views/BlockGridView.swift
Executable file
116
Examples/2048 Game/SwiftUI2048/Views/BlockGridView.swift
Executable file
@@ -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<IdentifiedBlock>
|
||||
|
||||
static var uniqueBlankId = 0
|
||||
|
||||
let indexedBlock: IndexedBlock<IdentifiedBlock>
|
||||
|
||||
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<IdentifiedBlock>
|
||||
|
||||
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
|
||||
118
Examples/2048 Game/SwiftUI2048/Views/BlockView.swift
Executable file
118
Examples/2048 Game/SwiftUI2048/Views/BlockView.swift
Executable file
@@ -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
|
||||
127
Examples/2048 Game/SwiftUI2048/Views/GameView.swift
Executable file
127
Examples/2048 Game/SwiftUI2048/Views/GameView.swift
Executable file
@@ -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
|
||||
Reference in New Issue
Block a user