diff --git a/FlappySwift.xcodeproj/project.pbxproj b/FlappySwift.xcodeproj/project.pbxproj index d3612a2..a68bf8c 100644 --- a/FlappySwift.xcodeproj/project.pbxproj +++ b/FlappySwift.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 1763B3CF1E9F7AA600FF0B0D /* FlyScene.sks in Resources */ = {isa = PBXBuildFile; fileRef = 1763B3CE1E9F7AA600FF0B0D /* FlyScene.sks */; }; + 1763B3D11E9F7AC400FF0B0D /* FlyScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1763B3D01E9F7AC400FF0B0D /* FlyScene.swift */; }; 873BE29B1D06195B00A69FF5 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 873BE29A1D06195B00A69FF5 /* AppDelegate.swift */; }; 873BE2A11D06195B00A69FF5 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 873BE2A01D06195B00A69FF5 /* ViewController.swift */; }; 873BE2A41D06195B00A69FF5 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 873BE2A21D06195B00A69FF5 /* Main.storyboard */; }; @@ -16,6 +18,8 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 1763B3CE1E9F7AA600FF0B0D /* FlyScene.sks */ = {isa = PBXFileReference; lastKnownFileType = file.sks; path = FlyScene.sks; sourceTree = ""; }; + 1763B3D01E9F7AC400FF0B0D /* FlyScene.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FlyScene.swift; sourceTree = ""; }; 873BE2971D06195B00A69FF5 /* FlappySwift.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = FlappySwift.app; sourceTree = BUILT_PRODUCTS_DIR; }; 873BE29A1D06195B00A69FF5 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 873BE2A01D06195B00A69FF5 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; @@ -58,6 +62,8 @@ children = ( 873BE29A1D06195B00A69FF5 /* AppDelegate.swift */, 873BE2A21D06195B00A69FF5 /* Main.storyboard */, + 1763B3CE1E9F7AA600FF0B0D /* FlyScene.sks */, + 1763B3D01E9F7AC400FF0B0D /* FlyScene.swift */, 873BE2A01D06195B00A69FF5 /* ViewController.swift */, 873BE2A51D06195B00A69FF5 /* Assets.xcassets */, 87AA11DB1D0B4D6E006C614F /* bird.atlas */, @@ -107,6 +113,7 @@ TargetAttributes = { 873BE2961D06195B00A69FF5 = { CreatedOnToolsVersion = 7.3.1; + DevelopmentTeam = 9N294DEXYZ; LastSwiftMigration = 0830; }; }; @@ -137,6 +144,7 @@ 873BE2A91D06195B00A69FF5 /* LaunchScreen.storyboard in Resources */, 873BE2A61D06195B00A69FF5 /* Assets.xcassets in Resources */, 87AA11DC1D0B4D6E006C614F /* bird.atlas in Resources */, + 1763B3CF1E9F7AA600FF0B0D /* FlyScene.sks in Resources */, 873BE2A41D06195B00A69FF5 /* Main.storyboard in Resources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -149,6 +157,7 @@ buildActionMask = 2147483647; files = ( 873BE2A11D06195B00A69FF5 /* ViewController.swift in Sources */, + 1763B3D11E9F7AC400FF0B0D /* FlyScene.swift in Sources */, 873BE29B1D06195B00A69FF5 /* AppDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -269,9 +278,10 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = 9N294DEXYZ; INFOPLIST_FILE = FlappySwift/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = "de.uni-heidelberg.ios-dev-kurs.FlappySwift"; + PRODUCT_BUNDLE_IDENTIFIER = Wurstsalat; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 3.0; }; @@ -281,9 +291,10 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = 9N294DEXYZ; INFOPLIST_FILE = FlappySwift/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = "de.uni-heidelberg.ios-dev-kurs.FlappySwift"; + PRODUCT_BUNDLE_IDENTIFIER = Wurstsalat; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 3.0; }; diff --git a/FlappySwift/Assets.xcassets/AppIcon.appiconset/Contents.json b/FlappySwift/Assets.xcassets/AppIcon.appiconset/Contents.json index eeea76c..1d060ed 100644 --- a/FlappySwift/Assets.xcassets/AppIcon.appiconset/Contents.json +++ b/FlappySwift/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -1,5 +1,15 @@ { "images" : [ + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "3x" + }, { "idiom" : "iphone", "size" : "29x29", @@ -30,6 +40,16 @@ "size" : "60x60", "scale" : "3x" }, + { + "idiom" : "ipad", + "size" : "20x20", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "20x20", + "scale" : "2x" + }, { "idiom" : "ipad", "size" : "29x29", diff --git a/FlappySwift/Base.lproj/LaunchScreen.storyboard b/FlappySwift/Base.lproj/LaunchScreen.storyboard index a9a24c1..5cfb086 100644 --- a/FlappySwift/Base.lproj/LaunchScreen.storyboard +++ b/FlappySwift/Base.lproj/LaunchScreen.storyboard @@ -1,8 +1,12 @@ - - + + + + + - + + @@ -14,14 +18,14 @@ - + - + - + diff --git a/FlappySwift/Base.lproj/Main.storyboard b/FlappySwift/Base.lproj/Main.storyboard index af837b3..4a56362 100644 --- a/FlappySwift/Base.lproj/Main.storyboard +++ b/FlappySwift/Base.lproj/Main.storyboard @@ -1,8 +1,12 @@ - - + + + + + - + + @@ -14,9 +18,9 @@ - + - + diff --git a/FlappySwift/FlyScene.sks b/FlappySwift/FlyScene.sks new file mode 100644 index 0000000..4ae85ee Binary files /dev/null and b/FlappySwift/FlyScene.sks differ diff --git a/FlappySwift/FlyScene.swift b/FlappySwift/FlyScene.swift new file mode 100644 index 0000000..aee9844 --- /dev/null +++ b/FlappySwift/FlyScene.swift @@ -0,0 +1,105 @@ +// +// FlyScene.swift +// FlappySwift +// +// Created by Florian Petatz on 13.04.17. +// Copyright © 2017 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import SpriteKit + +let worldCollisionCategory: UInt32 = 1 << 1 +let obstacleCollisionCategory: UInt32 = 1 << 2 +let birdCollisionCategory: UInt32 = 1 << 3 + +class FlyScene: SKScene, SKPhysicsContactDelegate { + + let impulseOnFlap: CGFloat = 500 + + let bird: SKSpriteNode = { + let spriteNote = SKSpriteNode (imageNamed: "bird-01") + let physicsBody = SKPhysicsBody(circleOfRadius: spriteNote.size.width / 2) + physicsBody.mass = 1 + physicsBody.linearDamping = 0 + physicsBody.restitution = 1 + physicsBody.collisionBitMask = worldCollisionCategory | obstacleCollisionCategory + physicsBody.categoryBitMask = birdCollisionCategory + physicsBody.contactTestBitMask = obstacleCollisionCategory + spriteNote.physicsBody = physicsBody + return spriteNote + }() + + let obstacles: SKNode = { + let node = SKNode() + return node + }() + + override func didMove(to view: SKView) { + super.didMove(to: view) + + self.physicsWorld.contactDelegate = self + + self.addChild(bird) + bird.position = CGPoint(x: 0, y: 0) + + let physicsBody = SKPhysicsBody(edgeLoopFrom: CGRect(x: -self.size.width / 2, y: -self.size.height / 2, width: self.size.width, height: self.size.height)) + physicsBody.categoryBitMask = worldCollisionCategory + self.physicsBody = physicsBody + + self.addChild(obstacles) + + self.obstacles.run(.repeatForever(.sequence([ + .wait(forDuration: 2), + self.spawnObstacle, + + ]))) + } + + + lazy var spawnObstacle: SKAction = { + let action = SKAction.run { + let upperObstacle = SKSpriteNode(imageNamed: "PipeDown") + let upperPhysicsBody = SKPhysicsBody(edgeLoopFrom: upperObstacle.frame) + upperPhysicsBody.categoryBitMask = obstacleCollisionCategory + upperObstacle.physicsBody = upperPhysicsBody + self.obstacles.addChild(upperObstacle) + upperObstacle.run(self.moveObstacle) + + } + return action + }() + + lazy var moveObstacle: SKAction = { + let distance: CGFloat = self.size.width + return .sequence([ + .move(to: CGPoint(x: self.size.width / 2, y: 0), duration: 0), + .move(by: CGVector(dx: -distance, dy: 0), duration: 3), + .removeFromParent() + ]) + }() + + override func touchesBegan(_ touches: Set, with event: UIEvent?) { + + + if self.speed == 0 { + self.speed = 1 + self.physicsWorld.speed = 1 + bird.physicsBody?.velocity = CGVector(dx: 0, dy: 0) + bird.position = CGPoint(x: -100, y: -100) + for obstacle in obstacles.children { + obstacle.removeFromParent() + } + } + + bird.physicsBody?.applyImpulse(CGVector (dx: 0, dy: impulseOnFlap)) + + } + + func didBegin(_ contact: SKPhysicsContact) { + print("Game over!") + self.speed = 0 + + } + +} diff --git a/FlappySwift/ViewController.swift b/FlappySwift/ViewController.swift index f2baeaa..f33c338 100644 --- a/FlappySwift/ViewController.swift +++ b/FlappySwift/ViewController.swift @@ -18,7 +18,16 @@ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - // Configure scene view + // Configure scene view + + guard let scene = FlyScene(fileNamed: "FlyScene") else { + return + } + + scene.size = self.view.bounds.size + + sceneView.presentScene(scene) + sceneView.showsFPS = true sceneView.showsNodeCount = true sceneView.showsDrawCount = true