Skip to content

Commit

Permalink
Add enemy ship.
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtCC committed Apr 12, 2023
1 parent 626e6f8 commit 38d5e17
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 58 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified space-war/Resources/Textures/EnemyTurbo.atlas/turbo_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified space-war/Resources/Textures/EnemyTurbo.atlas/turbo_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified space-war/Resources/Textures/EnemyTurbo.atlas/turbo_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified space-war/Resources/Textures/EnemyTurbo.atlas/turbo_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion space-war/Scenes/GameOverScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GameOverScene: SKScene {
private struct SceneTraits {
// Duration
static let wait: CGFloat = 3
static let animation: CGFloat = 3
static let animation: CGFloat = 0.3

// Size
static let fontSize: CGFloat = 40
Expand Down
154 changes: 100 additions & 54 deletions space-war/Scenes/GameScene+Create.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import SpriteKit

extension GameScene {

// MARK: - Background

func createParallaxBackground() {
let backgroundTexture = SKTexture(imageNamed: "img_background_game")

Expand All @@ -32,6 +34,8 @@ extension GameScene {
}
}

// MARK: - Score

func createScoreLabel() {
scoreLabel.text = String(format: "main.menu.score.title".localized(), String(enemiesDestroyed))
scoreLabel.fontSize = scoreFontSize
Expand All @@ -42,7 +46,9 @@ extension GameScene {
addChild(scoreLabel)
}

func createMenuMusic() {
// MARK: - Music

func createMusicGame() {
let music = SKAudioNode(fileNamed: "space-game.wav")
music.autoplayLooped = true
music.isPositional = false
Expand All @@ -52,6 +58,8 @@ extension GameScene {
music.run(SKAction.play())
}

// MARK: - Player

func createPlayer() {
player.name = GameSceneNodes.player.rawValue
player.zPosition = 1.0
Expand Down Expand Up @@ -142,7 +150,7 @@ extension GameScene {

let firstFrameTexture = normalPlayerFrames[0]
normalPlayer = SKSpriteNode(texture: firstFrameTexture)
normalPlayer.position = CGPoint(x: -55.0, y: 0.0)
normalPlayer.position = CGPoint(x: -player.size.width / 2.0, y: 0.0)

player.addChild(normalPlayer)

Expand Down Expand Up @@ -170,7 +178,7 @@ extension GameScene {

let firstFrameTexture = turboPlayerFrames[0]
turboPlayer = SKSpriteNode(texture: firstFrameTexture)
turboPlayer.position = CGPoint(x: -65.0, y: 0.0)
turboPlayer.position = CGPoint(x: -player.size.width / 2.0, y: 0.0)

player.addChild(turboPlayer)

Expand All @@ -184,85 +192,98 @@ extension GameScene {
turboPlayer.isHidden = true
}

func createPlayerExplosion(in position: CGPoint, completion: @escaping() -> Void) {
let animatedAtlas = SKTextureAtlas(named: "PlayerExplosion")
let numImages = animatedAtlas.textureNames.count

var explosion = SKSpriteNode()
var frames: [SKTexture] = []

for i in 1...numImages {
let textureName = "Ship6_Explosion_\(i)"
frames.append(animatedAtlas.textureNamed(textureName))
}

let firstFrameTexture = frames[0]
explosion = SKSpriteNode(texture: firstFrameTexture)
explosion.position = position

addChild(explosion)

run(SKAction.playSoundFileNamed("player-explosion.wav", waitForCompletion: false))

explosion.run(SKAction.animate(with: frames,
timePerFrame: 0.1,
resize: false,
restore: true)) {
explosion.removeFromParent()

completion()
}
}

// MARK: - Enemies

func createEnemy() {
enemy = SKSpriteNode(imageNamed: "img_enemy")
let enemy = SKSpriteNode(imageNamed: "img_enemy")
enemy.name = GameSceneNodes.enemy.rawValue
enemy.position = CGPoint(x: size.width - 75.0, y: size.height / 2)
enemy.zPosition = 1
enemy.physicsBody = SKPhysicsBody(rectangleOf: enemy.size)

let actualY = random(min: enemy.size.height / 2, max: size.height - enemy.size.height / 2)

enemy.position = CGPoint(x: size.width + enemy.frame.width, y: actualY)
enemy.zPosition = 1.0
enemy.physicsBody = SKPhysicsBody(rectangleOf: enemy.frame.size)
enemy.physicsBody?.categoryBitMask = PhysicsCategory.enemy
enemy.physicsBody?.contactTestBitMask = PhysicsCategory.player
enemy.physicsBody?.collisionBitMask = PhysicsCategory.none
enemy.physicsBody?.usesPreciseCollisionDetection = true

addChild(enemy)
}

func createAsteroid() {
let asteroid = SKSpriteNode(imageNamed: "img_asteroids")
asteroid.name = GameSceneNodes.asteroid.rawValue
asteroid.physicsBody = SKPhysicsBody(rectangleOf: asteroid.size)
asteroid.physicsBody?.isDynamic = true
asteroid.physicsBody?.categoryBitMask = PhysicsCategory.enemy
asteroid.physicsBody?.collisionBitMask = PhysicsCategory.none
asteroid.physicsBody?.usesPreciseCollisionDetection = true

let actualY = random(min: asteroid.size.height / 2, max: size.height - asteroid.size.height / 2)

asteroid.position = CGPoint(x: size.width + asteroid.size.width / 2, y: actualY)
createTurboEnemyShip(for: enemy)

addChild(asteroid)

let rotateAction = SKAction.rotate(byAngle: CGFloat.pi, duration: 2.0)
let repeatRotateAction = SKAction.repeatForever(rotateAction)
asteroid.run(repeatRotateAction)
addChild(enemy)

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

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

func createDefaultExplosion(in position: CGPoint) {
let animatedAtlas = SKTextureAtlas(named: "Explosion")
func createTurboEnemyShip(for enemy: SKSpriteNode) {
let animatedAtlas = SKTextureAtlas(named: "EnemyTurbo")
let numImages = animatedAtlas.textureNames.count

var explosion = SKSpriteNode()
var frames: [SKTexture] = []

for i in 1...numImages {
let textureName = "Explosion1_\(i)"
let textureName = "turbo_\(i)"
frames.append(animatedAtlas.textureNamed(textureName))
}

let firstFrameTexture = frames[0]
explosion = SKSpriteNode(texture: firstFrameTexture)
explosion.position = position
let turboEnemy = SKSpriteNode(texture: firstFrameTexture)
turboEnemy.position = CGPoint(x: (enemy.size.width / 2.0) - 10.0, y: 0.0)

addChild(explosion)
enemy.addChild(turboEnemy)

run(SKAction.playSoundFileNamed("enemy-explosion.wav", waitForCompletion: false))

explosion.run(SKAction.animate(with: frames,
timePerFrame: 0.1,
resize: false,
restore: true)) {
explosion.removeFromParent()
}
turboEnemy.run(SKAction.repeatForever(
SKAction.animate(with: frames,
timePerFrame: 0.1,
resize: false,
restore: true)),
withKey: "enemyTurbo")
}

func createPlayerExplosion(in position: CGPoint, completion: @escaping() -> Void) {
let animatedAtlas = SKTextureAtlas(named: "PlayerExplosion")
func createEnemyExplosion(in position: CGPoint) {
let animatedAtlas = SKTextureAtlas(named: "EnemyExplosion")
let numImages = animatedAtlas.textureNames.count

var explosion = SKSpriteNode()
var frames: [SKTexture] = []

for i in 1...numImages {
let textureName = "Ship6_Explosion_\(i)"
let textureName = "Ship2_Explosion_\(i)"
frames.append(animatedAtlas.textureNamed(textureName))
}

Expand All @@ -272,27 +293,52 @@ extension GameScene {

addChild(explosion)

run(SKAction.playSoundFileNamed("player-explosion.wav", waitForCompletion: false))
run(SKAction.playSoundFileNamed("enemy-explosion.wav", waitForCompletion: false))

explosion.run(SKAction.animate(with: frames,
timePerFrame: 0.1,
resize: false,
restore: true)) {
explosion.removeFromParent()

completion()
}
}

func createEnemyExplosion(in position: CGPoint) {
let animatedAtlas = SKTextureAtlas(named: "EnemyExplosion")
func createAsteroid() {
let asteroid = SKSpriteNode(imageNamed: "img_asteroids")
asteroid.name = GameSceneNodes.asteroid.rawValue
asteroid.physicsBody = SKPhysicsBody(rectangleOf: asteroid.size)
asteroid.physicsBody?.isDynamic = true
asteroid.physicsBody?.categoryBitMask = PhysicsCategory.enemy
asteroid.physicsBody?.collisionBitMask = PhysicsCategory.none
asteroid.physicsBody?.usesPreciseCollisionDetection = true

let actualY = random(min: asteroid.size.height / 2, max: size.height - asteroid.size.height / 2)

asteroid.position = CGPoint(x: size.width + asteroid.size.width / 2, y: actualY)

addChild(asteroid)

let rotateAction = SKAction.rotate(byAngle: CGFloat.pi, duration: 2.0)
let repeatRotateAction = SKAction.repeatForever(rotateAction)
asteroid.run(repeatRotateAction)

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

asteroid.run(SKAction.sequence([actionMove, actionMoveDone]))
}

func createDefaultExplosion(in position: CGPoint) {
let animatedAtlas = SKTextureAtlas(named: "Explosion")
let numImages = animatedAtlas.textureNames.count

var explosion = SKSpriteNode()
var frames: [SKTexture] = []

for i in 1...numImages {
let textureName = "Ship2_Explosion_\(i)"
let textureName = "Explosion1_\(i)"
frames.append(animatedAtlas.textureNamed(textureName))
}

Expand Down
14 changes: 11 additions & 3 deletions space-war/Scenes/GameScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class GameScene: SKScene {

// MARK: - Properties

var enemy = SKSpriteNode(imageNamed: "img_enemy")
var scoreLabel = SKLabelNode(fontNamed: Constants.robotoRegularFont)
var normalPlayer = SKSpriteNode()
var normalPlayerFrames: [SKTexture] = []
Expand Down Expand Up @@ -65,13 +64,14 @@ class GameScene: SKScene {
override func didMove(to view: SKView) {
createParallaxBackground()
createScoreLabel()
createMenuMusic()
createMusicGame()
createPlayer()
createPlayerControls()

setupPhysics()

addAsteroidToScene()
addEnemyToScene()
}

override func update(_ currentTime: TimeInterval) {
Expand Down Expand Up @@ -110,8 +110,16 @@ class GameScene: SKScene {
run(SKAction.repeatForever(
SKAction.sequence([
SKAction.run(createAsteroid),
SKAction.wait(forDuration: 3.0)
SKAction.wait(forDuration: 5.0)
])
))
}

private func addEnemyToScene() {
run(SKAction.repeatForever(
SKAction.sequence([
SKAction.run(createEnemy),
SKAction.wait(forDuration: 2.5)
])
)) }
}

0 comments on commit 38d5e17

Please sign in to comment.