From 28fa86c25c89e120b4e13f262987e5ec14179c0c Mon Sep 17 00:00:00 2001 From: Simon Kellner Date: Sun, 17 May 2020 19:18:00 +0200 Subject: [PATCH] add SkillButtonNode, Attack and Defence Skill -change Unit enum to UnitType --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 12 ++- .../Components/AtkBoostSkillComponent.swift | 48 +++++++----- .../Components/DefBoostSkillComponent.swift | 46 ++++++----- .../GoldWars/Components/SkillButtonNode.swift | 76 +++++++++++++++++++ GoldWars/GoldWars/Entities/Base.swift | 2 + .../GoldWars/Entities/EntityManager.swift | 11 ++- GoldWars/GoldWars/Entities/HUD.swift | 42 +++++----- .../Enums/{Unit.swift => UnitType.swift} | 3 +- 8 files changed, 164 insertions(+), 76 deletions(-) create mode 100644 GoldWars/GoldWars/Components/SkillButtonNode.swift rename GoldWars/GoldWars/Enums/{Unit.swift => UnitType.swift} (83%) diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index 8b35a21..2255b11 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -16,10 +16,11 @@ 110360EE244B101B008610AF /* GoldWarsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 110360ED244B101B008610AF /* GoldWarsTests.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 */; }; + 11738A3B24508F68004426F1 /* UnitType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11738A3A24508F68004426F1 /* UnitType.swift */; }; 2086465C2461B66200817C23 /* TimerComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2086465B2461B66200817C23 /* TimerComponent.swift */; }; 3EBD242C245D8044003CECE7 /* GameCenterHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EBD242B245D8044003CECE7 /* GameCenterHelper.swift */; }; 3EBD242E245D9332003CECE7 /* Team.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EBD242D245D9332003CECE7 /* Team.swift */; }; + 8BB6FF402472B8F000162BBD /* SkillButtonNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BB6FF3F2472B8F000162BBD /* SkillButtonNode.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 */; }; @@ -80,10 +81,11 @@ 110360EF244B101B008610AF /* 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 = ""; }; + 11738A3A24508F68004426F1 /* UnitType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UnitType.swift; sourceTree = ""; }; 2086465B2461B66200817C23 /* TimerComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimerComponent.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 = ""; }; + 8BB6FF3F2472B8F000162BBD /* SkillButtonNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SkillButtonNode.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 = ""; }; @@ -214,6 +216,7 @@ 2086465B2461B66200817C23 /* TimerComponent.swift */, 9EBFD7542462CF5A00E1E219 /* SliderComponent.swift */, 9EC7E48A2461FBF700396BCD /* SliderNode.swift */, + 8BB6FF3F2472B8F000162BBD /* SkillButtonNode.swift */, ); path = Components; sourceTree = ""; @@ -251,7 +254,7 @@ 9EC86BA2245C89B200796EF3 /* Enums */ = { isa = PBXGroup; children = ( - 11738A3A24508F68004426F1 /* Unit.swift */, + 11738A3A24508F68004426F1 /* UnitType.swift */, 9EC86BA5245C8AD000796EF3 /* ModalType.swift */, 3EBD242D245D9332003CECE7 /* Team.swift */, ); @@ -404,12 +407,13 @@ 9E174C88245DF1FF00209FF0 /* BackgroundComponent.swift in Sources */, 9E78ACBA245CBDAF00526FF7 /* HUD.swift in Sources */, 9EEDE02D246FCD770096C735 /* SpinningLogoEntity.swift in Sources */, - 11738A3B24508F68004426F1 /* Unit.swift in Sources */, + 11738A3B24508F68004426F1 /* UnitType.swift in Sources */, 9E174C86245DD91500209FF0 /* ButtonComponent.swift in Sources */, AE151589245F18EF001D363E /* MatchmakingHelper.swift in Sources */, 11036113244B3E30008610AF /* MenuScene.swift in Sources */, 9EA3ABE9245C6DAA006BC61D /* DefaultBaseComponent.swift in Sources */, 9E174C8A245E1A0A00209FF0 /* Background.swift in Sources */, + 8BB6FF402472B8F000162BBD /* SkillButtonNode.swift in Sources */, 9EA3ABED245C8143006BC61D /* ModalBackgroundComponent.swift in Sources */, C064E9A8246C0EA50022B228 /* LabelNode.swift in Sources */, 3EBD242C245D8044003CECE7 /* GameCenterHelper.swift in Sources */, diff --git a/GoldWars/GoldWars/Components/AtkBoostSkillComponent.swift b/GoldWars/GoldWars/Components/AtkBoostSkillComponent.swift index a2d36b3..3f2e1c3 100644 --- a/GoldWars/GoldWars/Components/AtkBoostSkillComponent.swift +++ b/GoldWars/GoldWars/Components/AtkBoostSkillComponent.swift @@ -9,25 +9,31 @@ import GameplayKit class AtkBoostSkillComponent: GKComponent{ - - let shapeNode: SKShapeNode - let labelNode: SKLabelNode - - init(text: String, texture: SKTexture?, anchorPoint: CGPoint) { - self.labelNode = SKLabelNode(text: text) - self.shapeNode = SKShapeNode(circleOfRadius: 30) - self.shapeNode.position = anchorPoint - self.labelNode.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 15) - if texture != nil { - shapeNode.fillTexture = texture - }else { - shapeNode.fillColor = SKColor.gray - } - super.init() - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - + + 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) + bases.forEach{ base in + if(base.unitType == .Defence){ + base.unitType = .AttackDefence + }else { + base.unitType = .Attack + } + } + }) + super.init() + } + + + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + } diff --git a/GoldWars/GoldWars/Components/DefBoostSkillComponent.swift b/GoldWars/GoldWars/Components/DefBoostSkillComponent.swift index 01c0aef..66ddba8 100644 --- a/GoldWars/GoldWars/Components/DefBoostSkillComponent.swift +++ b/GoldWars/GoldWars/Components/DefBoostSkillComponent.swift @@ -9,25 +9,29 @@ import GameplayKit class DefBoostSkillComponent: GKComponent{ - - let shapeNode: SKShapeNode - let labelNode: SKLabelNode - - init(text: String, texture: SKTexture?, anchorPoint: CGPoint) { - self.labelNode = SKLabelNode(text: text) - self.shapeNode = SKShapeNode(circleOfRadius: 30) - self.shapeNode.position = anchorPoint - self.labelNode.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 15) - if texture != nil { - shapeNode.fillTexture = texture - }else { - shapeNode.fillColor = SKColor.gray - } - super.init() - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - + + 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) + bases.forEach{ base in + if(base.unitType == .Attack){ + base.unitType = .AttackDefence + }else { + base.unitType = .Defence + } + } + }) + super.init() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + } diff --git a/GoldWars/GoldWars/Components/SkillButtonNode.swift b/GoldWars/GoldWars/Components/SkillButtonNode.swift new file mode 100644 index 0000000..1decb06 --- /dev/null +++ b/GoldWars/GoldWars/Components/SkillButtonNode.swift @@ -0,0 +1,76 @@ +// +// SkillButtonNode.swift +// GoldWars +// +// Created by Simon Kellner on 18.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import SpriteKit + +class SkillButtonNode: SKSpriteNode { + + var isEnabled: Bool{ + didSet{ + if isEnabled { + self.alpha = 1 + self.childNode(withName: "label")?.alpha = 1 + } else { + self.alpha = 0.3 + self.childNode(withName: "label")?.alpha = 0.3 + } + } + } + + let onButtonPress: () -> () + + init(iconName: String, text: String, isEnabled: Bool, position: CGPoint, onButtonPress: @escaping () -> ()) { + self.onButtonPress = onButtonPress + self.isEnabled = isEnabled + let texture = SKTexture(imageNamed: "yellow_circle") + 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: 0, y: 0) + } else { + label.position = CGPoint(x: size.width * 0.5, y: 0) + let icon = SKSpriteNode(imageNamed: iconName) + icon.position = CGPoint(x: -size.width * 0.5, y: 0) + icon.zPosition = 1 + self.addChild(icon) + } + 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) + self.isEnabled = false + onButtonPress() + } + } + + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + +} diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index 6cb0158..d9b865a 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -13,12 +13,14 @@ import GameKit class Base: GKEntity { var unitCount: Int + var unitType: UnitType var adjacencyList: Array var changeOwnership: Bool var ownershipPlayer: GKPlayer? init(position: CGPoint, player: GKPlayer! = nil, team: Team! = nil) { self.unitCount = 0 + self.unitType = .General self.adjacencyList = [Base]() self.changeOwnership = false self.ownershipPlayer = player diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index fd2ce27..66e9964 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -44,13 +44,12 @@ class EntityManager { scene.addChild(modal.body) scene.addChild(modal.footer) } - if let skill = entity.component(ofType: AtkBoostSkillComponent.self) { - scene.addChild(skill.shapeNode) - scene.addChild(skill.labelNode) + if let skillButtonNode = entity.component(ofType: AtkBoostSkillComponent.self)?.skillButtonNode { + scene.addChild(skillButtonNode) + isModal = true } - if let skill = entity.component(ofType: DefBoostSkillComponent.self) { - scene.addChild(skill.shapeNode) - scene.addChild(skill.labelNode) + if let skillButtonNode = entity.component(ofType: DefBoostSkillComponent.self)?.skillButtonNode { + scene.addChild(skillButtonNode) } if let skill = entity.component(ofType: SpySkillComponent.self) { scene.addChild(skill.shapeNode) diff --git a/GoldWars/GoldWars/Entities/HUD.swift b/GoldWars/GoldWars/Entities/HUD.swift index a161285..42c213c 100644 --- a/GoldWars/GoldWars/Entities/HUD.swift +++ b/GoldWars/GoldWars/Entities/HUD.swift @@ -9,29 +9,25 @@ import GameplayKit class HUD: GKEntity { - - init(size: CGSize) { - super.init() - addComponent(SpySkillComponent(text: "Spy", - texture: nil, - anchorPoint: CGPoint(x: size.width * 0.75, y: size.height * 0.1))) - - - addComponent(AtkBoostSkillComponent(text: "Atk", - texture: nil, - anchorPoint: CGPoint(x: size.width * 0.85, y: size.height * 0.1))) - - - addComponent(DefBoostSkillComponent(text: "Def", - texture: nil, - anchorPoint: CGPoint(x: size.width * 0.95, y: size.height * 0.1))) + + init(size: CGSize) { + super.init() + addComponent(SpySkillComponent(text: "Spy", + texture: nil, + anchorPoint: CGPoint(x: size.width * 0.75, y: size.height * 0.1))) + + + addComponent(AtkBoostSkillComponent(iconName: "", text: "Atk", position: CGPoint(x: size.width * 0.85, y: size.height * 0.1), isEnabled: true)) + + + addComponent(DefBoostSkillComponent(iconName: "", text: "Def", position: CGPoint(x: size.width * 0.95, y: size.height * 0.1), isEnabled: true)) addComponent(TimerComponent(text: "", - anchorPoint: CGPoint(x: size.width * 0.5, y: size.height * 0.9), duration: 30)) - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - + anchorPoint: CGPoint(x: size.width * 0.5, y: size.height * 0.9), duration: 30)) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + } diff --git a/GoldWars/GoldWars/Enums/Unit.swift b/GoldWars/GoldWars/Enums/UnitType.swift similarity index 83% rename from GoldWars/GoldWars/Enums/Unit.swift rename to GoldWars/GoldWars/Enums/UnitType.swift index a4fa851..aec5ccd 100644 --- a/GoldWars/GoldWars/Enums/Unit.swift +++ b/GoldWars/GoldWars/Enums/UnitType.swift @@ -6,8 +6,9 @@ // Copyright © 2020 SP2. All rights reserved. // -enum Unit{ +enum UnitType{ case General case Defence case Attack + case AttackDefence }