Created
March 8, 2023 18:55
-
-
Save wizard1066/914faca649566297499b89ccbfc44d68 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
class GameScene: SKScene, SKPhysicsContactDelegate { | |
var ball:SKSpriteNode! | |
var paddle:SKSpriteNode! | |
var begin = true | |
let points = Position.shared | |
override init() { | |
super.init() | |
} | |
override init(size: CGSize) { | |
super.init(size: size) | |
print("bar") | |
fireSubscriber = firePublisher | |
.debounce(for: .milliseconds(500), scheduler: RunLoop.main) | |
.sink(receiveValue: { [self] direct in | |
for point in points.points { | |
let box = SKSpriteNode(color: .green, size: CGSize(width: points.rect.width, height: points.rect.height)) | |
box.position = point | |
box.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: points.rect.width, height: points.rect.height)) | |
box.physicsBody?.isDynamic = false | |
box.physicsBody?.categoryBitMask = BodyType.type1.rawValue | |
box.physicsBody?.affectedByGravity = false | |
box.name = "ball" | |
addChild(box) | |
} | |
}) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
func didBegin(_ contact: SKPhysicsContact) { | |
if contact.bodyA.node?.name == "ball" { | |
contact.bodyA.node?.removeFromParent() | |
} | |
} | |
override func didMove(to view: SKView) { | |
physicsBody = SKPhysicsBody(edgeLoopFrom: frame) | |
physicsWorld.contactDelegate = self | |
physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0) | |
} | |
@objc private func didSwipe(_ sender: UISwipeGestureRecognizer) { | |
switch sender.direction { | |
case .left: | |
paddle.position.x += 64 | |
case .right: | |
paddle.position.x -= 64 | |
default: | |
break | |
} | |
} | |
override func update(_ currentTime: TimeInterval) { | |
if ball != nil { | |
points.point = ball.position | |
points.rotate = CGFloat(GLKMathRadiansToDegrees(Float(ball.zRotation))) | |
} | |
} | |
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { | |
guard let touch = touches.first else { return } | |
let location = touch.location(in: self) | |
paddle.position.x = location.x | |
} | |
fileprivate func buildBall(_ location: CGPoint) { | |
ball = SKSpriteNode(imageNamed: "64x64") | |
ball.scale(to: CGSize(width: 32, height: 32)) | |
ball.position = location | |
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width / 2.0) | |
ball.physicsBody?.isDynamic = true | |
ball.physicsBody?.categoryBitMask = UInt32(BodyType.type2.rawValue) | |
ball.physicsBody?.contactTestBitMask = UInt32(BodyType.type1.rawValue) | |
ball.physicsBody?.restitution = 1 | |
ball.physicsBody?.friction = 0.0 | |
ball.physicsBody?.linearDamping = 0.0 | |
ball.physicsBody?.angularDamping = 0.0 | |
ball.name = "ball" | |
addChild(ball) | |
ball.physicsBody?.applyImpulse(CGVector(dx: 24, dy: -24.0)) | |
} | |
fileprivate func buildPaddle() { | |
paddle = SKSpriteNode(imageNamed: "128x16") | |
paddle.position = CGPoint(x: screenWidth/2, y: 32) | |
paddle.physicsBody = SKPhysicsBody(rectangleOf: paddle.size) | |
paddle.physicsBody?.isDynamic = false | |
paddle.physicsBody?.categoryBitMask = UInt32(BodyType.type3.rawValue) | |
paddle.physicsBody?.contactTestBitMask = UInt32(BodyType.type2.rawValue) | |
paddle.physicsBody?.restitution = 1 | |
paddle.physicsBody?.friction = 0.0 | |
paddle.name = "paddle" | |
addChild(paddle) | |
} | |
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | |
if begin { | |
begin.toggle() | |
guard let touch = touches.first else { return } | |
let location = touch.location(in: self) | |
buildBall(location) | |
buildPaddle() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment