Как вынести лучшие рекорды игры в другой контроллер?
У меня есть ViewController из которого можно перейти в Start Game и Records и мне нужно обработать топ 3 рекорда из игры в контроллер Records
Код и GameViewController
import UIKit
import seibmarineGames.GameViewControllerDelegate
class GameViewController: UIViewController {
private let minY: CGFloat = 390
private let maxY: CGFloat = 750
weak var delegate: GameViewControllerDelegate?
var score = 0
var timer: Timer?
var score1: Int = 0
var score2: Int = 0
var score3: Int = 0
@IBOutlet var scoreLabel: UILabel!
@IBOutlet var backGround: UIImageView!
@IBOutlet var boatTwo: UIImageView!
@IBOutlet var boatOne: UIImageView!
@IBOutlet var sharkOne: UIImageView!
@IBOutlet var sharkTwo: UIImageView!
@IBOutlet var submarine: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
scoreLabel.text = "Score: \(score)"
scoreLabel.textColor = .white
scoreLabel.font = UIFont.systemFont(ofSize: 20)
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(increaseScore), userInfo: nil, repeats: true)
let submarineImage = UIImage(named: "submarine")
self.submarine.image = submarineImage
let boatOneImage = UIImage(named: "boat1")
self.boatOne.image = boatOneImage
let boatTwoImage = UIImage(named: "boat2")
self.boatTwo.image = boatTwoImage
let sharkOneImage = UIImage(named: "shark1")
self.sharkOne.image = sharkOneImage
let sharkTwoImage = UIImage(named: "shark2")
self.sharkTwo.image = sharkTwoImage
let backGroundImage = UIImage(named: "background")
self.backGround.image = backGroundImage
animateSharkOne()
animateSharkTwo()
animateBoatOne()
animateBoatTwo()
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
view.addGestureRecognizer(panGestureRecognizer)
}
@objc func increaseScore() {
score += 1
scoreLabel.text = "Score: \(score)"
if score > score1 {
score3 = score2
score2 = score1
score1 = score
} else if score > score2 {
score3 = score2
score2 = score
} else if score > score3 {
score3 = score
}
delegate?.gameViewController(self, didUpdateTopScores: [score1, score2, score3])
}
@objc func handlePan(_ sender: UIPanGestureRecognizer) {
if let submarinePresentation = submarine.layer.presentation(),
let boatOnePresentation = boatOne.layer.presentation(),
let boatTwoPresentation = boatTwo.layer.presentation(),
let sharkOnePresentation = sharkOne.layer.presentation(),
let sharkTwoPresentation = sharkTwo.layer.presentation(),
(submarinePresentation.frame.intersects(boatOnePresentation.frame)
|| submarinePresentation.frame.intersects(boatTwoPresentation.frame)
|| submarinePresentation.frame.intersects(sharkOnePresentation.frame)
|| submarinePresentation.frame.intersects(sharkTwoPresentation.frame)) {
endGame()
} else {
if sender.state == .changed {
let translation = sender.translation(in: self.view)
let newY = submarine.frame.origin.y + translation.y
if newY >= minY && newY <= maxY {
submarine.frame.origin.y = newY
}
sender.setTranslation(CGPoint.zero, in: self.view)
}
}
}
private func animateSharkOne() {
let animationDuration: TimeInterval = 5.0
self.sharkOne.frame.origin = CGPoint(x: 400, y: 500)
UIView.animate(withDuration: animationDuration) {
self.sharkOne.frame.origin.x -= 600
} completion: { _ in
self.animateSharkOne()
}
}
private func animateSharkTwo() {
let animationDuration: TimeInterval = 3.5
sharkTwo.frame.origin = CGPoint(x: 400, y: 670)
UIView.animate(withDuration: animationDuration) {
self.sharkTwo.frame.origin.x -= 600
} completion: { _ in
self.animateSharkTwo()
}
}
private func animateBoatOne() {
let animationDuration: TimeInterval = 7.0
boatOne.frame.origin = CGPoint(x: 400, y: 370)
UIView.animate(withDuration: animationDuration) {
self.boatOne.frame.origin.x -= 600
} completion: { _ in
self.animateBoatOne()
}
}
private func animateBoatTwo() {
let animationDuration: TimeInterval = 5.0
boatTwo.frame.origin = CGPoint(x: 450, y: 380)
UIView.animate(withDuration: animationDuration) {
self.boatTwo.frame.origin.x -= 600
} completion: { _ in
self.animateBoatTwo()
}
}
private func endGame() {
let controller = self.storyboard?.instantiateViewController(withIdentifier: "RestartViewController") as! RestartViewController
self.navigationController?.pushViewController(controller, animated: true)
}
deinit {
timer?.invalidate()
}
func gameViewController(_ viewController: GameViewController, didUpdateTopScores scores: [Int]) {
topScores = scores
updateScoreLabels()
}
}
Код из Records
import UIKit
class RecordsViewController: UIViewController, GameViewControllerDelegate, UIImagePickerControllerDelegate & UINavigationControllerDelegate {
@IBOutlet var score1Label: UILabel!
@IBOutlet var score2Label: UILabel!
@IBOutlet var score3Label: UILabel!
@IBOutlet var image1: UIImageView!
@IBOutlet var image2: UIImageView!
@IBOutlet var image3: UIImageView!
var topScores: [Int] = [0, 0, 0]
var imageViews: [UIImageView] = []
override func viewDidLoad() {
super.viewDidLoad()
imageViews = [image1, image2, image3]
updateScoreLabels()
}
private func updateScoreLabels() {
for (index, score) in topScores.enumerated() {
let scoreLabel = index == 0 ? score1Label : index == 1 ? score2Label : score3Label
scoreLabel.text = "Score: \(score)"
}
}
private func showImagePicker(forImageView imageView: UIImageView) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
present(imagePicker, animated: true, completion: nil)
}
extension RecordsViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let selectedImage = info[.originalImage] as? UIImage {
if image1.isFirstResponder {
image1.image = selectedImage
} else if image2.isFirstResponder {
image2.image = selectedImage
} else if image3.isFirstResponder {
image3.image = selectedImage
}
}
dismiss(animated: true, completion: nil)
}
}
@IBAction func addPhotoFor1Record(_ sender: Any) {
showImagePicker(forImageView: image1)
}
@IBAction func addPhotoFor2Record(_ sender: Any) {
showImagePicker(forImageView: image2)
}
@IBAction func addPhotoFor3Record(_ sender: Any) {
showImagePicker(forImageView: image3)
}
}
Я так же создал файл GameViewControllerDelegate.swift
import Foundation
protocol GameViewControllerDelegate: AnyObject {
func gameViewController(_ viewController: GameViewController, didUpdateTopScores scores: [Int])
}
Код из ViewController
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func startButtonPressed() {
let controller = self.storyboard?.instantiateViewController(withIdentifier: "GameViewController") as! GameViewController
self.navigationController?.pushViewController(controller, animated: true)
}
@IBAction func recordButtonPressed(_ sender: Any) {
let controller = self.storyboard?.instantiateViewController(withIdentifier: "RecordsViewController") as! RecordsViewController
self.navigationController?.pushViewController(controller, animated: true)
}
}
