diff --git a/FlappySwift.xcodeproj/project.pbxproj b/FlappySwift.xcodeproj/project.pbxproj index d3612a2..932415d 100644 --- a/FlappySwift.xcodeproj/project.pbxproj +++ b/FlappySwift.xcodeproj/project.pbxproj @@ -13,6 +13,8 @@ 873BE2A61D06195B00A69FF5 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 873BE2A51D06195B00A69FF5 /* Assets.xcassets */; }; 873BE2A91D06195B00A69FF5 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 873BE2A71D06195B00A69FF5 /* LaunchScreen.storyboard */; }; 87AA11DC1D0B4D6E006C614F /* bird.atlas in Resources */ = {isa = PBXBuildFile; fileRef = 87AA11DB1D0B4D6E006C614F /* bird.atlas */; }; + C2B44A101E9F7A9300CD4B11 /* FlyScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = C2B44A0F1E9F7A9300CD4B11 /* FlyScene.swift */; }; + C2B44A121E9F7B0800CD4B11 /* FlyScene.sks in Resources */ = {isa = PBXBuildFile; fileRef = C2B44A111E9F7B0800CD4B11 /* FlyScene.sks */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -24,6 +26,8 @@ 873BE2A81D06195B00A69FF5 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 873BE2AA1D06195B00A69FF5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 87AA11DB1D0B4D6E006C614F /* bird.atlas */ = {isa = PBXFileReference; lastKnownFileType = folder.skatlas; path = bird.atlas; sourceTree = ""; }; + C2B44A0F1E9F7A9300CD4B11 /* FlyScene.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FlyScene.swift; path = ../FlyScene.swift; sourceTree = ""; }; + C2B44A111E9F7B0800CD4B11 /* FlyScene.sks */ = {isa = PBXFileReference; lastKnownFileType = file.sks; path = FlyScene.sks; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -58,6 +62,8 @@ children = ( 873BE29A1D06195B00A69FF5 /* AppDelegate.swift */, 873BE2A21D06195B00A69FF5 /* Main.storyboard */, + C2B44A0F1E9F7A9300CD4B11 /* FlyScene.swift */, + C2B44A111E9F7B0800CD4B11 /* FlyScene.sks */, 873BE2A01D06195B00A69FF5 /* ViewController.swift */, 873BE2A51D06195B00A69FF5 /* Assets.xcassets */, 87AA11DB1D0B4D6E006C614F /* bird.atlas */, @@ -137,6 +143,7 @@ 873BE2A91D06195B00A69FF5 /* LaunchScreen.storyboard in Resources */, 873BE2A61D06195B00A69FF5 /* Assets.xcassets in Resources */, 87AA11DC1D0B4D6E006C614F /* bird.atlas in Resources */, + C2B44A121E9F7B0800CD4B11 /* FlyScene.sks in Resources */, 873BE2A41D06195B00A69FF5 /* Main.storyboard in Resources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -149,6 +156,7 @@ buildActionMask = 2147483647; files = ( 873BE2A11D06195B00A69FF5 /* ViewController.swift in Sources */, + C2B44A101E9F7A9300CD4B11 /* FlyScene.swift in Sources */, 873BE29B1D06195B00A69FF5 /* AppDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; 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..647b6ce Binary files /dev/null and b/FlappySwift/FlyScene.sks differ diff --git a/FlappySwift/ViewController.swift b/FlappySwift/ViewController.swift index f2baeaa..6f2c692 100644 --- a/FlappySwift/ViewController.swift +++ b/FlappySwift/ViewController.swift @@ -17,7 +17,18 @@ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() + + guard let scene = FlyScene(fileNamed: + "FlyScene") else { + return + } + + scene.size = self.view.bounds.size + + sceneView.presentScene(scene) + + // Configure scene view sceneView.showsFPS = true sceneView.showsNodeCount = true diff --git a/FlyScene.swift b/FlyScene.swift new file mode 100644 index 0000000..3e25b4c --- /dev/null +++ b/FlyScene.swift @@ -0,0 +1,105 @@ +// +// FlyScene.swift +// FlappySwift +// +// Created by Katja D on 13.04.17. +// Copyright © 2017 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import UIKit +import SpriteKit + +let worldCollisionCategory: UInt32 = 1 << 1 +let obstacleCollisonCategory: UInt32 = 1 << 2 +let birdCollisionCategory: UInt32 = 1 << 3 + +class FlyScene: SKScene, SKPhysicsContactDelegate { + + let impulseOnFlap: CGFloat = 500 + + let bird: SKSpriteNode = { + let spriteNode = SKSpriteNode(imageNamed: "bird-01") + let physicsBody = SKPhysicsBody(circleOfRadius: spriteNode.size.width / 2) + physicsBody.mass = 1 + physicsBody.linearDamping = 0 + physicsBody.restitution = 1 + physicsBody.collisionBitMask = worldCollisionCategory | + obstacleCollisonCategory + physicsBody.categoryBitMask = birdCollisionCategory + physicsBody.contactTestBitMask = obstacleCollisonCategory + spriteNode.physicsBody = physicsBody + return spriteNode + + }() + + 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.collisionBitMask = 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 = obstacleCollisonCategory + 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.position = CGPoint(x: -100, y: -100) + bird.physicsBody?.velocity = CGVector(dx: 0, dy: 0) + 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 + self.physicsWorld.speed = 0 + } +}