From 185358a3d5c5c78f85cf1401517c58c8d468b087 Mon Sep 17 00:00:00 2001 From: Niko Jochim Date: Wed, 6 May 2020 02:17:25 +0200 Subject: [PATCH] * Impl. basic attacking functionality * added attacking Modal --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 8 +++ .../Components/ModalContentComponent.swift | 2 +- GoldWars/GoldWars/Components/SliderNode.swift | 66 ++++++++++++++++++ GoldWars/GoldWars/Entities/Base.swift | 14 ++-- .../GoldWars/Entities/EntityManager.swift | 36 ++++++++++ GoldWars/GoldWars/Entities/Modal.swift | 13 +++- GoldWars/GoldWars/Partikels/Fire.sks | Bin 7801 -> 7975 bytes GoldWars/GoldWars/Scenes/GameScene.swift | 50 +++++++++---- 8 files changed, 166 insertions(+), 23 deletions(-) create mode 100644 GoldWars/GoldWars/Components/SliderNode.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index d28b1c8..072a1dd 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -36,6 +36,8 @@ 9EA3ABEB245C6DFA006BC61D /* BaseNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EA3ABEA245C6DFA006BC61D /* BaseNode.swift */; }; 9EA3ABED245C8143006BC61D /* ModalBackgroundComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EA3ABEC245C8143006BC61D /* ModalBackgroundComponent.swift */; }; 9EA3ABEF245C834B006BC61D /* ModalContentComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EA3ABEE245C834B006BC61D /* ModalContentComponent.swift */; }; + 9EC7E48B2461FBF700396BCD /* SliderNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EC7E48A2461FBF700396BCD /* SliderNode.swift */; }; + 9EC7E48D2461FE2C00396BCD /* SliderComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EC7E48C2461FE2C00396BCD /* SliderComponent.swift */; }; 9EC86B9F245C88A300796EF3 /* Modal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EC86B9E245C88A300796EF3 /* Modal.swift */; }; 9EC86BA6245C8AD000796EF3 /* ModalType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EC86BA5245C8AD000796EF3 /* ModalType.swift */; }; AB1D759C245DD18100671525 /* MapProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759A245DD18100671525 /* MapProtocol.swift */; }; @@ -88,6 +90,8 @@ 9EA3ABEA245C6DFA006BC61D /* BaseNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseNode.swift; sourceTree = ""; }; 9EA3ABEC245C8143006BC61D /* ModalBackgroundComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModalBackgroundComponent.swift; sourceTree = ""; }; 9EA3ABEE245C834B006BC61D /* ModalContentComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModalContentComponent.swift; sourceTree = ""; }; + 9EC7E48A2461FBF700396BCD /* SliderNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SliderNode.swift; sourceTree = ""; }; + 9EC7E48C2461FE2C00396BCD /* SliderComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SliderComponent.swift; sourceTree = ""; }; 9EC86B9E245C88A300796EF3 /* Modal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Modal.swift; sourceTree = ""; }; 9EC86BA5245C8AD000796EF3 /* ModalType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModalType.swift; sourceTree = ""; }; 9ECD3699245C91F7008DEEBD /* GoldWars.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = GoldWars.entitlements; sourceTree = ""; }; @@ -179,6 +183,8 @@ 9E174C85245DD91500209FF0 /* ButtonComponent.swift */, 9E174C87245DF1FF00209FF0 /* BackgroundComponent.swift */, 9E04AFAE245E2B73002D5CFC /* AttackActionComponent.swift */, + 9EC7E48A2461FBF700396BCD /* SliderNode.swift */, + 9EC7E48C2461FE2C00396BCD /* SliderComponent.swift */, ); path = Components; sourceTree = ""; @@ -374,8 +380,10 @@ AB1D759C245DD18100671525 /* MapProtocol.swift in Sources */, AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */, AB1D759D245DD18100671525 /* TwoPlayerDefaultTestMap.swift in Sources */, + 9EC7E48D2461FE2C00396BCD /* SliderComponent.swift in Sources */, ABA03DA0244BD54F00A66916 /* Base.swift in Sources */, 9E174C82245DD81D00209FF0 /* ButtonNode.swift in Sources */, + 9EC7E48B2461FBF700396BCD /* SliderNode.swift in Sources */, 9E174C84245DD8CE00209FF0 /* Button.swift in Sources */, 110360DB244B101A008610AF /* GameViewController.swift in Sources */, 110360D3244B101A008610AF /* AppDelegate.swift in Sources */, diff --git a/GoldWars/GoldWars/Components/ModalContentComponent.swift b/GoldWars/GoldWars/Components/ModalContentComponent.swift index 0b1a09e..df004a3 100644 --- a/GoldWars/GoldWars/Components/ModalContentComponent.swift +++ b/GoldWars/GoldWars/Components/ModalContentComponent.swift @@ -30,7 +30,7 @@ class ModalContentComponent: GKComponent{ self.body.fontSize = 40 self.footer = SKLabelNode(text: footer) - self.footer.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 20) + self.footer.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 40) self.footer.fontName = "HelveticaNeue-Bold" self.footer.fontSize = 40 super.init() diff --git a/GoldWars/GoldWars/Components/SliderNode.swift b/GoldWars/GoldWars/Components/SliderNode.swift new file mode 100644 index 0000000..93f42d2 --- /dev/null +++ b/GoldWars/GoldWars/Components/SliderNode.swift @@ -0,0 +1,66 @@ +// +// SliderNode.swift +// GoldWars +// +// Created by Niko Jochim on 05.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import SpriteKit + + +class SliderNode :SKNode { + + var sliderLine :SKShapeNode + var sliderKnob :SliderKnob + var width: CGFloat + + var getValue: CGFloat{ + get{ + return ((sliderKnob.position.x.rounded() - sliderKnob.min) / width) + } + } + + init(width: CGFloat, position: CGPoint) { + self.width = width + sliderLine = SKShapeNode(rectOf: CGSize(width: width, height: 8)) + sliderLine.position = position + sliderLine.fillColor = SKColor.white + sliderKnob = SliderKnob(circleOfRadius: 20) + sliderKnob.min = position.x - width / 2 + sliderKnob.max = position.x + width / 2 + sliderKnob.fillColor = SKColor.red + sliderKnob.zPosition = sliderLine.zPosition + 1 + sliderKnob.position = CGPoint(x: sliderLine.position.x, y: sliderLine.position.y + 1) + super.init() + + } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } +} + +class SliderKnob: SKShapeNode { + var min = CGFloat() + var max = CGFloat() + + override func touchesMoved(_ touches: Set, with event: UIEvent?) { + + for touch in touches { + let touchLocation = touch.location(in: self.scene!) + + if self.position.x >= min - 1 && self.position.x <= max + 1{ + self.position.x = touchLocation.x + } + + if(self.position.x <= min){ + self.position.x = min + } + if(self.position.x >= max){ + self.position.x = max + } + } + } +} + diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index 8292bbd..57af8e2 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -2,7 +2,7 @@ // Base.swift // GoldWars // -// Created by Marcel Schwarz on 18.04.20. +// Created by Aldin Duraki on 18.04.20. // Copyright © 2020 SP2. All rights reserved. // @@ -13,21 +13,27 @@ class Base: GKEntity { var unitCount: Int var adjacencyList: Array + var changeOwnerShip: Bool init(position: CGPoint, team: Team! = nil) { self.unitCount = 0 self.adjacencyList = [Base]() - + self.changeOwnerShip = false super.init() addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: "Base"), position: position)) if(team != nil){ addComponent(TeamComponent(team: team!, position: position)) - self.unitCount = 100 + self.unitCount = 500 } } - + func attackBase(base: Base, units:Int) -> [GKEntity]{ + base.changeOwnerShip = true + self.unitCount -= units + base.unitCount += units + return [self, base] + } 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 9d0758b..f10ef87 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -57,6 +57,10 @@ class EntityManager { scene.addChild(node) } } + if let sliderNode = entity.component(ofType: SliderComponent.self)?.sliderNode { + scene.addChild(sliderNode.sliderKnob) + scene.addChild(sliderNode.sliderLine) + } } func remove(_ entity: GKEntity) { @@ -72,9 +76,26 @@ class EntityManager { modal.body.removeFromParent() modal.footer.removeFromParent() } + if let sliderNode = entity.component(ofType: SliderComponent.self)?.sliderNode { + sliderNode.sliderKnob.removeFromParent() + sliderNode.sliderLine.removeFromParent() + } entities.remove(entity) } + func update(_ entities: [GKEntity]){ + for entity in entities { + self.entities.update(with: entity) + let base = (entity as! Base) + + if base.changeOwnerShip { + base.addComponent(TeamComponent(team: (entities[0] as! Base).component(ofType: TeamComponent.self)!.team, position: (base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)!)) + base.changeOwnerShip = false + scene.addChild(base.component(ofType: TeamComponent.self)!.fire) + } + } + + } func getBaseByTeam(for team: Team) -> GKEntity? { for entity in entities { if let teamComponent = entity.component(ofType: TeamComponent.self), @@ -91,6 +112,19 @@ class EntityManager { return entities.filter{$0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.team == team } as! Set } + func getTeamByBase(base: Base) -> Team? { + for entity in entities { + if entity is Base && entity == base{ + for component in entity.components{ + if component is TeamComponent { + return entity.component(ofType: TeamComponent.self)!.team + } + } + } + } + return nil + } + func getBackground() -> GKEntity? { return entities.filter{$0 is Background}[0] } @@ -102,4 +136,6 @@ class EntityManager { 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 3ef701c..cdafa53 100644 --- a/GoldWars/GoldWars/Entities/Modal.swift +++ b/GoldWars/GoldWars/Entities/Modal.swift @@ -9,10 +9,12 @@ import GameplayKit class Modal: GKEntity{ + + var unitCount:Int init(modaltype: ModalType, base: Base, anchorPoint: CGPoint) { + unitCount = base.unitCount super.init() - switch modaltype{ case .BaseDetails: addComponent(ModalBackgroundComponent(anchorPoint: anchorPoint)) @@ -20,8 +22,13 @@ class Modal: GKEntity{ body: "Diese Basis enthält \(base.unitCount) Einheiten", footer: "", anchorPoint: anchorPoint)) - default: - break + case .BaseAttack: + addComponent(ModalBackgroundComponent(anchorPoint: anchorPoint)) + addComponent(SliderComponent(width: 300, position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 80))) + addComponent(ModalContentComponent(header: "Angriff", + body: "Schicke \(unitCount / 2) Einheiten", + footer: "", + anchorPoint: anchorPoint)) } } diff --git a/GoldWars/GoldWars/Partikels/Fire.sks b/GoldWars/GoldWars/Partikels/Fire.sks index 839f2cae8f73829f35c01d7837f32551fe61ec66..f25e78f0519eda7cf34d37d2aaf74a2bb8d5574a 100644 GIT binary patch delta 2304 zcmZ8h3s_Xg6+Y+g-aB*WF1U&&+yQ|T!_kJ^H=0E5B zGjqH#c`iK2cjSvehn``Gqk`WD2AP|4}J^n@O$_J{1H0fPtXa+;RKw7 zf54Y;4X(ot=z&}CEqsrN63VC`fl8!k$5@QRhj1t+;|O$N7LLJjI0^G{8hY?C^kE4u z!o^sMWw-<_m00z&zjX%6#G3O&D~AfGgeoyAgQcSPhw;d~7?umP4C+J+>C2!2RzX0F z7lBvw#jsk8%3y7H9MXrZo)PUQut8XCgy&$B;BE|So`)c878XAjvw0GfwZ}<`GSc8A zl3oy#@URuapB;EF4m>I$_oE2FNf?L|AaHxQhlJe!^c?0w&m9SG-L+GMEAYgK1y2w% zBiKbA)OrhlNzCGT-{5sJ5H|+xbZ(#lUle)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4oy3)VAKkiB4-A24N z1x@|=#cf2>(8K7L;cjEZ81ogN=_nWWHll1a1IDO?C$tfGC^QK2Ltpu=#mgA>Mq8}gtK7J{GsUrX5E^emUi*||yFJkG}j zxfi*O+#B2h?lZ2F`;NQG6F!ko8~-`q&EF6NVW1EvxP%;G zuHY3ag%!ff!Zu-#@QH9z_+ID}W5h&pbV!^e&J~{!tHf2}7O`3UKs+k`UHn%3Q5q!O zCpjgz^sqErS|n9TE2QV8KT5AjZ%VDwVd<3gwRBPH#c_Bj9*G?|6+eKd;AuD)&%s{2 z1efDV{3~3ISK?Lpw|F&PgTr_O-iSBh&3Fs0#r1eA-iEj1&~E$}ydS@hTk%Ku06vHh z;lubS{uCd_r|}oK6L;fp@OSt;zKAd3%lIn3iEj}g9Kj@pB#`?_G8sdhB#TTWGe|yp zoD`F#q=JM9B`eAA$t$Fu>>xWy3)w^7AqU7&a*TXRj+4`*o17!P@&h4xnw%#W$i?z9 zxl&#wzaYOX?~?b)N95D;S^2ztLB1$ok}u0wrMDZC;pu}Y$1 zQ${Ew6^HVW;#MXq)0G0HSn(-~l_!;;vRYZAyr67SHY;0{MrD_>S9wo4q#RfNrJPqm zjZ#A~YOJcMhH6t&REL_XrmGohmO4(&Rp+b4YMHu9-K}=2U#eZ|MfHl>tM;ik)mz$h z&7&>U{8~T@YR_q_wM|;R)~GdUuWN5e2c@JxPC559&2~NMEnF=Nzt?}zuPo7f^y~U, with event: UIEvent?) { } @@ -79,6 +94,17 @@ class GameScene: SKScene{ } let touchLocation = touch.location(in: self) + for child in children { + if atPoint(touchLocation) == child { + child.touchesMoved(touches, with: event) + } + } + + for e in entityManager.entities{ + if let body = e.component(ofType: ModalContentComponent.self)?.body{ + body.text = "Schicke \( ((e.component(ofType: SliderComponent.self)?.sliderNode.getValue ?? 0) * CGFloat((e as! Modal).unitCount)).rounded(.up)) Einheiten " + } } + let bases = entityManager.getBasesByTeam(for: .team1) for base in bases { @@ -86,7 +112,6 @@ class GameScene: SKScene{ if !isMoveTouch { currentDraggedBasePos = base.component(ofType: DefaultBaseComponent.self)!.spriteNode.position currentDraggedBase = base - } isMoveTouch = true @@ -101,11 +126,6 @@ class GameScene: SKScene{ } } } - - - - - } override func update(_ currentTime: TimeInterval) {