diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index 5d8d41a..62abbb1 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -25,6 +25,7 @@ 9E174C84245DD8CE00209FF0 /* Button.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E174C83245DD8CE00209FF0 /* Button.swift */; }; 9E174C86245DD91500209FF0 /* ButtonComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E174C85245DD91500209FF0 /* ButtonComponent.swift */; }; 9E174C88245DF1FF00209FF0 /* BackgroundComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E174C87245DF1FF00209FF0 /* BackgroundComponent.swift */; }; + 9E174C8A245E1A0A00209FF0 /* Background.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E174C89245E1A0A00209FF0 /* Background.swift */; }; 9E78ACB6245C9A5300526FF7 /* GameKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9E78ACB5245C9A5300526FF7 /* GameKit.framework */; }; 9E78ACB8245CB75B00526FF7 /* TeamComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E78ACB7245CB75B00526FF7 /* TeamComponent.swift */; }; 9E78ACBA245CBDAF00526FF7 /* HUD.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E78ACB9245CBDAF00526FF7 /* HUD.swift */; }; @@ -85,6 +86,7 @@ 9E174C83245DD8CE00209FF0 /* Button.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Button.swift; sourceTree = ""; }; 9E174C85245DD91500209FF0 /* ButtonComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonComponent.swift; sourceTree = ""; }; 9E174C87245DF1FF00209FF0 /* BackgroundComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackgroundComponent.swift; sourceTree = ""; }; + 9E174C89245E1A0A00209FF0 /* Background.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Background.swift; sourceTree = ""; }; 9E78ACB5245C9A5300526FF7 /* GameKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GameKit.framework; path = System/Library/Frameworks/GameKit.framework; sourceTree = SDKROOT; }; 9E78ACB7245CB75B00526FF7 /* TeamComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TeamComponent.swift; sourceTree = ""; }; 9E78ACB9245CBDAF00526FF7 /* HUD.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HUD.swift; sourceTree = ""; }; @@ -215,6 +217,7 @@ 116060F6245C57D2004E5A36 /* EntityManager.swift */, 9E78ACB9245CBDAF00526FF7 /* HUD.swift */, 9E174C83245DD8CE00209FF0 /* Button.swift */, + 9E174C89245E1A0A00209FF0 /* Background.swift */, ); path = Entities; sourceTree = ""; @@ -420,6 +423,7 @@ 9E174C86245DD91500209FF0 /* ButtonComponent.swift in Sources */, 11036113244B3E30008610AF /* MenuScene.swift in Sources */, 9EA3ABE9245C6DAA006BC61D /* DefaultBaseComponent.swift in Sources */, + 9E174C8A245E1A0A00209FF0 /* Background.swift in Sources */, 9EA3ABED245C8143006BC61D /* ModalBackgroundComponent.swift in Sources */, 3EBD242C245D8044003CECE7 /* GameCenterHelper.swift in Sources */, AB1D759C245DD18100671525 /* MapProtocol.swift in Sources */, diff --git a/GoldWars/GoldWars/Components/BackgroundComponent.swift b/GoldWars/GoldWars/Components/BackgroundComponent.swift index 49244c0..349cf2a 100644 --- a/GoldWars/GoldWars/Components/BackgroundComponent.swift +++ b/GoldWars/GoldWars/Components/BackgroundComponent.swift @@ -7,4 +7,39 @@ // import Foundation +import GameplayKit +class BackgroundComponent: GKComponent{ + + var nodes = [SKSpriteNode]() + let size: CGSize + + init(size: CGSize) { + self.size = size + for i in 0...2 { + let sky = SKSpriteNode(imageNamed: "SkyBackground") + sky.name = "clouds" + sky.zPosition = -1 + sky.size = CGSize(width: size.width, height: size.height) + sky.position = CGPoint(x: CGFloat(i) * sky.size.width , y: (size.height / 2)) + nodes.append(sky) + } + super.init() + } + + func update(){ + for node in nodes{ + node.position.x -= 2 + if node.position.x < -(size.width) { + node.position.x += (size.width) * 3 + } + } + } + + + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + +} diff --git a/GoldWars/GoldWars/Entities/Background.swift b/GoldWars/GoldWars/Entities/Background.swift new file mode 100644 index 0000000..423168e --- /dev/null +++ b/GoldWars/GoldWars/Entities/Background.swift @@ -0,0 +1,30 @@ +// +// Background.swift +// GoldWars +// +// Created by Niko Jochim on 02.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import Foundation +import GameplayKit + +class Background: GKEntity { + + init(size: CGSize) { + super.init() + addComponent(BackgroundComponent(size: size)) + } + + + override func update(deltaTime seconds: TimeInterval) { + component(ofType: BackgroundComponent.self)?.update() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + + +} diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 0c9ff60..4511bf4 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -53,6 +53,11 @@ class EntityManager { if let buttonNode = entity.component(ofType: ButtonComponent.self)?.buttonNode { scene.addChild(buttonNode) } + if let nodes = entity.component(ofType: BackgroundComponent.self)?.nodes { + for node in nodes { + scene.addChild(node) + } + } } @@ -73,7 +78,7 @@ class EntityManager { entities.remove(entity) } - func base(for team: Team) -> GKEntity? { + func getBaseByTeam(for team: Team) -> GKEntity? { for entity in entities { if let teamComponent = entity.component(ofType: TeamComponent.self), let _ = entity.component(ofType: DefaultBaseComponent.self) { @@ -85,11 +90,15 @@ class EntityManager { return nil } - func baseNode(for team: Team) -> SKSpriteNode?{ - return base(for: team)?.component(ofType: DefaultBaseComponent.self)?.spriteNode + func getBackground() -> GKEntity? { + return entities.filter{$0 is Background}[0] + } + + func getBaseNodeByTeam(for team: Team) -> SKSpriteNode?{ + return getBaseByTeam(for: team)?.component(ofType: DefaultBaseComponent.self)?.spriteNode } func getButtonByName(buttonName:String) -> Button{ - return entities.filter{($0 as! Button).name == buttonName}[0] as! Button + return entities.filter{$0 is Button && ($0 as! Button).name == buttonName }[0] as! Button } } diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 678b327..e255a05 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -16,22 +16,11 @@ class GameScene: SKScene{ override func sceneDidLoad() { entityManager = EntityManager(scene: self) entityManager.add(HUD(size: self.size)) + entityManager.add(Background(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) @@ -76,15 +65,9 @@ class GameScene: SKScene{ } } - + 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 - } - })) + entityManager.getBackground()?.update(deltaTime: currentTime) } } diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index 8557396..8164439 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -30,32 +30,16 @@ class MenuScene: SKScene { onButtonPress: { //TODO: create Settings Scene })) - } - - func backgroundInit(){ - for i in 0...2 { - let sky = SKSpriteNode(imageNamed: "SkyBackground") - sky.name = "clouds" - sky.zPosition = -1 - sky.size = CGSize(width: self.size.width, height: self.size.height) - sky.position = CGPoint(x: CGFloat(i) * sky.size.width , y: (self.frame.size.height / 2)) - self.addChild(sky) - } + entityManager.add(Background(size: self.size)) } func loadScene(scene: SKScene) { - let transition = SKTransition.moveIn(with: .right, duration: 1) + let transition = SKTransition.flipVertical(withDuration: 0.5) self.view?.presentScene(scene, transition: transition) } 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 - } - })) + entityManager.getBackground()!.update(deltaTime: currentTime) entityManager.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated } }