Merge branch '33-basisownership-anhand-des-gkplayers' into 'development'

Resolve "Basisownership anhand des GKPlayers"

Closes #33

See merge request marcel.schwarz/software-projekt-2!68
This commit is contained in:
Aldin Duraki 2020-05-15 10:14:17 +00:00
commit df222bba84
6 changed files with 139 additions and 107 deletions

View File

@ -8,12 +8,14 @@
import SpriteKit import SpriteKit
import GameplayKit import GameplayKit
import GameKit
class TeamComponent: GKComponent { class TeamComponent: GKComponent {
let team: Team let team: Team
let fire: SKEmitterNode let fire: SKEmitterNode
let player: GKPlayer
init(team: Team, position: CGPoint) { init(team: Team, player: GKPlayer, position: CGPoint) {
fire = SKEmitterNode(fileNamed: "Fire")! fire = SKEmitterNode(fileNamed: "Fire")!
fire.zPosition = -1 fire.zPosition = -1
fire.position = position fire.position = position
@ -29,6 +31,7 @@ class TeamComponent: GKComponent {
} }
self.team = team self.team = team
self.player = player
super.init() super.init()
} }

View File

@ -8,32 +8,36 @@
import SpriteKit import SpriteKit
import GameplayKit import GameplayKit
import GameKit
class Base: GKEntity { class Base: GKEntity {
var unitCount: Int var unitCount: Int
var adjacencyList: Array<Base> var adjacencyList: Array<Base>
var changeOwnerShip: Bool var changeOwnership: Bool
var ownershipPlayer: GKPlayer?
init(position: CGPoint, team: Team! = nil) { init(position: CGPoint, player: GKPlayer! = nil, team: Team! = nil) {
self.unitCount = 0 self.unitCount = 0
self.adjacencyList = [Base]() self.adjacencyList = [Base]()
self.changeOwnerShip = false self.changeOwnership = false
self.ownershipPlayer = player
super.init() super.init()
addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: "Base"), position: position)) addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: "Base"), position: position))
if(team != nil){ if(team != nil && player != nil){
addComponent(TeamComponent(team: team!, position: position)) addComponent(TeamComponent(team: team!, player: player!, position: position))
self.unitCount = 500 self.unitCount = 500
} }
} }
func attackBase(base: Base, units:Int) -> [GKEntity]{ func attackBase(base: Base, units:Int) -> [GKEntity]{
base.changeOwnerShip = true base.changeOwnership = true
self.unitCount -= units base.ownershipPlayer = self.ownershipPlayer
base.unitCount += units self.unitCount -= units
return [self, base] base.unitCount += units
} 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

@ -8,46 +8,47 @@
import SpriteKit import SpriteKit
import GameplayKit import GameplayKit
import GameKit
class EntityManager { class EntityManager {
var entities = Set<GKEntity>() var entities = Set<GKEntity>()
let scene: SKScene let scene: SKScene
var isModal: Bool var isModal: Bool
init(scene: SKScene) { init(scene: SKScene) {
self.scene = scene self.scene = scene
isModal = false isModal = false
} }
func add(_ entity: GKEntity) { func add(_ entity: GKEntity) {
entities.insert(entity) entities.insert(entity)
if let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode { if let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode {
scene.addChild(spriteNode) scene.addChild(spriteNode)
} }
if let fire = entity.component(ofType: TeamComponent.self)?.fire{ if let fire = entity.component(ofType: TeamComponent.self)?.fire{
scene.addChild(fire) scene.addChild(fire)
} }
if let spriteNode = entity.component(ofType: ModalBackgroundComponent.self)?.spriteNode { if let spriteNode = entity.component(ofType: ModalBackgroundComponent.self)?.spriteNode {
scene.addChild(spriteNode) scene.addChild(spriteNode)
} }
if let modal = entity.component(ofType: ModalContentComponent.self) { if let modal = entity.component(ofType: ModalContentComponent.self) {
scene.addChild(modal.header) scene.addChild(modal.header)
scene.addChild(modal.body) scene.addChild(modal.body)
scene.addChild(modal.footer) scene.addChild(modal.footer)
} }
if let skill = entity.component(ofType: AtkBoostSkillComponent.self) { if let skill = entity.component(ofType: AtkBoostSkillComponent.self) {
scene.addChild(skill.shapeNode) scene.addChild(skill.shapeNode)
scene.addChild(skill.labelNode) scene.addChild(skill.labelNode)
} }
if let skill = entity.component(ofType: DefBoostSkillComponent.self) { if let skill = entity.component(ofType: DefBoostSkillComponent.self) {
scene.addChild(skill.shapeNode) scene.addChild(skill.shapeNode)
scene.addChild(skill.labelNode) scene.addChild(skill.labelNode)
} }
if let skill = entity.component(ofType: SpySkillComponent.self) { if let skill = entity.component(ofType: SpySkillComponent.self) {
scene.addChild(skill.shapeNode) scene.addChild(skill.shapeNode)
scene.addChild(skill.labelNode) scene.addChild(skill.labelNode)
} }
if let timer = entity.component(ofType: TimerComponent.self) { if let timer = entity.component(ofType: TimerComponent.self) {
scene.addChild(timer.labelNode) scene.addChild(timer.labelNode)
} }
@ -60,10 +61,10 @@ class EntityManager {
scene.addChild(node) scene.addChild(node)
} }
} }
if let sliderNode = entity.component(ofType: SliderComponent.self)?.sliderNode { if let sliderNode = entity.component(ofType: SliderComponent.self)?.sliderNode {
scene.addChild(sliderNode.sliderKnob) scene.addChild(sliderNode.sliderKnob)
scene.addChild(sliderNode.sliderLine) scene.addChild(sliderNode.sliderLine)
} }
if let labelNode = entity.component(ofType: LabelComponent.self)?.labelNode { if let labelNode = entity.component(ofType: LabelComponent.self)?.labelNode {
scene.addChild(labelNode) scene.addChild(labelNode)
} }
@ -73,18 +74,18 @@ class EntityManager {
if let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode { if let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode {
spriteNode.removeFromParent() spriteNode.removeFromParent()
} }
if let spriteNode = entity.component(ofType: ModalBackgroundComponent.self)?.spriteNode { if let spriteNode = entity.component(ofType: ModalBackgroundComponent.self)?.spriteNode {
spriteNode.removeFromParent() spriteNode.removeFromParent()
} }
if let modal = entity.component(ofType: ModalContentComponent.self) { if let modal = entity.component(ofType: ModalContentComponent.self) {
modal.header.removeFromParent() modal.header.removeFromParent()
modal.body.removeFromParent() modal.body.removeFromParent()
modal.footer.removeFromParent() modal.footer.removeFromParent()
} }
if let sliderNode = entity.component(ofType: SliderComponent.self)?.sliderNode { if let sliderNode = entity.component(ofType: SliderComponent.self)?.sliderNode {
sliderNode.sliderKnob.removeFromParent() sliderNode.sliderKnob.removeFromParent()
sliderNode.sliderLine.removeFromParent() sliderNode.sliderLine.removeFromParent()
} }
if let modalButton = entity.component(ofType: ButtonComponent.self) { if let modalButton = entity.component(ofType: ButtonComponent.self) {
modalButton.buttonNode.removeFromParent() modalButton.buttonNode.removeFromParent()
isModal = false isModal = false
@ -92,55 +93,77 @@ class EntityManager {
entities.remove(entity) entities.remove(entity)
} }
func update(_ entities: [GKEntity]){ func update(_ entities: [GKEntity]){
for entity in entities { for entity in entities {
self.entities.update(with: entity) self.entities.update(with: entity)
let base = (entity as! Base) let base = (entity as! Base)
if base.changeOwnerShip { 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.addComponent(TeamComponent(
base.changeOwnerShip = false team: (entities[0] as! Base).component(ofType: TeamComponent.self)!.team,
scene.addChild(base.component(ofType: TeamComponent.self)!.fire) 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 getBaseByTeam(for team: Team) -> GKEntity? {
for entity in entities { func getBaseByPlayer(for player: GKPlayer) -> GKEntity? {
if let teamComponent = entity.component(ofType: TeamComponent.self), for entity in entities {
let _ = entity.component(ofType: DefaultBaseComponent.self) { 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),
let _ = entity.component(ofType: DefaultBaseComponent.self) {
if teamComponent.team == team { if teamComponent.team == team {
return entity return entity
} }
} }
} }
return nil return nil
} }
func getBasesByTeam(for team: Team) -> Set<Base> { func getBasesByTeam(for team: Team) -> Set<Base> {
return entities.filter{$0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.team == team } as! Set<Base> return entities.filter{$0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.team == team } as! Set<Base>
} }
func getTeamByBase(base: Base) -> Team? { func getBasesByPlayer(for player: GKPlayer) -> Set<Base> {
for entity in entities { return entities.filter{$0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.player == player } as! Set<Base>
if entity is Base && entity == base{ }
for component in entity.components{
if component is TeamComponent { func getTeamByBase(base: Base) -> Team? {
return entity.component(ofType: TeamComponent.self)!.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 }
} }
}
}
return nil
}
func getBackground() -> GKEntity? { func getBackground() -> GKEntity? {
return entities.filter{$0 is Background}[0] 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 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 return entities.filter{$0 is Button && ($0 as! Button).name == buttonName }[0] as! Button

View File

@ -8,6 +8,7 @@
import Foundation import Foundation
import SpriteKit import SpriteKit
import GameKit
class TwoPlayerDefaultTestMap: MapProtocol { class TwoPlayerDefaultTestMap: MapProtocol {
@ -24,6 +25,7 @@ class TwoPlayerDefaultTestMap: MapProtocol {
// Create Bases // Create Bases
let basePlayerOne = Base( let basePlayerOne = Base(
position: CGPoint(x: self.size.width * 0.1, y: self.size.height / 2), 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 team: .team1
) )
@ -46,6 +48,7 @@ class TwoPlayerDefaultTestMap: MapProtocol {
let basePlayerTwo = Base( let basePlayerTwo = Base(
position: CGPoint(x: self.size.width * 0.9, y: self.size.height / 2), 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 team: .team2
) )

View File

@ -33,8 +33,6 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
var viewController: UIViewController? var viewController: UIViewController?
var mpMatchStarted: Bool var mpMatchStarted: Bool
var isServer: Bool var isServer: Bool
var spieler1: GKPlayer?
var nameSpieler1 = ""
var menusc: MenuScene? var menusc: MenuScene?
let localPlayer: GKLocalPlayer = GKLocalPlayer.local let localPlayer: GKLocalPlayer = GKLocalPlayer.local
@ -154,9 +152,6 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
if player == GKLocalPlayer.local { if player == GKLocalPlayer.local {
self.isServer = true self.isServer = true
self.spieler1 = player
self.nameSpieler1 = self.spieler1!.displayName
} else { } else {
self.isServer = false self.isServer = false
} }

View File

@ -8,6 +8,7 @@
import SpriteKit import SpriteKit
import GameplayKit import GameplayKit
import GameKit
class GameScene: SKScene{ class GameScene: SKScene{
@ -46,12 +47,14 @@ class GameScene: SKScene{
if !(entityManager.getTeamByBase(base: currentDraggedBase!) == entityManager.getTeamByBase(base: base)){ if !(entityManager.getTeamByBase(base: currentDraggedBase!) == entityManager.getTeamByBase(base: base)){
entityManager.add(Modal(modaltype: .BaseAttack, entityManager.add(Modal(modaltype: .BaseAttack,
base: currentDraggedBase!, base: currentDraggedBase!,
anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), entityManager: entityManager, gameScene: self)) anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2),
entityManager: entityManager, gameScene: self))
entityManager.update((currentDraggedBase?.attackBase(base: base, units: 100))!) entityManager.update((currentDraggedBase?.attackBase(base: base, units: 100))!)
}else { }else {
entityManager.add(Modal(modaltype: .BaseAttack, entityManager.add(Modal(modaltype: .BaseAttack,
base: currentDraggedBase!, base: currentDraggedBase!,
anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), entityManager: entityManager, gameScene: self)) anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2),
entityManager: entityManager, gameScene: self))
} }
} }
@ -71,7 +74,8 @@ class GameScene: SKScene{
} }
entityManager.add(Modal(modaltype: .BaseDetails, entityManager.add(Modal(modaltype: .BaseDetails,
base: entity as! Base, base: entity as! Base,
anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), entityManager: entityManager, gameScene: self)) anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2),
entityManager: entityManager, gameScene: self))
} }
} }
} }
@ -96,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 " 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 { for base in bases {
if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode{ if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode{