diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 6e43573..b7d5de6 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -12,15 +12,22 @@ import GameKit class EntityManager { + static let sharedInstance = EntityManager() + var entities = Set() - let scene: SKScene + var scene: SKScene var isModal: Bool - init(scene: SKScene) { - self.scene = scene + private init() { isModal = false + scene = SKScene.init() } + func setScene(scene: SKScene){ + self.scene = scene + } + + func add(_ entity: GKEntity) { entities.insert(entity) if let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode { diff --git a/GoldWars/GoldWars/GameViewController.swift b/GoldWars/GoldWars/GameViewController.swift index e4a2f43..05b2706 100644 --- a/GoldWars/GoldWars/GameViewController.swift +++ b/GoldWars/GoldWars/GameViewController.swift @@ -11,7 +11,7 @@ import SpriteKit import GameplayKit class GameViewController: UIViewController { - + override func viewDidLoad() { super.viewDidLoad() diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index c8cd2c5..723bad6 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -12,21 +12,20 @@ import GameKit class GameScene: SKScene{ - var entityManager: EntityManager! var isMoveTouch = false var currentDraggedBasePos = CGPoint() var currentDraggedBase : Base? static var sendUnits: CGFloat = 0 override func sceneDidLoad() { - entityManager = EntityManager(scene: self) - entityManager.add(HUD(size: self.size)) - entityManager.add(Background(size: self.size)) + EntityManager.sharedInstance.setScene(scene: self) + EntityManager.sharedInstance.add(HUD(size: self.size)) + EntityManager.sharedInstance.add(Background(size: self.size)) initMap() } func initMap() { - MapFactory(scene: self, entityManager: self.entityManager).loadMap(playerCount: 2) + MapFactory(scene: self, entityManager: EntityManager.sharedInstance).loadMap(playerCount: 2) } override func touchesEnded(_ touches: Set, with event: UIEvent?) { diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index 2d86084..764a1da 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -9,14 +9,12 @@ import SpriteKit import SceneKit class MenuScene: SKScene { - - var entityManager: EntityManager! - + override func sceneDidLoad() { - entityManager = EntityManager(scene: self) + EntityManager.sharedInstance.setScene(scene: self) let midX = self.size.width / 2 let midY = self.size.height / 2 - entityManager.add(Button(name: "startGameButton", + EntityManager.sharedInstance.add(Button(name: "startGameButton", iconName: "", text: "Start Game", position: CGPoint(x: midX, y: midY), @@ -28,15 +26,15 @@ class MenuScene: SKScene { MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self) } })) - entityManager.add(Button(name: "settingsButton", + EntityManager.sharedInstance.add(Button(name: "settingsButton", iconName: "", text: "Settings", position: CGPoint(x: midX, y: midY - 80 ), onButtonPress: { self.loadScene(scene: SettingsScene(size: self.size)) })) - entityManager.add(Background(size: self.size)) - entityManager.add(SpinningLogoEntity(position: CGPoint(x: midX, y: midY + 200))) + EntityManager.sharedInstance.add(Background(size: self.size)) + EntityManager.sharedInstance.add(SpinningLogoEntity(position: CGPoint(x: midX, y: midY + 200))) if SoundManager.sharedInstance.isMusicPlaying == false && SoundManager.sharedInstance.isMusicEnabled == true { SoundManager.sharedInstance.startMenuMusic() @@ -49,7 +47,7 @@ class MenuScene: SKScene { } override func update(_ currentTime: TimeInterval) { - entityManager.getBackground()!.update(deltaTime: currentTime) - entityManager.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated + EntityManager.sharedInstance.getBackground()!.update(deltaTime: currentTime) + EntityManager.sharedInstance.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated } } diff --git a/GoldWars/GoldWars/SettingsScene.swift b/GoldWars/GoldWars/SettingsScene.swift index 8eb22f5..a9cd9e0 100644 --- a/GoldWars/GoldWars/SettingsScene.swift +++ b/GoldWars/GoldWars/SettingsScene.swift @@ -9,21 +9,20 @@ import SpriteKit class SettingsScene: SKScene { - var entityManager: EntityManager! override func sceneDidLoad() { - entityManager = EntityManager(scene: self) + EntityManager.sharedInstance.setScene(scene: self) let positionX = self.size.width * 0.1 let positionY = self.size.height * 0.05 - entityManager.add(Button(name: "backToMenuScene", + EntityManager.sharedInstance.add(Button(name: "backToMenuScene", iconName: "", text: "Back", position: CGPoint(x: positionX, y: positionY), onButtonPress: { self.loadScene(scene: MenuScene(size: self.size)) })) - entityManager.add(Button(name: "StopMenuMusic", + EntityManager.sharedInstance.add(Button(name: "StopMenuMusic", iconName: "", text: "ON/OFF", position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2), @@ -36,7 +35,7 @@ class SettingsScene: SKScene { SoundManager.sharedInstance.startMenuMusic() } })) - entityManager.add(Button(name: "StopMovingBackground", + EntityManager.sharedInstance.add(Button(name: "StopMovingBackground", iconName: "", text: "MOVE/STOP", position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2 - 100), @@ -47,7 +46,7 @@ class SettingsScene: SKScene { BackgroundComponent.isMovingBackgroundEnabled = true } })) - entityManager.add(Label(fontnamed: "Courier-Bold", + EntityManager.sharedInstance.add(Label(fontnamed: "Courier-Bold", name: "SettingsLabel", text: "Settings", fontSize: 200.0, @@ -58,7 +57,7 @@ class SettingsScene: SKScene { isAnimationEnabled: true, isAnimationInfinite: true) ) - entityManager.add(Label(fontnamed: "Courier-Bold", + EntityManager.sharedInstance.add(Label(fontnamed: "Courier-Bold", name: "LabelMusic", text: "Music", fontSize: 50.0, fontColor: .black, @@ -68,7 +67,7 @@ class SettingsScene: SKScene { isAnimationEnabled: true, isAnimationInfinite: false) ) - entityManager.add(Label(fontnamed: "Courier-Bold", + EntityManager.sharedInstance.add(Label(fontnamed: "Courier-Bold", name: "LabelBackground", text: "Background", fontSize: 50.0, @@ -79,7 +78,7 @@ class SettingsScene: SKScene { isAnimationEnabled: true, isAnimationInfinite: false) ) - entityManager.add(Background(size: self.size)) + EntityManager.sharedInstance.add(Background(size: self.size)) } func loadScene(scene: SKScene) { @@ -88,7 +87,7 @@ class SettingsScene: SKScene { } override func update(_ currentTime: TimeInterval) { - entityManager.getBackground()!.update(deltaTime: currentTime) + EntityManager.sharedInstance.getBackground()!.update(deltaTime: currentTime) } }