Merge branch 'development' into 36-runde-beenden-mit-berechnetem-angriff
# Conflicts: # GoldWars/GoldWars/Entities/Base.swift # GoldWars/GoldWars/Entities/EntityManager.swift # GoldWars/GoldWars/MatchmakingHelper.swift
This commit is contained in:
commit
d949f57a87
@ -8,12 +8,14 @@
|
||||
|
||||
import SpriteKit
|
||||
import GameplayKit
|
||||
import GameKit
|
||||
|
||||
class TeamComponent: GKComponent {
|
||||
let team: Team
|
||||
let fire: SKEmitterNode
|
||||
let player: GKPlayer
|
||||
|
||||
init(team: Team, position: CGPoint) {
|
||||
init(team: Team, player: GKPlayer, position: CGPoint) {
|
||||
fire = SKEmitterNode(fileNamed: "Fire")!
|
||||
fire.zPosition = -1
|
||||
fire.position = position
|
||||
@ -29,6 +31,7 @@ class TeamComponent: GKComponent {
|
||||
}
|
||||
|
||||
self.team = team
|
||||
self.player = player
|
||||
super.init()
|
||||
}
|
||||
|
||||
|
@ -8,29 +8,39 @@
|
||||
|
||||
import SpriteKit
|
||||
import GameplayKit
|
||||
import GameKit
|
||||
|
||||
class Base: GKEntity{
|
||||
static var BASE_ID_COUNT: Int = 0
|
||||
var unitCount: Int
|
||||
var adjacencyList: Array<Base>
|
||||
var changeOwnerShip: Bool
|
||||
var changeOwnership: Bool
|
||||
var ownershipPlayer: GKPlayer?
|
||||
var baseID: Int
|
||||
|
||||
init(position: CGPoint, team: Team! = nil) {
|
||||
init(position: CGPoint, player: GKPlayer! = nil, team: Team! = nil) {
|
||||
self.unitCount = 0
|
||||
self.adjacencyList = [Base]()
|
||||
self.changeOwnerShip = false
|
||||
baseID = Base.BASE_ID_COUNT
|
||||
self.changeOwnership = false
|
||||
self.ownershipPlayer = player
|
||||
Base.BASE_ID_COUNT += 1
|
||||
super.init()
|
||||
|
||||
addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: "Base"), position: position))
|
||||
if(team != nil){
|
||||
addComponent(TeamComponent(team: team!, position: position))
|
||||
if(team != nil && player != nil){
|
||||
addComponent(TeamComponent(team: team!, player: player!, position: position))
|
||||
self.unitCount = 500
|
||||
}
|
||||
}
|
||||
|
||||
func attackBase(base: Base, units:Int) -> [GKEntity]{
|
||||
base.changeOwnership = true
|
||||
base.ownershipPlayer = self.ownershipPlayer
|
||||
self.unitCount -= units
|
||||
base.unitCount += units
|
||||
DataService.sharedInstance.addMove(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
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
import SpriteKit
|
||||
import GameplayKit
|
||||
import GameKit
|
||||
|
||||
class EntityManager {
|
||||
|
||||
@ -30,7 +31,6 @@ class EntityManager {
|
||||
}
|
||||
if let spriteNode = entity.component(ofType: ModalBackgroundComponent.self)?.spriteNode {
|
||||
scene.addChild(spriteNode)
|
||||
isModal = true
|
||||
}
|
||||
if let modal = entity.component(ofType: ModalContentComponent.self) {
|
||||
scene.addChild(modal.header)
|
||||
@ -54,6 +54,7 @@ class EntityManager {
|
||||
}
|
||||
if let buttonNode = entity.component(ofType: ButtonComponent.self)?.buttonNode {
|
||||
scene.addChild(buttonNode)
|
||||
isModal = true
|
||||
}
|
||||
if let nodes = entity.component(ofType: BackgroundComponent.self)?.nodes {
|
||||
for node in nodes {
|
||||
@ -75,7 +76,6 @@ class EntityManager {
|
||||
}
|
||||
if let spriteNode = entity.component(ofType: ModalBackgroundComponent.self)?.spriteNode {
|
||||
spriteNode.removeFromParent()
|
||||
isModal = false
|
||||
}
|
||||
if let modal = entity.component(ofType: ModalContentComponent.self) {
|
||||
modal.header.removeFromParent()
|
||||
@ -86,6 +86,10 @@ class EntityManager {
|
||||
sliderNode.sliderKnob.removeFromParent()
|
||||
sliderNode.sliderLine.removeFromParent()
|
||||
}
|
||||
if let modalButton = entity.component(ofType: ButtonComponent.self) {
|
||||
modalButton.buttonNode.removeFromParent()
|
||||
isModal = false
|
||||
}
|
||||
entities.remove(entity)
|
||||
}
|
||||
|
||||
@ -94,15 +98,32 @@ class EntityManager {
|
||||
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
|
||||
if base.changeOwnership {
|
||||
base.addComponent(TeamComponent(
|
||||
team: (entities[0] as! Base).component(ofType: TeamComponent.self)!.team,
|
||||
player: (entities[0] as! Base).component(ofType: TeamComponent.self)!.player,
|
||||
position: (base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)!
|
||||
)
|
||||
)
|
||||
base.changeOwnership = false
|
||||
scene.addChild(base.component(ofType: TeamComponent.self)!.fire)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func getBaseByPlayer(for player: GKPlayer) -> GKEntity? {
|
||||
for entity in entities {
|
||||
if let teamComponent = entity.component(ofType: TeamComponent.self),
|
||||
let _ = entity.component(ofType: DefaultBaseComponent.self) {
|
||||
if teamComponent.player == player {
|
||||
return entity
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getBaseByTeam(for team: Team) -> GKEntity? {
|
||||
for entity in entities {
|
||||
if let teamComponent = entity.component(ofType: TeamComponent.self),
|
||||
@ -119,6 +140,10 @@ class EntityManager {
|
||||
return entities.filter{$0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.team == team } as! Set<Base>
|
||||
}
|
||||
|
||||
func getBasesByPlayer(for player: GKPlayer) -> Set<Base> {
|
||||
return entities.filter{$0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.player == player } as! Set<Base>
|
||||
}
|
||||
|
||||
func getTeamByBase(base: Base) -> Team? {
|
||||
for entity in entities {
|
||||
if entity is Base && entity == base{
|
||||
@ -132,7 +157,6 @@ class EntityManager {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func getBasebyID(id: Int) -> Base?{
|
||||
for entity in entities {
|
||||
if entity is Base && (entity as! Base).baseID == id {
|
||||
@ -141,6 +165,7 @@ class EntityManager {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getBackground() -> GKEntity? {
|
||||
return entities.filter{$0 is Background}[0]
|
||||
}
|
||||
|
@ -12,23 +12,24 @@ class Modal: GKEntity{
|
||||
|
||||
var unitCount:Int
|
||||
|
||||
init(modaltype: ModalType, base: Base, anchorPoint: CGPoint) {
|
||||
init(modaltype: ModalType, base: Base, anchorPoint: CGPoint, entityManager: EntityManager, gameScene: SKScene) {
|
||||
unitCount = base.unitCount
|
||||
super.init()
|
||||
switch modaltype{
|
||||
case .BaseDetails:
|
||||
addComponent(ModalBackgroundComponent(anchorPoint: anchorPoint))
|
||||
addComponent(ModalContentComponent(header: "Basis Information",
|
||||
body: "Diese Basis enthält \(base.unitCount) Einheiten",
|
||||
footer: "",
|
||||
anchorPoint: anchorPoint))
|
||||
addComponent(ModalContentComponent(header: "Basis Information", body: "Diese Basis enthält \(base.unitCount) Einheiten", footer: "", anchorPoint: anchorPoint))
|
||||
addComponent(ButtonComponent(iconName: "", text: "Zurück", position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 120), isEnabled: true, onButtonPress: {
|
||||
self.removeModalEntities(entityManager: entityManager, gameScene: gameScene)
|
||||
}))
|
||||
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))
|
||||
addComponent(SliderComponent(width: 300, position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 50)))
|
||||
addComponent(ModalContentComponent(header: "Angriff", body: "Schicke \(unitCount / 2) Einheiten",
|
||||
footer: "", anchorPoint: anchorPoint))
|
||||
addComponent(ButtonComponent(iconName: "", text: "Senden", position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 120), isEnabled: true, onButtonPress: {
|
||||
self.removeModalEntities(entityManager: entityManager, gameScene: gameScene)
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,4 +37,16 @@ class Modal: GKEntity{
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
func removeModalEntities(entityManager: EntityManager, gameScene: SKScene){
|
||||
for entity in entityManager.entities {
|
||||
if entityManager.isModal && entity.isMember(of: Modal.self) {
|
||||
entityManager.remove(entity)
|
||||
}
|
||||
for child in gameScene.children {
|
||||
if(child.name != "fire"){
|
||||
child.alpha = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
import Foundation
|
||||
import SpriteKit
|
||||
import GameKit
|
||||
|
||||
class TwoPlayerDefaultTestMap: MapProtocol {
|
||||
|
||||
@ -24,6 +25,7 @@ class TwoPlayerDefaultTestMap: MapProtocol {
|
||||
// Create Bases
|
||||
let basePlayerOne = Base(
|
||||
position: CGPoint(x: self.size.width * 0.1, y: self.size.height / 2),
|
||||
player: (MatchmakingHelper.sharedInstance.isServer) ? GKLocalPlayer.local : MatchmakingHelper.sharedInstance.mpMatch?.players[0],
|
||||
team: .team1
|
||||
)
|
||||
|
||||
@ -46,6 +48,7 @@ class TwoPlayerDefaultTestMap: MapProtocol {
|
||||
|
||||
let basePlayerTwo = Base(
|
||||
position: CGPoint(x: self.size.width * 0.9, y: self.size.height / 2),
|
||||
player: (!MatchmakingHelper.sharedInstance.isServer) ? GKLocalPlayer.local : MatchmakingHelper.sharedInstance.mpMatch?.players[0],
|
||||
team: .team2
|
||||
)
|
||||
|
||||
|
@ -101,13 +101,9 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
Vom match erhaltene Spielerdaten
|
||||
|
||||
*/
|
||||
|
||||
func match(_ match: GKMatch, didReceive data: Data, fromRemotePlayer player: GKPlayer) {
|
||||
if mpMatch != match { return }
|
||||
|
||||
@ -137,7 +133,6 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
|
||||
/*
|
||||
Wird beim ändern des States/Zustands des Spielers aufgerufen udn gibt dessen Zustand zurück.
|
||||
*/
|
||||
|
||||
func match(_ match: GKMatch, player: GKPlayer, didChange state: GKPlayerConnectionState) {
|
||||
if mpMatch != match {
|
||||
return
|
||||
@ -167,18 +162,16 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
|
||||
self.mpMatchStarted = true
|
||||
|
||||
if player == GKLocalPlayer.local {
|
||||
print("ich bin host")
|
||||
self.isServer = true
|
||||
self.spieler1 = player
|
||||
self.nameSpieler1 = self.spieler1!.displayName
|
||||
DataService.sharedInstance.setGameHost(host: Host(playerID: player!.displayName))
|
||||
|
||||
} else {
|
||||
self.isServer = false
|
||||
}
|
||||
|
||||
self.delegate?.matchStarted()
|
||||
self.menusc!.loadScene(scene: GameScene(size: self.menusc!.size))
|
||||
MultiplayerNetwork.sharedInstance.sendHostIdentifier()
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
import SpriteKit
|
||||
import GameplayKit
|
||||
import GameKit
|
||||
|
||||
class GameScene: SKScene{
|
||||
|
||||
@ -46,12 +47,14 @@ class GameScene: SKScene{
|
||||
if !(entityManager.getTeamByBase(base: currentDraggedBase!) == entityManager.getTeamByBase(base: base)){
|
||||
entityManager.add(Modal(modaltype: .BaseAttack,
|
||||
base: currentDraggedBase!,
|
||||
anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2)))
|
||||
anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2),
|
||||
entityManager: entityManager, gameScene: self))
|
||||
entityManager.update((currentDraggedBase?.attackBase(base: base, units: 100))!)
|
||||
}else {
|
||||
entityManager.add(Modal(modaltype: .BaseAttack,
|
||||
base: currentDraggedBase!,
|
||||
anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2)))
|
||||
anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2),
|
||||
entityManager: entityManager, gameScene: self))
|
||||
}
|
||||
|
||||
}
|
||||
@ -61,15 +64,6 @@ class GameScene: SKScene{
|
||||
for entity in entityManager.entities {
|
||||
let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode
|
||||
|
||||
if entityManager.isModal && entity.isMember(of: Modal.self) {
|
||||
entityManager.remove(entity)
|
||||
for child in self.children {
|
||||
if(child.name != "fire"){
|
||||
child.alpha = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if atPoint(touchLocation) == spriteNode && !entityManager.isModal {
|
||||
spriteNode?.touchesBegan(touches, with: event)
|
||||
if !entityManager.isModal {
|
||||
@ -80,7 +74,8 @@ class GameScene: SKScene{
|
||||
}
|
||||
entityManager.add(Modal(modaltype: .BaseDetails,
|
||||
base: entity as! Base,
|
||||
anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2)))
|
||||
anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2),
|
||||
entityManager: entityManager, gameScene: self))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -105,7 +100,7 @@ class GameScene: SKScene{
|
||||
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)
|
||||
let bases = entityManager.getBasesByPlayer(for: GKLocalPlayer.local)
|
||||
|
||||
for base in bases {
|
||||
if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode{
|
||||
|
Loading…
Reference in New Issue
Block a user