diff --git a/GoldWars/GoldWars/Map/MapProtocoll.swift b/GoldWars/GoldWars/Map/MapProtocoll.swift index 5d3412a..b452231 100644 --- a/GoldWars/GoldWars/Map/MapProtocoll.swift +++ b/GoldWars/GoldWars/Map/MapProtocoll.swift @@ -7,9 +7,13 @@ // import Foundation +import SpriteKit protocol MapProtocoll { + var size: CGSize! {get set} + + func create() func adjacent(base: Base) -> Array } diff --git a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift index ab9387b..c5e818a 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift @@ -7,13 +7,78 @@ // import Foundation +import SpriteKit class TwoPlayerDefaultTestMap: MapProtocoll { - - func adjacent(base: Base) -> Array { - return Array() + var entityManager: EntityManager! + var size: CGSize! + var adjacencyList: Dictionary>! + + init(scene: SKScene, entityManager: EntityManager) { + self.entityManager = entityManager; + self.size = scene.size } + func adjacent(base: Base) -> Array { + return adjacencyList[base]! + } + func create() { + createNodes() + createNeighbourMapping() + } + + func createNodes() { + for i in 0...7 { + let base: Base + 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(textureName: "Base", team: nil, position: position) + entityManager.add(base) + } + } + + func createNeighbourMapping() { + + } } diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index e9380f2..6096538 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -10,140 +10,91 @@ import SpriteKit import GameplayKit class GameScene: SKScene{ - - var entityManager: EntityManager! - - override func sceneDidLoad() { - entityManager = EntityManager(scene: self) - entityManager.add(Base(textureName: "Base", - team: .team1 , - position: CGPoint(x: self.size.width * 0.1, y: self.size.height / 2))) - - entityManager.add(Base(textureName: "Base", - team: .team2 , - position: CGPoint(x: self.size.width * 0.9, y: self.size.height / 2))) - entityManager.add(HUD(size: self.size)) - initMap() - createBackground() - - } - - func createBackground() { - for i in 0...2 { - let sky = SKSpriteNode(imageNamed: "SkyBackground") - sky.name = "clouds" - sky.zPosition = -1 - sky.size = CGSize(width: (self.scene?.size.width)!, height: (self.scene?.size.height)!) - sky.position = CGPoint(x: CGFloat(i) * sky.size.width , y: (self.frame.size.height / 2)) - - self.addChild(sky) - } - } - - // TODO: Issue #24 create Map generation Service - func initMap() { - - createVirginBases() - } - - func createVirginBases() { - for i in 0...7 { - let base:Base - 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(textureName: "Base", team: nil, position: position) - entityManager.add(base) - } - - - } - - override func touchesBegan(_ touches: Set, with event: UIEvent?) { - guard let touch = touches.first else { - return - } - - let touchLocation = touch.location(in: self) - - for entity in entityManager.entities { - - let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode - - if entityManager.isModal && entity.isMember(of: Modal.self) { - entityManager.remove(entity) - for child in self.children { - if(child.name != "fire"){ - child.alpha = 1 - } - } - - } - - if atPoint(touchLocation) == spriteNode && !entityManager.isModal { - spriteNode?.touchesBegan(touches, with: event) - if !entityManager.isModal { - for child in self.children { - if(child.name != "fire"){ - child.alpha = 0.3 - } - - } - entityManager.add(Modal(modaltype: .BaseDetails, - base: entity as! Base, - anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2))) - } - } - } - } - - - - override func update(_ currentTime: TimeInterval) { - self.enumerateChildNodes(withName: "clouds", using: ({ - (node, error) in - node.position.x -= 2 - if node.position.x < -(self.scene?.size.width)! { - node.position.x += (self.scene?.size.width)! * 3 - } - })) - } + + var entityManager: EntityManager! + var map: MapProtocoll! + + override func sceneDidLoad() { + entityManager = EntityManager(scene: self) + entityManager.add(Base(textureName: "Base", + team: .team1 , + position: CGPoint(x: self.size.width * 0.1, y: self.size.height / 2))) + + entityManager.add(Base(textureName: "Base", + team: .team2 , + position: CGPoint(x: self.size.width * 0.9, y: self.size.height / 2))) + entityManager.add(HUD(size: self.size)) + initMap() + createBackground() + + } + + func createBackground() { + for i in 0...2 { + let sky = SKSpriteNode(imageNamed: "SkyBackground") + sky.name = "clouds" + sky.zPosition = -1 + sky.size = CGSize(width: (self.scene?.size.width)!, height: (self.scene?.size.height)!) + sky.position = CGPoint(x: CGFloat(i) * sky.size.width , y: (self.frame.size.height / 2)) + + self.addChild(sky) + } + } + + // TODO: Issue #24 create Map generation Service + func initMap() { + self.map = TwoPlayerDefaultTestMap(scene: self, entityManager: self.entityManager) + self.map.create() + } + + + override func touchesBegan(_ touches: Set, with event: UIEvent?) { + guard let touch = touches.first else { + return + } + + let touchLocation = touch.location(in: self) + + for entity in entityManager.entities { + + let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode + + if entityManager.isModal && entity.isMember(of: Modal.self) { + entityManager.remove(entity) + for child in self.children { + if(child.name != "fire"){ + child.alpha = 1 + } + } + + } + + if atPoint(touchLocation) == spriteNode && !entityManager.isModal { + spriteNode?.touchesBegan(touches, with: event) + if !entityManager.isModal { + for child in self.children { + if(child.name != "fire"){ + child.alpha = 0.3 + } + + } + entityManager.add(Modal(modaltype: .BaseDetails, + base: entity as! Base, + anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2))) + } + } + } + } + + + + override func update(_ currentTime: TimeInterval) { + self.enumerateChildNodes(withName: "clouds", using: ({ + (node, error) in + node.position.x -= 2 + if node.position.x < -(self.scene?.size.width)! { + node.position.x += (self.scene?.size.width)! * 3 + } + })) + } }