Merge branch '60-global-accessible-entitymanager' into 'development'

Resolve "Global accessible EntityManager"

Closes #60

See merge request marcel.schwarz/software-projekt-2!71
This commit is contained in:
Aldin Duraki 2020-05-18 17:58:59 +00:00
commit 8d4c136f6b
5 changed files with 47 additions and 44 deletions

View File

@ -12,15 +12,22 @@ import GameKit
class EntityManager { class EntityManager {
static let sharedInstance = EntityManager()
var entities = Set<GKEntity>() var entities = Set<GKEntity>()
let scene: SKScene var scene: SKScene
var isModal: Bool var isModal: Bool
init(scene: SKScene) { private init() {
self.scene = scene
isModal = false isModal = false
scene = SKScene.init()
} }
func setScene(scene: SKScene){
self.scene = scene
}
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 {

View File

@ -12,20 +12,19 @@ import GameKit
class GameScene: SKScene{ class GameScene: SKScene{
var entityManager: EntityManager!
var isMoveTouch = false var isMoveTouch = false
var currentDraggedBasePos = CGPoint() var currentDraggedBasePos = CGPoint()
var currentDraggedBase : Base? var currentDraggedBase : Base?
override func sceneDidLoad() { override func sceneDidLoad() {
entityManager = EntityManager(scene: self) EntityManager.sharedInstance.setScene(scene: self)
entityManager.add(HUD(size: self.size)) EntityManager.sharedInstance.add(HUD(size: self.size))
entityManager.add(Background(size: self.size)) EntityManager.sharedInstance.add(Background(size: self.size))
initMap() initMap()
} }
func initMap() { func initMap() {
MapFactory(scene: self, entityManager: self.entityManager).loadMap(playerCount: 2) MapFactory(scene: self, entityManager: EntityManager.sharedInstance).loadMap(playerCount: 2)
} }
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
@ -44,38 +43,38 @@ class GameScene: SKScene{
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.sharedInstance.getTeamByBase(base: currentDraggedBase!) == EntityManager.sharedInstance.getTeamByBase(base: base)){
entityManager.add(Modal(modaltype: .BaseAttack, EntityManager.sharedInstance.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.sharedInstance, gameScene: self))
entityManager.update((currentDraggedBase?.attackBase(base: base, units: 100))!) EntityManager.sharedInstance.update((currentDraggedBase?.attackBase(base: base, units: 100))!)
}else { }else {
entityManager.add(Modal(modaltype: .BaseAttack, EntityManager.sharedInstance.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.sharedInstance, gameScene: self))
} }
} }
} }
} }
else { else {
for entity in entityManager.entities { for entity in EntityManager.sharedInstance.entities {
let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode
if atPoint(touchLocation) == spriteNode && !entityManager.isModal { if atPoint(touchLocation) == spriteNode && !EntityManager.sharedInstance.isModal {
spriteNode?.touchesBegan(touches, with: event) spriteNode?.touchesBegan(touches, with: event)
if !entityManager.isModal { if !EntityManager.sharedInstance.isModal {
for child in self.children { for child in self.children {
if(child.name != "fire"){ if(child.name != "fire"){
child.alpha = 0.3 child.alpha = 0.3
} }
} }
entityManager.add(Modal(modaltype: .BaseDetails, EntityManager.sharedInstance.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.sharedInstance, gameScene: self))
} }
} }
} }
@ -95,12 +94,12 @@ class GameScene: SKScene{
} }
} }
for e in entityManager.entities{ for e in EntityManager.sharedInstance.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 " body.text = "Schicke \( ((e.component(ofType: SliderComponent.self)?.sliderNode.getValue ?? 0) * CGFloat((e as! Modal).unitCount)).rounded(.up)) Einheiten "
} } } }
let bases = entityManager.getBasesByPlayer(for: GKLocalPlayer.local) let bases = EntityManager.sharedInstance.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{
@ -124,7 +123,7 @@ class GameScene: SKScene{
} }
override func update(_ currentTime: TimeInterval) { override func update(_ currentTime: TimeInterval) {
entityManager.getBackground()?.update(deltaTime: currentTime) EntityManager.sharedInstance.getBackground()?.update(deltaTime: currentTime)
entityManager.getHUD()?.component(ofType: TimerComponent.self)?.update() EntityManager.sharedInstance.getHUD()?.component(ofType: TimerComponent.self)?.update()
} }
} }

View File

