From 3b417cbe0e64a6de3f83dc8e669e865277ced2f3 Mon Sep 17 00:00:00 2001 From: Aldin Duraki Date: Tue, 5 May 2020 11:16:27 -0700 Subject: [PATCH] Minor code cleanup --- .../Components/AtkBoostSkillComponent.swift | 5 ++--- .../Components/AttackActionComponent.swift | 8 ++------ .../Components/BackgroundComponent.swift | 5 +---- GoldWars/GoldWars/Components/BaseNode.swift | 11 ++++------ .../GoldWars/Components/ButtonComponent.swift | 5 ++--- GoldWars/GoldWars/Components/ButtonNode.swift | 20 ++++++++----------- .../Components/DefBoostSkillComponent.swift | 5 ++--- .../Components/DefaultBaseComponent.swift | 3 +-- .../Components/ModalBackgroundComponent.swift | 3 +-- .../Components/ModalContentComponent.swift | 1 - .../Components/SpySkillComponent.swift | 5 ++--- .../GoldWars/Components/TeamComponent.swift | 5 +++-- GoldWars/GoldWars/Entities/Background.swift | 5 ----- GoldWars/GoldWars/Entities/Base.swift | 3 ++- GoldWars/GoldWars/Entities/Button.swift | 14 +++++-------- .../GoldWars/Entities/EntityManager.swift | 10 ++++------ GoldWars/GoldWars/Entities/Modal.swift | 6 ++---- GoldWars/GoldWars/Enums/ModalType.swift | 2 -- GoldWars/GoldWars/GameViewController.swift | 6 +++--- GoldWars/GoldWars/Scenes/GameScene.swift | 5 ----- 20 files changed, 44 insertions(+), 83 deletions(-) diff --git a/GoldWars/GoldWars/Components/AtkBoostSkillComponent.swift b/GoldWars/GoldWars/Components/AtkBoostSkillComponent.swift index febccec..a2d36b3 100644 --- a/GoldWars/GoldWars/Components/AtkBoostSkillComponent.swift +++ b/GoldWars/GoldWars/Components/AtkBoostSkillComponent.swift @@ -10,8 +10,8 @@ import GameplayKit class AtkBoostSkillComponent: GKComponent{ - let shapeNode :SKShapeNode - let labelNode :SKLabelNode + let shapeNode: SKShapeNode + let labelNode: SKLabelNode init(text: String, texture: SKTexture?, anchorPoint: CGPoint) { self.labelNode = SKLabelNode(text: text) @@ -24,7 +24,6 @@ class AtkBoostSkillComponent: GKComponent{ shapeNode.fillColor = SKColor.gray } super.init() - } required init?(coder: NSCoder) { diff --git a/GoldWars/GoldWars/Components/AttackActionComponent.swift b/GoldWars/GoldWars/Components/AttackActionComponent.swift index c627fbb..ba46e1a 100644 --- a/GoldWars/GoldWars/Components/AttackActionComponent.swift +++ b/GoldWars/GoldWars/Components/AttackActionComponent.swift @@ -6,17 +6,13 @@ // Copyright © 2020 SP2. All rights reserved. // -import Foundation import GameplayKit -class AttackActionComponent : GKComponent { +class AttackActionComponent: GKComponent { init(unitCount: Int, adjacencyList: Array, position: CGPoint) { - - super.init() - } required init?(coder: NSCoder) { @@ -25,7 +21,7 @@ class AttackActionComponent : GKComponent { func action() { - + // Not implemented yet } } diff --git a/GoldWars/GoldWars/Components/BackgroundComponent.swift b/GoldWars/GoldWars/Components/BackgroundComponent.swift index 349cf2a..7239741 100644 --- a/GoldWars/GoldWars/Components/BackgroundComponent.swift +++ b/GoldWars/GoldWars/Components/BackgroundComponent.swift @@ -6,7 +6,6 @@ // Copyright © 2020 SP2. All rights reserved. // -import Foundation import GameplayKit class BackgroundComponent: GKComponent{ @@ -21,7 +20,7 @@ class BackgroundComponent: GKComponent{ 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)) + sky.position = CGPoint(x: CGFloat(i) * sky.size.width, y: (size.height / 2)) nodes.append(sky) } super.init() @@ -36,8 +35,6 @@ class BackgroundComponent: GKComponent{ } } - - required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } diff --git a/GoldWars/GoldWars/Components/BaseNode.swift b/GoldWars/GoldWars/Components/BaseNode.swift index 40e27fc..00da238 100644 --- a/GoldWars/GoldWars/Components/BaseNode.swift +++ b/GoldWars/GoldWars/Components/BaseNode.swift @@ -6,23 +6,20 @@ // Copyright © 2020 SP2. All rights reserved. // -import Foundation import SpriteKit - class BaseNode: SKSpriteNode{ override func touchesBegan(_ touches: Set, with event: UIEvent?) { // TODO: PopUp Einheiten + Close PopUp self.run( SKAction.sequence( - [ - SKAction.resize(byWidth: 20, height: 20, duration: 0.5), - SKAction.resize(byWidth: -20, height: -20, duration: 0.5) - ] + [ + SKAction.resize(byWidth: 20, height: 20, duration: 0.5), + SKAction.resize(byWidth: -20, height: -20, duration: 0.5) + ] ) ) - } override func touchesMoved(_ touches: Set, with event: UIEvent?) { diff --git a/GoldWars/GoldWars/Components/ButtonComponent.swift b/GoldWars/GoldWars/Components/ButtonComponent.swift index 57476db..0fe059f 100644 --- a/GoldWars/GoldWars/Components/ButtonComponent.swift +++ b/GoldWars/GoldWars/Components/ButtonComponent.swift @@ -6,14 +6,13 @@ // Copyright © 2020 SP2. All rights reserved. // -import Foundation import GameplayKit class ButtonComponent: GKComponent { - var buttonNode : ButtonNode + var buttonNode: ButtonNode - init(iconName: String, text: String, position: CGPoint, isEnabled:Bool , onButtonPress: @escaping () -> ()) { + init(iconName: String, text: String, position: CGPoint, isEnabled:Bool, onButtonPress: @escaping () -> ()) { buttonNode = ButtonNode(iconName: iconName, text: text, isEnabled: isEnabled, diff --git a/GoldWars/GoldWars/Components/ButtonNode.swift b/GoldWars/GoldWars/Components/ButtonNode.swift index d1d6ad2..2ebd012 100644 --- a/GoldWars/GoldWars/Components/ButtonNode.swift +++ b/GoldWars/GoldWars/Components/ButtonNode.swift @@ -6,12 +6,11 @@ // Copyright © 2020 SP2. All rights reserved. // -import Foundation import SpriteKit -class ButtonNode : SKSpriteNode { +class ButtonNode: SKSpriteNode { - var isEnabled:Bool{ + var isEnabled: Bool{ didSet{ if isEnabled { self.alpha = 1 @@ -25,7 +24,7 @@ class ButtonNode : SKSpriteNode { let onButtonPress: () -> () - init(iconName: String, text: String,isEnabled:Bool, position: CGPoint, onButtonPress: @escaping () -> ()) { + init(iconName: String, text: String, isEnabled: Bool, position: CGPoint, onButtonPress: @escaping () -> ()) { self.onButtonPress = onButtonPress self.isEnabled = isEnabled let texture = SKTexture(imageNamed: "yellow_button04") @@ -33,42 +32,39 @@ class ButtonNode : SKSpriteNode { 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 { + if iconName.isEmpty { + label.position = CGPoint(x: 0, y: 0) + } else { 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() } - } diff --git a/GoldWars/GoldWars/Components/DefBoostSkillComponent.swift b/GoldWars/GoldWars/Components/DefBoostSkillComponent.swift index 62b9b23..01c0aef 100644 --- a/GoldWars/GoldWars/Components/DefBoostSkillComponent.swift +++ b/GoldWars/GoldWars/Components/DefBoostSkillComponent.swift @@ -10,8 +10,8 @@ import GameplayKit class DefBoostSkillComponent: GKComponent{ - let shapeNode :SKShapeNode - let labelNode :SKLabelNode + let shapeNode: SKShapeNode + let labelNode: SKLabelNode init(text: String, texture: SKTexture?, anchorPoint: CGPoint) { self.labelNode = SKLabelNode(text: text) @@ -24,7 +24,6 @@ class DefBoostSkillComponent: GKComponent{ shapeNode.fillColor = SKColor.gray } super.init() - } required init?(coder: NSCoder) { diff --git a/GoldWars/GoldWars/Components/DefaultBaseComponent.swift b/GoldWars/GoldWars/Components/DefaultBaseComponent.swift index f668990..715840b 100644 --- a/GoldWars/GoldWars/Components/DefaultBaseComponent.swift +++ b/GoldWars/GoldWars/Components/DefaultBaseComponent.swift @@ -6,14 +6,13 @@ // Copyright © 2020 SP2. All rights reserved. // -import Foundation import GameplayKit import SpriteKit class DefaultBaseComponent: GKComponent { var spriteNode: BaseNode - init(texture: SKTexture,position: CGPoint) { + init(texture: SKTexture, position: CGPoint) { spriteNode = BaseNode(texture: texture, size: CGSize(width: 80, height: 80)) spriteNode.position = position super.init() diff --git a/GoldWars/GoldWars/Components/ModalBackgroundComponent.swift b/GoldWars/GoldWars/Components/ModalBackgroundComponent.swift index 2d26122..e81fcf9 100644 --- a/GoldWars/GoldWars/Components/ModalBackgroundComponent.swift +++ b/GoldWars/GoldWars/Components/ModalBackgroundComponent.swift @@ -6,12 +6,11 @@ // Copyright © 2020 SP2. All rights reserved. // -import Foundation import GameplayKit import SpriteKit class ModalBackgroundComponent: GKComponent { - let spriteNode : SKSpriteNode + let spriteNode: SKSpriteNode init(anchorPoint: CGPoint) { let texture = SKTexture(imageNamed:"ModalBackground") diff --git a/GoldWars/GoldWars/Components/ModalContentComponent.swift b/GoldWars/GoldWars/Components/ModalContentComponent.swift index 7c4f803..0b1a09e 100644 --- a/GoldWars/GoldWars/Components/ModalContentComponent.swift +++ b/GoldWars/GoldWars/Components/ModalContentComponent.swift @@ -6,7 +6,6 @@ // Copyright © 2020 SP2. All rights reserved. // -import Foundation import GameplayKit import SpriteKit diff --git a/GoldWars/GoldWars/Components/SpySkillComponent.swift b/GoldWars/GoldWars/Components/SpySkillComponent.swift index d4fb374..b193de5 100644 --- a/GoldWars/GoldWars/Components/SpySkillComponent.swift +++ b/GoldWars/GoldWars/Components/SpySkillComponent.swift @@ -10,8 +10,8 @@ import GameplayKit class SpySkillComponent: GKComponent{ - let shapeNode :SKShapeNode - let labelNode :SKLabelNode + let shapeNode: SKShapeNode + let labelNode: SKLabelNode init(text: String, texture: SKTexture?, anchorPoint: CGPoint) { self.labelNode = SKLabelNode(text: text) @@ -24,7 +24,6 @@ class SpySkillComponent: GKComponent{ shapeNode.fillColor = SKColor.gray } super.init() - } required init?(coder: NSCoder) { diff --git a/GoldWars/GoldWars/Components/TeamComponent.swift b/GoldWars/GoldWars/Components/TeamComponent.swift index dc24f45..379a1cc 100644 --- a/GoldWars/GoldWars/Components/TeamComponent.swift +++ b/GoldWars/GoldWars/Components/TeamComponent.swift @@ -11,7 +11,7 @@ import GameplayKit class TeamComponent: GKComponent { let team: Team - let fire:SKEmitterNode + let fire: SKEmitterNode init(team: Team, position: CGPoint) { fire = SKEmitterNode(fileNamed: "Fire")! @@ -20,7 +20,8 @@ class TeamComponent: GKComponent { fire.name = "fire" fire.particleColorSequence = nil fire.particleColorBlendFactor = 1.0 - switch team { + + switch team { case .team1: fire.particleColor = SKColor.red case .team2: fire.particleColor = SKColor.purple case .team3: fire.particleColor = SKColor.green diff --git a/GoldWars/GoldWars/Entities/Background.swift b/GoldWars/GoldWars/Entities/Background.swift index 423168e..6cc1b74 100644 --- a/GoldWars/GoldWars/Entities/Background.swift +++ b/GoldWars/GoldWars/Entities/Background.swift @@ -6,7 +6,6 @@ // Copyright © 2020 SP2. All rights reserved. // -import Foundation import GameplayKit class Background: GKEntity { @@ -16,7 +15,6 @@ class Background: GKEntity { addComponent(BackgroundComponent(size: size)) } - override func update(deltaTime seconds: TimeInterval) { component(ofType: BackgroundComponent.self)?.update() } @@ -24,7 +22,4 @@ class Background: GKEntity { required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } - - - } diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index 6ae0a10..8292bbd 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -9,7 +9,8 @@ import SpriteKit import GameplayKit -class Base : GKEntity { +class Base: GKEntity { + var unitCount: Int var adjacencyList: Array diff --git a/GoldWars/GoldWars/Entities/Button.swift b/GoldWars/GoldWars/Entities/Button.swift index e2c52e2..8ac174d 100644 --- a/GoldWars/GoldWars/Entities/Button.swift +++ b/GoldWars/GoldWars/Entities/Button.swift @@ -6,24 +6,20 @@ // Copyright © 2020 SP2. All rights reserved. // -import Foundation import GameKit -class Button : GKEntity{ - let name : String +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)) + 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 b8d8f71..9d0758b 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -6,7 +6,6 @@ // Copyright © 2020 SP2. All rights reserved. // -import Foundation import SpriteKit import GameplayKit @@ -14,7 +13,7 @@ class EntityManager { var entities = Set() let scene: SKScene - var isModal:Bool + var isModal: Bool init(scene: SKScene) { self.scene = scene @@ -58,9 +57,8 @@ class EntityManager { scene.addChild(node) } } - - } + func remove(_ entity: GKEntity) { if let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode { spriteNode.removeFromParent() @@ -97,11 +95,11 @@ class EntityManager { return entities.filter{$0 is Background}[0] } - func getBaseNodeByTeam(for team: Team) -> SKSpriteNode?{ + func getBaseNodeByTeam(for team: Team) -> SKSpriteNode? { return getBaseByTeam(for: team)?.component(ofType: DefaultBaseComponent.self)?.spriteNode } - func getButtonByName(buttonName:String) -> Button{ + func getButtonByName(buttonName:String) -> Button { return entities.filter{$0 is Button && ($0 as! Button).name == buttonName }[0] as! Button } } diff --git a/GoldWars/GoldWars/Entities/Modal.swift b/GoldWars/GoldWars/Entities/Modal.swift index 0d96a15..3ef701c 100644 --- a/GoldWars/GoldWars/Entities/Modal.swift +++ b/GoldWars/GoldWars/Entities/Modal.swift @@ -6,12 +6,10 @@ // Copyright © 2020 SP2. All rights reserved. // -import Foundation import GameplayKit -class Modal:GKEntity{ - - +class Modal: GKEntity{ + init(modaltype: ModalType, base: Base, anchorPoint: CGPoint) { super.init() diff --git a/GoldWars/GoldWars/Enums/ModalType.swift b/GoldWars/GoldWars/Enums/ModalType.swift index 3c98a25..40bcedf 100644 --- a/GoldWars/GoldWars/Enums/ModalType.swift +++ b/GoldWars/GoldWars/Enums/ModalType.swift @@ -6,8 +6,6 @@ // Copyright © 2020 SP2. All rights reserved. // -import Foundation - enum ModalType: String{ case BaseDetails case BaseAttack diff --git a/GoldWars/GoldWars/GameViewController.swift b/GoldWars/GoldWars/GameViewController.swift index 4eed433..9fb663d 100644 --- a/GoldWars/GoldWars/GameViewController.swift +++ b/GoldWars/GoldWars/GameViewController.swift @@ -22,9 +22,9 @@ class GameViewController: UIViewController { //TODO: create dev profile or remove on delivery view.showsFPS = true view.showsNodeCount = true - - GameCenterHelper.helper.viewController = self - } + + GameCenterHelper.helper.viewController = self + } } override var shouldAutorotate: Bool { diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index c9f0dde..92f7819 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -28,7 +28,6 @@ class GameScene: SKScene{ } override func touchesEnded(_ touches: Set, with event: UIEvent?) { - guard let touch = touches.first else { return } @@ -36,9 +35,7 @@ class GameScene: SKScene{ let touchLocation = touch.location(in: self) if !isMoveTouch { - for entity in entityManager.entities { - let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode if entityManager.isModal && entity.isMember(of: Modal.self) { @@ -48,7 +45,6 @@ class GameScene: SKScene{ child.alpha = 1 } } - } if atPoint(touchLocation) == spriteNode && !entityManager.isModal { @@ -58,7 +54,6 @@ class GameScene: SKScene{ if(child.name != "fire"){ child.alpha = 0.3 } - } entityManager.add(Modal(modaltype: .BaseDetails, base: entity as! Base,