import SwiftUI
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate, ObservableObject { // <<: Here 1
// @Published var updates = 0
@Published var gameScore = 0 // <<: Here 2
@Published var lenght: CGFloat = 1
var up: Bool = false
// let removeLabel = SKAction.sequence([SKAction.fadeIn(withDuration: 0.3), SKAction.wait(forDuration: 0.8), SKAction.fadeOut(withDuration: 0.3)])
let tball = SKShapeNode(circleOfRadius: 20)
var lastUpdateTime2 : TimeInterval = 0
override func didMove(to view: SKView) {
backgroundColor = .yellow
physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
tball.position = CGPoint(x: 0, y: 0)
tball.fillColor = .blue
self.addChild(tball)
// tball.zPosition = 10
}
func change(){
if self.lenght > 100 {
up = false
}else if (self.lenght < 10){
up = true
}
up ? (self.lenght += 1) : (self.lenght -= 1)
}
override func update(_ currentTime: TimeInterval) {
// self.change()
if lastUpdateTime2 == 0 {
lastUpdateTime2 = currentTime
}
if currentTime - lastUpdateTime2 > 1 - 0.9 {
self.change()
lastUpdateTime2 = currentTime
}
}
/*
func ball() {
let ball = SKShapeNode(circleOfRadius: 20)
let MinValue = self.size.width / 8
let MaxValue = self.size.width - 50
let SpawnPoint = UInt32(MaxValue - MinValue)
ball.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: 200)
ball.fillColor = .blue
self.addChild(ball)
}
*/
func ball() {
let moveDown = SKAction.moveTo(x:130, duration: 2.5)
let moveUp = SKAction.moveTo(x: -130, duration: 2.5)
let actionSequence2 = SKAction.sequence([moveDown, moveUp])
let moveRepeat = SKAction.repeatForever(actionSequence2)
tball.run(moveRepeat, withKey: "moveX")
}
func ball2() {
let moveDown2 = SKAction.moveTo(x:130, duration: -2.5)
let moveUp2 = SKAction.moveTo(x: -130, duration: -2.5)
let actionSequence3 = SKAction.sequence([moveDown2, moveUp2])
let moveRepeat2 = SKAction.repeatForever(actionSequence3)
tball.run(moveRepeat2, withKey: "moveX2")
}
func addScore(){
if gameScore < 10 {
gameScore += 1
} else {
gameScore += 5
}
}
}
struct ContentView: View {
@State var flag = true
@State var showingSheet : Bool = false
// @StateObject var gameScene = GameScene() // <<: Here 3
/*
let scene: GameScene = {
let scene = GameScene()
scene.size = CGSize(width: 300, height: 300)
scene.scaleMode = .fill
return scene
}()
*/
@StateObject private var scene: GameScene = {
let scene = GameScene()
scene.size = CGSize(width: 300, height: 300)
scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
scene.scaleMode = .fill
return scene
}()
var body: some View {
Spacer()
VStack {
Button(action: {
if(self.flag){
scene.ball()
self.flag = false
}
else{
scene.ball2()
self.flag = true
}
}
){
Text("ball start on off")
.font(.title3)
.foregroundColor(Color.purple)
.padding(.bottom, 20)
}
Button(
action: { showingSheet = true },
label: {
Text("ShowSheet")
})
.sheet(isPresented: $showingSheet){
SecondSheet()
}
}
ZStack {
SpriteView(scene: scene)
.frame(width: 300, height: 300)
.ignoresSafeArea()
Circle().frame(width: scene.lenght, height: scene.lenght)
.foregroundColor(.green)
.padding(.top, 150)
Button(action: {
scene.addScore()
}) {
Text("Score: \(scene.gameScore)")
.font(.title3)
.frame(width: 110, height: 30, alignment: .center)
.foregroundColor(Color.white)
.background(Color.pink)
.cornerRadius(5, antialiased: true)
.padding(.bottom, 130)
}
}
.padding(.bottom, 110)
}
}
struct SecondSheet: View {
@Environment(\.dismiss) var dismiss
var body: some View {
VStack{
Text("SecondSheet")
.fontWeight(.heavy)
.font(.title)
Button("画面を閉じる") {
dismiss()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}