Merge branch '77-entity-manager-ueberarbeiten' into 'development'

Resolve "Entity-Manager ueberarbeiten"

Closes #77

See merge request marcel.schwarz/software-projekt-2!82
This commit is contained in:
Aldin Duraki 2020-05-23 18:15:43 +00:00
commit bb9582bee9
13 changed files with 102 additions and 78 deletions

View File

@ -18,7 +18,7 @@ class AtkBoostSkillComponent: GKComponent{
isEnabled: isEnabled, isEnabled: isEnabled,
position: position, position: position,
onButtonPress: { onButtonPress: {
var bases: Set<Base> = EntityManager.sharedInstance.getBasesByTeam(for: .team2) var bases: Set<Base> = EntityManager.gameEMInstance.getBasesByTeam(for: .team2)
bases.forEach{ base in bases.forEach{ base in
if(base.unitType == .Defence){ if(base.unitType == .Defence){
base.unitType = .AttackDefence base.unitType = .AttackDefence

View File

@ -18,7 +18,7 @@ class DefBoostSkillComponent: GKComponent{
isEnabled: isEnabled, isEnabled: isEnabled,
position: position, position: position,
onButtonPress: { onButtonPress: {
var bases: Set<Base> = EntityManager.sharedInstance.getBasesByTeam(for: .team2) var bases: Set<Base> = EntityManager.gameEMInstance.getBasesByTeam(for: .team2)
bases.forEach{ base in bases.forEach{ base in
if(base.unitType == .Attack){ if(base.unitType == .Attack){
base.unitType = .AttackDefence base.unitType = .AttackDefence

View File

@ -13,6 +13,7 @@ import GameKit
class PlayerInfoComponent: GKComponent { class PlayerInfoComponent: GKComponent {
var entityManager = EntityManager.gameEMInstance
var hostLabel:SKLabelNode var hostLabel:SKLabelNode
var hostUnitsLabel:SKLabelNode var hostUnitsLabel:SKLabelNode
@ -44,8 +45,8 @@ class PlayerInfoComponent: GKComponent {
} }
func update(){ func update(){
hostUnitsLabel.text = "\(EntityManager.sharedInstance.getUnitSum(by: host!))" hostUnitsLabel.text = "\(entityManager.getUnitSum(by: host!))"
peerUnitsLabel.text = "\(EntityManager.sharedInstance.getUnitSum(by: peer!))" peerUnitsLabel.text = "\(entityManager.getUnitSum(by: peer!))"
} }
func setColor(labelNodes: [SKLabelNode]) -> Void { func setColor(labelNodes: [SKLabelNode]) -> Void {

View File

@ -43,6 +43,7 @@ class DataService {
var snapshotModel: SnapshotModel? var snapshotModel: SnapshotModel?
var gameHost: Host? var gameHost: Host?
var mapModel: MapGenerationModel? var mapModel: MapGenerationModel?
var entityManager = EntityManager.gameEMInstance
func addMove(playerMove: PlayerMove) { func addMove(playerMove: PlayerMove) {
var equalMove = localPlayerMoves.filter { (ele) -> Bool in var equalMove = localPlayerMoves.filter { (ele) -> Bool in
@ -73,6 +74,6 @@ class DataService {
func setMapModel(model: MapGenerationModel) { func setMapModel(model: MapGenerationModel) {
self.mapModel = model self.mapModel = model
MapFactory(scene: EntityManager.sharedInstance.scene, entityManager: EntityManager.sharedInstance).loadMap(fromModel: DataService.sharedInstance.mapModel!) MapFactory(scene: entityManager.scene, entityManager: entityManager).loadMap(fromModel: DataService.sharedInstance.mapModel!)
} }
} }

View File

@ -12,7 +12,9 @@ import GameKit
class EntityManager { class EntityManager {
static let sharedInstance = EntityManager() static let gameEMInstance = EntityManager()
static let menuEMInstance = EntityManager()
static let settingsEMInstance = EntityManager()
var entities = Set<GKEntity>() var entities = Set<GKEntity>()
var scene: SKScene var scene: SKScene

View File

@ -10,6 +10,7 @@ import GameplayKit
class Modal: GKEntity{ class Modal: GKEntity{
var entityManager = EntityManager.gameEMInstance
var unitCount:Int var unitCount:Int
init(modaltype: ModalType, base: Base, anchorPoint: CGPoint, gameScene: GameScene, currentDraggedBase: Base?, touchLocation: CGPoint, collisionBase: Base?) { init(modaltype: ModalType, base: Base, anchorPoint: CGPoint, gameScene: GameScene, currentDraggedBase: Base?, touchLocation: CGPoint, collisionBase: Base?) {
@ -42,9 +43,9 @@ class Modal: GKEntity{
} }
func removeModalEntities(gameScene: SKScene){ func removeModalEntities(gameScene: SKScene){
for entity in EntityManager.sharedInstance.entities { for entity in entityManager.entities {
if EntityManager.sharedInstance.isModal && entity.isMember(of: Modal.self) { if entityManager.isModal && entity.isMember(of: Modal.self) {
EntityManager.sharedInstance.remove(entity) entityManager.remove(entity)
} }
for child in gameScene.children { for child in gameScene.children {
if(child.name != "fire"){ if(child.name != "fire"){
@ -57,7 +58,7 @@ class Modal: GKEntity{
func sendUnits(currentDraggedBase: Base?, touchLocation: CGPoint, gameScene: GameScene, collisionBase: Base?){ func sendUnits(currentDraggedBase: Base?, touchLocation: CGPoint, gameScene: GameScene, collisionBase: Base?){
for base in currentDraggedBase!.adjacencyList { for base in currentDraggedBase!.adjacencyList {
if base == collisionBase { if base == collisionBase {
EntityManager.sharedInstance.update((currentDraggedBase?.doPlayerMoveTypeToBase(base: base, playerMoveType: PlayerMoveType.AtkMove, units: Int(GameScene.sendUnits)))!) entityManager.update((currentDraggedBase?.doPlayerMoveTypeToBase(base: base, playerMoveType: PlayerMoveType.AtkMove, units: Int(GameScene.sendUnits)))!)
GameScene.sendUnits = 0 GameScene.sendUnits = 0
} }
} }

View File

@ -17,12 +17,12 @@ class GameViewController: UIViewController {
if let view = self.view as! SKView? { if let view = self.view as! SKView? {
let scene = MenuScene(size: self.view.bounds.size) let scene = MenuScene(size: self.view.bounds.size)
EntityManager.menuEMInstance.setScene(scene: scene)
scene.scaleMode = .aspectFill scene.scaleMode = .aspectFill
view.presentScene(scene) view.presentScene(scene)
//TODO: create dev profile or remove on delivery //TODO: create dev profile or remove on delivery
view.showsFPS = true view.showsFPS = true
view.showsNodeCount = true view.showsNodeCount = true
GameCenterHelper.helper.viewController = self GameCenterHelper.helper.viewController = self
MatchmakingHelper.sharedInstance.viewController = self MatchmakingHelper.sharedInstance.viewController = self
} }

View File

@ -37,6 +37,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
var nameSpieler1 = "" var nameSpieler1 = ""
var menusc: MenuScene? var menusc: MenuScene?
let localPlayer: GKLocalPlayer = GKLocalPlayer.local let localPlayer: GKLocalPlayer = GKLocalPlayer.local
var entityManager = EntityManager.gameEMInstance
static let sharedInstance = MatchmakingHelper() static let sharedInstance = MatchmakingHelper()
@ -119,8 +120,8 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) { if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) {
DataService.sharedInstance.snapshotModel = snapshotModel DataService.sharedInstance.snapshotModel = snapshotModel
EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: snapshotModel) entityManager.updateSnapshotModel(snapshotModel: snapshotModel)
EntityManager.sharedInstance.getTimer().startWithDuration(duration: 31) entityManager.getTimer().startWithDuration(duration: 31)
} }
if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) { if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) {
@ -182,7 +183,9 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
} }
self.delegate?.matchStarted() self.delegate?.matchStarted()
self.menusc!.loadScene(scene: GameScene(size: self.menusc!.size)) let scene = GameScene(size: self.menusc!.size)
self.entityManager.setScene(scene: scene)
self.menusc!.loadScene(scene: scene)
MultiplayerNetwork.sharedInstance.sendHostIdentifier() MultiplayerNetwork.sharedInstance.sendHostIdentifier()
}) })
} }

View File

@ -15,6 +15,7 @@ class RoundCalculatorService {
var allPlayerMoves: [String: [PlayerMove]] = [:] var allPlayerMoves: [String: [PlayerMove]] = [:]
var baseSpecificMoves: [Int: [String: [PlayerMove]]] = [:] var baseSpecificMoves: [Int: [String: [PlayerMove]]] = [:]
var entityManager = EntityManager.gameEMInstance
var isCalculating = false var isCalculating = false
@ -110,9 +111,9 @@ class RoundCalculatorService {
DataService.sharedInstance.localPlayerMoves.removeAll() DataService.sharedInstance.localPlayerMoves.removeAll()
MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers() MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers()
DataService.sharedInstance.snapshotModel = currentSnapshotModel DataService.sharedInstance.snapshotModel = currentSnapshotModel
EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: currentSnapshotModel!) entityManager.updateSnapshotModel(snapshotModel: currentSnapshotModel!)
sleep(1) sleep(1)
EntityManager.sharedInstance.getTimer().startWithDuration(duration: 31) entityManager.getTimer().startWithDuration(duration: 31)
os_log("Finished calculating Round", log: RoundCalculatorService.LOG, type: .info) os_log("Finished calculating Round", log: RoundCalculatorService.LOG, type: .info)
} }

View File

@ -12,26 +12,28 @@ import GameKit
class GameScene: SKScene{ class GameScene: SKScene{
var entityManager = EntityManager.gameEMInstance
var isMoveTouch = false var isMoveTouch = false
var currentDraggedBase : Base? var currentDraggedBase : Base?
static var sendUnits: CGFloat = 0 static var sendUnits: CGFloat = 0
var collisionBase: Base? var collisionBase: Base?
override func sceneDidLoad() { override func sceneDidLoad() {
EntityManager.sharedInstance.setScene(scene: self) entityManager.setScene(scene: self)
EntityManager.sharedInstance.add(HUD(size: self.size)) entityManager.add(HUD(size: self.size))
EntityManager.sharedInstance.add(Background(size: self.size)) entityManager.add(Background(size: self.size))
initMap() initMap()
} }
func initMap() { func initMap() {
if (DataService.sharedInstance.gameHost?.playerName == GKLocalPlayer.local.displayName) { if (DataService.sharedInstance.gameHost?.playerName == GKLocalPlayer.local.displayName) {
let mapModel = MapFactory(scene: self, entityManager: EntityManager.sharedInstance).loadMap() let mapModel = MapFactory(scene: self, entityManager: entityManager).loadMap()
MultiplayerNetwork.sharedInstance.sendMapModelToPlayers(mapModel: mapModel) MultiplayerNetwork.sharedInstance.sendMapModelToPlayers(mapModel: mapModel)
DataService.sharedInstance.setSnapshotModel(snapshotModel: EntityManager.sharedInstance.getSnapshotModel()) DataService.sharedInstance.setSnapshotModel(snapshotModel: entityManager.getSnapshotModel())
} }
if CommandLine.arguments.contains("--no-matchmaking") { if CommandLine.arguments.contains("--no-matchmaking") {
MapFactory(scene: self, entityManager: EntityManager.sharedInstance).loadMap() _ = MapFactory(scene: self, entityManager: entityManager).loadMap()
} }
} }
@ -50,7 +52,7 @@ class GameScene: SKScene{
addAttackDetails(touchLocation: touchLocation) addAttackDetails(touchLocation: touchLocation)
} }
else { else {
for entity in EntityManager.sharedInstance.entities { for entity in entityManager.entities {
let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode
//FIXME: this is confusing //FIXME: this is confusing
@ -73,18 +75,18 @@ class GameScene: SKScene{
} }
checkSlider() checkSlider()
let bases = EntityManager.sharedInstance.getBasesByPlayer(for: GKLocalPlayer.local) let bases = entityManager.getBasesByPlayer(for: GKLocalPlayer.local)
checkBases(bases: bases, touchLocation: touchLocation) checkBases(bases: bases, touchLocation: touchLocation)
} }
override func update(_ currentTime: TimeInterval) { override func update(_ currentTime: TimeInterval) {
EntityManager.sharedInstance.getBackground()?.update(deltaTime: currentTime) entityManager.getBackground()?.update(deltaTime: currentTime)
EntityManager.sharedInstance.getHUD()?.component(ofType: TimerComponent.self)?.update() entityManager.getHUD()?.component(ofType: TimerComponent.self)?.update()
} }
func addBaseDetails(touchLocation: CGPoint, spriteNode: SKNode?, touches: Set<UITouch>, event: UIEvent?, entity: GKEntity){ func addBaseDetails(touchLocation: CGPoint, spriteNode: SKNode?, touches: Set<UITouch>, event: UIEvent?, entity: GKEntity){
if atPoint(touchLocation) == spriteNode && !EntityManager.sharedInstance.isModal { if atPoint(touchLocation) == spriteNode && !entityManager.isModal {
spriteNode?.touchesBegan(touches, with: event) spriteNode?.touchesBegan(touches, with: event)
if let baseEntity = entity as? Base { if let baseEntity = entity as? Base {
if baseEntity.ownershipPlayer == GKLocalPlayer.local { if baseEntity.ownershipPlayer == GKLocalPlayer.local {
@ -93,7 +95,7 @@ class GameScene: SKScene{
child.alpha = 0.3 child.alpha = 0.3
} }
} }
EntityManager.sharedInstance.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),
gameScene: self, gameScene: self,
@ -110,7 +112,7 @@ class GameScene: SKScene{
collisionBase = base collisionBase = base
// TODO: change interaction based on collision instead of touchlocation // TODO: change interaction based on collision instead of touchlocation
if currentDraggedBase!.unitCount > 1 { if currentDraggedBase!.unitCount > 1 {
EntityManager.sharedInstance.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),
gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, collisionBase: collisionBase)) gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, collisionBase: collisionBase))
@ -121,7 +123,7 @@ class GameScene: SKScene{
} }
func checkSlider(){ func checkSlider(){
for e in EntityManager.sharedInstance.entities{ for e in entityManager.entities{
if let body = e.component(ofType: ModalContentComponent.self)?.body{ if let body = e.component(ofType: ModalContentComponent.self)?.body{
GameScene.sendUnits = ((e.component(ofType: SliderComponent.self)?.sliderNode.getValue ?? 0) * CGFloat((e as! Modal).unitCount)).rounded(.up) GameScene.sendUnits = ((e.component(ofType: SliderComponent.self)?.sliderNode.getValue ?? 0) * CGFloat((e as! Modal).unitCount)).rounded(.up)

View File

@ -10,11 +10,14 @@ import SpriteKit
import SceneKit import SceneKit
class MenuScene: SKScene { class MenuScene: SKScene {
var entityManager = EntityManager.menuEMInstance
override func sceneDidLoad() { override func sceneDidLoad() {
EntityManager.sharedInstance.setScene(scene: self)
entityManager.setScene(scene: self)
let midX = self.size.width / 2 let midX = self.size.width / 2
let midY = self.size.height / 2 let midY = self.size.height / 2
EntityManager.sharedInstance.add(Button(name: "startGameButton", entityManager.add(Button(name: "startGameButton",
iconName: "", iconName: "",
text: "Start Game", text: "Start Game",
position: CGPoint(x: midX, y: midY), position: CGPoint(x: midX, y: midY),
@ -26,15 +29,16 @@ class MenuScene: SKScene {
MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self) MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self)
} }
})) }))
EntityManager.sharedInstance.add(Button(name: "settingsButton", entityManager.add(Button(name: "settingsButton",
iconName: "", iconName: "",
text: "Settings", text: "Settings",
position: CGPoint(x: midX, y: midY - 80 ), position: CGPoint(x: midX, y: midY - 80 ),
onButtonPress: { onButtonPress: {
self.loadScene(scene: SettingsScene(size: self.size)) let scene = SettingsScene(size: self.size)
self.loadScene(scene: scene)
})) }))
EntityManager.sharedInstance.add(Background(size: self.size)) entityManager.add(Background(size: self.size))
EntityManager.sharedInstance.add(SpinningLogoEntity(position: CGPoint(x: midX, y: midY + 200))) entityManager.add(SpinningLogoEntity(position: CGPoint(x: midX, y: midY + 200)))
if SoundManager.sharedInstance.isMusicPlaying == false && SoundManager.sharedInstance.isMusicEnabled == true { if SoundManager.sharedInstance.isMusicPlaying == false && SoundManager.sharedInstance.isMusicEnabled == true {
SoundManager.sharedInstance.startMenuMusic() SoundManager.sharedInstance.startMenuMusic()
@ -43,11 +47,14 @@ class MenuScene: SKScene {
func loadScene(scene: SKScene) { func loadScene(scene: SKScene) {
let transition = SKTransition.flipVertical(withDuration: 0.5) let transition = SKTransition.flipVertical(withDuration: 0.5)
entityManager.entities.removeAll()
self.view?.presentScene(scene, transition: transition) self.view?.presentScene(scene, transition: transition)
} }
override func update(_ currentTime: TimeInterval) { override func update(_ currentTime: TimeInterval) {
EntityManager.sharedInstance.getBackground()!.update(deltaTime: currentTime) if entityManager.entities.count != 0 {
EntityManager.sharedInstance.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated entityManager.getBackground()!.update(deltaTime: currentTime)
entityManager.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated
}
} }
} }

View File

@ -10,19 +10,22 @@ import SpriteKit
class SettingsScene: SKScene { class SettingsScene: SKScene {
var entityManager = EntityManager.settingsEMInstance
override func sceneDidLoad() { override func sceneDidLoad() {
EntityManager.sharedInstance.setScene(scene: self) entityManager.setScene(scene: self)
let positionX = self.size.width * 0.1 let positionX = self.size.width * 0.1
let positionY = self.size.height * 0.05 let positionY = self.size.height * 0.05
print("SceneDidLoad Setting")
EntityManager.sharedInstance.add(Button(name: "backToMenuScene", entityManager.add(Button(name: "backToMenuScene",
iconName: "", iconName: "",
text: "Back", text: "Back",
position: CGPoint(x: positionX, y: positionY), position: CGPoint(x: positionX, y: positionY),
onButtonPress: { onButtonPress: {
self.loadScene(scene: MenuScene(size: self.size)) let scene = MenuScene(size: self.size)
self.loadScene(scene: scene)
})) }))
EntityManager.sharedInstance.add(Button(name: "StopMenuMusic", entityManager.add(Button(name: "StopMenuMusic",
iconName: "", iconName: "",
text: "ON/OFF", text: "ON/OFF",
position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2), position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2),
@ -35,7 +38,7 @@ class SettingsScene: SKScene {
SoundManager.sharedInstance.startMenuMusic() SoundManager.sharedInstance.startMenuMusic()
} }
})) }))
EntityManager.sharedInstance.add(Button(name: "StopMovingBackground", entityManager.add(Button(name: "StopMovingBackground",
iconName: "", iconName: "",
text: "MOVE/STOP", text: "MOVE/STOP",
position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2 - 100), position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2 - 100),
@ -46,7 +49,7 @@ class SettingsScene: SKScene {
BackgroundComponent.isMovingBackgroundEnabled = true BackgroundComponent.isMovingBackgroundEnabled = true
} }
})) }))
EntityManager.sharedInstance.add(Label(fontnamed: "Courier-Bold", entityManager.add(Label(fontnamed: "Courier-Bold",
name: "SettingsLabel", name: "SettingsLabel",
text: "Settings", text: "Settings",
fontSize: 200.0, fontSize: 200.0,
@ -57,7 +60,7 @@ class SettingsScene: SKScene {
isAnimationEnabled: true, isAnimationEnabled: true,
isAnimationInfinite: true) isAnimationInfinite: true)
) )
EntityManager.sharedInstance.add(Label(fontnamed: "Courier-Bold", entityManager.add(Label(fontnamed: "Courier-Bold",
name: "LabelMusic", name: "LabelMusic",
text: "Music", fontSize: 50.0, text: "Music", fontSize: 50.0,
fontColor: .black, fontColor: .black,
@ -67,7 +70,7 @@ class SettingsScene: SKScene {
isAnimationEnabled: true, isAnimationEnabled: true,
isAnimationInfinite: false) isAnimationInfinite: false)
) )
EntityManager.sharedInstance.add(Label(fontnamed: "Courier-Bold", entityManager.add(Label(fontnamed: "Courier-Bold",
name: "LabelBackground", name: "LabelBackground",
text: "Background", text: "Background",
fontSize: 50.0, fontSize: 50.0,
@ -78,16 +81,19 @@ class SettingsScene: SKScene {
isAnimationEnabled: true, isAnimationEnabled: true,
isAnimationInfinite: false) isAnimationInfinite: false)
) )
EntityManager.sharedInstance.add(Background(size: self.size)) entityManager.add(Background(size: self.size))
} }
func loadScene(scene: SKScene) { func loadScene(scene: SKScene) {
let transition = SKTransition.flipVertical(withDuration: 0.5) let transition = SKTransition.flipVertical(withDuration: 0.5)
entityManager.entities.removeAll()
self.view?.presentScene(scene, transition: transition) self.view?.presentScene(scene, transition: transition)
} }
override func update(_ currentTime: TimeInterval) { override func update(_ currentTime: TimeInterval) {
EntityManager.sharedInstance.getBackground()!.update(deltaTime: currentTime) if entityManager.entities.count != 0 {
entityManager.getBackground()!.update(deltaTime: currentTime)
}
} }
} }