Pedestal

Картинки

Создаем новый файл PedestalView.swift

struct PedestalView: View {
  var body: some View {
    GeometryReader { proxy in
      ZStack {
        Image("pedestal")
      }
      .frame(width: proxy.size.width,
             height: proxy.size.height,
             alignment: .bottomLeading)
    }
  }
}
struct PedestalView: View {
  let isKing = true
  let score = 0
  let highScore = 100
  var win: Bool {
    score > highScore
  }
  var name: String {
    (isKing ? "king" : "player") + (win ? "2" : "1")
  }
  var div: CGFloat {
    if win {
      if isKing {
        return CGFloat(highScore) / CGFloat(score)
      } else {
        return 1
      }
    } else {
      if isKing {
        return 1
      } else {
        return CGFloat(score) / CGFloat(highScore)
      }
    }
  }
  func offset(size: CGSize) -> CGFloat {
    return div * (scale(size.width) * (pedestalMinHeight
      + kingHeight) - size.height)
      - scale(size.width) * pedestalMinHeight
  }
  func scale(_ width: CGFloat) -> CGFloat {
    return width / imageWidth
  }
  let imageWidth = CGFloat(83.0)
  let kingHeight = CGFloat(98.0)
  let pedestalMinHeight = CGFloat(13.0)
  var body: some View {
    GeometryReader { proxy in
      ZStack {
        Image("pedestal")
          .scaleEffect(self.scale(proxy.size.width),
                       anchor: .bottomLeading)
        Image(self.name)
          .scaleEffect(self.scale(proxy.size.width),
                       anchor: .bottomLeading)
          .offset(y: self.offset(size: proxy.size))
      }
      .frame(width: proxy.size.width,
             height: proxy.size.height,
             alignment: .bottomLeading)
      .clipped()
    }
  }
}
struct ContentView: View {
  var body: some View {
    GeometryReader { proxy in
      ZStack {
        Color.black
          .edgesIgnoringSafeArea(.all)
        GameFieldView(width: min(proxy.size.width, proxy.size.height) / 9)
        HStack {
          PedestalView()
            .frame(width: 0.25 * min(proxy.size.width, proxy.size.height),
                   height: 0.75 * min(proxy.size.width, proxy.size.height))
          Spacer()
          PedestalView(isKing: false)
            .frame(width: 0.25 * min(proxy.size.width, proxy.size.height),
                   height: 0.75 * min(proxy.size.width, proxy.size.height))
        }
        ControlPanelView()
      }
    }
  }
}
struct ContentView: View {
  var body: some View {
    GeometryReader { proxy in
      ZStack {
        Color.black
          .edgesIgnoringSafeArea(.all)
        if proxy.size.width >= 1.5 * proxy.size.height {
          HStack {
            PedestalView()
              .frame(width: 0.25 * min(proxy.size.width, proxy.size.height),
                     height: 0.75 * min(proxy.size.width, proxy.size.height))
            Spacer()
            PedestalView(isKing: false)
              .frame(width: 0.25 * min(proxy.size.width, proxy.size.height),
                     height: 0.75 * min(proxy.size.width, proxy.size.height))
          }
          .padding(.horizontal, (proxy.size.width - 1.5 * proxy.size.height) / 4)
        }
        ControlPanelView()
        GameFieldView(width: min(proxy.size.width, proxy.size.height) / 9)
      }
    }
  }
}