Skip to content

Commit

Permalink
Merge pull request #2 from Thinkful-Ed/Swift3
Browse files Browse the repository at this point in the history
Updated for Swift 3
  • Loading branch information
craiggrummitt authored Sep 27, 2016
2 parents 92b747e + 3253daa commit f2a037c
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 109 deletions.
12 changes: 11 additions & 1 deletion Llamas in Pajamas.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,16 @@
attributes = {
LastSwiftMigration = 0700;
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0700;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = Thinkful;
TargetAttributes = {
1D39B89319971341003CFECA = {
CreatedOnToolsVersion = 6.0;
LastSwiftMigration = 0800;
};
1D39B8A919971341003CFECA = {
CreatedOnToolsVersion = 6.0;
LastSwiftMigration = 0800;
TestTargetID = 1D39B89319971341003CFECA;
};
};
Expand Down Expand Up @@ -308,6 +310,7 @@
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
Expand Down Expand Up @@ -350,6 +353,7 @@
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
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;
Expand All @@ -372,6 +376,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.thinkful.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -384,6 +389,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.thinkful.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -403,6 +410,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.thinkful.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Llamas in Pajamas.app/Llamas in Pajamas";
};
name = Debug;
Expand All @@ -419,6 +427,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.thinkful.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Llamas in Pajamas.app/Llamas in Pajamas";
};
name = Release;
Expand Down
17 changes: 8 additions & 9 deletions Llamas in Pajamas/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(application: UIApplication) {
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
Expand Down
12 changes: 7 additions & 5 deletions Llamas in Pajamas/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6162" systemVersion="14A238h" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="LVB-YS-i5n">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11129.15" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="LVB-YS-i5n">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6160"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11103.10"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Game View Controller-->
<scene sceneID="CtS-rA-C9T">
<objects>
<viewController id="LVB-YS-i5n" customClass="GameViewController" customModuleProvider="target" sceneMemberID="viewController">
<viewController id="LVB-YS-i5n" customClass="GameViewController" customModule="Llamas_in_Pajamas" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="PCf-gQ-wO7"/>
<viewControllerLayoutGuide type="bottom" id="b3d-qh-BLL"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="bdc-N1-crn" customClass="SKView">
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" white="0" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="btX-cZ-eVz" sceneMemberID="firstResponder"/>
Expand Down
12 changes: 6 additions & 6 deletions Llamas in Pajamas/GameCharacter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import SpriteKit

enum CharacterType: UInt32 {
case Llama
case Pajama
case Lion
case Edge
case llama
case pajama
case lion
case edge
}

class GameCharacter: SKSpriteNode {
Expand All @@ -23,7 +23,7 @@ class GameCharacter: SKSpriteNode {
self.xScale = 0.4 // 0.5 for retina only images... slighlty smaller to make gameplay better
self.yScale = 0.4
self.physicsBody = SKPhysicsBody(circleOfRadius: 25) // why 25?
self.physicsBody!.dynamic = true
self.physicsBody!.isDynamic = true
self.physicsBody!.affectedByGravity = false
self.physicsBody!.restitution = 1.0
self.physicsBody!.friction = 0
Expand All @@ -37,7 +37,7 @@ class GameCharacter: SKSpriteNode {

var textureNames = atlas.textureNames

textureNames.sortInPlace({ $0 < $1 } )
textureNames.sort(by: { $0 < $1 } )

let textures = textureNames.map {
textureName -> SKTexture in
Expand Down
70 changes: 35 additions & 35 deletions Llamas in Pajamas/GameScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
var llama = Llama()
var pajamaCount = 0
var userWonTheGame = false
var startTime = NSDate()
var startTime = Date()

//MARK:- SKScene Methods

override func didMoveToView(view: SKView) {
override func didMove(to view: SKView) {
/* Setup your scene here */
playRect = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 120)
physicsBody = SKPhysicsBody(edgeLoopFromRect: playRect)
playRect = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height - 120)
physicsBody = SKPhysicsBody(edgeLoopFrom: playRect)
userWonTheGame = false
startTime = NSDate()
startTime = Date()

// add background
let background = SKSpriteNode(texture: SKTexture(imageNamed: "background"))
background.anchorPoint = CGPointZero
background.anchorPoint = CGPoint.zero
addChild(background)

addCharacters()
Expand All @@ -36,26 +36,26 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
physicsWorld.contactDelegate = self
}

override func update(currentTime: CFTimeInterval) {
override func update(_ currentTime: TimeInterval) {
/* Called before each frame is rendered */
}

//MARK:- Track Touches
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {

// Move llama to position of touch

var locationInScene = touch.locationInNode(self)
var locationInScene = touch.location(in: self)

//don't let the llama walk into the sky because that's silly
if locationInScene.y > playRect.size.height {
locationInScene.y = playRect.size.height
}

let moveAction = SKAction.moveTo(locationInScene, duration: 0.8)
moveAction.timingMode = SKActionTimingMode.EaseInEaseOut
llama.runAction(moveAction)
let moveAction = SKAction.move(to: locationInScene, duration: 0.8)
moveAction.timingMode = SKActionTimingMode.easeInEaseOut
llama.run(moveAction)

llama.animateWalk(1)
}
Expand All @@ -64,7 +64,7 @@ class GameScene: SKScene, SKPhysicsContactDelegate {

//MARK:- SKPhysicsContactDelegate Methods

func didBeginContact(contact: SKPhysicsContact) {
func didBegin(_ contact: SKPhysicsContact) {
var otherNode = SKNode()

if contact.bodyA.node == llama {
Expand All @@ -74,7 +74,7 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
otherNode = contact.bodyA.node!
}

if otherNode.isKindOfClass(GameCharacter) {
if otherNode is GameCharacter {
self.handleLlamaCollisions(otherNode as! GameCharacter)
}
}
Expand Down Expand Up @@ -106,41 +106,41 @@ class GameScene: SKScene, SKPhysicsContactDelegate {

// Add lions
let lion1 = Lion(runningSpeedInSeconds: 1.2)
lion1.position = CGPointMake(60, 100)
lion1.position = CGPoint(x: 60, y: 100)
addChild(lion1)

let lion2 = Lion(runningSpeedInSeconds: 0.9)
lion2.position = CGPointMake(60, 200)
lion2.position = CGPoint(x: 60, y: 200)
addChild(lion2)

let lion3 = Lion(runningSpeedInSeconds: 0.6)
lion3.position = CGPointMake(60, 300)
lion3.position = CGPoint(x: 60, y: 300)
addChild(lion3)
}

func addPajama(pajama: Pajama) {
func addPajama(_ pajama: Pajama) {
let randomX = CGFloat(arc4random() % UInt32(playRect.width))
let randomY = CGFloat(arc4random() % UInt32(playRect.height))
pajama.position = CGPointMake(randomX, randomY)
pajama.position = CGPoint(x: randomX, y: randomY)
addChild(pajama)
pajamaCount++
pajamaCount += 1
}

func addLlama() {
llama.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame)+110)
llama.position = CGPoint(x:self.frame.midX, y:self.frame.midY+110)
addChild(llama)
}

func handleLlamaCollisions(character: GameCharacter) {
func handleLlamaCollisions(_ character: GameCharacter) {

if character.isKindOfClass(Lion) {
if character is Lion {
lionContacted(character as! Lion)
} else if character.isKindOfClass(Pajama) {
} else if character is Pajama {
pajamaContacted(character as! Pajama)
}
}

func lionContacted(lion: Lion) {
func lionContacted(_ lion: Lion) {

llama.health -= 10

Expand All @@ -155,7 +155,7 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
}
}

func pajamaContacted(pajama: Pajama) {
func pajamaContacted(_ pajama: Pajama) {

// update the score based on pajama color
switch pajama.pajamaColor {
Expand Down Expand Up @@ -183,7 +183,7 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
pajama.removeFromParent()

// update count of pajamas remaining
pajamaCount--
pajamaCount -= 1

print("Llama now has \(llama.points) points and \(pajamaCount) pajamas remaining")

Expand All @@ -195,8 +195,8 @@ class GameScene: SKScene, SKPhysicsContactDelegate {

func showGameOverMessage() {

let currentTime = NSDate()
let gameTime = currentTime.timeIntervalSinceDate(startTime)
let currentTime = Date()
let gameTime = currentTime.timeIntervalSince(startTime)
print("Game completed in \(gameTime) seconds")
if userWonTheGame == true {
print("You Won!")
Expand All @@ -209,17 +209,17 @@ class GameScene: SKScene, SKPhysicsContactDelegate {

let gameOverAlert = UIAlertController(title: userWonTheGame ? "You Won!" : "You Lost!",
message: userWonTheGame ? "Great job!" : "Sorry!",
preferredStyle: UIAlertControllerStyle.Alert)
preferredStyle: UIAlertControllerStyle.alert)

gameOverAlert.addAction(UIAlertAction(title: "Restart",
style: UIAlertActionStyle.Default, handler: restartGame))
style: UIAlertActionStyle.default, handler: restartGame))

self.view?.window?.rootViewController?.presentViewController(gameOverAlert, animated: true, completion: nil)
self.view?.window?.rootViewController?.present(gameOverAlert, animated: true, completion: nil)
}

func restartGame(sender: UIAlertAction?) {
func restartGame(_ sender: UIAlertAction?) {
for child: AnyObject in self.children {
if child.isKindOfClass(GameCharacter) {
if child is GameCharacter {
child.removeFromParent()
}
}
Expand All @@ -229,7 +229,7 @@ class GameScene: SKScene, SKPhysicsContactDelegate {

pajamaCount = 0
userWonTheGame = false
startTime = NSDate()
startTime = Date()

addCharacters()

Expand Down
14 changes: 7 additions & 7 deletions Llamas in Pajamas/GameViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ class GameViewController: UIViewController {
skView.ignoresSiblingOrder = false

/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
scene.scaleMode = .aspectFill

skView.presentScene(scene)
}
}

override func shouldAutorotate() -> Bool {
override var shouldAutorotate:Bool {
return true
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return UIInterfaceOrientationMask.AllButUpsideDown
override var supportedInterfaceOrientations:UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return UIInterfaceOrientationMask.allButUpsideDown
} else {
return UIInterfaceOrientationMask.All
return UIInterfaceOrientationMask.all
}
}

Expand All @@ -46,7 +46,7 @@ class GameViewController: UIViewController {
// Release any cached data, images, etc that aren't in use.
}

override func prefersStatusBarHidden() -> Bool {
override var prefersStatusBarHidden:Bool {
return true
}
}
Loading

0 comments on commit f2a037c

Please sign in to comment.