Thanks. I really value your friendly help.
I want to:
1) begin by placing a bunch of differently colored SKShapeNode objects on the SKScene. I'd cram there as much 32x32 CGSize as possible.
2) The game will then begin by a pair of automatic turns:
a) having my (lower-left corner shape) initialized (changing its color and actually turning around in order to contact the surrounding similarly colored shapes using SKPhysicsContactDelegate.didBegin() ) and take possession of them
b) same for the computer opponent (upper right corner)
3) At this point, the game may begin for real: either I or the computer play(s) and choose a color which cannot be the opponent's in order to chain-reaction all of his shapes to absorb even more similarly colored, unonwned surrounding ones.
But then I am currently stuck because the inital tour doesn't quite take place: only my tile NSLogs me it has rotated but it actually doesn't.
Bits of code:
override func didMove(to view: SKView) {
/* Set the scale mode to scale to fit the window */
scaleMode = .resizeFill
// Set physics
physicsWorld.gravity = .zero
physicsWorld.contactDelegate = self
backgroundColor = SKColor.darkGray
label = SKLabelNode(fontNamed: "Chalkduster")
label.text = "Color ZooZ 2"
label.horizontalAlignmentMode = .center
label.verticalAlignmentMode = .center
label.color = SKColor.systemPink
label.fontSize = 48
label.position = CGPoint(x: size.width/2, y: size.height/2)
addChild(label)
installShapes()
}
func installShapes() {
X = size.width / (SIZE + SPACE) - 1
Y = size.height / (SIZE + SPACE) - 1
dX = (size.width - (X * (SIZE + SPACE))) / 1
dY = (size.height - (Y * (SIZE + SPACE))) / 1
NSLog("MIRKO display: %0.0f x %0.0f & Nb: %0.0f x %0.0f & dX: %0.0f / dY: %0.0f", size.width, size.height, X, Y, dX, dY)
self.run(SKAction.run {
for x in 0...Int8(self.X - 1) {
for y in 0...Int8(self.Y - 1) {
self.addShape(atX: x, andY: y)
}
}
}, completion: {
NSLog("End of installShapes, about to rename the player shapes")
self.renameShape(from: "\(0).\(0)", to: "me")
self.renameShape(from: "\(self.X-1).\(self.Y-1)", to: "him")
})
}
func renameShape(from oldName : String, to newName : String) {
if let s = shapeByName(name: oldName) as SKShapeNode? {
s.strokeColor = s.fillColor
s.physicsBody?.categoryBitMask = PhysicsCategory.mine
s.physicsBody?.contactTestBitMask = PhysicsCategory.unset
s.physicsBody?.collisionBitMask = PhysicsCategory.none
s.name = newName
s.alpha = 1.0
propagate(shape: s)
}
}
func addShape(atX x: Int8, andY y : Int8) {
let shape = SKShapeNode(rectOf: CGSize(width: SIZE, height: SIZE), cornerRadius: SIZE / 4)
shape.name = "\(x).\(y)"
shape.position = CGPoint(x: 0, y: 0)
let generator = ColorGenerator()
shape.fillColor = generator.random()
shape.strokeColor = UIColor.lightGray
shape.lineWidth = BORDER
shape.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: SIZE, height: SIZE))
shape.physicsBody?.isDynamic = false
shape.physicsBody?.categoryBitMask = PhysicsCategory.unset
shape.physicsBody?.contactTestBitMask = PhysicsCategory.mine | PhysicsCategory.his
shape.physicsBody?.collisionBitMask = PhysicsCategory.none
shape.physicsBody?.usesPreciseCollisionDetection = true
shape.isPaused = false
addChild(shape)
let placeIt = SKAction.group(
[SKAction.fadeAlpha(by: 0.5, duration: 2),
SKAction.move(to: CGPoint(x: CGFloat(x) * (SIZE + SPACE) + dX, y: CGFloat(y) * (SIZE + SPACE) + dY), duration: 2),
SKAction.wait(forDuration: 0.1)])
placeIt.timingMode = SKActionTimingMode.easeInEaseOut
NSLog("moving %@", shape.name!)
shape.run(placeIt, withKey: "placeIt \(shape.name!)")
NSLog("%@ moved", shape.name!)
}
func shapeAt(x mX : Int8, y mY : Int8) -> SKShapeNode? {
let allNodes = nodes(at: CGPoint(x: CGFloat(mX) * (SIZE + SPACE) + dX, y: CGFloat(mY) * (SIZE + SPACE) + dY))
for node in allNodes {
if let o = node as? SKShapeNode {
return o
}
}
return nil
}
func shapeByName(name s:String) -> SKShapeNode? {
if let c = childNode(withName: s) as? SKShapeNode {
return c
}
return nil
}
func propagate(shape s : SKShapeNode) {
NSLog("Propagating...")
if s.isPaused { s.isPaused = false }
let rotate = SKAction.rotate(toAngle: .pi, duration: 0.5)
rotate.timingMode = SKActionTimingMode.easeInEaseOut
NSLog("rotating")
s.run(rotate, withKey: "rotate \(s.name!)")
NSLog("rotated")
}
Which SKScene member funcion should I override for which part of the game init/turn, etc?
Once again, thanks!