Skip to content

Commit

Permalink
refine code
Browse files Browse the repository at this point in the history
  • Loading branch information
miochen1226 committed Oct 17, 2023
1 parent baeba62 commit 867b146
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 78 deletions.
3 changes: 1 addition & 2 deletions Shared/UI/GameScene+GameController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import Foundation
import GameController

extension GameScene
{
extension GameScene {
@objc func didConnectController(_ notification: Notification) {
let controller = notification.object as! GCController
gameController = controller
Expand Down
11 changes: 4 additions & 7 deletions Shared/UI/GameScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ import Foundation
import SpriteKit
import GameController

protocol IRenderScreen
{
protocol IRenderScreen {
func renderScreen()
}

class GameScene: SKScene,IRenderScreen {
class GameScene: SKScene, IRenderScreen {

func getFpsInfo()->String
{
func getFpsInfo() -> String {
return nes.getFpsInfo()
}

Expand All @@ -34,8 +32,7 @@ class GameScene: SKScene,IRenderScreen {
#endif
}

func adjRenderCavansSize(_ oldSize: CGSize)
{
func adjRenderCavansSize(_ oldSize: CGSize) {
let windowWidth = oldSize.width
let windowHeight = oldSize.height

Expand Down
134 changes: 67 additions & 67 deletions iOS/GameViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class GameViewController: UIViewController {
GCInputButtonB,
GCInputButtonA,
GCInputButtonX,
GCInputButtonY,
GCInputButtonY
]
self.virtualController = GCVirtualController(configuration: config)

Expand All @@ -32,112 +32,112 @@ class GameViewController: UIViewController {
}
}


if let dpad: GCControllerDirectionPad = self.virtualController.controller?.extendedGamepad?.dpad {
dpad.valueChangedHandler = { (dpad: GCControllerDirectionPad, xValue: Float, yValue: Float) in
if dpad.up.isPressed {
print("")
self.nes.pressU()
}
if !dpad.up.isPressed {
self.nes.pressU(false)
}
if dpad.down.isPressed {
print("")
self.nes.pressD()
}
if !dpad.down.isPressed {
print("")
self.nes.pressD(false)
}
if dpad.left.isPressed {
print("")
self.nes.pressL()
}
if !dpad.left.isPressed {
print("")
self.nes.pressL(false)
}
if dpad.right.isPressed {
self.nes.pressR()
}
if !dpad.right.isPressed {
self.nes.pressR(false)
}
}
dpad.valueChangedHandler = { (dpad: GCControllerDirectionPad, xValue: Float, yValue: Float) in
if dpad.up.isPressed {
print("")
self.nes.pressU()
}
if !dpad.up.isPressed {
self.nes.pressU(false)
}
if dpad.down.isPressed {
print("")
self.nes.pressD()
}
if !dpad.down.isPressed {
print("")
self.nes.pressD(false)
}
if dpad.left.isPressed {
print("")
self.nes.pressL()
}
if !dpad.left.isPressed {
print("")
self.nes.pressL(false)
}
if dpad.right.isPressed {
self.nes.pressR()
}
if !dpad.right.isPressed {
self.nes.pressR(false)
}
}
}

if let buttonA: GCControllerButtonInput = self.virtualController.controller?.extendedGamepad?.buttonA {
buttonA.valueChangedHandler = { (button: GCControllerButtonInput, value: Float, pressed: Bool) in
if buttonA.isPressed {
if let buttonB: GCControllerButtonInput = self.virtualController.controller?.extendedGamepad?.buttonA {
buttonB.valueChangedHandler = { (button: GCControllerButtonInput, value: Float, pressed: Bool) in
if buttonB.isPressed {
print("B")
self.nes.pressB()
}
if !buttonA.isPressed {
if !buttonB.isPressed {
print("B")
self.nes.pressB(false)
}
}
}

if let buttonB: GCControllerButtonInput = self.virtualController.controller?.extendedGamepad?.buttonB {
buttonB.valueChangedHandler = { (button: GCControllerButtonInput, value: Float, pressed: Bool) in
if buttonB.isPressed {
if let buttonA: GCControllerButtonInput = self.virtualController.controller?.extendedGamepad?.buttonB {
buttonA.valueChangedHandler = { (button: GCControllerButtonInput, value: Float, pressed: Bool) in
if buttonA.isPressed {
print("A")
self.nes.pressA()
}
if !buttonB.isPressed {
if !buttonA.isPressed {
print("A")
self.nes.pressA(false)
}
}
}

if let buttonB: GCControllerButtonInput = self.virtualController.controller?.extendedGamepad?.buttonX {
buttonB.valueChangedHandler = { (button: GCControllerButtonInput, value: Float, pressed: Bool) in
if buttonB.isPressed {
print("Option")
if let buttonSelect: GCControllerButtonInput = self.virtualController.controller?.extendedGamepad?.buttonX {
buttonSelect.valueChangedHandler = { (button: GCControllerButtonInput, value: Float, pressed: Bool) in
if buttonSelect.isPressed {
print("Select")
self.nes.pressSelect()
}
if !buttonB.isPressed {
print("Option")
if !buttonSelect.isPressed {
print("Select")
self.nes.pressSelect(false)
}
}
}

if let buttonB: GCControllerButtonInput = self.virtualController.controller?.extendedGamepad?.buttonY {
buttonB.valueChangedHandler = { (button: GCControllerButtonInput, value: Float, pressed: Bool) in
if buttonB.isPressed {
if let buttonStart: GCControllerButtonInput = self.virtualController.controller?.extendedGamepad?.buttonY {
buttonStart.valueChangedHandler = { (button: GCControllerButtonInput, value: Float, pressed: Bool) in
if buttonStart.isPressed {
print("Start")
self.nes.pressStart()
}
if !buttonB.isPressed {
if !buttonStart.isPressed {
print("Start")
self.nes.pressStart(false)
}
}
}
}

func updateConfig()
{
func updateConfig() {
self.virtualController.updateConfiguration(forElement: GCInputButtonA, configuration: { _ in
let elementConfiguration: GCVirtualController.ElementConfiguration = GCVirtualController.ElementConfiguration()

elementConfiguration.path = UIBezierPath()

let bezierPath: UIBezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: 0, y: 0))
bezierPath.addLine(to: CGPoint(x: -10, y: -10))
bezierPath.addLine(to: CGPoint(x: 10, y: -10))
bezierPath.addLine(to: CGPoint(x: 10, y: 10))
bezierPath.addLine(to: CGPoint(x: -10, y: 10))
bezierPath.close()

elementConfiguration.path?.append(bezierPath)
let elementConfiguration: GCVirtualController.ElementConfiguration = GCVirtualController.ElementConfiguration()
elementConfiguration.path = UIBezierPath()
let bezierPath: UIBezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: 0, y: 0))
bezierPath.addLine(to: CGPoint(x: -10, y: -10))
bezierPath.addLine(to: CGPoint(x: 10, y: -10))
bezierPath.addLine(to: CGPoint(x: 10, y: 10))
bezierPath.addLine(to: CGPoint(x: -10, y: 10))
bezierPath.close()
elementConfiguration.path?.append(bezierPath)

return elementConfiguration
})
return elementConfiguration
})
}

override func viewWillDisappear(_ animated: Bool) {
Expand Down
3 changes: 1 addition & 2 deletions iOS/VirtualConteollerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import SwiftUI
struct VirtualControllerView: UIViewControllerRepresentable {
typealias UIViewControllerType = GameViewController

func makeUIViewController(context: Self.Context) -> GameViewController
{
func makeUIViewController(context: Self.Context) -> GameViewController {
return GameViewController()
}

Expand Down

0 comments on commit 867b146

Please sign in to comment.