From 4acd12ddc52cef7480df3934566a35547f182981 Mon Sep 17 00:00:00 2001 From: 127-Z3R0 <81heti1bif@hft-stuttgart.de> Date: Mon, 20 Apr 2020 14:21:55 +0200 Subject: [PATCH] add PhysicsBody for Drag&Drop Functionality --- GoldWars/GoldWars/Game/GameScene.swift | 51 ++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/GoldWars/GoldWars/Game/GameScene.swift b/GoldWars/GoldWars/Game/GameScene.swift index d8db3a2..4e508d0 100644 --- a/GoldWars/GoldWars/Game/GameScene.swift +++ b/GoldWars/GoldWars/Game/GameScene.swift @@ -9,26 +9,52 @@ import SpriteKit import GameplayKit -class GameScene: SKScene { +class GameScene: SKScene, SKPhysicsContactDelegate { var entities = [GKEntity]() var graphs = [String : GKGraph]() var bases = [Base]() var player = [Base]() + var menu = SKSpriteNode() + + struct physicsBodyNumber { + static let basePlayer1Number: UInt32 = 0b1 + static let basePlayer2Number: UInt32 = 0b10 + static let base5Number: UInt32 = 0b101 + static let base6Number: UInt32 = 0b110 + static let base7Number: UInt32 = 0b111 + static let emptyNumber: UInt32 = 0b100 + } override func sceneDidLoad() { + + self.physicsWorld.gravity = CGVector(dx: 0, dy: 0) + self.physicsWorld.contactDelegate = self let maxX = self.size.width let midY = self.size.height / 2 let basePlayer1 = Base(color: SKColor.red, position: CGPoint(x: maxX * 0.1, y: midY), name: "Player1" ) let basePlayer2 = Base(color: SKColor.blue, position: CGPoint(x: maxX * 0.9, y: midY), name: "Player2" ) + basePlayer2.physicsBody = SKPhysicsBody(circleOfRadius: 50) + basePlayer2.physicsBody?.categoryBitMask = physicsBodyNumber.basePlayer2Number + basePlayer2.physicsBody?.collisionBitMask = physicsBodyNumber.emptyNumber + basePlayer2.physicsBody?.contactTestBitMask = physicsBodyNumber.base5Number & physicsBodyNumber.base6Number & physicsBodyNumber.base7Number + + menu = SKSpriteNode(color: SKColor.blue , size: CGSize(width: self.size.width * 0.4, height: self.size.height * 0.4)) + menu.position = CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.5) self.addChild(basePlayer1) self.addChild(basePlayer2) player.append(basePlayer1) player.append(basePlayer2) createVirginBases() + connectBases() + + bases[5].physicsBody = SKPhysicsBody(circleOfRadius: 50) + bases[5].physicsBody?.categoryBitMask = physicsBodyNumber.base5Number + bases[5].physicsBody?.collisionBitMask = physicsBodyNumber.emptyNumber + bases[5].physicsBody?.contactTestBitMask = physicsBodyNumber.basePlayer2Number // Spawn bases /*for _ in 1...15 { @@ -164,15 +190,34 @@ class GameScene: SKScene { } override func touchesBegan(_ touches: Set, with event: UIEvent?) { - + for touch in touches { + print("hit") + let locationUser = touch.location(in: self) + if atPoint(locationUser) == menu { + menu.removeFromParent() + } + } } override func touchesMoved(_ touches: Set, with event: UIEvent?) { - + for touch in touches { + print("hit") + let locationUser = touch.location(in: self) + if atPoint(locationUser) == player[1]{ + player[1].position = locationUser + } + } } override func touchesEnded(_ touches: Set, with event: UIEvent?) { + let maxX = self.size.width + let midY = self.size.height / 2 + player[1].position = CGPoint(x: maxX * 0.9, y: midY) + } + + func didBegin(_ contact: SKPhysicsContact) { + self.addChild(menu) } override func touchesCancelled(_ touches: Set, with event: UIEvent?) {