diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index 37f6e37..e5de42f 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -35,8 +35,9 @@ 9EA3ABEF245C834B006BC61D /* ModalContentComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EA3ABEE245C834B006BC61D /* ModalContentComponent.swift */; }; 9EC86B9F245C88A300796EF3 /* Modal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EC86B9E245C88A300796EF3 /* Modal.swift */; }; 9EC86BA6245C8AD000796EF3 /* ModalType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EC86BA5245C8AD000796EF3 /* ModalType.swift */; }; - AB1D759C245DD18100671525 /* MapProtocoll.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759A245DD18100671525 /* MapProtocoll.swift */; }; + AB1D759C245DD18100671525 /* MapProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759A245DD18100671525 /* MapProtocol.swift */; }; AB1D759D245DD18100671525 /* TwoPlayerDefaultTestMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */; }; + AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759F245DEC0500671525 /* MapFactory.swift */; }; ABA03DA0244BD54F00A66916 /* Base.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABA03D9F244BD54F00A66916 /* Base.swift */; }; /* End PBXBuildFile section */ @@ -93,8 +94,9 @@ 9EC86B9E245C88A300796EF3 /* Modal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Modal.swift; sourceTree = ""; }; 9EC86BA5245C8AD000796EF3 /* ModalType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModalType.swift; sourceTree = ""; }; 9ECD3699245C91F7008DEEBD /* GoldWars.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = GoldWars.entitlements; sourceTree = ""; }; - AB1D759A245DD18100671525 /* MapProtocoll.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MapProtocoll.swift; sourceTree = ""; }; + AB1D759A245DD18100671525 /* MapProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MapProtocol.swift; sourceTree = ""; }; AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TwoPlayerDefaultTestMap.swift; sourceTree = ""; }; + AB1D759F245DEC0500671525 /* MapFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapFactory.swift; sourceTree = ""; }; ABA03D9F244BD54F00A66916 /* Base.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -258,7 +260,8 @@ AB1D759E245DD2EA00671525 /* Map */ = { isa = PBXGroup; children = ( - AB1D759A245DD18100671525 /* MapProtocoll.swift */, + AB1D759A245DD18100671525 /* MapProtocol.swift */, + AB1D759F245DEC0500671525 /* MapFactory.swift */, AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */, ); path = Map; @@ -413,7 +416,8 @@ 9EA3ABE9245C6DAA006BC61D /* DefaultBaseComponent.swift in Sources */, 9EA3ABED245C8143006BC61D /* ModalBackgroundComponent.swift in Sources */, 3EBD242C245D8044003CECE7 /* GameCenterHelper.swift in Sources */, - AB1D759C245DD18100671525 /* MapProtocoll.swift in Sources */, + AB1D759C245DD18100671525 /* MapProtocol.swift in Sources */, + AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */, AB1D759D245DD18100671525 /* TwoPlayerDefaultTestMap.swift in Sources */, ABA03DA0244BD54F00A66916 /* Base.swift in Sources */, 110360DB244B101A008610AF /* GameViewController.swift in Sources */, diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index 75415f5..6ae0a10 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -9,21 +9,27 @@ import SpriteKit import GameplayKit -class Base : GKEntity{ - var unitCount:Int - - init(textureName:String, team: Team?,position: CGPoint ) { - self.unitCount = 0 +class Base : GKEntity { + var unitCount: Int + var adjacencyList: Array + + init(position: CGPoint, team: Team! = nil) { + self.unitCount = 0 + self.adjacencyList = [Base]() + super.init() - addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: textureName), position: position)) - if(team != nil){ - addComponent(TeamComponent(team: team!, position: position)) + + addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: "Base"), position: position)) + if(team != nil){ + addComponent(TeamComponent(team: team!, position: position)) self.unitCount = 100 } } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - + + + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + } diff --git a/GoldWars/GoldWars/Map/MapFactory.swift b/GoldWars/GoldWars/Map/MapFactory.swift new file mode 100644 index 0000000..ab4321a --- /dev/null +++ b/GoldWars/GoldWars/Map/MapFactory.swift @@ -0,0 +1,36 @@ +// +// MapFactory.swift +// GoldWars +// +// Created by Marcel Schwarz on 02.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import Foundation +import SpriteKit + +class MapFactory { + + var twoPlayerMaps: [MapProtocol]! + var threePlayerMaps: [MapProtocol]! + var fourPlayerMaps: [MapProtocol]! + + init(scene: SKScene, entityManager: EntityManager) { + self.twoPlayerMaps = [ + TwoPlayerDefaultTestMap(scene: scene, entityManager: entityManager) + ] + + self.threePlayerMaps = [MapProtocol]() + self.fourPlayerMaps = [MapProtocol]() + } + + func loadMap(playerCount: Int) { + if playerCount == 2 { + twoPlayerMaps.randomElement()?.load() + } else if playerCount == 3 { + threePlayerMaps.randomElement()?.load() + } else { + fourPlayerMaps.randomElement()?.load() + } + } +} diff --git a/GoldWars/GoldWars/Map/MapProtocol.swift b/GoldWars/GoldWars/Map/MapProtocol.swift new file mode 100644 index 0000000..2560d39 --- /dev/null +++ b/GoldWars/GoldWars/Map/MapProtocol.swift @@ -0,0 +1,20 @@ +// +// MapProtocoll.swift +// GoldWars +// +// Created by Marcel Schwarz on 02.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import Foundation +import SpriteKit + +protocol MapProtocol { + + var size: CGSize! {get set} + var entityManager: EntityManager! {get set} + + init(scene: SKScene, entityManager: EntityManager) + + func load() +} diff --git a/GoldWars/GoldWars/Map/MapProtocoll.swift b/GoldWars/GoldWars/Map/MapProtocoll.swift deleted file mode 100644 index 5d3412a..0000000 --- a/GoldWars/GoldWars/Map/MapProtocoll.swift +++ /dev/null @@ -1,15 +0,0 @@ -// -// MapProtocoll.swift -// GoldWars -// -// Created by Marcel Schwarz on 02.05.20. -// Copyright © 2020 SP2. All rights reserved. -// - -import Foundation - -protocol MapProtocoll { - - func adjacent(base: Base) -> Array - -} diff --git a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift index ab9387b..beff433 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift @@ -7,13 +7,75 @@ // import Foundation +import SpriteKit -class TwoPlayerDefaultTestMap: MapProtocoll { - +class TwoPlayerDefaultTestMap: MapProtocol { - func adjacent(base: Base) -> Array { - return Array() + var entityManager: EntityManager! + var size: CGSize! + + required init(scene: SKScene, entityManager: EntityManager) { + self.entityManager = entityManager; + self.size = scene.size } - + func load() { + + // Create Bases + let basePlayerOne = Base( + position: CGPoint(x: self.size.width * 0.1, y: self.size.height / 2), + team: .team1 + ) + + let column1 = [ + Base(position: CGPoint(x: self.size.width * 0.25, y: self.size.height * 0.25)), + Base(position: CGPoint(x: self.size.width * 0.25, y: self.size.height * 0.5)), + Base(position: CGPoint(x: self.size.width * 0.25, y: self.size.height * 0.75)) + ] + + let column2 = [ + Base(position: CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.333)), + Base(position: CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.666)) + ] + + let column3 = [ + Base(position: CGPoint(x: self.size.width * 0.75, y: self.size.height * 0.25)), + Base(position: CGPoint(x: self.size.width * 0.75, y: self.size.height * 0.5)), + Base(position: CGPoint(x: self.size.width * 0.75, y: self.size.height * 0.75)) + ] + + let basePlayerTwo = Base( + position: CGPoint(x: self.size.width * 0.9, y: self.size.height / 2), + team: .team2 + ) + + // Create adjacency Mapping + basePlayerOne.adjacencyList.append(contentsOf: column1) + + column1.forEach({currBase in + currBase.adjacencyList.append(contentsOf: column2) + currBase.adjacencyList.append(basePlayerOne) + }) + + column2.forEach({currBase in + currBase.adjacencyList.append(contentsOf: column3) + currBase.adjacencyList.append(contentsOf: column1) + }) + + column3.forEach({currBase in + currBase.adjacencyList.append(basePlayerTwo) + currBase.adjacencyList.append(contentsOf: column3) + }) + + basePlayerTwo.adjacencyList.append(contentsOf: column3) + + // Register bases with the EntityManager + entityManager.add(basePlayerOne) + + column1.forEach({base in entityManager.add(base)}) + column2.forEach({base in entityManager.add(base)}) + column3.forEach({base in entityManager.add(base)}) + + entityManager.add(basePlayerTwo) + } } diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index e9380f2..678b327 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -10,140 +10,81 @@ 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! + + override func sceneDidLoad() { + entityManager = EntityManager(scene: self) + 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) + } + } + + func initMap() { + MapFactory(scene: self, entityManager: self.entityManager).loadMap(playerCount: 2) + } + + + 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 + } + })) + } }