diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index 37f6e37..675a919 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -8,7 +8,6 @@ /* Begin PBXBuildFile section */ 110360D3244B101A008610AF /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 110360D2244B101A008610AF /* AppDelegate.swift */; }; - 110360D5244B101A008610AF /* GameScene.sks in Resources */ = {isa = PBXBuildFile; fileRef = 110360D4244B101A008610AF /* GameScene.sks */; }; 110360D9244B101A008610AF /* GameScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = 110360D8244B101A008610AF /* GameScene.swift */; }; 110360DB244B101A008610AF /* GameViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 110360DA244B101A008610AF /* GameViewController.swift */; }; 110360DE244B101A008610AF /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 110360DC244B101A008610AF /* Main.storyboard */; }; @@ -16,13 +15,16 @@ 110360E3244B101B008610AF /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 110360E1244B101B008610AF /* LaunchScreen.storyboard */; }; 110360EE244B101B008610AF /* GoldWarsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 110360ED244B101B008610AF /* GoldWarsTests.swift */; }; 110360F9244B101B008610AF /* GoldWarsUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 110360F8244B101B008610AF /* GoldWarsUITests.swift */; }; - 11036111244B3D6A008610AF /* MenuScene.sks in Resources */ = {isa = PBXBuildFile; fileRef = 11036110244B3D6A008610AF /* MenuScene.sks */; }; 11036113244B3E30008610AF /* MenuScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11036112244B3E30008610AF /* MenuScene.swift */; }; 116060F7245C57D2004E5A36 /* EntityManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 116060F6245C57D2004E5A36 /* EntityManager.swift */; }; 11738A3B24508F68004426F1 /* Unit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11738A3A24508F68004426F1 /* Unit.swift */; }; 3EBD242C245D8044003CECE7 /* GameCenterHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EBD242B245D8044003CECE7 /* GameCenterHelper.swift */; }; 3EBD242E245D9332003CECE7 /* Team.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EBD242D245D9332003CECE7 /* Team.swift */; }; 9E11FF79245CD81100EED3BE /* Fire.sks in Resources */ = {isa = PBXBuildFile; fileRef = 9E11FF77245CD81100EED3BE /* Fire.sks */; }; + 9E174C82245DD81D00209FF0 /* ButtonNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E174C81245DD81D00209FF0 /* ButtonNode.swift */; }; + 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 */; }; 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 */; }; @@ -60,7 +62,6 @@ /* Begin PBXFileReference section */ 110360CF244B101A008610AF /* GoldWars.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = GoldWars.app; sourceTree = BUILT_PRODUCTS_DIR; }; 110360D2244B101A008610AF /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; - 110360D4244B101A008610AF /* GameScene.sks */ = {isa = PBXFileReference; lastKnownFileType = file.sks; path = GameScene.sks; sourceTree = ""; }; 110360D8244B101A008610AF /* GameScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameScene.swift; sourceTree = ""; }; 110360DA244B101A008610AF /* GameViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameViewController.swift; sourceTree = ""; }; 110360DD244B101A008610AF /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; @@ -73,13 +74,16 @@ 110360F4244B101B008610AF /* GoldWarsUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GoldWarsUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 110360F8244B101B008610AF /* GoldWarsUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoldWarsUITests.swift; sourceTree = ""; }; 110360FA244B101B008610AF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 11036110244B3D6A008610AF /* MenuScene.sks */ = {isa = PBXFileReference; lastKnownFileType = file.sks; path = MenuScene.sks; sourceTree = ""; }; 11036112244B3E30008610AF /* MenuScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuScene.swift; sourceTree = ""; }; 116060F6245C57D2004E5A36 /* EntityManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntityManager.swift; sourceTree = ""; }; 11738A3A24508F68004426F1 /* Unit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Unit.swift; sourceTree = ""; }; 3EBD242B245D8044003CECE7 /* GameCenterHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameCenterHelper.swift; sourceTree = ""; }; 3EBD242D245D9332003CECE7 /* Team.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Team.swift; sourceTree = ""; }; 9E11FF77245CD81100EED3BE /* Fire.sks */ = {isa = PBXFileReference; lastKnownFileType = file.sks; path = Fire.sks; sourceTree = ""; }; + 9E174C81245DD81D00209FF0 /* ButtonNode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ButtonNode.swift; sourceTree = ""; }; + 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 = ""; }; 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 = ""; }; @@ -186,6 +190,7 @@ 116060F4245C56EA004E5A36 /* Components */ = { isa = PBXGroup; children = ( + 9E174C81245DD81D00209FF0 /* ButtonNode.swift */, 9EA3ABE8245C6DAA006BC61D /* DefaultBaseComponent.swift */, 9EA3ABEC245C8143006BC61D /* ModalBackgroundComponent.swift */, 9EA3ABEE245C834B006BC61D /* ModalContentComponent.swift */, @@ -194,6 +199,8 @@ 9E78ACBD245CC9C000526FF7 /* AtkBoostSkillComponent.swift */, 9E78ACC3245CCA3600526FF7 /* SpySkillComponent.swift */, 9E78ACC1245CC9EE00526FF7 /* DefBoostSkillComponent.swift */, + 9E174C85245DD91500209FF0 /* ButtonComponent.swift */, + 9E174C87245DF1FF00209FF0 /* BackgroundComponent.swift */, ); path = Components; sourceTree = ""; @@ -205,6 +212,7 @@ 9EC86B9E245C88A300796EF3 /* Modal.swift */, 116060F6245C57D2004E5A36 /* EntityManager.swift */, 9E78ACB9245CBDAF00526FF7 /* HUD.swift */, + 9E174C83245DD8CE00209FF0 /* Button.swift */, ); path = Entities; sourceTree = ""; @@ -247,10 +255,8 @@ 9EC86BA4245C8A1E00796EF3 /* Scenes */ = { isa = PBXGroup; children = ( - 11036110244B3D6A008610AF /* MenuScene.sks */, 11036112244B3E30008610AF /* MenuScene.swift */, 110360D8244B101A008610AF /* GameScene.swift */, - 110360D4244B101A008610AF /* GameScene.sks */, ); path = Scenes; sourceTree = ""; @@ -369,8 +375,6 @@ buildActionMask = 2147483647; files = ( 110360DE244B101A008610AF /* Main.storyboard in Resources */, - 11036111244B3D6A008610AF /* MenuScene.sks in Resources */, - 110360D5244B101A008610AF /* GameScene.sks in Resources */, 9E11FF79245CD81100EED3BE /* Fire.sks in Resources */, 110360E0244B101B008610AF /* Assets.xcassets in Resources */, 110360E3244B101B008610AF /* LaunchScreen.storyboard in Resources */, @@ -407,8 +411,10 @@ 110360D9244B101A008610AF /* GameScene.swift in Sources */, 116060F7245C57D2004E5A36 /* EntityManager.swift in Sources */, 3EBD242E245D9332003CECE7 /* Team.swift in Sources */, + 9E174C88245DF1FF00209FF0 /* BackgroundComponent.swift in Sources */, 9E78ACBA245CBDAF00526FF7 /* HUD.swift in Sources */, 11738A3B24508F68004426F1 /* Unit.swift in Sources */, + 9E174C86245DD91500209FF0 /* ButtonComponent.swift in Sources */, 11036113244B3E30008610AF /* MenuScene.swift in Sources */, 9EA3ABE9245C6DAA006BC61D /* DefaultBaseComponent.swift in Sources */, 9EA3ABED245C8143006BC61D /* ModalBackgroundComponent.swift in Sources */, @@ -416,6 +422,8 @@ AB1D759C245DD18100671525 /* MapProtocoll.swift in Sources */, AB1D759D245DD18100671525 /* TwoPlayerDefaultTestMap.swift in Sources */, ABA03DA0244BD54F00A66916 /* Base.swift in Sources */, + 9E174C82245DD81D00209FF0 /* ButtonNode.swift in Sources */, + 9E174C84245DD8CE00209FF0 /* Button.swift in Sources */, 110360DB244B101A008610AF /* GameViewController.swift in Sources */, 110360D3244B101A008610AF /* AppDelegate.swift in Sources */, 9EC86B9F245C88A300796EF3 /* Modal.swift in Sources */, diff --git a/GoldWars/GoldWars/Components/BackgroundComponent.swift b/GoldWars/GoldWars/Components/BackgroundComponent.swift new file mode 100644 index 0000000..49244c0 --- /dev/null +++ b/GoldWars/GoldWars/Components/BackgroundComponent.swift @@ -0,0 +1,10 @@ +// +// BackgroundComponent.swift +// GoldWars +// +// Created by Niko Jochim on 02.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import Foundation + diff --git a/GoldWars/GoldWars/Components/ButtonComponent.swift b/GoldWars/GoldWars/Components/ButtonComponent.swift new file mode 100644 index 0000000..57476db --- /dev/null +++ b/GoldWars/GoldWars/Components/ButtonComponent.swift @@ -0,0 +1,28 @@ +// +// ButtonComponent.swift +// GoldWars +// +// Created by Niko Jochim on 02.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import Foundation +import GameplayKit + +class ButtonComponent: GKComponent { + + var buttonNode : ButtonNode + + init(iconName: String, text: String, position: CGPoint, isEnabled:Bool , onButtonPress: @escaping () -> ()) { + buttonNode = ButtonNode(iconName: iconName, + text: text, + isEnabled: isEnabled, + position: position, + onButtonPress: onButtonPress) + super.init() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } +} diff --git a/GoldWars/GoldWars/Components/ButtonNode.swift b/GoldWars/GoldWars/Components/ButtonNode.swift new file mode 100644 index 0000000..be4176c --- /dev/null +++ b/GoldWars/GoldWars/Components/ButtonNode.swift @@ -0,0 +1,70 @@ +// +// ButtonNode.swift +// GoldWars +// +// Created by Niko Jochim on 01.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import Foundation +import SpriteKit + +class ButtonNode : SKSpriteNode { + + var isEnabled:Bool + + let onButtonPress: () -> () + + init(iconName: String, text: String,isEnabled:Bool, position: CGPoint, onButtonPress: @escaping () -> ()) { + + self.onButtonPress = onButtonPress + self.isEnabled = isEnabled + let texture = SKTexture(imageNamed: "yellow_button04") + super.init(texture: texture, color: SKColor.white, size: texture.size()) + self.position = position + + let label = SKLabelNode(fontNamed: "Courier-Bold") + + label.fontSize = 30 + label.fontColor = SKColor.black + + label.zPosition = 1 + label.verticalAlignmentMode = .center + label.text = text + label.name = "label" + + if !iconName.isEmpty { + label.position = CGPoint(x: size.width * 0.25, y: 0) + let icon = SKSpriteNode(imageNamed: iconName) + icon.position = CGPoint(x: -size.width * 0.25, y: 0) + icon.zPosition = 1 + self.addChild(icon) + } else { + label.position = CGPoint(x: 0, y: 0) + } + self.addChild(label) + isUserInteractionEnabled = true + } + + override func touchesBegan(_ touches: Set, with event: UIEvent?) { + + if isEnabled { + let action = SKAction.sequence( + [ + SKAction.scale(by: (3/4), duration: 0.3), + SKAction.scale(by: (4/3), duration: 0.3), + ]) + + self.childNode(withName: "label")?.run(action) + self.run(action) + onButtonPress() + } + + } + + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + +} diff --git a/GoldWars/GoldWars/Entities/Button.swift b/GoldWars/GoldWars/Entities/Button.swift new file mode 100644 index 0000000..e2c52e2 --- /dev/null +++ b/GoldWars/GoldWars/Entities/Button.swift @@ -0,0 +1,29 @@ +// +// Button.swift +// GoldWars +// +// Created by Niko Jochim on 02.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import Foundation +import GameKit + +class Button : GKEntity{ + let name : String + var isEnabled = true + + init(name: String, iconName: String, text: String, position: CGPoint, onButtonPress: @escaping () -> ()) { + self.name = name + super.init() + self.addComponent(ButtonComponent(iconName: iconName, text: text,position: position, isEnabled: isEnabled, onButtonPress: onButtonPress)) + } + + + + 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 8f94ce6..0c9ff60 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -50,6 +50,9 @@ class EntityManager { scene.addChild(skill.shapeNode) scene.addChild(skill.labelNode) } + if let buttonNode = entity.component(ofType: ButtonComponent.self)?.buttonNode { + scene.addChild(buttonNode) + } } @@ -85,4 +88,8 @@ class EntityManager { func baseNode(for team: Team) -> SKSpriteNode?{ return base(for: team)?.component(ofType: DefaultBaseComponent.self)?.spriteNode } + + func getButtonByName(buttonName:String) -> Button{ + return entities.filter{($0 as! Button).name == buttonName}[0] as! Button + } } diff --git a/GoldWars/GoldWars/GameViewController.swift b/GoldWars/GoldWars/GameViewController.swift index 158c6de..4eed433 100644 --- a/GoldWars/GoldWars/GameViewController.swift +++ b/GoldWars/GoldWars/GameViewController.swift @@ -16,17 +16,16 @@ class GameViewController: UIViewController { super.viewDidLoad() if let view = self.view as! SKView? { - if let scene = SKScene(fileNamed: "MenuScene") { - scene.scaleMode = .aspectFill - view.presentScene(scene) - //TODO: create dev profile or remove on delivery - view.showsFPS = true - view.showsNodeCount = true - } - } + let scene = MenuScene(size: self.view.bounds.size) + scene.scaleMode = .aspectFill + view.presentScene(scene) + //TODO: create dev profile or remove on delivery + view.showsFPS = true + view.showsNodeCount = true GameCenterHelper.helper.viewController = self } + } override var shouldAutorotate: Bool { return true diff --git a/GoldWars/GoldWars/Scenes/GameScene.sks b/GoldWars/GoldWars/Scenes/GameScene.sks deleted file mode 100644 index ebf306a..0000000 Binary files a/GoldWars/GoldWars/Scenes/GameScene.sks and /dev/null differ diff --git a/GoldWars/GoldWars/Scenes/MenuScene.sks b/GoldWars/GoldWars/Scenes/MenuScene.sks deleted file mode 100644 index 0e09135..0000000 Binary files a/GoldWars/GoldWars/Scenes/MenuScene.sks and /dev/null differ diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index 38e4f64..46f3ae6 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -9,41 +9,48 @@ import SpriteKit class MenuScene: SKScene { - - var startGameButtonNode:SKSpriteNode! - var settingsButtonNode:SKSpriteNode! + + var entityManager: EntityManager! override func sceneDidLoad() { - 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)) + entityManager = EntityManager(scene: self) + let midX = self.size.width / 2 + let midY = self.size.height / 2 + entityManager.add(Button(name: "startGameButton", + iconName: "", + text: "Start Game", + position: CGPoint(x: midX, y: midY), + onButtonPress: { + self.loadScene(scene: GameScene(size: self.size)) + })) + + entityManager.add(Button(name: "settingsButton", + iconName: "", + text: "Settings", + position: CGPoint(x: midX, y: midY - 80 ), + onButtonPress: { + + })) - self.addChild(sky) - } } - override func touchesBegan(_ touches: Set, with event: UIEvent?) { - guard let touch = touches.first else { - return + 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) } - let gameScene = GameScene(size: self.size) - let location = touch.location(in: self) - let frontTouchedNode = atPoint(location).name - + } + + func loadScene(scene: SKScene) { let transition = SKTransition.moveIn(with: .right, duration: 1) - - if frontTouchedNode == "startGameBtn" || frontTouchedNode == "tmpStartGameLbl"{ - self.view?.presentScene(gameScene, transition: transition) - } - - if frontTouchedNode == "settingsBtn" || frontTouchedNode == "tmpSettingsLbl"{ - //TODO: Implement settings functionality - } + self.view?.presentScene(scene, transition: transition) } override func update(_ currentTime: TimeInterval) { @@ -54,16 +61,7 @@ class MenuScene: SKScene { node.position.x += (self.scene?.size.width)! * 3 } })) - - // TODO: Notfalllösung - if(!GameCenterHelper.isAuthenticated) { - let authLabel = SKLabelNode(text: "You are not logged in to Gamecenter. Please login via GameCenterApp") - authLabel.name = "authLabel" - authLabel.zPosition = 2 - authLabel.position = CGPoint(x: self.size.width / 2, y: self.size.height / 5) - self.addChild(authLabel) - } else { - self.childNode(withName: "authLabel")?.removeFromParent() - } + + entityManager.getButtonByName(buttonName: "startGameButton").isEnabled = GameCenterHelper.isAuthenticated } }