<commit_before>//
//  LanderSceneController.swift
//  KJLunarLander
//
//  Copyright © 2016 <NAME>. All rights reserved.
//

import SpriteKit

/// Implements the lander game.

class LanderSceneController: NSObject {
    private let view: SKView
    private let scene: SKScene

    fileprivate let lander: LanderSprite
    fileprivate let surface: SurfaceSprite
    fileprivate var hud: HUD?

    fileprivate var lastHUDUpdateTime: TimeInterval = 0

    weak var controlInput: ControlInput?
    
    init(view: SKView) {
        self.view = view

        scene = SKScene(size: Constant.sceneSize)
        scene.backgroundColor = .black
        scene.scaleMode = .aspectFit

        scene.physicsWorld.gravity = CGVector(dx: 0,
                                              dy: Constant.lunarGravity)

        view.presentScene(scene)

        view.ignoresSiblingOrder = true

        if true {
            view.showsFPS = true
            view.showsDrawCount = true
            view.showsNodeCount = true
            view.showsQuadCount = true
            view.showsPhysics = true
            view.showsFields = true
        }

        hud = HUD(parent: scene)

        surface = SurfaceSprite()
        surface.position = CGPoint(x: surface.size.width / 2,
                                   y: surface.size.height / 2)
        scene.addChild(surface)

        lander = LanderSprite.sprite(scene: scene,
                                     position: Constant.landerInitialPosition)

        super.init()

        scene.delegate = self
        scene.physicsWorld.contactDelegate = self
    }

    func viewDidLayoutSubviews() {
        if hud != nil {
            scene.removeChildren(in: [hud!])
        }
        hud = HUD(parent: scene)
    }
}

// MARK: - SKSceneDelegate
extension LanderSceneController: SKSceneDelegate {
    public func update(_ currentTime: TimeInterval, for scene: SKScene) {
        guard let controlInput = controlInput else { return }
        guard let landerBody = lander.physicsBody else { return }

        // If lander goes off left or right edge, wrap around to opposite edge.

        let sceneWidth = scene.size.width
        let landerX = lander.position.x
        if landerX < 0 {
            lander.wraparoundX(to: sceneWidth - 1)
        }
        else if landerX > sceneWidth {
            lander.wraparoundX(to: 1)
        }

        // Apply control inputs.
        
        let thrustLevel = controlInput.thrustInput
        lander.thrustLevel = thrustLevel
        if thrustLevel > 0 {
            let thrustForce = controlInput.thrustInput * Constant.landerThrust
            let thrustDirection = lander.zRotation + pi2
            let thrustVector = CGVector(angle: thrustDirection, length: thrustForce)
            landerBody.applyForce(thrustVector)
        }<commit_msg>Use removeFromParent instead of removeChildren<commit_after>//
//  LanderSceneController.swift
//  KJLunarLander
//
//  Copyright © 2016 <NAME>. All rights reserved.
//

import SpriteKit

/// Implements the lander game.

class LanderSceneController: NSObject {
    private let view: SKView
    private let scene: SKScene

    fileprivate let lander: LanderSprite
    fileprivate let surface: SurfaceSprite
    fileprivate var hud: HUD?

    fileprivate var lastHUDUpdateTime: TimeInterval = 0

    weak var controlInput: ControlInput?
    
    init(view: SKView) {
        self.view = view

        scene = SKScene(size: Constant.sceneSize)
        scene.backgroundColor = .black
        scene.scaleMode = .aspectFit

        scene.physicsWorld.gravity = CGVector(dx: 0,
                                              dy: Constant.lunarGravity)

        view.presentScene(scene)

        view.ignoresSiblingOrder = true

        // Show SpriteKit diagnostics
        if true {
            view.showsFPS = true
            view.showsDrawCount = true
            view.showsNodeCount = true
            view.showsQuadCount = true
            view.showsPhysics = true
            view.showsFields = true
        }

        hud = HUD(parent: scene)

        surface = SurfaceSprite()
        surface.position = CGPoint(x: surface.size.width / 2,
                                   y: surface.size.height / 2)
        scene.addChild(surface)

        lander = LanderSprite.sprite(scene: scene,
                                     position: Constant.landerInitialPosition)

        super.init()

        scene.delegate = self
        scene.physicsWorld.contactDelegate = self
    }

    func viewDidLayoutSubviews() {
        // Recreate the HUD
        hud?.removeFromParent()
        hud = HUD(parent: scene)
    }
}

// MARK: - SKSceneDelegate
extension LanderSceneController: SKSceneDelegate {
    public func update(_ currentTime: TimeInterval, for scene: SKScene) {
        guard let controlInput = controlInput else { return }
        guard let landerBody = lander.physicsBody else { return }

        // If lander goes off left or right edge, wrap around to opposite edge.

        let sceneWidth = scene.size.width
        let landerX = lander.position.x
        if landerX < 0 {
            lander.wraparoundX(to: sceneWidth - 1)
        }
        else if landerX > sceneWidth {
            lander.wraparoundX(to: 1)
        }

        // Apply control inputs.
        
        let thrustLevel = controlInput.thrustInput
        lander.thrustLevel = thrustLevel
        if thrustLevel > 0 {
            let thrustForce = controlInput.thrustInput * Constant.landerThrust
            let thrustDirection = lander.zRotation + pi2
            let thrustVector = CGVector(angle: thrustDirection, length: thrustForce)
            landerBody.applyForce(thrustVector)
        }
{'fraction_non_alphanumeric': 0.07396345748418834, 'fraction_numerical': 0.004567814476458187, 'mean_word_length': 1.9224845995893223, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5887776', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1524, 'n_words': 398}