Created
April 28, 2017 14:13
-
-
Save devpolant/48b26c6b6650f5a3d53055955556edb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
import SpriteKit | |
import PlaygroundSupport | |
let sceneView = SKView(frame: CGRect(x: 0, y: 0, width: 480, height: 320)) | |
let scene = SKScene(size: CGSize(width: 480, height: 320)) | |
scene.physicsWorld.gravity = .zero | |
scene.physicsBody = SKPhysicsBody(edgeLoopFrom: scene.frame) | |
sceneView.showsFPS = true | |
sceneView.showsPhysics = true | |
sceneView.presentScene(scene) | |
PlaygroundPage.current.liveView = sceneView | |
let square = SKSpriteNode(imageNamed: "square") | |
square.name = "square" | |
square.position = CGPoint(x: scene.size.width * 0.25, | |
y: scene.size.height * 0.5) | |
let circle = SKSpriteNode(imageNamed: "circle") | |
circle.name = "shape" | |
circle.position = CGPoint(x: scene.size.width * 0.5, | |
y: scene.size.height * 0.5) | |
let triangle = SKSpriteNode(imageNamed: "triangle") | |
triangle.name = "shape" | |
triangle.position = CGPoint(x: scene.size.width * 0.75, | |
y: scene.size.height * 0.5) | |
scene.addChild(square) | |
scene.addChild(circle) | |
scene.addChild(triangle) | |
square.physicsBody = SKPhysicsBody(rectangleOf: square.frame.size) | |
circle.physicsBody = SKPhysicsBody(circleOfRadius: circle.size.width / 2) | |
circle.physicsBody?.isDynamic = false | |
circle.run( | |
SKAction.repeatForever( | |
SKAction.move(by: CGVector(dx: -2, dy: -2), duration: 0.1) | |
) | |
) | |
var trianglePath = CGMutablePath() | |
trianglePath.move(to: CGPoint(x: -triangle.size.width / 2, | |
y: -triangle.size.height / 2)) | |
trianglePath.addLine(to: CGPoint(x: triangle.size.width / 2, | |
y: -triangle.size.height / 2)) | |
trianglePath.addLine(to: CGPoint(x: 0, | |
y: triangle.size.height / 2)) | |
trianglePath.addLine(to: CGPoint(x: -triangle.size.width / 2, | |
y: -triangle.size.height / 2)) | |
triangle.physicsBody = SKPhysicsBody(polygonFrom: trianglePath) | |
let l = SKSpriteNode(imageNamed: "L") | |
l.name = "shape" | |
l.position = CGPoint(x: scene.size.width * 0.5, | |
y: scene.size.height * 0.75) | |
l.physicsBody = SKPhysicsBody(texture: l.texture!, size: l.size) | |
scene.addChild(l) | |
func spawnSand() { | |
let sand = SKSpriteNode(imageNamed: "sand") | |
sand.position = CGPoint( | |
x: random(min: 0.0, max: scene.size.width), | |
y: scene.size.height - sand.size.height) | |
sand.physicsBody = SKPhysicsBody(circleOfRadius: sand.size.width/2) | |
sand.name = "sand" | |
sand.physicsBody?.restitution = 0.0 // default 0.2 | |
sand.physicsBody?.density = 20.0 // default 1.0 | |
scene.addChild(sand) | |
} | |
func shake() { | |
scene.enumerateChildNodes(withName: "sand") { node, stopPointer in | |
node.physicsBody?.applyImpulse( | |
CGVector(dx: 0, | |
dy: random(min: 20, max: 40)) | |
) | |
} | |
scene.enumerateChildNodes(withName: "shape") { node, stopPointer in | |
node.physicsBody?.applyImpulse( | |
CGVector(dx: random(min: 20, max: 60), | |
dy: random(min: 20, max: 60)) | |
) | |
} | |
} | |
delay(seconds: 1) { | |
scene.physicsWorld.gravity = CGVector(dx: 0, dy: -9.8) | |
scene.run( | |
SKAction.repeatForever( | |
SKAction.sequence([ | |
SKAction.run(spawnSand), | |
SKAction.wait(forDuration: 0.1) | |
]) | |
) | |
) | |
delay(seconds: 5, completion: shake) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment