Merge branch '8-controller-laufwege' into 'development'

Resolve "[Controller] Laufwege"

Closes #8

See merge request marcel.schwarz/software-projekt-2!10
This commit is contained in:
Aldin Duraki 2020-04-22 14:29:55 +00:00
commit dd07cf9ad8
6 changed files with 219 additions and 66 deletions

1
.gitignore vendored
View File

@ -49,6 +49,7 @@ DerivedData/
*.perspectivev3
!default.perspectivev3
xcuserdata/
xcshareddata/
## Other
*.moved-aside

View File

@ -9,21 +9,34 @@
import SpriteKit
import GameplayKit
class Base {
var spriteNode: BaseNode!
class Base : SKSpriteNode{
public static let colorSelected = SKColor.yellow
var availableBases = [Base]()
var defaultColor = SKColor.green
init(frame: CGRect, color: UIColor, position: CGPoint) {
spriteNode = BaseNode(texture: SKTexture(imageNamed: "Base"), color: color, size: CGSize(width: 50.0, height: 50.0))
spriteNode.colorBlendFactor = 1
spriteNode.name = "Base"
spriteNode.position = position
spriteNode.isUserInteractionEnabled = true
}
init(color: UIColor, position: CGPoint, name: String) {
super.init(texture: SKTexture(imageNamed: "Base"),
color: color,
size: CGSize(width: 50.0, height: 50.0)
)
defaultColor = color
self.colorBlendFactor = 1
self.name = name
self.position = position
self.zPosition = 2
self.isUserInteractionEnabled = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addAvailableBase(base: Base) {
availableBases.append(base)
}
class BaseNode: SKSpriteNode {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("\(self.name!) was touched and is located at \(self.position)")
self.run(
SKAction.sequence(
[
@ -34,13 +47,22 @@ class BaseNode: SKSpriteNode {
)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
self.color = SKColor.yellow
for base in availableBases {
base.color = Base.colorSelected
base.size = CGSize(width: 60, height: 60)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
self.color = defaultColor
for base in availableBases {
base.color = base.defaultColor
base.size = CGSize(width: 50, height: 50)
}
}
enum PlayerColors {
static let colors = [
UIColor(red: 231/255, green: 76/255, blue: 60/255, alpha: 1.0),
UIColor(red: 241/255, green: 196/255, blue: 15/255, alpha: 1.0),
UIColor(red: 46/255, green: 204/255, blue: 113/255, alpha: 1.0),
UIColor(red: 52/255, green: 152/255, blue: 219/255, alpha: 1.0),
]
}

View File

@ -9,63 +9,195 @@
import SpriteKit
import GameplayKit
class GameScene: SKScene {
class GameScene: SKScene, SKPhysicsContactDelegate {
var entities = [GKEntity]()
var graphs = [String : GKGraph]()
var bases = [Base]()
var player = [Base]()
var popUpOnBaseCollision = SKSpriteNode()
struct physicsBodyNumber {
static let basePlayer1Number: UInt32 = 0b1
static let basePlayer2Number: UInt32 = 0b10
static let base5Number: UInt32 = 0b101
static let base6Number: UInt32 = 0b110
static let base7Number: UInt32 = 0b111
static let emptyNumber: UInt32 = 0b100
}
override func sceneDidLoad() {
// Spawn bases
for _ in 1...15 {
let color = PlayerColors.colors.randomElement()
let xRange = Int(frame.minX + frame.width * 0.07)...Int(frame.maxX - frame.width * 0.07)
let yRange = Int(frame.minY + frame.height * 0.07)...Int(frame.maxY - frame.width * 0.07)
let position = CGPoint(
x: Int.random(in: xRange),
y: Int.random(in: yRange)
)
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
self.physicsWorld.contactDelegate = self
print("Generating Base at \(position)")
let maxX = self.size.width
let midY = self.size.height / 2
let base = Base(frame: frame, color: color!, position: position)
let basePlayer1 = Base(color: SKColor.red, position: CGPoint(x: maxX * 0.1, y: midY), name: "Player1" )
let basePlayer2 = Base(color: SKColor.blue, position: CGPoint(x: maxX * 0.9, y: midY), name: "Player2" )
basePlayer2.physicsBody = SKPhysicsBody(circleOfRadius: 50)
basePlayer2.physicsBody?.categoryBitMask = physicsBodyNumber.basePlayer2Number
basePlayer2.physicsBody?.collisionBitMask = physicsBodyNumber.emptyNumber
basePlayer2.physicsBody?.contactTestBitMask = physicsBodyNumber.base5Number & physicsBodyNumber.base6Number & physicsBodyNumber.base7Number
addChild(base.spriteNode)
popUpOnBaseCollision = SKSpriteNode(color: SKColor.blue , size: CGSize(width: self.size.width * 0.4, height: self.size.height * 0.4))
popUpOnBaseCollision.position = CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.5)
popUpOnBaseCollision.zPosition = 2
self.addChild(basePlayer1)
self.addChild(basePlayer2)
player.append(basePlayer1)
player.append(basePlayer2)
createVirginBases()
connectBases()
addPhysicsBodyToBase()
}
func addPhysicsBodyToBase() {
bases[5].physicsBody = SKPhysicsBody(circleOfRadius: 20)
bases[5].physicsBody?.categoryBitMask = physicsBodyNumber.base5Number
bases[5].physicsBody?.collisionBitMask = physicsBodyNumber.emptyNumber
bases[5].physicsBody?.contactTestBitMask = physicsBodyNumber.basePlayer2Number
bases[6].physicsBody = SKPhysicsBody(circleOfRadius: 20)
bases[6].physicsBody?.categoryBitMask = physicsBodyNumber.base6Number
bases[6].physicsBody?.collisionBitMask = physicsBodyNumber.emptyNumber
bases[6].physicsBody?.contactTestBitMask = physicsBodyNumber.basePlayer2Number
bases[7].physicsBody = SKPhysicsBody(circleOfRadius: 20)
bases[7].physicsBody?.categoryBitMask = physicsBodyNumber.base7Number
bases[7].physicsBody?.collisionBitMask = physicsBodyNumber.emptyNumber
bases[7].physicsBody?.contactTestBitMask = physicsBodyNumber.basePlayer2Number
}
func createVirginBases() {
for i in 0...7 {
let base:Base
let color = SKColor.green
var position = CGPoint(x: 0, y: 0)
switch i {
case 0...2:
let width = self.size.width * 0.25
if i == 0 {
position = CGPoint(x: width, y: self.size.height * 0.25)
}
if i == 1 {
position = CGPoint(x: width, y: self.size.height * 0.5)
}
if i == 2{
position = CGPoint(x: width, y: self.size.height * 0.75)
}
case 3...4:
let width = self.size.width * 0.5
if i == 3{
position = CGPoint(x: width, y: self.size.height * 0.333)
}
if i == 4{
position = CGPoint(x: width, y: self.size.height * 0.666)
}
case 5...7:
let width = self.size.width * 0.75
if i == 5{
position = CGPoint(x: width, y: self.size.height * 0.25)
}
if i == 6{
position = CGPoint(x: width, y: self.size.height * 0.5)
}
if i == 7{
position = CGPoint(x: width, y: self.size.height * 0.75)
}
default:
break
}
base = Base(color: color, position: position, name: "Base\(i)")
bases.append(base)
self.addChild(base)
}
}
func addLine(base1: Base , base2: Base){
let line = SKShapeNode()
let linePath = CGMutablePath()
linePath.move(to: base1.position)
linePath.addLine(to: base2.position)
line.path = linePath
line.strokeColor = SKColor.white
addChild(line)
}
func touchDown(atPoint pos : CGPoint) {
func connectBases(){
player[0].addAvailableBase(base: bases[0])
player[0].addAvailableBase(base: bases[1])
player[0].addAvailableBase(base: bases[2])
bases[0].addAvailableBase(base: bases[3])
bases[1].addAvailableBase(base: bases[3])
bases[1].addAvailableBase(base: bases[4])
bases[2].addAvailableBase(base: bases[4])
bases[3].addAvailableBase(base: bases[5])
bases[3].addAvailableBase(base: bases[6])
bases[4].addAvailableBase(base: bases[7])
bases[4].addAvailableBase(base: bases[6])
player[1].addAvailableBase(base: bases[5])
player[1].addAvailableBase(base: bases[6])
player[1].addAvailableBase(base: bases[7])
for base in player{
for availableBase in base.availableBases{
addLine(base1: base, base2: availableBase)
}
}
func touchMoved(toPoint pos : CGPoint) {
for base in bases{
for availableBase in base.availableBases{
addLine(base1: base, base2: availableBase)
}
}
func touchUp(atPoint pos : CGPoint) {
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let locationUser = touch.location(in: self)
if atPoint(locationUser) == popUpOnBaseCollision {
popUpOnBaseCollision.removeFromParent()
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let locationUser = touch.location(in: self)
if atPoint(locationUser) == player[1]{
player[1].position = locationUser
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let maxX = self.size.width
let midY = self.size.height / 2
player[1].position = CGPoint(x: maxX * 0.9, y: midY)
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func update(_ currentTime: TimeInterval) {
func didBegin(_ contact: SKPhysicsContact) {
self.addChild(popUpOnBaseCollision)
}
}

View File

@ -17,7 +17,7 @@ class GameViewController: UIViewController {
if let view = self.view as! SKView? {
if let scene = SKScene(fileNamed: "MenuScene") {
scene.scaleMode = .aspectFill
//scene.scaleMode = .aspectFill
view.presentScene(scene)
//TODO: create dev profile or remove on delivery
view.showsFPS = true

View File

@ -38,8 +38,6 @@
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>

View File

@ -18,7 +18,7 @@ class MenuScene: SKScene {
guard let touch = touches.first else {
return
}
let gameScene = GameScene(fileNamed: "GameScene")!
let gameScene = GameScene(size: self.size)
let location = touch.location(in: self)
let frontTouchedNode = atPoint(location).name