From d2f32f41ee7fd18f730db9077b6250e57e6f1d24 Mon Sep 17 00:00:00 2001 From: 127-Z3R0 <81heti1bif@hft-stuttgart.de> Date: Wed, 13 May 2020 19:13:33 +0200 Subject: [PATCH] Impl. PlayerMovesService for data transfer --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 4 +++ .../GoldWars/Components/TimerComponent.swift | 1 + GoldWars/GoldWars/Entities/Base.swift | 27 +++++++++---------- GoldWars/GoldWars/MessageProtocol.swift | 9 ++----- GoldWars/GoldWars/PlayerMovesService.swift | 23 ++++++++++++++++ 5 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 GoldWars/GoldWars/PlayerMovesService.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index 0ff7f49..206c98b 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -53,6 +53,7 @@ C064E9A8246C0EA50022B228 /* LabelNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = C064E9A7246C0EA50022B228 /* LabelNode.swift */; }; C064E9AA246C114C0022B228 /* LabelComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = C064E9A9246C114C0022B228 /* LabelComponent.swift */; }; C064E9AC246C151F0022B228 /* Label.swift in Sources */ = {isa = PBXBuildFile; fileRef = C064E9AB246C151F0022B228 /* Label.swift */; }; + C099579C246C5E5C0016AA22 /* PlayerMovesService.swift in Sources */ = {isa = PBXBuildFile; fileRef = C099579B246C5E5C0016AA22 /* PlayerMovesService.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -117,6 +118,7 @@ C064E9A7246C0EA50022B228 /* LabelNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelNode.swift; sourceTree = ""; }; C064E9A9246C114C0022B228 /* LabelComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelComponent.swift; sourceTree = ""; }; C064E9AB246C151F0022B228 /* Label.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Label.swift; sourceTree = ""; }; + C099579B246C5E5C0016AA22 /* PlayerMovesService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerMovesService.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -174,6 +176,7 @@ 110360DA244B101A008610AF /* GameViewController.swift */, 110360DF244B101B008610AF /* Assets.xcassets */, 9EC239E0246878A900952F74 /* MessageProtocol.swift */, + C099579B246C5E5C0016AA22 /* PlayerMovesService.swift */, 110360E4244B101B008610AF /* Info.plist */, AE151588245F18EF001D363E /* MatchmakingHelper.swift */, 3EBD242B245D8044003CECE7 /* GameCenterHelper.swift */, @@ -405,6 +408,7 @@ 9E174C86245DD91500209FF0 /* ButtonComponent.swift in Sources */, AE151589245F18EF001D363E /* MatchmakingHelper.swift in Sources */, 11036113244B3E30008610AF /* MenuScene.swift in Sources */, + C099579C246C5E5C0016AA22 /* PlayerMovesService.swift in Sources */, 9EA3ABE9245C6DAA006BC61D /* DefaultBaseComponent.swift in Sources */, 9E174C8A245E1A0A00209FF0 /* Background.swift in Sources */, 9EA3ABED245C8143006BC61D /* ModalBackgroundComponent.swift in Sources */, diff --git a/GoldWars/GoldWars/Components/TimerComponent.swift b/GoldWars/GoldWars/Components/TimerComponent.swift index 816ca04..42d27f1 100644 --- a/GoldWars/GoldWars/Components/TimerComponent.swift +++ b/GoldWars/GoldWars/Components/TimerComponent.swift @@ -45,6 +45,7 @@ class TimerComponent: GKComponent { if(isFinished()){ self.labelNode.text = "Synching" + MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: PlayerMovesService.sharedInstance.playerMoves) } } diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index 250521d..596721e 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -10,18 +10,18 @@ import SpriteKit import GameplayKit class Base: GKEntity{ - static var BASE_ID_COUNT: Int = 0 + static var BASE_ID_COUNT: Int = 0 var unitCount: Int var adjacencyList: Array - var changeOwnerShip: Bool - var baseID: Int + var changeOwnerShip: Bool + var baseID: Int init(position: CGPoint, team: Team! = nil) { self.unitCount = 0 self.adjacencyList = [Base]() - self.changeOwnerShip = false - baseID = Base.BASE_ID_COUNT - Base.BASE_ID_COUNT += 1 + self.changeOwnerShip = false + baseID = Base.BASE_ID_COUNT + Base.BASE_ID_COUNT += 1 super.init() addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: "Base"), position: position)) @@ -31,14 +31,13 @@ class Base: GKEntity{ } } - func attackBase(base: Base, units: Int) -> [GKEntity]{ - base.changeOwnerShip = true - self.unitCount -= units - base.unitCount += units - MultiplayerNetwork.sharedInstance.sendPlayerMove(playerMove: PlayerMove(fromBase: self.baseID, toBase: base.baseID, unitCount: units)) - return [self, base] - - } + func attackBase(base: Base, units: Int) -> [GKEntity]{ + base.changeOwnerShip = true + self.unitCount -= units + base.unitCount += units + PlayerMovesService.sharedInstance.addMove(playerMove: PlayerMove(fromBase: self.baseID, toBase: base.baseID, unitCount: units)) + return [self, base] + } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") diff --git a/GoldWars/GoldWars/MessageProtocol.swift b/GoldWars/GoldWars/MessageProtocol.swift index 5038135..133cf43 100644 --- a/GoldWars/GoldWars/MessageProtocol.swift +++ b/GoldWars/GoldWars/MessageProtocol.swift @@ -10,12 +10,7 @@ import GameplayKit import Foundation -struct PlayerMove : Codable{ - let fromBase: Int - let toBase: Int - let unitCount: Int -} class MultiplayerNetwork{ static let sharedInstance = MultiplayerNetwork() @@ -31,9 +26,9 @@ class MultiplayerNetwork{ } } - func sendPlayerMove(playerMove:PlayerMove) { + func sendPlayerMoves(playerMoves: [PlayerMove]) { let encoder = JSONEncoder() - let encoded = (try? encoder.encode(playerMove))! + let encoded = (try? encoder.encode(playerMoves))! sendData(data: encoded) } diff --git a/GoldWars/GoldWars/PlayerMovesService.swift b/GoldWars/GoldWars/PlayerMovesService.swift new file mode 100644 index 0000000..3dd8de2 --- /dev/null +++ b/GoldWars/GoldWars/PlayerMovesService.swift @@ -0,0 +1,23 @@ +// +// PlayerMovesService.swift +// GoldWars +// +// Created by Tim Herbst on 13.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +struct PlayerMove : Codable{ + let fromBase: Int + let toBase: Int + let unitCount: Int +} + +class PlayerMovesService { + static let sharedInstance = PlayerMovesService() + var playerMoves: [PlayerMove] = [] + + + func addMove(playerMove: PlayerMove) { + self.playerMoves.append(playerMove) + } +}