From 9b3bdbf34b87dae74d83fdbe6806261edb2beb3d Mon Sep 17 00:00:00 2001 From: Niko <> Date: Sun, 3 May 2020 15:46:48 +0200 Subject: [PATCH] implemented touch functionality --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 123 +----------------- .../Components/AttackActionComponent.swift | 31 +++++ .../GoldWars/Entities/EntityManager.swift | 7 +- GoldWars/GoldWars/Scenes/GameScene.swift | 106 ++++++++++----- .../GoldWarsUITests/GoldWarsUITests.swift | 43 ------ GoldWars/GoldWarsUITests/Info.plist | 22 ---- 6 files changed, 116 insertions(+), 216 deletions(-) create mode 100644 GoldWars/GoldWars/Components/AttackActionComponent.swift delete mode 100644 GoldWars/GoldWarsUITests/GoldWarsUITests.swift delete mode 100644 GoldWars/GoldWarsUITests/Info.plist diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index 62abbb1..d28b1c8 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -14,12 +14,12 @@ 110360E0244B101B008610AF /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 110360DF244B101B008610AF /* Assets.xcassets */; }; 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 */; }; 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 */; }; + 9E04AFAF245E2B73002D5CFC /* AttackActionComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E04AFAE245E2B73002D5CFC /* AttackActionComponent.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 */; }; @@ -52,13 +52,6 @@ remoteGlobalIDString = 110360CE244B101A008610AF; remoteInfo = GoldWars; }; - 110360F5244B101B008610AF /* PBXContainerItemProxy */ = { - isa = PBXContainerItemProxy; - containerPortal = 110360C7244B101A008610AF /* Project object */; - proxyType = 1; - remoteGlobalIDString = 110360CE244B101A008610AF; - remoteInfo = GoldWars; - }; /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ @@ -73,14 +66,12 @@ 110360E9244B101B008610AF /* GoldWarsTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GoldWarsTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 110360ED244B101B008610AF /* GoldWarsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoldWarsTests.swift; sourceTree = ""; }; 110360EF244B101B008610AF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 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 = ""; }; 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 = ""; }; + 9E04AFAE245E2B73002D5CFC /* AttackActionComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttackActionComponent.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 = ""; }; @@ -122,13 +113,6 @@ ); runOnlyForDeploymentPostprocessing = 0; }; - 110360F1244B101B008610AF /* Frameworks */ = { - isa = PBXFrameworksBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - runOnlyForDeploymentPostprocessing = 0; - }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ @@ -137,7 +121,6 @@ children = ( 110360D1244B101A008610AF /* GoldWars */, 110360EC244B101B008610AF /* GoldWarsTests */, - 110360F7244B101B008610AF /* GoldWarsUITests */, 110360D0244B101A008610AF /* Products */, 9E78ACB4245C9A5300526FF7 /* Frameworks */, ); @@ -148,7 +131,6 @@ children = ( 110360CF244B101A008610AF /* GoldWars.app */, 110360E9244B101B008610AF /* GoldWarsTests.xctest */, - 110360F4244B101B008610AF /* GoldWarsUITests.xctest */, ); name = Products; sourceTree = ""; @@ -182,15 +164,6 @@ path = GoldWarsTests; sourceTree = ""; }; - 110360F7244B101B008610AF /* GoldWarsUITests */ = { - isa = PBXGroup; - children = ( - 110360F8244B101B008610AF /* GoldWarsUITests.swift */, - 110360FA244B101B008610AF /* Info.plist */, - ); - path = GoldWarsUITests; - sourceTree = ""; - }; 116060F4245C56EA004E5A36 /* Components */ = { isa = PBXGroup; children = ( @@ -205,6 +178,7 @@ 9E78ACC1245CC9EE00526FF7 /* DefBoostSkillComponent.swift */, 9E174C85245DD91500209FF0 /* ButtonComponent.swift */, 9E174C87245DF1FF00209FF0 /* BackgroundComponent.swift */, + 9E04AFAE245E2B73002D5CFC /* AttackActionComponent.swift */, ); path = Components; sourceTree = ""; @@ -314,24 +288,6 @@ productReference = 110360E9244B101B008610AF /* GoldWarsTests.xctest */; productType = "com.apple.product-type.bundle.unit-test"; }; - 110360F3244B101B008610AF /* GoldWarsUITests */ = { - isa = PBXNativeTarget; - buildConfigurationList = 11036103244B101B008610AF /* Build configuration list for PBXNativeTarget "GoldWarsUITests" */; - buildPhases = ( - 110360F0244B101B008610AF /* Sources */, - 110360F1244B101B008610AF /* Frameworks */, - 110360F2244B101B008610AF /* Resources */, - ); - buildRules = ( - ); - dependencies = ( - 110360F6244B101B008610AF /* PBXTargetDependency */, - ); - name = GoldWarsUITests; - productName = GoldWarsUITests; - productReference = 110360F4244B101B008610AF /* GoldWarsUITests.xctest */; - productType = "com.apple.product-type.bundle.ui-testing"; - }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ @@ -349,10 +305,6 @@ CreatedOnToolsVersion = 11.3.1; TestTargetID = 110360CE244B101A008610AF; }; - 110360F3244B101B008610AF = { - CreatedOnToolsVersion = 11.3.1; - TestTargetID = 110360CE244B101A008610AF; - }; }; }; buildConfigurationList = 110360CA244B101A008610AF /* Build configuration list for PBXProject "GoldWars" */; @@ -370,7 +322,6 @@ targets = ( 110360CE244B101A008610AF /* GoldWars */, 110360E8244B101B008610AF /* GoldWarsTests */, - 110360F3244B101B008610AF /* GoldWarsUITests */, ); }; /* End PBXProject section */ @@ -394,13 +345,6 @@ ); runOnlyForDeploymentPostprocessing = 0; }; - 110360F2244B101B008610AF /* Resources */ = { - isa = PBXResourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - runOnlyForDeploymentPostprocessing = 0; - }; /* End PBXResourcesBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ @@ -414,6 +358,7 @@ 9E78ACBE245CC9C000526FF7 /* AtkBoostSkillComponent.swift in Sources */, 9E78ACC4245CCA3600526FF7 /* SpySkillComponent.swift in Sources */, 9EA3ABEB245C6DFA006BC61D /* BaseNode.swift in Sources */, + 9E04AFAF245E2B73002D5CFC /* AttackActionComponent.swift in Sources */, 110360D9244B101A008610AF /* GameScene.swift in Sources */, 116060F7245C57D2004E5A36 /* EntityManager.swift in Sources */, 3EBD242E245D9332003CECE7 /* Team.swift in Sources */, @@ -447,14 +392,6 @@ ); runOnlyForDeploymentPostprocessing = 0; }; - 110360F0244B101B008610AF /* Sources */ = { - isa = PBXSourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - 110360F9244B101B008610AF /* GoldWarsUITests.swift in Sources */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; /* End PBXSourcesBuildPhase section */ /* Begin PBXTargetDependency section */ @@ -463,11 +400,6 @@ target = 110360CE244B101A008610AF /* GoldWars */; targetProxy = 110360EA244B101B008610AF /* PBXContainerItemProxy */; }; - 110360F6244B101B008610AF /* PBXTargetDependency */ = { - isa = PBXTargetDependency; - target = 110360CE244B101A008610AF /* GoldWars */; - targetProxy = 110360F5244B101B008610AF /* PBXContainerItemProxy */; - }; /* End PBXTargetDependency section */ /* Begin PBXVariantGroup section */ @@ -696,44 +628,6 @@ }; name = Release; }; - 11036104244B101B008610AF /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; - CODE_SIGN_STYLE = Automatic; - INFOPLIST_FILE = GoldWarsUITests/Info.plist; - LD_RUNPATH_SEARCH_PATHS = ( - "$(inherited)", - "@executable_path/Frameworks", - "@loader_path/Frameworks", - ); - PRODUCT_BUNDLE_IDENTIFIER = com.hft.stuttgart.GoldWarsUITests; - PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 5.0; - TARGETED_DEVICE_FAMILY = "1,2"; - TEST_TARGET_NAME = GoldWars; - }; - name = Debug; - }; - 11036105244B101B008610AF /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; - CODE_SIGN_STYLE = Automatic; - INFOPLIST_FILE = GoldWarsUITests/Info.plist; - LD_RUNPATH_SEARCH_PATHS = ( - "$(inherited)", - "@executable_path/Frameworks", - "@loader_path/Frameworks", - ); - PRODUCT_BUNDLE_IDENTIFIER = com.hft.stuttgart.GoldWarsUITests; - PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 5.0; - TARGETED_DEVICE_FAMILY = "1,2"; - TEST_TARGET_NAME = GoldWars; - }; - name = Release; - }; /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ @@ -764,15 +658,6 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; - 11036103244B101B008610AF /* Build configuration list for PBXNativeTarget "GoldWarsUITests" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - 11036104244B101B008610AF /* Debug */, - 11036105244B101B008610AF /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; /* End XCConfigurationList section */ }; rootObject = 110360C7244B101A008610AF /* Project object */; diff --git a/GoldWars/GoldWars/Components/AttackActionComponent.swift b/GoldWars/GoldWars/Components/AttackActionComponent.swift new file mode 100644 index 0000000..c627fbb --- /dev/null +++ b/GoldWars/GoldWars/Components/AttackActionComponent.swift @@ -0,0 +1,31 @@ +// +// AttackActionComponent.swift +// GoldWars +// +// Created by Niko Jochim on 03.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import Foundation +import GameplayKit + +class AttackActionComponent : GKComponent { + + + init(unitCount: Int, adjacencyList: Array, position: CGPoint) { + + + super.init() + + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + + func action() { + + } + +} diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 4511bf4..b8d8f71 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -61,7 +61,6 @@ class EntityManager { } - func remove(_ entity: GKEntity) { if let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode { spriteNode.removeFromParent() @@ -88,7 +87,11 @@ class EntityManager { } } return nil - } + } + + func getBasesByTeam(for team: Team) -> Set { + return entities.filter{$0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.team == team } as! Set + } func getBackground() -> GKEntity? { return entities.filter{$0 is Background}[0] diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index e255a05..c9f0dde 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -12,6 +12,9 @@ import GameplayKit class GameScene: SKScene{ var entityManager: EntityManager! + var isMoveTouch = false + var currentDraggedBasePos = CGPoint() + var currentDraggedBase : Base? override func sceneDidLoad() { entityManager = EntityManager(scene: self) @@ -19,53 +22,96 @@ class GameScene: SKScene{ entityManager.add(Background(size: self.size)) initMap() } - - func initMap() { MapFactory(scene: self, entityManager: self.entityManager).loadMap(playerCount: 2) } - - - override func touchesBegan(_ touches: Set, with event: UIEvent?) { + + override func touchesEnded(_ 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 { + + if !isMoveTouch { + + 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 = 0.3 + child.alpha = 1 } - } - entityManager.add(Modal(modaltype: .BaseDetails, - base: entity as! Base, - anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2))) + + } + + 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))) + } } } } + else { + isMoveTouch = false + currentDraggedBase!.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = currentDraggedBasePos + currentDraggedBase!.component(ofType: TeamComponent.self)?.fire.position = currentDraggedBasePos + } } - + override func touchesBegan(_ touches: Set, with event: UIEvent?) { + + } + + override func touchesMoved(_ touches: Set, with event: UIEvent?) { + guard let touch = touches.first else { + return + } + let touchLocation = touch.location(in: self) + + let bases = entityManager.getBasesByTeam(for: .team1) + + for base in bases { + if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode{ + if !isMoveTouch { + currentDraggedBasePos = base.component(ofType: DefaultBaseComponent.self)!.spriteNode.position + currentDraggedBase = base + + } + isMoveTouch = true + + base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = touchLocation + base.component(ofType: TeamComponent.self)?.fire.position = touchLocation + for adjacencyBase in base.adjacencyList { + let node = adjacencyBase.component(ofType: DefaultBaseComponent.self)?.spriteNode + node?.run(SKAction.sequence([ + SKAction.resize(byWidth: 2, height: 2, duration: 0.5), + SKAction.resize(byWidth: -2, height: -2, duration: 0.5) + ])) + } + } + } + + + + + + } override func update(_ currentTime: TimeInterval) { entityManager.getBackground()?.update(deltaTime: currentTime) diff --git a/GoldWars/GoldWarsUITests/GoldWarsUITests.swift b/GoldWars/GoldWarsUITests/GoldWarsUITests.swift deleted file mode 100644 index f296a38..0000000 --- a/GoldWars/GoldWarsUITests/GoldWarsUITests.swift +++ /dev/null @@ -1,43 +0,0 @@ -// -// GoldWarsUITests.swift -// GoldWarsUITests -// -// Created by Aldin Duraki on 18.04.20. -// Copyright © 2020 SP2. All rights reserved. -// - -import XCTest - -class GoldWarsUITests: XCTestCase { - - override func setUp() { - // Put setup code here. This method is called before the invocation of each test method in the class. - - // In UI tests it is usually best to stop immediately when a failure occurs. - continueAfterFailure = false - - // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. - } - - override func tearDown() { - // Put teardown code here. This method is called after the invocation of each test method in the class. - } - - func testExample() { - // UI tests must launch the application that they test. - let app = XCUIApplication() - app.launch() - - // Use recording to get started writing UI tests. - // Use XCTAssert and related functions to verify your tests produce the correct results. - } - - func testLaunchPerformance() { - if #available(macOS 10.15, iOS 13.0, tvOS 13.0, *) { - // This measures how long it takes to launch your application. - measure(metrics: [XCTOSSignpostMetric.applicationLaunch]) { - XCUIApplication().launch() - } - } - } -} diff --git a/GoldWars/GoldWarsUITests/Info.plist b/GoldWars/GoldWarsUITests/Info.plist deleted file mode 100644 index 64d65ca..0000000 --- a/GoldWars/GoldWarsUITests/Info.plist +++ /dev/null @@ -1,22 +0,0 @@ - - - - - CFBundleDevelopmentRegion - $(DEVELOPMENT_LANGUAGE) - CFBundleExecutable - $(EXECUTABLE_NAME) - CFBundleIdentifier - $(PRODUCT_BUNDLE_IDENTIFIER) - CFBundleInfoDictionaryVersion - 6.0 - CFBundleName - $(PRODUCT_NAME) - CFBundlePackageType - $(PRODUCT_BUNDLE_PACKAGE_TYPE) - CFBundleShortVersionString - 1.0 - CFBundleVersion - 1 - -