From e447a377579344db70aab7348cde46ed836d49a9 Mon Sep 17 00:00:00 2001 From: 127-Z3R0 <81heti1bif@hft-stuttgart.de> Date: Sat, 23 May 2020 18:36:59 +0200 Subject: [PATCH] * Add 3 Instances for each Scene * Refactor in Code that the right instances is called * Probs to Aldin und nicht an Niko, der nur im Bett liegt --- .../Components/AtkBoostSkillComponent.swift | 2 +- .../Components/DefBoostSkillComponent.swift | 3 +- .../Components/PlayerInfoComponent.swift | 5 ++- GoldWars/GoldWars/DataService.swift | 3 +- .../GoldWars/Entities/EntityManager.swift | 4 +- GoldWars/GoldWars/Entities/Modal.swift | 9 ++-- GoldWars/GoldWars/GameViewController.swift | 8 ++-- GoldWars/GoldWars/MatchmakingHelper.swift | 9 ++-- GoldWars/GoldWars/MultiplayerNetwork.swift | 28 ++++++------ .../GoldWars/RoundCalculatorService.swift | 5 ++- GoldWars/GoldWars/Scenes/GameScene.swift | 44 ++++++++++--------- GoldWars/GoldWars/Scenes/MenuScene.swift | 25 +++++++---- GoldWars/GoldWars/SettingsScene.swift | 29 +++++++----- 13 files changed, 100 insertions(+), 74 deletions(-) diff --git a/GoldWars/GoldWars/Components/AtkBoostSkillComponent.swift b/GoldWars/GoldWars/Components/AtkBoostSkillComponent.swift index 8829c19..facd9b3 100644 --- a/GoldWars/GoldWars/Components/AtkBoostSkillComponent.swift +++ b/GoldWars/GoldWars/Components/AtkBoostSkillComponent.swift @@ -18,7 +18,7 @@ class AtkBoostSkillComponent: GKComponent{ isEnabled: isEnabled, position: position, onButtonPress: { - var bases: Set = EntityManager.sharedInstance.getBasesByTeam(for: .team2) + var bases: Set = EntityManager.gameEMInstance.getBasesByTeam(for: .team2) bases.forEach{ base in if(base.unitType == .Defence){ base.unitType = .AttackDefence diff --git a/GoldWars/GoldWars/Components/DefBoostSkillComponent.swift b/GoldWars/GoldWars/Components/DefBoostSkillComponent.swift index 66ddba8..1d4a43d 100644 --- a/GoldWars/GoldWars/Components/DefBoostSkillComponent.swift +++ b/GoldWars/GoldWars/Components/DefBoostSkillComponent.swift @@ -13,12 +13,13 @@ class DefBoostSkillComponent: GKComponent{ var skillButtonNode: SkillButtonNode init(iconName: String, text: String, position: CGPoint, isEnabled:Bool) { + skillButtonNode = SkillButtonNode(iconName: iconName, text: text, isEnabled: isEnabled, position: position, onButtonPress: { - var bases: Set = EntityManager.sharedInstance.getBasesByTeam(for: .team2) + var bases: Set = EntityManager.gameEMInstance.getBasesByTeam(for: .team2) bases.forEach{ base in if(base.unitType == .Attack){ base.unitType = .AttackDefence diff --git a/GoldWars/GoldWars/Components/PlayerInfoComponent.swift b/GoldWars/GoldWars/Components/PlayerInfoComponent.swift index d48b6e9..26d1b1d 100644 --- a/GoldWars/GoldWars/Components/PlayerInfoComponent.swift +++ b/GoldWars/GoldWars/Components/PlayerInfoComponent.swift @@ -13,6 +13,7 @@ import GameKit class PlayerInfoComponent: GKComponent { + var entityManager = EntityManager.gameEMInstance var hostLabel:SKLabelNode var hostUnitsLabel:SKLabelNode @@ -44,8 +45,8 @@ class PlayerInfoComponent: GKComponent { } func update(){ - hostUnitsLabel.text = "\(EntityManager.sharedInstance.getUnitSum(by: host!))" - peerUnitsLabel.text = "\(EntityManager.sharedInstance.getUnitSum(by: peer!))" + hostUnitsLabel.text = "\(entityManager.getUnitSum(by: host!))" + peerUnitsLabel.text = "\(entityManager.getUnitSum(by: peer!))" } func setColor(labelNodes: [SKLabelNode]) -> Void { diff --git a/GoldWars/GoldWars/DataService.swift b/GoldWars/GoldWars/DataService.swift index d94a992..f12623f 100644 --- a/GoldWars/GoldWars/DataService.swift +++ b/GoldWars/GoldWars/DataService.swift @@ -43,6 +43,7 @@ class DataService { var snapshotModel: SnapshotModel? var gameHost: Host? var mapModel: MapGenerationModel? + var entityManager = EntityManager.gameEMInstance func addMove(playerMove: PlayerMove) { var equalMove = localPlayerMoves.filter { (ele) -> Bool in @@ -73,6 +74,6 @@ class DataService { func setMapModel(model: MapGenerationModel) { self.mapModel = model - MapFactory(scene: EntityManager.sharedInstance.scene, entityManager: EntityManager.sharedInstance).loadMap(fromModel: DataService.sharedInstance.mapModel!) + MapFactory(scene: entityManager.scene, entityManager: entityManager).loadMap(fromModel: DataService.sharedInstance.mapModel!) } } diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index a1c69d3..19f8a5c 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -12,7 +12,9 @@ import GameKit class EntityManager { - static let sharedInstance = EntityManager() + static let gameEMInstance = EntityManager() + static let menuEMInstance = EntityManager() + static let settingsEMInstance = EntityManager() var entities = Set() var scene: SKScene diff --git a/GoldWars/GoldWars/Entities/Modal.swift b/GoldWars/GoldWars/Entities/Modal.swift index 489aed4..d125291 100644 --- a/GoldWars/GoldWars/Entities/Modal.swift +++ b/GoldWars/GoldWars/Entities/Modal.swift @@ -10,6 +10,7 @@ import GameplayKit class Modal: GKEntity{ + var entityManager = EntityManager.gameEMInstance var unitCount:Int init(modaltype: ModalType, base: Base, anchorPoint: CGPoint, gameScene: GameScene, currentDraggedBase: Base?, touchLocation: CGPoint, collisionBase: Base?) { @@ -42,9 +43,9 @@ class Modal: GKEntity{ } func removeModalEntities(gameScene: SKScene){ - for entity in EntityManager.sharedInstance.entities { - if EntityManager.sharedInstance.isModal && entity.isMember(of: Modal.self) { - EntityManager.sharedInstance.remove(entity) + for entity in entityManager.entities { + if entityManager.isModal && entity.isMember(of: Modal.self) { + entityManager.remove(entity) } for child in gameScene.children { if(child.name != "fire"){ @@ -57,7 +58,7 @@ class Modal: GKEntity{ func sendUnits(currentDraggedBase: Base?, touchLocation: CGPoint, gameScene: GameScene, collisionBase: Base?){ for base in currentDraggedBase!.adjacencyList { if base == collisionBase { - EntityManager.sharedInstance.update((currentDraggedBase?.doPlayerMoveTypeToBase(base: base, playerMoveType: PlayerMoveType.AtkMove, units: Int(GameScene.sendUnits)))!) + entityManager.update((currentDraggedBase?.doPlayerMoveTypeToBase(base: base, playerMoveType: PlayerMoveType.AtkMove, units: Int(GameScene.sendUnits)))!) GameScene.sendUnits = 0 } } diff --git a/GoldWars/GoldWars/GameViewController.swift b/GoldWars/GoldWars/GameViewController.swift index 05b2706..d879e8c 100644 --- a/GoldWars/GoldWars/GameViewController.swift +++ b/GoldWars/GoldWars/GameViewController.swift @@ -17,15 +17,15 @@ class GameViewController: UIViewController { if let view = self.view as! SKView? { let scene = MenuScene(size: self.view.bounds.size) + EntityManager.menuEMInstance.setScene(scene: scene) scene.scaleMode = .aspectFill view.presentScene(scene) //TODO: create dev profile or remove on delivery view.showsFPS = true view.showsNodeCount = true - - GameCenterHelper.helper.viewController = self - MatchmakingHelper.sharedInstance.viewController = self - } + GameCenterHelper.helper.viewController = self + MatchmakingHelper.sharedInstance.viewController = self + } } override var shouldAutorotate: Bool { diff --git a/GoldWars/GoldWars/MatchmakingHelper.swift b/GoldWars/GoldWars/MatchmakingHelper.swift index 60dc5d2..ec5477a 100644 --- a/GoldWars/GoldWars/MatchmakingHelper.swift +++ b/GoldWars/GoldWars/MatchmakingHelper.swift @@ -37,6 +37,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe var nameSpieler1 = "" var menusc: MenuScene? let localPlayer: GKLocalPlayer = GKLocalPlayer.local + var entityManager = EntityManager.gameEMInstance static let sharedInstance = MatchmakingHelper() @@ -119,8 +120,8 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) { DataService.sharedInstance.snapshotModel = snapshotModel - EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: snapshotModel) - EntityManager.sharedInstance.getTimer().startWithDuration(duration: 31) + entityManager.updateSnapshotModel(snapshotModel: snapshotModel) + entityManager.getTimer().startWithDuration(duration: 31) } if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) { @@ -182,7 +183,9 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe } self.delegate?.matchStarted() - self.menusc!.loadScene(scene: GameScene(size: self.menusc!.size)) + let scene = GameScene(size: self.menusc!.size) + self.entityManager.setScene(scene: scene) + self.menusc!.loadScene(scene: scene) MultiplayerNetwork.sharedInstance.sendHostIdentifier() }) } diff --git a/GoldWars/GoldWars/MultiplayerNetwork.swift b/GoldWars/GoldWars/MultiplayerNetwork.swift index 5cf2937..9535514 100644 --- a/GoldWars/GoldWars/MultiplayerNetwork.swift +++ b/GoldWars/GoldWars/MultiplayerNetwork.swift @@ -11,20 +11,20 @@ import Foundation import GameKit class MultiplayerNetwork{ - static let sharedInstance = MultiplayerNetwork() + static let sharedInstance = MultiplayerNetwork() var isSending = false - func sendData(data: Data) { - let mmHelper = MatchmakingHelper.sharedInstance - if let multiplayerMatch = mmHelper.mpMatch { - do { - try multiplayerMatch.sendData(toAllPlayers: data, with: .reliable) - } catch { - //TODO: Add logging - } - } - } + func sendData(data: Data) { + let mmHelper = MatchmakingHelper.sharedInstance + if let multiplayerMatch = mmHelper.mpMatch { + do { + try multiplayerMatch.sendData(toAllPlayers: data, with: .reliable) + } catch { + //TODO: Add logging + } + } + } func sendDataToHost(data: Data) { let mmHelper = MatchmakingHelper.sharedInstance @@ -38,7 +38,7 @@ class MultiplayerNetwork{ } } } - + func sendPlayerMoves(playerMoves: [PlayerMove]) { if MatchmakingHelper.sharedInstance.isServer == false { self.isSending = true @@ -47,7 +47,7 @@ class MultiplayerNetwork{ sendDataToHost(data: encoded) DataService.sharedInstance.localPlayerMoves.removeAll() } - } + } func sendHostIdentifier() { let encoder = JSONEncoder() @@ -66,5 +66,5 @@ class MultiplayerNetwork{ let encoded = (try? encoder.encode(mapModel))! sendData(data: encoded) } - + } diff --git a/GoldWars/GoldWars/RoundCalculatorService.swift b/GoldWars/GoldWars/RoundCalculatorService.swift index b9186c6..2a0a877 100644 --- a/GoldWars/GoldWars/RoundCalculatorService.swift +++ b/GoldWars/GoldWars/RoundCalculatorService.swift @@ -15,6 +15,7 @@ class RoundCalculatorService { var allPlayerMoves: [String: [PlayerMove]] = [:] var baseSpecificMoves: [Int: [String: [PlayerMove]]] = [:] + var entityManager = EntityManager.gameEMInstance var isCalculating = false @@ -110,9 +111,9 @@ class RoundCalculatorService { DataService.sharedInstance.localPlayerMoves.removeAll() MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers() DataService.sharedInstance.snapshotModel = currentSnapshotModel - EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: currentSnapshotModel!) + entityManager.updateSnapshotModel(snapshotModel: currentSnapshotModel!) sleep(1) - EntityManager.sharedInstance.getTimer().startWithDuration(duration: 31) + entityManager.getTimer().startWithDuration(duration: 31) os_log("Finished calculating Round", log: RoundCalculatorService.LOG, type: .info) } diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 7618d1a..16519a3 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -12,26 +12,28 @@ import GameKit class GameScene: SKScene{ + var entityManager = EntityManager.gameEMInstance + var isMoveTouch = false var currentDraggedBase : Base? static var sendUnits: CGFloat = 0 var collisionBase: Base? override func sceneDidLoad() { - EntityManager.sharedInstance.setScene(scene: self) - EntityManager.sharedInstance.add(HUD(size: self.size)) - EntityManager.sharedInstance.add(Background(size: self.size)) + entityManager.setScene(scene: self) + entityManager.add(HUD(size: self.size)) + entityManager.add(Background(size: self.size)) initMap() } func initMap() { if (DataService.sharedInstance.gameHost?.playerName == GKLocalPlayer.local.displayName) { - let mapModel = MapFactory(scene: self, entityManager: EntityManager.sharedInstance).loadMap() + let mapModel = MapFactory(scene: self, entityManager: entityManager).loadMap() MultiplayerNetwork.sharedInstance.sendMapModelToPlayers(mapModel: mapModel) - DataService.sharedInstance.setSnapshotModel(snapshotModel: EntityManager.sharedInstance.getSnapshotModel()) + DataService.sharedInstance.setSnapshotModel(snapshotModel: entityManager.getSnapshotModel()) } if CommandLine.arguments.contains("--no-matchmaking") { - MapFactory(scene: self, entityManager: EntityManager.sharedInstance).loadMap() + _ = MapFactory(scene: self, entityManager: entityManager).loadMap() } } @@ -50,7 +52,7 @@ class GameScene: SKScene{ addAttackDetails(touchLocation: touchLocation) } else { - for entity in EntityManager.sharedInstance.entities { + for entity in entityManager.entities { let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode //FIXME: this is confusing @@ -73,18 +75,18 @@ class GameScene: SKScene{ } checkSlider() - let bases = EntityManager.sharedInstance.getBasesByPlayer(for: GKLocalPlayer.local) + let bases = entityManager.getBasesByPlayer(for: GKLocalPlayer.local) checkBases(bases: bases, touchLocation: touchLocation) } override func update(_ currentTime: TimeInterval) { - EntityManager.sharedInstance.getBackground()?.update(deltaTime: currentTime) - EntityManager.sharedInstance.getHUD()?.component(ofType: TimerComponent.self)?.update() + entityManager.getBackground()?.update(deltaTime: currentTime) + entityManager.getHUD()?.component(ofType: TimerComponent.self)?.update() } func addBaseDetails(touchLocation: CGPoint, spriteNode: SKNode?, touches: Set, event: UIEvent?, entity: GKEntity){ - if atPoint(touchLocation) == spriteNode && !EntityManager.sharedInstance.isModal { + if atPoint(touchLocation) == spriteNode && !entityManager.isModal { spriteNode?.touchesBegan(touches, with: event) if let baseEntity = entity as? Base { if baseEntity.ownershipPlayer == GKLocalPlayer.local { @@ -93,11 +95,11 @@ class GameScene: SKScene{ child.alpha = 0.3 } } - EntityManager.sharedInstance.add(Modal(modaltype: .BaseDetails, - base: entity as! Base, - anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), - gameScene: self, - currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, collisionBase: collisionBase)) + entityManager.add(Modal(modaltype: .BaseDetails, + base: entity as! Base, + anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), + gameScene: self, + currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, collisionBase: collisionBase)) } } @@ -110,10 +112,10 @@ class GameScene: SKScene{ collisionBase = base // TODO: change interaction based on collision instead of touchlocation if currentDraggedBase!.unitCount > 1 { - EntityManager.sharedInstance.add(Modal(modaltype: .BaseAttack, - base: currentDraggedBase!, - anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), - gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, collisionBase: collisionBase)) + entityManager.add(Modal(modaltype: .BaseAttack, + base: currentDraggedBase!, + anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), + gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, collisionBase: collisionBase)) GameScene.sendUnits = CGFloat(currentDraggedBase!.unitCount / 2) } } @@ -121,7 +123,7 @@ class GameScene: SKScene{ } func checkSlider(){ - for e in EntityManager.sharedInstance.entities{ + for e in entityManager.entities{ if let body = e.component(ofType: ModalContentComponent.self)?.body{ GameScene.sendUnits = ((e.component(ofType: SliderComponent.self)?.sliderNode.getValue ?? 0) * CGFloat((e as! Modal).unitCount)).rounded(.up) diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index 764a1da..64699d2 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -9,12 +9,15 @@ import SpriteKit import SceneKit class MenuScene: SKScene { - + + var entityManager = EntityManager.menuEMInstance + override func sceneDidLoad() { - EntityManager.sharedInstance.setScene(scene: self) + + entityManager.setScene(scene: self) let midX = self.size.width / 2 let midY = self.size.height / 2 - EntityManager.sharedInstance.add(Button(name: "startGameButton", + entityManager.add(Button(name: "startGameButton", iconName: "", text: "Start Game", position: CGPoint(x: midX, y: midY), @@ -26,15 +29,16 @@ class MenuScene: SKScene { MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self) } })) - EntityManager.sharedInstance.add(Button(name: "settingsButton", + entityManager.add(Button(name: "settingsButton", iconName: "", text: "Settings", position: CGPoint(x: midX, y: midY - 80 ), onButtonPress: { - self.loadScene(scene: SettingsScene(size: self.size)) + let scene = SettingsScene(size: self.size) + self.loadScene(scene: scene) })) - EntityManager.sharedInstance.add(Background(size: self.size)) - EntityManager.sharedInstance.add(SpinningLogoEntity(position: CGPoint(x: midX, y: midY + 200))) + entityManager.add(Background(size: self.size)) + entityManager.add(SpinningLogoEntity(position: CGPoint(x: midX, y: midY + 200))) if SoundManager.sharedInstance.isMusicPlaying == false && SoundManager.sharedInstance.isMusicEnabled == true { SoundManager.sharedInstance.startMenuMusic() @@ -43,11 +47,14 @@ class MenuScene: SKScene { func loadScene(scene: SKScene) { let transition = SKTransition.flipVertical(withDuration: 0.5) + entityManager.entities.removeAll() self.view?.presentScene(scene, transition: transition) } override func update(_ currentTime: TimeInterval) { - EntityManager.sharedInstance.getBackground()!.update(deltaTime: currentTime) - EntityManager.sharedInstance.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated + if entityManager.entities.count != 0 { + entityManager.getBackground()!.update(deltaTime: currentTime) + entityManager.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated + } } } diff --git a/GoldWars/GoldWars/SettingsScene.swift b/GoldWars/GoldWars/SettingsScene.swift index a9cd9e0..e568ed2 100644 --- a/GoldWars/GoldWars/SettingsScene.swift +++ b/GoldWars/GoldWars/SettingsScene.swift @@ -10,19 +10,23 @@ import SpriteKit class SettingsScene: SKScene { + var entityManager = EntityManager.settingsEMInstance + override func sceneDidLoad() { - EntityManager.sharedInstance.setScene(scene: self) + + entityManager.setScene(scene: self) let positionX = self.size.width * 0.1 let positionY = self.size.height * 0.05 - - EntityManager.sharedInstance.add(Button(name: "backToMenuScene", + print("SceneDidLoad Setting") + entityManager.add(Button(name: "backToMenuScene", iconName: "", text: "Back", position: CGPoint(x: positionX, y: positionY), onButtonPress: { - self.loadScene(scene: MenuScene(size: self.size)) + let scene = MenuScene(size: self.size) + self.loadScene(scene: scene) })) - EntityManager.sharedInstance.add(Button(name: "StopMenuMusic", + entityManager.add(Button(name: "StopMenuMusic", iconName: "", text: "ON/OFF", position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2), @@ -35,7 +39,7 @@ class SettingsScene: SKScene { SoundManager.sharedInstance.startMenuMusic() } })) - EntityManager.sharedInstance.add(Button(name: "StopMovingBackground", + entityManager.add(Button(name: "StopMovingBackground", iconName: "", text: "MOVE/STOP", position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2 - 100), @@ -46,7 +50,7 @@ class SettingsScene: SKScene { BackgroundComponent.isMovingBackgroundEnabled = true } })) - EntityManager.sharedInstance.add(Label(fontnamed: "Courier-Bold", + entityManager.add(Label(fontnamed: "Courier-Bold", name: "SettingsLabel", text: "Settings", fontSize: 200.0, @@ -57,7 +61,7 @@ class SettingsScene: SKScene { isAnimationEnabled: true, isAnimationInfinite: true) ) - EntityManager.sharedInstance.add(Label(fontnamed: "Courier-Bold", + entityManager.add(Label(fontnamed: "Courier-Bold", name: "LabelMusic", text: "Music", fontSize: 50.0, fontColor: .black, @@ -67,7 +71,7 @@ class SettingsScene: SKScene { isAnimationEnabled: true, isAnimationInfinite: false) ) - EntityManager.sharedInstance.add(Label(fontnamed: "Courier-Bold", + entityManager.add(Label(fontnamed: "Courier-Bold", name: "LabelBackground", text: "Background", fontSize: 50.0, @@ -78,16 +82,19 @@ class SettingsScene: SKScene { isAnimationEnabled: true, isAnimationInfinite: false) ) - EntityManager.sharedInstance.add(Background(size: self.size)) + entityManager.add(Background(size: self.size)) } func loadScene(scene: SKScene) { let transition = SKTransition.flipVertical(withDuration: 0.5) + entityManager.entities.removeAll() self.view?.presentScene(scene, transition: transition) } override func update(_ currentTime: TimeInterval) { - EntityManager.sharedInstance.getBackground()!.update(deltaTime: currentTime) + if entityManager.entities.count != 0 { + entityManager.getBackground()!.update(deltaTime: currentTime) + } } }