implement Background Entity and Component

This commit is contained in:
Niko 2020-05-03 00:14:32 +02:00
parent 94d7be35ff
commit d72c10fa75
6 changed files with 89 additions and 44 deletions

View File

@ -25,6 +25,7 @@
9E174C84245DD8CE00209FF0 /* Button.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E174C83245DD8CE00209FF0 /* Button.swift */; }; 9E174C84245DD8CE00209FF0 /* Button.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E174C83245DD8CE00209FF0 /* Button.swift */; };
9E174C86245DD91500209FF0 /* ButtonComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E174C85245DD91500209FF0 /* ButtonComponent.swift */; }; 9E174C86245DD91500209FF0 /* ButtonComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E174C85245DD91500209FF0 /* ButtonComponent.swift */; };
9E174C88245DF1FF00209FF0 /* BackgroundComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E174C87245DF1FF00209FF0 /* BackgroundComponent.swift */; }; 9E174C88245DF1FF00209FF0 /* BackgroundComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E174C87245DF1FF00209FF0 /* BackgroundComponent.swift */; };
9E174C8A245E1A0A00209FF0 /* Background.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E174C89245E1A0A00209FF0 /* Background.swift */; };
9E78ACB6245C9A5300526FF7 /* GameKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9E78ACB5245C9A5300526FF7 /* GameKit.framework */; }; 9E78ACB6245C9A5300526FF7 /* GameKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9E78ACB5245C9A5300526FF7 /* GameKit.framework */; };
9E78ACB8245CB75B00526FF7 /* TeamComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E78ACB7245CB75B00526FF7 /* TeamComponent.swift */; }; 9E78ACB8245CB75B00526FF7 /* TeamComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E78ACB7245CB75B00526FF7 /* TeamComponent.swift */; };
9E78ACBA245CBDAF00526FF7 /* HUD.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E78ACB9245CBDAF00526FF7 /* HUD.swift */; }; 9E78ACBA245CBDAF00526FF7 /* HUD.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E78ACB9245CBDAF00526FF7 /* HUD.swift */; };
@ -85,6 +86,7 @@
9E174C83245DD8CE00209FF0 /* Button.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Button.swift; sourceTree = "<group>"; }; 9E174C83245DD8CE00209FF0 /* Button.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Button.swift; sourceTree = "<group>"; };
9E174C85245DD91500209FF0 /* ButtonComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonComponent.swift; sourceTree = "<group>"; }; 9E174C85245DD91500209FF0 /* ButtonComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonComponent.swift; sourceTree = "<group>"; };
9E174C87245DF1FF00209FF0 /* BackgroundComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackgroundComponent.swift; sourceTree = "<group>"; }; 9E174C87245DF1FF00209FF0 /* BackgroundComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackgroundComponent.swift; sourceTree = "<group>"; };
9E174C89245E1A0A00209FF0 /* Background.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Background.swift; sourceTree = "<group>"; };
9E78ACB5245C9A5300526FF7 /* GameKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GameKit.framework; path = System/Library/Frameworks/GameKit.framework; sourceTree = SDKROOT; }; 9E78ACB5245C9A5300526FF7 /* GameKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GameKit.framework; path = System/Library/Frameworks/GameKit.framework; sourceTree = SDKROOT; };
9E78ACB7245CB75B00526FF7 /* TeamComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TeamComponent.swift; sourceTree = "<group>"; }; 9E78ACB7245CB75B00526FF7 /* TeamComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TeamComponent.swift; sourceTree = "<group>"; };
9E78ACB9245CBDAF00526FF7 /* HUD.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HUD.swift; sourceTree = "<group>"; }; 9E78ACB9245CBDAF00526FF7 /* HUD.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HUD.swift; sourceTree = "<group>"; };
@ -215,6 +217,7 @@
116060F6245C57D2004E5A36 /* EntityManager.swift */, 116060F6245C57D2004E5A36 /* EntityManager.swift */,
9E78ACB9245CBDAF00526FF7 /* HUD.swift */, 9E78ACB9245CBDAF00526FF7 /* HUD.swift */,
9E174C83245DD8CE00209FF0 /* Button.swift */, 9E174C83245DD8CE00209FF0 /* Button.swift */,
9E174C89245E1A0A00209FF0 /* Background.swift */,
); );
path = Entities; path = Entities;
sourceTree = "<group>"; sourceTree = "<group>";
@ -420,6 +423,7 @@
9E174C86245DD91500209FF0 /* ButtonComponent.swift in Sources */, 9E174C86245DD91500209FF0 /* ButtonComponent.swift in Sources */,
11036113244B3E30008610AF /* MenuScene.swift in Sources */, 11036113244B3E30008610AF /* MenuScene.swift in Sources */,
9EA3ABE9245C6DAA006BC61D /* DefaultBaseComponent.swift in Sources */, 9EA3ABE9245C6DAA006BC61D /* DefaultBaseComponent.swift in Sources */,
9E174C8A245E1A0A00209FF0 /* Background.swift in Sources */,
9EA3ABED245C8143006BC61D /* ModalBackgroundComponent.swift in Sources */, 9EA3ABED245C8143006BC61D /* ModalBackgroundComponent.swift in Sources */,
3EBD242C245D8044003CECE7 /* GameCenterHelper.swift in Sources */, 3EBD242C245D8044003CECE7 /* GameCenterHelper.swift in Sources */,
AB1D759C245DD18100671525 /* MapProtocol.swift in Sources */, AB1D759C245DD18100671525 /* MapProtocol.swift in Sources */,

View File

