I'm currently developing a game where the players fire projectiles at each other from either side of the screen. (like that Bow masters game on the appstore.)
I'm currently trying to integrate an Ai into the game to play as the opposite team using gameplaykit.
My problem is i'm reading up on how to do it and they use a board and have all the board points laid out in an array, My game doesn't use a board it just has player health and throws projectiles using vectors and impulses so there is no board, just locations on where the projectile lands and if it hits a player or not.
How do i create a GKGameModel for this when I don't have an actual board?
Here is my code that I have so far:
Projectile Shooting Code:
let touchDX = touchLocation.x//your calculation
let touchDY = touchLocation.y//your calculation
let touchLength = sqrt(touchDX*touchDX+touchDY*touchDY)
let unitVectorDX = touchDX / touchLength
let unitVectorDY = touchDY / touchLength
let speed:CGFloat = touchLength / 150
vector = CGVector(dx: -unitVectorDX * speed, dy: unitVectorDY * speed)
playerBullet.physicsBody?.applyImpulse(vector)
AI Code So Far:
class Player: SKNode, GKGameModelPlayer {
var playerSprite = SKSpriteNode()
var playerId: Int = 0
var health = 100
func createPlayer(playerColor: UIColor, pos: CGPoint, posZ: CGFloat) {
//CREATE PLAYER HERE
playerSprite = SKSpriteNode(texture: nil, color: playerColor ?? .white, size: CGSize(width: 50, height: 50))
playerSprite.position = pos
playerSprite.zPosition = posZ
}
}
class Move: NSObject, GKGameModelUpdate {
var value: Int = 0
var coordinate: CGPoint
init(_ coordinate: CGPoint) {
self.coordinate = coordinate
}
}
class Board: NSObject, GKGameModel {
//Zone is an old concept that doesn't really help but it works
func copy(with zone: NSZone? = nil) -> Any {
let copy = Board()
copy.setGameModel(self)
return copy
}
func setGameModel(_ gameModel: GKGameModel) {
if let board = gameModel as? Board {
//DO STUFF HERE
//slots = board.slots
//currentPlayer = board.currentPlayer
}
}
}