Skip to content

Commit

Permalink
Add enemy shot.
Browse files Browse the repository at this point in the history
Add collision between enemy projectile and player.
  • Loading branch information
ArtCC committed Apr 12, 2023
1 parent 38d5e17 commit 6d47e8c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "img_enemy_shot.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 14 additions & 2 deletions space-war/Scenes/GameScene+Collisions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import SpriteKit

extension GameScene {

func projectileDidCollideWithEnemy(projectile: SKSpriteNode, enemy: SKSpriteNode) {
func projectileDidCollideWithEnemy(_ projectile: SKSpriteNode, _ enemy: SKSpriteNode) {
if enemy.name == GameSceneNodes.asteroid.rawValue {
createDefaultExplosion(in: enemy.position)
} else if enemy.name == GameSceneNodes.enemy.rawValue {
Expand All @@ -34,7 +34,7 @@ extension GameScene {
}
}

func playerDidCollideWithEnemy(player: SKSpriteNode, enemy: SKSpriteNode) {
func playerDidCollideWithEnemy(_ player: SKSpriteNode, _ enemy: SKSpriteNode) {
createPlayerExplosion(in: player.position) {
let reveal = SKTransition.crossFade(withDuration: 0.5)
let gameOverScene = GameOverScene(size: self.size, won: false)
Expand All @@ -45,4 +45,16 @@ extension GameScene {
player.removeFromParent()
enemy.removeFromParent()
}

func enemyProjectileDidCollideWithEnemy(_ enemyProjectile: SKSpriteNode, _ player: SKSpriteNode) {
createPlayerExplosion(in: enemyProjectile.position) {
let reveal = SKTransition.crossFade(withDuration: 0.5)
let gameOverScene = GameOverScene(size: self.size, won: false)

self.view?.presentScene(gameOverScene, transition: reveal)
}

enemyProjectile.removeFromParent()
player.removeFromParent()
}
}
58 changes: 47 additions & 11 deletions space-war/Scenes/GameScene+Create.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ extension GameScene {
player.physicsBody?.isDynamic = true
player.physicsBody?.categoryBitMask = PhysicsCategory.player
player.physicsBody?.contactTestBitMask = PhysicsCategory.enemy
player.physicsBody?.collisionBitMask = PhysicsCategory.enemy
player.physicsBody?.collisionBitMask = PhysicsCategory.all
player.physicsBody?.usesPreciseCollisionDetection = true

if let physicsBody = player.physicsBody {
Expand Down Expand Up @@ -98,15 +98,14 @@ extension GameScene {

let angle = player.zRotation - CGFloat.pi * 2
let direction = CGVector(dx: cos(angle), dy: sin(angle))
let bulletSpeed: CGFloat = 500.0
let bulletDistance: CGFloat = 1000.0
let bulletMove = SKAction.move(by: CGVector(dx: direction.dx * bulletDistance,
dy: direction.dy * bulletDistance),
duration: bulletDistance / bulletSpeed)
let bulletRemove = SKAction.removeFromParent()
let bulletAction = SKAction.sequence([bulletMove, bulletRemove])
let speed: CGFloat = 500.0
let distance: CGFloat = 750.0
let move = SKAction.move(by: CGVector(dx: direction.dx * distance, dy: direction.dy * distance),
duration: distance / speed)
let remove = SKAction.removeFromParent()
let action = SKAction.sequence([move, remove])

projectile.run(bulletAction)
projectile.run(action)

run(SKAction.playSoundFileNamed("short-laser-gun-shot.wav", waitForCompletion: false))
}
Expand Down Expand Up @@ -242,12 +241,49 @@ extension GameScene {

addChild(enemy)

let actualDuration = random(min: CGFloat(2.0), max: CGFloat(4.0))
let actualDuration = random(min: CGFloat(1.0), max: CGFloat(3.5))
let actionMove = SKAction.move(to: CGPoint(x: -enemy.size.width / 2, y: actualY),
duration: TimeInterval(actualDuration))
let actionMoveDone = SKAction.removeFromParent()

enemy.run(SKAction.sequence([actionMove, actionMoveDone]))

createEnemyShot(for: enemy)

let shotDelay = SKAction.wait(forDuration: 1.0)
let shotAction = SKAction.run {
self.createEnemyShot(for: enemy)
}
let shotSequence = SKAction.sequence([shotDelay, shotAction])
let shotForever = SKAction.repeatForever(shotSequence)

enemy.run(shotForever)
}

func createEnemyShot(for enemy: SKSpriteNode) {
let projectile = SKSpriteNode(imageNamed: "img_enemy_shot")
projectile.position = CGPoint(x: enemy.position.x - 55.0, y: enemy.position.y)
projectile.zPosition = 1.0
projectile.setScale(1.5)
projectile.name = GameSceneNodes.enemyProjectile.rawValue
projectile.physicsBody = SKPhysicsBody(rectangleOf: projectile.size)
projectile.physicsBody?.categoryBitMask = PhysicsCategory.enemyProjectile
projectile.physicsBody?.contactTestBitMask = PhysicsCategory.player
projectile.physicsBody?.collisionBitMask = PhysicsCategory.none
projectile.physicsBody?.usesPreciseCollisionDetection = true

addChild(projectile)

let direction = CGVector(dx: -1.0, dy: 0.0)
let speed: CGFloat = 500.0
let distance: CGFloat = 750.0
let action = SKAction.move(by: CGVector(dx: direction.dx * distance, dy: direction.dy * distance),
duration: distance / speed)
let remove = SKAction.removeFromParent()

projectile.run(SKAction.sequence([action, remove]))

run(SKAction.playSoundFileNamed("short-laser-gun-shot.wav", waitForCompletion: false))
}

func createTurboEnemyShip(for enemy: SKSpriteNode) {
Expand Down Expand Up @@ -322,7 +358,7 @@ extension GameScene {
let repeatRotateAction = SKAction.repeatForever(rotateAction)
asteroid.run(repeatRotateAction)

let actualDuration = random(min: CGFloat(1.0), max: CGFloat(3.0))
let actualDuration = random(min: CGFloat(1.0), max: CGFloat(2.5))
let actionMove = SKAction.move(to: CGPoint(x: -asteroid.size.width / 2, y: actualY),
duration: TimeInterval(actualDuration))
let actionMoveDone = SKAction.removeFromParent()
Expand Down
10 changes: 8 additions & 2 deletions space-war/Scenes/GameScene+Delegates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@ extension GameScene: SKPhysicsContactDelegate {
(secondBody.categoryBitMask == PhysicsCategory.projectile && PhysicsCategory.projectile != 0) {
if let enemy = firstBody.node as? SKSpriteNode,
let projectile = secondBody.node as? SKSpriteNode {
projectileDidCollideWithEnemy(projectile: projectile, enemy: enemy)
projectileDidCollideWithEnemy(projectile, enemy)
}
} else if (firstBody.categoryBitMask == PhysicsCategory.enemy && PhysicsCategory.enemy != 0) &&
(secondBody.categoryBitMask == PhysicsCategory.player && PhysicsCategory.player != 0) {
if let enemy = firstBody.node as? SKSpriteNode,
let player = secondBody.node as? SKSpriteNode {
playerDidCollideWithEnemy(player: player, enemy: enemy)
playerDidCollideWithEnemy(player, enemy)
}
} else if (firstBody.categoryBitMask == PhysicsCategory.player && PhysicsCategory.player != 0) &&
(secondBody.categoryBitMask == PhysicsCategory.enemyProjectile && PhysicsCategory.enemyProjectile != 0) {
if let enemyProjectile = firstBody.node as? SKSpriteNode,
let player = secondBody.node as? SKSpriteNode {
enemyProjectileDidCollideWithEnemy(enemyProjectile, player)
}
}
}
Expand Down

0 comments on commit 6d47e8c

Please sign in to comment.