class BoardView: UIView {
weak var delegate: BoardViewDelegate?
code
Assistant
Questo è il codice di BoardView convertito in SwiftUI:
swift
User
e quale sarebbe il GameViewModel
Assistant
Ecco un esempio di implementazione per il GameViewModel. Ho assunto che la logica del gioco sia scritta in una struttura chiamata GameLogic. Ovviamente, sostituisci il codice del tuo gioco al posto di GameLogic.
swift
private(set) var squares: [UIImageView] = []
private(set) var pieces: [String: UIImageView] = [:]
private(set) var moveIndicators: [UIView] = []
var board = Board() {
didSet { updatePieces() }
}
var selection: Position? {
didSet { updateSelection() }
}
var moves: [Position] = [] {
didSet { updateMoveIndicators() }
}
func jigglePiece(at position: Position) {
if let piece = board.piece(at: position) {
pieces[piece.id]?.jiggle()
}
}
func pulsePiece(at position: Position, completion: (() -> Void)?) {
if let piece = board.piece(at: position) {
pieces[piece.id]?.pulse(completion: completion)
}
}
override init(frame: CGRect) {
super.init(frame: frame)
sharedSetup()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
sharedSetup()
}
private func sharedSetup() {
for i in 0 ..< 8 {
for j in 0 ..< 8 {
let white = i % 2 == j % 2
let image = UIImage(named: white ? "square_white": "square_black")
let view = UIImageView(image: image)
squares.append(view)
addSubview(view)
}
}
for row in board.pieces {
for piece in row {
guard let piece = piece else {
continue
}
let view = UIImageView()
view.contentMode = .scaleAspectFit
pieces[piece.id] = view
addSubview(view)
}
}
for _ in 0 ..< 8 {
for _ in 0 ..< 8 {
let view = UIView()
view.backgroundColor = .white
moveIndicators.append(view)
addSubview(view)
}
}
let tap = UITapGestureRecognizer(target: self, action: #selector(didTap))
addGestureRecognizer(tap)
}
@objc private func didTap(_ gesture: UITapGestureRecognizer) {
let size = squareSize
let location = gesture.location(in: self)
let position = Position(
x: Int(location.x / size.width),
y: Int(location.y / size.height)
)
delegate?.boardView(self, didTap: position)
}
private func updatePieces() {
var usedIDs = Set<String>()
let size = squareSize
for (i, row) in board.pieces.enumerated() {
for (j, piece) in row.enumerated() {
guard let piece = piece, let view = pieces[piece.id] else {
continue
}
usedIDs.insert(piece.id)
view.image = UIImage(named: piece.imageName)
view.frame = frame(x: j, y: i, size: size)
view.layer.transform = CATransform3DMakeScale(0.8, 0.8, 0)
}
}
for (id, view) in pieces where !usedIDs.contains(id) {
view.alpha = 0
}
updateSelection()
}
private func updateSelection() {
for (i, row) in board.pieces.enumerated() {
for (j, piece) in row.enumerated() {
guard let piece = piece, let view = pieces[piece.id] else {
continue
}
view.alpha = selection == Position(x: j, y: i) ? 0.5 : 1
}
}
}
private func updateMoveIndicators() {
let size = squareSize
for i in 0 ..< 8 {
for j in 0 ..< 8 {
let position = Position(x: j, y: i)
let view = moveIndicators[i * 8 + j]
view.frame = frame(x: j, y: i, size: size)
view.layer.cornerRadius = size.width / 2
view.layer.transform = CATransform3DMakeScale(0.2, 0.2, 0)
view.alpha = moves.contains(position) ? 0.5 : 0
}
}
}
private var squareSize: CGSize {
let bounds = self.bounds.size
return CGSize(width: ceil(bounds.width / 8), height: ceil(frame.height / 8))
}
private func frame(x: Int, y: Int, size: CGSize) -> CGRect {
let offset = CGPoint(x: CGFloat(x) * size.width, y: CGFloat(y) * size.height)
return CGRect(origin: offset, size: size)
}
override func layoutSubviews() {
super.layoutSubviews()
let size = squareSize
for i in 0 ..< 8 {
for j in 0 ..< 8 {
squares[i * 8 + j].frame = frame(x: j, y: i, size: size)
}
}
updatePieces()
updateMoveIndicators()
}
Per implementare questa vista con un BoardViewDelegate, è necessario modificare l'implementazione ViewModel chiamato 'GameViewModel' e implementare la funzione 'boardViewDidTap' nelle istanze del modello.
import SwiftUI
class GameViewModel: ObservableObject {
@Published var board: Board
@Published var selection: Position?
@Published var moves: [Position] = []
private var gameLogic: GameLogic
init() {
board = Board()
gameLogic = GameLogic(board: board)
}
func boardViewDidTap(_ position: Position) {
if let selectedPosition = selection {
if gameLogic.isValidMove(from: selectedPosition, to: position) {
gameLogic.performMove(from: selectedPosition, to: position)
board.update() // Aggiorna la board dopo aver eseguito la mossa
selection = nil
moves.removeAll()
} else {
selection = nil
}
} else {
selection = position
moves = gameLogic.availableMoves(at: position)
}
}
}
In questo esempio, ho assunto che GameLogic abbia i seguenti metodi:
init(board: Board): inizializza il gameLogic con la board fornita
isValidMove(from: Position, to: Position) -> Bool: restituisce true se la mossa è valida, altrimenti false
performMove(from: Position, to: Position): esegue la mossa sulla board e aggiorna la posizione delle piece
availableMoves(at: Position) -> [Position]: restituisce un array di mosse disponibili per la posizione data
Ovviamente, sostituisci questa logica con quella del tuo gioco. Assicurati inoltre di aggiornare la tua board con il segnale di pubblicazione appropriato se necessario.