@ -7,4 +7,39 @@
// //
import Foundation import Foundation
import GameplayKit
class BackgroundComponent: GKComponent{
var nodes = [SKSpriteNode]()
let size: CGSize
init(size: CGSize) {
self.size = size
for i in 0...2 {
let sky = SKSpriteNode(imageNamed: "SkyBackground")
sky.name = "clouds"
sky.zPosition = -1
sky.size = CGSize(width: size.width, height: size.height)
sky.position = CGPoint(x: CGFloat(i) * sky.size.width , y: (size.height / 2))
nodes.append(sky)
}
super.init()
}
func update(){
for node in nodes{
node.position.x -= 2
if node.position.x < -(size.width) {
node.position.x += (size.width) * 3
}
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

View File

@ -0,0 +1,30 @@
//
// Background.swift
// GoldWars
//
// Created by Niko Jochim on 02.05.20.
// Copyright © 2020 SP2. All rights reserved.
//
import Foundation
import GameplayKit
class Background: GKEntity {
init(size: CGSize) {
super.init()
addComponent(BackgroundComponent(size: size))
}
override func update(deltaTime seconds: TimeInterval) {
component(ofType: BackgroundComponent.self)?.update()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

View File

@ -53,6 +53,11 @@ class EntityManager {
if let buttonNode = entity.component(ofType: ButtonComponent.self)?.buttonNode { if let buttonNode = entity.component(ofType: ButtonComponent.self)?.buttonNode {
scene.addChild(buttonNode) scene.addChild(buttonNode)
} }
if let nodes = entity.component(ofType: BackgroundComponent.self)?.nodes {
for node in nodes {
scene.addChild(node)
}
}
} }
@ -73,7 +78,7 @@ class EntityManager {
entities.remove(entity) entities.remove(entity)
} }
func base(for team: Team) -> GKEntity? { func getBaseByTeam(for team: Team) -> GKEntity? {
for entity in entities { for entity in entities {
if let teamComponent = entity.component(ofType: TeamComponent.self), if let teamComponent = entity.component(ofType: TeamComponent.self),
let _ = entity.component(ofType: DefaultBaseComponent.self) { let _ = entity.component(ofType: DefaultBaseComponent.self) {
@ -85,11 +90,15 @@ class EntityManager {
return nil return nil
} }
func baseNode(for team: Team) -> SKSpriteNode?{ func getBackground() -> GKEntity? {
return base(for: team)?.component(ofType: DefaultBaseComponent.self)?.spriteNode return entities.filter{$0 is Background}[0]
}
func getBaseNodeByTeam(for team: Team) -> SKSpriteNode?{
return getBaseByTeam(for: team)?.component(ofType: DefaultBaseComponent.self)?.spriteNode
} }
func getButtonByName(buttonName:String) -> Button{ func getButtonByName(buttonName:String) -> Button{
return entities.filter{($0 as! Button).name == buttonName}[0] as! Button return entities.filter{$0 is Button && ($0 as! Button).name == buttonName }[0] as! Button
} }
} }

View File

@ -16,22 +16,11 @@ class GameScene: SKScene{
override func sceneDidLoad() { override func sceneDidLoad() {
entityManager = EntityManager(scene: self) entityManager = EntityManager(scene: self)
entityManager.add(HUD(size: self.size)) entityManager.add(HUD(size: self.size))
entityManager.add(Background(size: self.size))
initMap() initMap()
createBackground()
} }
func createBackground() {
for i in 0...2 {
let sky = SKSpriteNode(imageNamed: "SkyBackground")
sky.name = "clouds"
sky.zPosition = -1
sky.size = CGSize(width: (self.scene?.size.width)!, height: (self.scene?.size.height)!)
sky.position = CGPoint(x: CGFloat(i) * sky.size.width , y: (self.frame.size.height / 2))
self.addChild(sky)
}
}
func initMap() { func initMap() {
MapFactory(scene: self, entityManager: self.entityManager).loadMap(playerCount: 2) MapFactory(scene: self, entityManager: self.entityManager).loadMap(playerCount: 2)
@ -79,12 +68,6 @@ class GameScene: SKScene{
override func update(_ currentTime: TimeInterval) { override func update(_ currentTime: TimeInterval) {
self.enumerateChildNodes(withName: "clouds", using: ({ entityManager.getBackground()?.update(deltaTime: currentTime)
(node, error) in
node.position.x -= 2
if node.position.x < -(self.scene?.size.width)! {
node.position.x += (self.scene?.size.width)! * 3
}
}))
} }
} }

View File

@ -30,32 +30,16 @@ class MenuScene: SKScene {
onButtonPress: { onButtonPress: {
//TODO: create Settings Scene //TODO: create Settings Scene
})) }))
} entityManager.add(Background(size: self.size))
func backgroundInit(){
for i in 0...2 {
let sky = SKSpriteNode(imageNamed: "SkyBackground")
sky.name = "clouds"
sky.zPosition = -1
sky.size = CGSize(width: self.size.width, height: self.size.height)
sky.position = CGPoint(x: CGFloat(i) * sky.size.width , y: (self.frame.size.height / 2))
self.addChild(sky)
}
} }
func loadScene(scene: SKScene) { func loadScene(scene: SKScene) {
let transition = SKTransition.moveIn(with: .right, duration: 1) let transition = SKTransition.flipVertical(withDuration: 0.5)
self.view?.presentScene(scene, transition: transition) self.view?.presentScene(scene, transition: transition)
} }
override func update(_ currentTime: TimeInterval) { override func update(_ currentTime: TimeInterval) {
self.enumerateChildNodes(withName: "clouds", using: ({ entityManager.getBackground()!.update(deltaTime: currentTime)
(node, error) in
node.position.x -= 2
if node.position.x < -(self.scene?.size.width)! {
node.position.x += (self.scene?.size.width)! * 3
}
}))
entityManager.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated entityManager.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated
} }
} }