@ -10,13 +10,11 @@ import SpriteKit
import SceneKit import SceneKit
class MenuScene: SKScene { class MenuScene: SKScene {
var entityManager: EntityManager!
override func sceneDidLoad() { override func sceneDidLoad() {
entityManager = EntityManager(scene: self) EntityManager.sharedInstance.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.add(Button(name: "startGameButton", EntityManager.sharedInstance.add(Button(name: "startGameButton",
iconName: "", iconName: "",
text: "Start Game", text: "Start Game",
position: CGPoint(x: midX, y: midY), position: CGPoint(x: midX, y: midY),
@ -28,15 +26,15 @@ class MenuScene: SKScene {
MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self) MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self)
} }
})) }))
entityManager.add(Button(name: "settingsButton", EntityManager.sharedInstance.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)) self.loadScene(scene: SettingsScene(size: self.size))
})) }))
entityManager.add(Background(size: self.size)) EntityManager.sharedInstance.add(Background(size: self.size))
entityManager.add(SpinningLogoEntity(position: CGPoint(x: midX, y: midY + 200))) EntityManager.sharedInstance.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()
@ -49,7 +47,7 @@ class MenuScene: SKScene {
} }
override func update(_ currentTime: TimeInterval) { override func update(_ currentTime: TimeInterval) {
entityManager.getBackground()!.update(deltaTime: currentTime) EntityManager.sharedInstance.getBackground()!.update(deltaTime: currentTime)
entityManager.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated EntityManager.sharedInstance.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated
} }
} }

View File

@ -9,21 +9,20 @@
import SpriteKit import SpriteKit
class SettingsScene: SKScene { class SettingsScene: SKScene {
var entityManager: EntityManager!
override func sceneDidLoad() { override func sceneDidLoad() {
entityManager = EntityManager(scene: self) EntityManager.sharedInstance.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
entityManager.add(Button(name: "backToMenuScene", EntityManager.sharedInstance.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)) self.loadScene(scene: MenuScene(size: self.size))
})) }))
entityManager.add(Button(name: "StopMenuMusic", EntityManager.sharedInstance.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),
@ -36,7 +35,7 @@ class SettingsScene: SKScene {
SoundManager.sharedInstance.startMenuMusic() SoundManager.sharedInstance.startMenuMusic()
} }
})) }))
entityManager.add(Button(name: "StopMovingBackground", EntityManager.sharedInstance.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),
@ -47,7 +46,7 @@ class SettingsScene: SKScene {
BackgroundComponent.isMovingBackgroundEnabled = true BackgroundComponent.isMovingBackgroundEnabled = true
} }
})) }))
entityManager.add(Label(fontnamed: "Courier-Bold", EntityManager.sharedInstance.add(Label(fontnamed: "Courier-Bold",
name: "SettingsLabel", name: "SettingsLabel",
text: "Settings", text: "Settings",
fontSize: 200.0, fontSize: 200.0,
@ -58,7 +57,7 @@ class SettingsScene: SKScene {
isAnimationEnabled: true, isAnimationEnabled: true,
isAnimationInfinite: true) isAnimationInfinite: true)
) )
entityManager.add(Label(fontnamed: "Courier-Bold", EntityManager.sharedInstance.add(Label(fontnamed: "Courier-Bold",
name: "LabelMusic", name: "LabelMusic",
text: "Music", fontSize: 50.0, text: "Music", fontSize: 50.0,
fontColor: .black, fontColor: .black,
@ -68,7 +67,7 @@ class SettingsScene: SKScene {
isAnimationEnabled: true, isAnimationEnabled: true,
isAnimationInfinite: false) isAnimationInfinite: false)
) )
entityManager.add(Label(fontnamed: "Courier-Bold", EntityManager.sharedInstance.add(Label(fontnamed: "Courier-Bold",
name: "LabelBackground", name: "LabelBackground",
text: "Background", text: "Background",
fontSize: 50.0, fontSize: 50.0,
@ -79,7 +78,7 @@ class SettingsScene: SKScene {
isAnimationEnabled: true, isAnimationEnabled: true,
isAnimationInfinite: false) isAnimationInfinite: false)
) )
entityManager.add(Background(size: self.size)) EntityManager.sharedInstance.add(Background(size: self.size))
} }
func loadScene(scene: SKScene) { func loadScene(scene: SKScene) {
@ -88,7 +87,7 @@ class SettingsScene: SKScene {
} }
override func update(_ currentTime: TimeInterval) { override func update(_ currentTime: TimeInterval) {
entityManager.getBackground()!.update(deltaTime: currentTime) EntityManager.sharedInstance.getBackground()!.update(deltaTime: currentTime)
} }
} }