Feuer ergreift neue Basis nur, wenn im Modal Einheiten senden gedrückt wurde und gesendete Einheiten kommen an

This commit is contained in:
Chauntalle Schüle 2020-05-16 18:09:41 +02:00
parent 7bebcf69df
commit a7e6cd9f10
2 changed files with 73 additions and 12 deletions

View File

@ -11,8 +11,9 @@ import GameplayKit
class Modal: GKEntity{ class Modal: GKEntity{
var unitCount:Int var unitCount:Int
//var currentDraggedBase: Base?
init(modaltype: ModalType, base: Base, anchorPoint: CGPoint, entityManager: EntityManager, gameScene: SKScene) { init(modaltype: ModalType, base: Base, anchorPoint: CGPoint, entityManager: EntityManager, gameScene: GameScene, currentDraggedBase: Base?, touchLocation: CGPoint) {
unitCount = base.unitCount unitCount = base.unitCount
super.init() super.init()
switch modaltype{ switch modaltype{
@ -28,7 +29,22 @@ class Modal: GKEntity{
addComponent(ModalContentComponent(header: "Angriff", body: "Schicke \(unitCount / 2) Einheiten", addComponent(ModalContentComponent(header: "Angriff", body: "Schicke \(unitCount / 2) Einheiten",
footer: "", anchorPoint: anchorPoint)) footer: "", anchorPoint: anchorPoint))
addComponent(ButtonComponent(iconName: "", text: "Senden", position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 120), isEnabled: true, onButtonPress: { addComponent(ButtonComponent(iconName: "", text: "Senden", position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 120), isEnabled: true, onButtonPress: {
//on Button press: sollen einheiten verrechnet werden,bei einheiten größer 0 feuer, entities removed werden
// self.currentDraggedBase!.component(ofType: TeamComponent.self)?.fire.position = currentDraggedBasePos
for base in currentDraggedBase!.adjacencyList {
print("in here")
if gameScene.atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode {
print("in here 2")
if !(entityManager.getTeamByBase(base: currentDraggedBase!) == entityManager.getTeamByBase(base: base)){
print("sendUnits \(Int(GameScene.sendUnits))")
entityManager.update((currentDraggedBase?.attackBase(base: base, units: Int(GameScene.sendUnits)))!)
// print("in here 3")
// sendUnits = 0
}
}
}
self.removeModalEntities(entityManager: entityManager, gameScene: gameScene) self.removeModalEntities(entityManager: entityManager, gameScene: gameScene)
})) }))
addComponent(CancelBtnComponent(iconName: "", text: "", position: CGPoint(x: anchorPoint.x + 160, y: anchorPoint.y + 140), isEnabled: true, onButtonPress: { addComponent(CancelBtnComponent(iconName: "", text: "", position: CGPoint(x: anchorPoint.x + 160, y: anchorPoint.y + 140), isEnabled: true, onButtonPress: {
self.removeModalEntities(entityManager: entityManager, gameScene: gameScene) self.removeModalEntities(entityManager: entityManager, gameScene: gameScene)

View File

@ -16,6 +16,7 @@ class GameScene: SKScene{
var isMoveTouch = false var isMoveTouch = false
var currentDraggedBasePos = CGPoint() var currentDraggedBasePos = CGPoint()
var currentDraggedBase : Base? var currentDraggedBase : Base?
static var sendUnits: CGFloat = 0
override func sceneDidLoad() { override func sceneDidLoad() {
entityManager = EntityManager(scene: self) entityManager = EntityManager(scene: self)
@ -28,7 +29,11 @@ class GameScene: SKScene{
MapFactory(scene: self, entityManager: self.entityManager).loadMap(playerCount: 2) MapFactory(scene: self, entityManager: self.entityManager).loadMap(playerCount: 2)
} }
/*
Aufgerufen: wenn Prozess beendet: Basis auf eine andere gezogen
*/
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touchesEnded")
guard let touch = touches.first else { guard let touch = touches.first else {
return return
} }
@ -36,31 +41,57 @@ class GameScene: SKScene{
let touchLocation = touch.location(in: self) let touchLocation = touch.location(in: self)
if isMoveTouch{ if isMoveTouch{
/*
Aufgerufen: wenn Prozess beendet: Basis auf eine andere gezogen
sendet 0 Einheiten, weil noch nicht slider betätugt wurde
*/
isMoveTouch = false isMoveTouch = false
currentDraggedBase!.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = currentDraggedBasePos print("isMoveTouch false")
currentDraggedBase!.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = currentDraggedBasePos //currentdraggedbase position gespeichert
currentDraggedBase!.component(ofType: TeamComponent.self)?.fire.position = currentDraggedBasePos currentDraggedBase!.component(ofType: TeamComponent.self)?.fire.position = currentDraggedBasePos
//currentDraggedBase bekommt Feuerposition?: wenn auskommentiert verliert nur die alte Position das Feuer, 2 Feuer sind dann auf der neusten Position
for base in currentDraggedBase!.adjacencyList { for base in currentDraggedBase!.adjacencyList {
//basis wurde berührt
if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode {
// TODO: change interaction based on collision instead of touchlocation // TODO: change interaction based on collision instead of touchlocation
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), anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2),
entityManager: entityManager, gameScene: self)) entityManager: entityManager, gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation))
entityManager.update((currentDraggedBase?.attackBase(base: base, units: 100))!) /* print("if")
print("sendUnits \(Int(self.sendUnits))")
entityManager.update((currentDraggedBase?.attackBase(base: base, units: Int(self.sendUnits)))!)*/
}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), anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2),
entityManager: entityManager, gameScene: self)) entityManager: entityManager, gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation))
print("else")
} }
} }
} }
} }
else { else {
print("else ismoveTouch is false")
/*
for base in currentDraggedBase!.adjacencyList {
print("in here")
if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode {
print("in here 2")
// if !(entityManager.getTeamByBase(base: currentDraggedBase!) == entityManager.getTeamByBase(base: base)){
print("sendUnits \(Int(self.sendUnits))")
entityManager.update((currentDraggedBase?.attackBase(base: base, units: Int(self.sendUnits)))!)
// print("in here 3")
self.sendUnits = 0
// }
}
}*/
//add Base Details
for entity in entityManager.entities { for entity in entityManager.entities {
let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode
@ -75,7 +106,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), anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2),
entityManager: entityManager, gameScene: self)) entityManager: entityManager, gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation))
print("Added Base Details")
} }
} }
} }
@ -83,7 +115,11 @@ class GameScene: SKScene{
} }
/*
Dauernd aufgerufen beim Prozess: Eine Basis auf eine andere zu ziehen.
*/
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touchesMoved")
guard let touch = touches.first else { guard let touch = touches.first else {
return return
} }
@ -94,24 +130,33 @@ class GameScene: SKScene{
child.touchesMoved(touches, with: event) child.touchesMoved(touches, with: event)
} }
} }
//checken des Modals/slider des Modals, anzeigen der ausgewählten Units
for e in entityManager.entities{ for e in entityManager.entities{
if let body = e.component(ofType: ModalContentComponent.self)?.body{ 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 " GameScene.sendUnits = ((e.component(ofType: SliderComponent.self)?.sliderNode.getValue ?? 0) * CGFloat((e as! Modal).unitCount)).rounded(.up)
} } print("slider unit \(GameScene.sendUnits)")
body.text = "Schicke \(GameScene.sendUnits) Einheiten "
}
}
// alle Basen eines Players
let bases = entityManager.getBasesByPlayer(for: GKLocalPlayer.local) let bases = entityManager.getBasesByPlayer(for: GKLocalPlayer.local)
for base in bases { for base in bases {
//wenn die Touchlocation die position der Basis trifft
if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode{ if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode{
if !isMoveTouch { if !isMoveTouch {
print("neue currentdraggedbase + position")
currentDraggedBasePos = base.component(ofType: DefaultBaseComponent.self)!.spriteNode.position currentDraggedBasePos = base.component(ofType: DefaultBaseComponent.self)!.spriteNode.position
currentDraggedBase = base currentDraggedBase = base
} }
isMoveTouch = true isMoveTouch = true
print("isMoveTouch true")
//Feuer & Basis müssen sich mitbewegen können
base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = touchLocation base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = touchLocation
base.component(ofType: TeamComponent.self)?.fire.position = touchLocation base.component(ofType: TeamComponent.self)?.fire.position = touchLocation
// jede basis die in der Nähe ist soll vergrößert werden.
for adjacencyBase in base.adjacencyList { for adjacencyBase in base.adjacencyList {
let node = adjacencyBase.component(ofType: DefaultBaseComponent.self)?.spriteNode let node = adjacencyBase.component(ofType: DefaultBaseComponent.self)?.spriteNode
node?.run(SKAction.sequence([ node?.run(SKAction.sequence([