232 lines
7.2 KiB
Swift
232 lines
7.2 KiB
Swift
//
|
|
// GameScene.swift
|
|
// GoldWars
|
|
//
|
|
// Created by Aldin Duraki on 18.04.20.
|
|
// Copyright © 2020 SP2. All rights reserved.
|
|
//
|
|
|
|
import SpriteKit
|
|
import GameplayKit
|
|
|
|
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)
|
|
menu.zPosition = 2
|
|
|
|
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 {
|
|
let color = PlayerColors.colors.randomElement()
|
|
let xRange = Int(frame.minX + frame.width * 0.07)...Int(frame.maxX - frame.width * 0.07)
|
|
let yRange = Int(frame.minY + frame.height * 0.07)...Int(frame.maxY - frame.width * 0.07)
|
|
let position = CGPoint(
|
|
x: Int.random(in: xRange),
|
|
y: Int.random(in: yRange)
|
|
)
|
|
|
|
print("Generating Base at \(position)")
|
|
|
|
let base = Base(frame: frame, color: color!, position: position)
|
|
|
|
addChild(base.spriteNode)
|
|
bases.append(base)
|
|
}
|
|
*/
|
|
}
|
|
|
|
func createVirginBases() {
|
|
for i in 0...7 {
|
|
let base:Base
|
|
let color = SKColor.green
|
|
|
|
var position = CGPoint(x: 0, y: 0)
|
|
switch i {
|
|
case 0...2:
|
|
let width = self.size.width * 0.25
|
|
|
|
if i == 0 {
|
|
position = CGPoint(x: width, y: self.size.height * 0.25)
|
|
}
|
|
if i == 1 {
|
|
position = CGPoint(x: width, y: self.size.height * 0.5)
|
|
}
|
|
if i == 2{
|
|
position = CGPoint(x: width, y: self.size.height * 0.75)
|
|
}
|
|
|
|
case 3...4:
|
|
let width = self.size.width * 0.5
|
|
|
|
if i == 3{
|
|
position = CGPoint(x: width, y: self.size.height * 0.333)
|
|
}
|
|
if i == 4{
|
|
position = CGPoint(x: width, y: self.size.height * 0.666)
|
|
}
|
|
|
|
case 5...7:
|
|
let width = self.size.width * 0.75
|
|
|
|
if i == 5{
|
|
position = CGPoint(x: width, y: self.size.height * 0.25)
|
|
}
|
|
if i == 6{
|
|
position = CGPoint(x: width, y: self.size.height * 0.5)
|
|
}
|
|
if i == 7{
|
|
position = CGPoint(x: width, y: self.size.height * 0.75)
|
|
}
|
|
|
|
default:
|
|
break
|
|
}
|
|
|
|
base = Base(color: color, position: position, name: "Base\(i)")
|
|
print(base.position)
|
|
bases.append(base)
|
|
self.addChild(base)
|
|
}
|
|
}
|
|
|
|
func addLine(base1: Base , base2: Base){
|
|
let line = SKShapeNode()
|
|
let linePath = CGMutablePath()
|
|
linePath.move(to: base1.position)
|
|
linePath.addLine(to: base2.position)
|
|
line.path = linePath
|
|
line.strokeColor = SKColor.white
|
|
addChild(line)
|
|
|
|
}
|
|
|
|
func connectBases(){
|
|
player[0].addAvailableBase(base: bases[0])
|
|
player[0].addAvailableBase(base: bases[1])
|
|
player[0].addAvailableBase(base: bases[2])
|
|
|
|
bases[0].addAvailableBase(base: bases[3])
|
|
|
|
bases[1].addAvailableBase(base: bases[3])
|
|
bases[1].addAvailableBase(base: bases[4])
|
|
|
|
bases[2].addAvailableBase(base: bases[4])
|
|
|
|
bases[3].addAvailableBase(base: bases[5])
|
|
bases[3].addAvailableBase(base: bases[6])
|
|
|
|
bases[4].addAvailableBase(base: bases[7])
|
|
bases[4].addAvailableBase(base: bases[6])
|
|
|
|
player[1].addAvailableBase(base: bases[5])
|
|
player[1].addAvailableBase(base: bases[6])
|
|
player[1].addAvailableBase(base: bases[7])
|
|
|
|
for base in player{
|
|
for availableBase in base.availableBases{
|
|
addLine(base1: base, base2: availableBase)
|
|
}
|
|
}
|
|
|
|
for base in bases{
|
|
for availableBase in base.availableBases{
|
|
addLine(base1: base, base2: availableBase)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func touchDown(atPoint pos : CGPoint) {
|
|
|
|
}
|
|
|
|
func touchMoved(toPoint pos : CGPoint) {
|
|
|
|
}
|
|
|
|
func touchUp(atPoint pos : CGPoint) {
|
|
|
|
}
|
|
|
|
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?) {
|
|
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?) {
|
|
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 update(_ currentTime: TimeInterval) {
|
|
|
|
}
|
|
}
|