add PhysicsBody for Drag&Drop Functionality

This commit is contained in:
127-Z3R0 2020-04-20 14:21:55 +02:00
parent 7d57decb93
commit 4acd12ddc5

View File

@ -9,26 +9,52 @@
import SpriteKit import SpriteKit
import GameplayKit import GameplayKit
class GameScene: SKScene { class GameScene: SKScene, SKPhysicsContactDelegate {
var entities = [GKEntity]() var entities = [GKEntity]()
var graphs = [String : GKGraph]() var graphs = [String : GKGraph]()
var bases = [Base]() var bases = [Base]()
var player = [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() { override func sceneDidLoad() {
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
self.physicsWorld.contactDelegate = self
let maxX = self.size.width let maxX = self.size.width
let midY = self.size.height / 2 let midY = self.size.height / 2
let basePlayer1 = Base(color: SKColor.red, position: CGPoint(x: maxX * 0.1, y: midY), name: "Player1" ) 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" ) 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(basePlayer1)
self.addChild(basePlayer2) self.addChild(basePlayer2)
player.append(basePlayer1) player.append(basePlayer1)
player.append(basePlayer2) player.append(basePlayer2)
createVirginBases() 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 // Spawn bases
/*for _ in 1...15 { /*for _ in 1...15 {
@ -164,15 +190,34 @@ class GameScene: SKScene {
} }
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { override func touchesBegan(_ touches: Set<UITouch>, 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<UITouch>, with event: UIEvent?) { override func touchesMoved(_ touches: Set<UITouch>, 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<UITouch>, with event: UIEvent?) { override func touchesEnded(_ touches: Set<UITouch>, 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<UITouch>, with event: UIEvent?) { override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {