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,
position: position,
onButtonPress: {
var bases: Set<Base> = EntityManager.sharedInstance.getBasesByTeam(for: .team2)
var bases: Set<Base> = EntityManager.gameEMInstance.getBasesByTeam(for: .team2)
bases.forEach{ base in
if(base.unitType == .Defence){
base.unitType = .AttackDefence

View File

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

View File

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

View File

@ -43,6 +43,7 @@ class DataService {
var snapshotModel: SnapshotModel?
var gameHost: Host?
var mapModel: MapGenerationModel?
var entityManager = EntityManager.gameEMInstance
func addMove(playerMove: PlayerMove) {
var equalMove = localPlayerMoves.filter { (ele) -> Bool in
@ -73,6 +74,6 @@ class DataService {
func setMapModel(model: MapGenerationModel) {
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 {
static let sharedInstance = EntityManager()
static let gameEMInstance = EntityManager()
static let menuEMInstance = EntityManager()
static let settingsEMInstance = EntityManager()
var entities = Set<GKEntity>()
var scene: SKScene

View File

@ -10,6 +10,7 @@ import GameplayKit
class Modal: GKEntity{
var entityManager = EntityManager.gameEMInstance
var unitCount:Int
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){
for entity in EntityManager.sharedInstance.entities {
if EntityManager.sharedInstance.isModal && entity.isMember(of: Modal.self) {
EntityManager.sharedInstance.remove(entity)
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"){
@ -57,7 +58,7 @@ class Modal: GKEntity{
func sendUnits(currentDraggedBase: Base?, touchLocation: CGPoint, gameScene: GameScene, collisionBase: Base?){
for base in currentDraggedBase!.adjacencyList {
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
}
}

View File

@ -14,24 +14,24 @@ class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
let scene = MenuScene(size: self.view.bounds.size)
EntityManager.menuEMInstance.setScene(scene: scene)
scene.scaleMode = .aspectFill
view.presentScene(scene)
//TODO: create dev profile or remove on delivery
view.showsFPS = true
view.showsNodeCount = true
GameCenterHelper.helper.viewController = self
MatchmakingHelper.sharedInstance.viewController = self
GameCenterHelper.helper.viewController = self
MatchmakingHelper.sharedInstance.viewController = self
}
}
}
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return .allButUpsideDown
@ -39,7 +39,7 @@ class GameViewController: UIViewController {
return .all
}
}
override var prefersStatusBarHidden: Bool {
return true
}

View File

@ -37,6 +37,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
var nameSpieler1 = ""
var menusc: MenuScene?
let localPlayer: GKLocalPlayer = GKLocalPlayer.local
var entityManager = EntityManager.gameEMInstance
static let sharedInstance = MatchmakingHelper()
@ -119,8 +120,8 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) {
DataService.sharedInstance.snapshotModel = snapshotModel
EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: snapshotModel)
EntityManager.sharedInstance.getTimer().startWithDuration(duration: 31)
entityManager.updateSnapshotModel(snapshotModel: snapshotModel)
entityManager.getTimer().startWithDuration(duration: 31)
}
if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) {
@ -182,7 +183,9 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
}
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()
})
}

View File

@ -11,20 +11,20 @@ import Foundation
import GameKit
class MultiplayerNetwork{
static let sharedInstance = MultiplayerNetwork()
static let sharedInstance = MultiplayerNetwork()
var isSending = false
func sendData(data: Data) {
let mmHelper = MatchmakingHelper.sharedInstance
if let multiplayerMatch = mmHelper.mpMatch {
do {
try multiplayerMatch.sendData(toAllPlayers: data, with: .reliable)
} catch {
//TODO: Add logging
}
}
}
func sendData(data: Data) {
let mmHelper = MatchmakingHelper.sharedInstance
if let multiplayerMatch = mmHelper.mpMatch {
do {
try multiplayerMatch.sendData(toAllPlayers: data, with: .reliable)
} catch {
//TODO: Add logging
}
}
}
func sendDataToHost(data: Data) {
let mmHelper = MatchmakingHelper.sharedInstance
@ -38,7 +38,7 @@ class MultiplayerNetwork{
}
}
}
func sendPlayerMoves(playerMoves: [PlayerMove]) {
if MatchmakingHelper.sharedInstance.isServer == false {
self.isSending = true
@ -47,7 +47,7 @@ class MultiplayerNetwork{
sendDataToHost(data: encoded)
DataService.sharedInstance.localPlayerMoves.removeAll()
}
}
}
func sendHostIdentifier() {
let encoder = JSONEncoder()
@ -66,5 +66,5 @@ class MultiplayerNetwork{
let encoded = (try? encoder.encode(mapModel))!
sendData(data: encoded)
}
}

View File

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

View File

@ -12,26 +12,28 @@ import GameKit
class GameScene: SKScene{
var entityManager = EntityManager.gameEMInstance
var isMoveTouch = false
var currentDraggedBase : Base?
static var sendUnits: CGFloat = 0
var collisionBase: Base?
override func sceneDidLoad() {
EntityManager.sharedInstance.setScene(scene: self)
EntityManager.sharedInstance.add(HUD(size: self.size))
EntityManager.sharedInstance.add(Background(size: self.size))
entityManager.setScene(scene: self)
entityManager.add(HUD(size: self.size))
entityManager.add(Background(size: self.size))
initMap()
}
func initMap() {
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)
DataService.sharedInstance.setSnapshotModel(snapshotModel: EntityManager.sharedInstance.getSnapshotModel())
DataService.sharedInstance.setSnapshotModel(snapshotModel: entityManager.getSnapshotModel())
}
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)
}
else {
for entity in EntityManager.sharedInstance.entities {
for entity in entityManager.entities {
let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode
//FIXME: this is confusing
@ -73,18 +75,18 @@ class GameScene: SKScene{
}
checkSlider()
let bases = EntityManager.sharedInstance.getBasesByPlayer(for: GKLocalPlayer.local)
let bases = entityManager.getBasesByPlayer(for: GKLocalPlayer.local)
checkBases(bases: bases, touchLocation: touchLocation)
}
override func update(_ currentTime: TimeInterval) {
EntityManager.sharedInstance.getBackground()?.update(deltaTime: currentTime)
EntityManager.sharedInstance.getHUD()?.component(ofType: TimerComponent.self)?.update()
entityManager.getBackground()?.update(deltaTime: currentTime)
entityManager.getHUD()?.component(ofType: TimerComponent.self)?.update()
}
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)
if let baseEntity = entity as? Base {
if baseEntity.ownershipPlayer == GKLocalPlayer.local {
@ -93,11 +95,11 @@ class GameScene: SKScene{
child.alpha = 0.3
}
}
EntityManager.sharedInstance.add(Modal(modaltype: .BaseDetails,
base: entity as! Base,
anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2),
gameScene: self,
currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, collisionBase: collisionBase))
entityManager.add(Modal(modaltype: .BaseDetails,
base: entity as! Base,
anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2),
gameScene: self,
currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, collisionBase: collisionBase))
}
}
@ -110,10 +112,10 @@ class GameScene: SKScene{
collisionBase = base
// TODO: change interaction based on collision instead of touchlocation
if currentDraggedBase!.unitCount > 1 {
EntityManager.sharedInstance.add(Modal(modaltype: .BaseAttack,
base: currentDraggedBase!,
anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2),
gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, collisionBase: collisionBase))
entityManager.add(Modal(modaltype: .BaseAttack,
base: currentDraggedBase!,
anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2),
gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, collisionBase: collisionBase))
GameScene.sendUnits = CGFloat(currentDraggedBase!.unitCount / 2)
}
}
@ -121,7 +123,7 @@ class GameScene: SKScene{
}
func checkSlider(){
for e in EntityManager.sharedInstance.entities{
for e in entityManager.entities{
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)

View File

@ -9,12 +9,15 @@
import SpriteKit
import SceneKit
class MenuScene: SKScene {
var entityManager = EntityManager.menuEMInstance
override func sceneDidLoad() {
EntityManager.sharedInstance.setScene(scene: self)
entityManager.setScene(scene: self)
let midX = self.size.width / 2
let midY = self.size.height / 2
EntityManager.sharedInstance.add(Button(name: "startGameButton",
entityManager.add(Button(name: "startGameButton",
iconName: "",
text: "Start Game",
position: CGPoint(x: midX, y: midY),
@ -26,15 +29,16 @@ class MenuScene: SKScene {
MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self)
}
}))
EntityManager.sharedInstance.add(Button(name: "settingsButton",
entityManager.add(Button(name: "settingsButton",
iconName: "",
text: "Settings",
position: CGPoint(x: midX, y: midY - 80 ),
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.sharedInstance.add(SpinningLogoEntity(position: CGPoint(x: midX, y: midY + 200)))
entityManager.add(Background(size: self.size))
entityManager.add(SpinningLogoEntity(position: CGPoint(x: midX, y: midY + 200)))
if SoundManager.sharedInstance.isMusicPlaying == false && SoundManager.sharedInstance.isMusicEnabled == true {
SoundManager.sharedInstance.startMenuMusic()
@ -43,11 +47,14 @@ class MenuScene: SKScene {
func loadScene(scene: SKScene) {
let transition = SKTransition.flipVertical(withDuration: 0.5)
entityManager.entities.removeAll()
self.view?.presentScene(scene, transition: transition)
}
override func update(_ currentTime: TimeInterval) {
EntityManager.sharedInstance.getBackground()!.update(deltaTime: currentTime)
EntityManager.sharedInstance.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated
if entityManager.entities.count != 0 {
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 {
var entityManager = EntityManager.settingsEMInstance
override func sceneDidLoad() {
EntityManager.sharedInstance.setScene(scene: self)
entityManager.setScene(scene: self)
let positionX = self.size.width * 0.1
let positionY = self.size.height * 0.05
EntityManager.sharedInstance.add(Button(name: "backToMenuScene",
print("SceneDidLoad Setting")
entityManager.add(Button(name: "backToMenuScene",
iconName: "",
text: "Back",
position: CGPoint(x: positionX, y: positionY),
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: "",
text: "ON/OFF",
position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2),
@ -35,7 +38,7 @@ class SettingsScene: SKScene {
SoundManager.sharedInstance.startMenuMusic()
}
}))
EntityManager.sharedInstance.add(Button(name: "StopMovingBackground",
entityManager.add(Button(name: "StopMovingBackground",
iconName: "",
text: "MOVE/STOP",
position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2 - 100),
@ -46,7 +49,7 @@ class SettingsScene: SKScene {
BackgroundComponent.isMovingBackgroundEnabled = true
}
}))
EntityManager.sharedInstance.add(Label(fontnamed: "Courier-Bold",
entityManager.add(Label(fontnamed: "Courier-Bold",
name: "SettingsLabel",
text: "Settings",
fontSize: 200.0,
@ -57,7 +60,7 @@ class SettingsScene: SKScene {
isAnimationEnabled: true,
isAnimationInfinite: true)
)
EntityManager.sharedInstance.add(Label(fontnamed: "Courier-Bold",
entityManager.add(Label(fontnamed: "Courier-Bold",
name: "LabelMusic",
text: "Music", fontSize: 50.0,
fontColor: .black,
@ -67,7 +70,7 @@ class SettingsScene: SKScene {
isAnimationEnabled: true,
isAnimationInfinite: false)
)
EntityManager.sharedInstance.add(Label(fontnamed: "Courier-Bold",
entityManager.add(Label(fontnamed: "Courier-Bold",
name: "LabelBackground",
text: "Background",
fontSize: 50.0,
@ -78,16 +81,19 @@ class SettingsScene: SKScene {
isAnimationEnabled: true,
isAnimationInfinite: false)
)
EntityManager.sharedInstance.add(Background(size: self.size))
entityManager.add(Background(size: self.size))
}
func loadScene(scene: SKScene) {
let transition = SKTransition.flipVertical(withDuration: 0.5)
entityManager.entities.removeAll()
self.view?.presentScene(scene, transition: transition)
}
override func update(_ currentTime: TimeInterval) {
EntityManager.sharedInstance.getBackground()!.update(deltaTime: currentTime)
if entityManager.entities.count != 0 {
entityManager.getBackground()!.update(deltaTime: currentTime)
}
}
}