From 6127f89e78f81ea42d93bdc2afb0b206f4f0dd55 Mon Sep 17 00:00:00 2001 From: Aldin Duraki <62dual1bif@hft-stuttgart.de> Date: Fri, 1 May 2020 15:28:47 +0200 Subject: [PATCH] impf. EntityManager --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 4 +++ GoldWars/GoldWars/EntityManager.swift | 37 +++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 GoldWars/GoldWars/EntityManager.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index 66c4df2..e7014d4 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -18,6 +18,7 @@ 110360F9244B101B008610AF /* GoldWarsUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 110360F8244B101B008610AF /* GoldWarsUITests.swift */; }; 11036111244B3D6A008610AF /* MenuScene.sks in Resources */ = {isa = PBXBuildFile; fileRef = 11036110244B3D6A008610AF /* MenuScene.sks */; }; 11036113244B3E30008610AF /* MenuScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11036112244B3E30008610AF /* MenuScene.swift */; }; + 116060F7245C57D2004E5A36 /* EntityManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 116060F6245C57D2004E5A36 /* EntityManager.swift */; }; 11738A3B24508F68004426F1 /* Unit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11738A3A24508F68004426F1 /* Unit.swift */; }; ABA03DA0244BD54F00A66916 /* Base.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABA03D9F244BD54F00A66916 /* Base.swift */; }; /* End PBXBuildFile section */ @@ -57,6 +58,7 @@ 110360FA244B101B008610AF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 11036110244B3D6A008610AF /* MenuScene.sks */ = {isa = PBXFileReference; lastKnownFileType = file.sks; path = MenuScene.sks; sourceTree = ""; }; 11036112244B3E30008610AF /* MenuScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuScene.swift; sourceTree = ""; }; + 116060F6245C57D2004E5A36 /* EntityManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntityManager.swift; sourceTree = ""; }; 11738A3A24508F68004426F1 /* Unit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Unit.swift; sourceTree = ""; }; ABA03D9F244BD54F00A66916 /* Base.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -123,6 +125,7 @@ 110360DF244B101B008610AF /* Assets.xcassets */, 110360E1244B101B008610AF /* LaunchScreen.storyboard */, 110360E4244B101B008610AF /* Info.plist */, + 116060F6245C57D2004E5A36 /* EntityManager.swift */, ); path = GoldWars; sourceTree = ""; @@ -293,6 +296,7 @@ buildActionMask = 2147483647; files = ( 110360D9244B101A008610AF /* GameScene.swift in Sources */, + 116060F7245C57D2004E5A36 /* EntityManager.swift in Sources */, 11738A3B24508F68004426F1 /* Unit.swift in Sources */, 11036113244B3E30008610AF /* MenuScene.swift in Sources */, ABA03DA0244BD54F00A66916 /* Base.swift in Sources */, diff --git a/GoldWars/GoldWars/EntityManager.swift b/GoldWars/GoldWars/EntityManager.swift new file mode 100644 index 0000000..cc19b3a --- /dev/null +++ b/GoldWars/GoldWars/EntityManager.swift @@ -0,0 +1,37 @@ +// +// EntityManager.swift +// GoldWars +// +// Created by Aldin Duraki on 01.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import Foundation +import SpriteKit +import GameplayKit + +class EntityManager { + + var entities = Set() + let scene: SKScene + + init(scene: SKScene) { + self.scene = scene + } + + func add(_ entity: GKEntity) { + entities.insert(entity) + + if let spriteNode = entity.component(ofType: SpriteComponent.self)?.node { + scene.addChild(spriteNode) + } + } + + func remove(_ entity: GKEntity) { + if let spriteNode = entity.component(ofType: SpriteComponent.self)?.node { + spriteNode.removeFromParent() + } + + entities.remove(entity) + } +}