Impl. PlayerMovesService for data transfer

This commit is contained in:
127-Z3R0 2020-05-13 19:13:33 +02:00
parent 924a3430ea
commit d2f32f41ee
5 changed files with 43 additions and 21 deletions

View File

@ -53,6 +53,7 @@
C064E9A8246C0EA50022B228 /* LabelNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = C064E9A7246C0EA50022B228 /* LabelNode.swift */; }; C064E9A8246C0EA50022B228 /* LabelNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = C064E9A7246C0EA50022B228 /* LabelNode.swift */; };
C064E9AA246C114C0022B228 /* LabelComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = C064E9A9246C114C0022B228 /* LabelComponent.swift */; }; C064E9AA246C114C0022B228 /* LabelComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = C064E9A9246C114C0022B228 /* LabelComponent.swift */; };
C064E9AC246C151F0022B228 /* Label.swift in Sources */ = {isa = PBXBuildFile; fileRef = C064E9AB246C151F0022B228 /* Label.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 */ /* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */ /* Begin PBXContainerItemProxy section */
@ -117,6 +118,7 @@
C064E9A7246C0EA50022B228 /* LabelNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelNode.swift; sourceTree = "<group>"; }; C064E9A7246C0EA50022B228 /* LabelNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelNode.swift; sourceTree = "<group>"; };
C064E9A9246C114C0022B228 /* LabelComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelComponent.swift; sourceTree = "<group>"; }; C064E9A9246C114C0022B228 /* LabelComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelComponent.swift; sourceTree = "<group>"; };
C064E9AB246C151F0022B228 /* Label.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Label.swift; sourceTree = "<group>"; }; C064E9AB246C151F0022B228 /* Label.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Label.swift; sourceTree = "<group>"; };
C099579B246C5E5C0016AA22 /* PlayerMovesService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerMovesService.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
@ -174,6 +176,7 @@
110360DA244B101A008610AF /* GameViewController.swift */, 110360DA244B101A008610AF /* GameViewController.swift */,
110360DF244B101B008610AF /* Assets.xcassets */, 110360DF244B101B008610AF /* Assets.xcassets */,
9EC239E0246878A900952F74 /* MessageProtocol.swift */, 9EC239E0246878A900952F74 /* MessageProtocol.swift */,
C099579B246C5E5C0016AA22 /* PlayerMovesService.swift */,
110360E4244B101B008610AF /* Info.plist */, 110360E4244B101B008610AF /* Info.plist */,
AE151588245F18EF001D363E /* MatchmakingHelper.swift */, AE151588245F18EF001D363E /* MatchmakingHelper.swift */,
3EBD242B245D8044003CECE7 /* GameCenterHelper.swift */, 3EBD242B245D8044003CECE7 /* GameCenterHelper.swift */,
@ -405,6 +408,7 @@
9E174C86245DD91500209FF0 /* ButtonComponent.swift in Sources */, 9E174C86245DD91500209FF0 /* ButtonComponent.swift in Sources */,
AE151589245F18EF001D363E /* MatchmakingHelper.swift in Sources */, AE151589245F18EF001D363E /* MatchmakingHelper.swift in Sources */,
11036113244B3E30008610AF /* MenuScene.swift in Sources */, 11036113244B3E30008610AF /* MenuScene.swift in Sources */,
C099579C246C5E5C0016AA22 /* PlayerMovesService.swift in Sources */,
9EA3ABE9245C6DAA006BC61D /* DefaultBaseComponent.swift in Sources */, 9EA3ABE9245C6DAA006BC61D /* DefaultBaseComponent.swift in Sources */,
9E174C8A245E1A0A00209FF0 /* Background.swift in Sources */, 9E174C8A245E1A0A00209FF0 /* Background.swift in Sources */,
9EA3ABED245C8143006BC61D /* ModalBackgroundComponent.swift in Sources */, 9EA3ABED245C8143006BC61D /* ModalBackgroundComponent.swift in Sources */,

View File

@ -45,6 +45,7 @@ class TimerComponent: GKComponent {
if(isFinished()){ if(isFinished()){
self.labelNode.text = "Synching" self.labelNode.text = "Synching"
MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: PlayerMovesService.sharedInstance.playerMoves)
} }
} }

View File

@ -10,18 +10,18 @@ import SpriteKit
import GameplayKit import GameplayKit
class Base: GKEntity{ class Base: GKEntity{
static var BASE_ID_COUNT: Int = 0 static var BASE_ID_COUNT: Int = 0
var unitCount: Int var unitCount: Int
var adjacencyList: Array<Base> var adjacencyList: Array<Base>
var changeOwnerShip: Bool var changeOwnerShip: Bool
var baseID: Int var baseID: Int
init(position: CGPoint, team: Team! = nil) { init(position: CGPoint, team: Team! = nil) {
self.unitCount = 0 self.unitCount = 0
self.adjacencyList = [Base]() self.adjacencyList = [Base]()
self.changeOwnerShip = false self.changeOwnerShip = false
baseID = Base.BASE_ID_COUNT baseID = Base.BASE_ID_COUNT
Base.BASE_ID_COUNT += 1 Base.BASE_ID_COUNT += 1
super.init() super.init()
addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: "Base"), position: position)) addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: "Base"), position: position))
@ -31,14 +31,13 @@ class Base: GKEntity{
} }
} }
func attackBase(base: Base, units: Int) -> [GKEntity]{ func attackBase(base: Base, units: Int) -> [GKEntity]{
base.changeOwnerShip = true base.changeOwnerShip = true
self.unitCount -= units self.unitCount -= units
base.unitCount += units base.unitCount += units
MultiplayerNetwork.sharedInstance.sendPlayerMove(playerMove: PlayerMove(fromBase: self.baseID, toBase: base.baseID, unitCount: units)) PlayerMovesService.sharedInstance.addMove(playerMove: PlayerMove(fromBase: self.baseID, toBase: base.baseID, unitCount: units))
return [self, base] return [self, base]
}
}
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")

View File

@ -10,12 +10,7 @@ import GameplayKit
import Foundation import Foundation
struct PlayerMove : Codable{
let fromBase: Int
let toBase: Int
let unitCount: Int
}
class MultiplayerNetwork{ class MultiplayerNetwork{
static let sharedInstance = MultiplayerNetwork() static let sharedInstance = MultiplayerNetwork()
@ -31,9 +26,9 @@ class MultiplayerNetwork{
} }
} }
func sendPlayerMove(playerMove:PlayerMove) { func sendPlayerMoves(playerMoves: [PlayerMove]) {
let encoder = JSONEncoder() let encoder = JSONEncoder()
let encoded = (try? encoder.encode(playerMove))! let encoded = (try? encoder.encode(playerMoves))!
sendData(data: encoded) sendData(data: encoded)
} }

View File

@ -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)
}
}