Skip to content

Commit

Permalink
[misc] fix: resolve merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
didymental committed Apr 15, 2024
2 parents c54470d + bb7568d commit 113b50e
Show file tree
Hide file tree
Showing 84 changed files with 1,712 additions and 605 deletions.
17 changes: 15 additions & 2 deletions ArkGameExample/RootViewController.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import UIKit

class RootViewController: UINavigationController {
class RootViewController: UINavigationController, UIPopoverPresentationControllerDelegate {
override func viewDidLoad() {
let homePage = ArkDemoHomePage()
homePage.rootViewControllerDelegate = self
Expand All @@ -10,6 +10,19 @@ class RootViewController: UINavigationController {

protocol RootViewControllerDelegate: AnyObject {
func pushViewController(_ viewController: UIViewController, animated: Bool)
func presentPopover(_ popoverViewController: UIViewController, sourceView: UIView,
sourceRect: CGRect, animated: Bool)
}

extension RootViewController: RootViewControllerDelegate { }
extension RootViewController: RootViewControllerDelegate {
func presentPopover(_ popoverViewController: UIViewController, sourceView: UIView, sourceRect: CGRect, animated: Bool) {
popoverViewController.modalPresentationStyle = .popover
if let popoverPresentationController = popoverViewController.popoverPresentationController {
popoverPresentationController.sourceView = sourceView
popoverPresentationController.sourceRect = sourceRect
popoverPresentationController.permittedArrowDirections = .any
popoverPresentationController.delegate = self
}
present(popoverViewController, animated: animated)
}
}
1 change: 1 addition & 0 deletions ArkGameExample/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ extension SceneDelegate {
return
}
ark = Ark(rootView: rootView, blueprint: blueprint)
// ark?.multiplayer(serviceName: "tankGame")
ark?.start()
}
}
4 changes: 2 additions & 2 deletions ArkGameExample/games/SnakeGame/SnakeGame.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension SnakeGame {
let canvasCenter = CGPoint(x: canvasWidth / 2, y: canvasHeight / 2)

ecs.createEntity(with: [
BitmapImageRenderableComponent(imageResourcePath: SnakeGameImages.map,
BitmapImageRenderableComponent(arkImageResourcePath: SnakeGameImages.map,
width: canvasWidth, height: canvasHeight)
.center(canvasCenter)
.zPosition(0)
Expand Down Expand Up @@ -153,7 +153,7 @@ extension SnakeGame {
SnakeGameApple(),
SnakeGridPositionComponent(gridPosition: emptyPosition),
PositionComponent(position: self.grid.toActualPosition(emptyPosition)),
BitmapImageRenderableComponent(imageResourcePath: SnakeGameImages.apple,
BitmapImageRenderableComponent(arkImageResourcePath: SnakeGameImages.apple,
width: Double(self.grid.boxSideLength),
height: Double(self.grid.boxSideLength))
.layer(.canvas)
Expand Down
17 changes: 9 additions & 8 deletions ArkGameExample/games/TankGame/ImpactExplosionAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ struct ImpactExplosionAnimation {
private func makeBitmapComponent(imageResourcePath: TankGameExplosionAnimationKeyframes) ->
BitmapImageRenderableComponent {
BitmapImageRenderableComponent(
imageResourcePath: imageResourcePath,
arkImageResourcePath: imageResourcePath,
width: width,
height: height)
.scaleAspectFill()
.zPosition(100)
.shouldRerender { old, new in
old.imageResourcePath.rawValue != new.imageResourcePath.rawValue
old.imageResourcePath != new.imageResourcePath
}
}

Expand All @@ -47,17 +47,18 @@ struct ImpactExplosionAnimation {
for: entity) ?? makeBitmapComponent(
imageResourcePath: imageResourcePath)

bitmapComponent.imageResourcePath = imageResourcePath
bitmapComponent.imageResourcePath = imageResourcePath.rawValue

ecs.upsertComponent(bitmapComponent, to: entity)
}
.onComplete { instance in
instance.markForDestroyal()
ecs.removeEntity(entity)
if instance.shouldDestroy {
ecs.removeEntity(entity)
}
}
let animationsComponent = ArkAnimationsComponent(animations: [
animationInstance
])

var animationsComponent = ArkAnimationsComponent()
animationsComponent.addAnimation(animationInstance)
ecs.upsertComponent(animationsComponent, to: entity)

let bitmapComponent = makeBitmapComponent(imageResourcePath: .Sprite_Effects_Explosion_001)
Expand Down
4 changes: 2 additions & 2 deletions ArkGameExample/games/TankGame/TankGameEntityCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ enum TankGameEntityCreator {
let zPosition = tankContext.zPosition
let tankEntity = ecsContext.createEntity(with: [
BitmapImageRenderableComponent(
imageResourcePath: tankIndexToImageAsset[tankContext.tankIndex] ?? .tank_1,
arkImageResourcePath: tankIndexToImageAsset[tankContext.tankIndex] ?? .tank_1,
width: 80,
height: 100
)
Expand Down Expand Up @@ -145,7 +145,7 @@ enum TankGameEntityCreator {
let radius = ballContext.radius
ecsContext.createEntity(with: [
BitmapImageRenderableComponent(
imageResourcePath: TankGameImages.ball, width: radius * 2.2, height: radius * 2.2
arkImageResourcePath: TankGameImages.ball, width: radius * 2.2, height: radius * 2.2
)
.center(ballContext.position)
.zPosition(ballContext.zPosition)
Expand Down
99 changes: 58 additions & 41 deletions ArkGameExample/games/TankGame/TankGameManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,64 @@ class TankGameManager {
setUpEntities()
setUpSystems()
setUpRules()
self.blueprint = self.blueprint
.supportNetworkMultiPlayer(
roomName: "TankFightGame", numberOfPlayers: 2
)
.setupPlayer { context in
let ecs = context.ecs
let events = context.events
let screenWidth = context.display.screenSize.width
let screenHeight = context.display.screenSize.height

let joystick1Entity = TankGameEntityCreator.createJoyStick(
center: CGPoint(x: screenWidth * 1 / 6, y: screenHeight * 7 / 8),
tankId: 1,
in: ecs,
eventContext: events,
zPosition: 999)

let shootButton1Entity = TankGameEntityCreator.createShootButton(
with: TankShootButtonCreationContext(
position: CGPoint(x: screenWidth * 5 / 6, y: screenHeight * 7 / 8),
tankId: 1,
zPosition: 999,
rotate: false
),
in: ecs,
eventContext: events
)

self.joystick1 = joystick1Entity.id
self.shootButton1 = shootButton1Entity.id
}
.setupPlayer { context in
let ecs = context.ecs
let events = context.events
let screenWidth = context.display.screenSize.width
let screenHeight = context.display.screenSize.height

let joystick2Entity = TankGameEntityCreator.createJoyStick(
center: CGPoint(x: screenWidth * 5 / 6, y: screenHeight * 1 / 8),
tankId: 2,
in: ecs,
eventContext: events,
zPosition: 999)

let shootButton2Entity = TankGameEntityCreator.createShootButton(
with: TankShootButtonCreationContext(
position: CGPoint(x: screenWidth * 1 / 6, y: screenHeight * 1 / 8),
tankId: 2,
zPosition: 999,
rotate: true
),
in: ecs,
eventContext: events
)

self.joystick2 = joystick2Entity.id
self.shootButton2 = shootButton2Entity.id
}
}

func setUpAudio() {
Expand Down Expand Up @@ -68,45 +126,6 @@ class TankGameManager {
hp: 50),
in: ecs)
self.tankIdEntityMap[2] = tankEntity2

let joystick1Entity = TankGameEntityCreator.createJoyStick(
center: CGPoint(x: screenWidth * 1 / 6, y: screenHeight * 7 / 8),
tankId: 1,
in: ecs,
eventContext: events,
zPosition: 999)
let joystick2Entity = TankGameEntityCreator.createJoyStick(
center: CGPoint(x: screenWidth * 5 / 6, y: screenHeight * 1 / 8),
tankId: 2,
in: ecs,
eventContext: events,
zPosition: 999)

let shootButton1Entity = TankGameEntityCreator.createShootButton(
with: TankShootButtonCreationContext(
position: CGPoint(x: screenWidth * 5 / 6, y: screenHeight * 7 / 8),
tankId: 1,
zPosition: 999,
rotate: false
),
in: ecs,
eventContext: events
)
let shootButton2Entity = TankGameEntityCreator.createShootButton(
with: TankShootButtonCreationContext(
position: CGPoint(x: screenWidth * 1 / 6, y: screenHeight * 1 / 8),
tankId: 2,
zPosition: 999,
rotate: true
),
in: ecs,
eventContext: events
)

self.joystick1 = joystick1Entity.id
self.joystick2 = joystick2Entity.id
self.shootButton1 = shootButton1Entity.id
self.shootButton2 = shootButton2Entity.id
}
}

Expand All @@ -121,8 +140,6 @@ class TankGameManager {
}

func setUpRules() {
blueprint = blueprint.setupMultiplayer(serviceName: "tankGame")

blueprint = blueprint
.on(ScreenResizeEvent.self) { event, context in
self.handleScreenResize(event, in: context)
Expand Down
2 changes: 1 addition & 1 deletion ArkGameExample/games/TankGame/TankGameMapBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class TankGameMapBuilder {
for (x, value) in row.enumerated() {
for strategy in strategies {
if let imageResourcePath = strategy.imageResourcePath(forValue: value) {
let component = BitmapImageRenderableComponent(imageResourcePath: imageResourcePath,
let component = BitmapImageRenderableComponent(arkImageResourcePath: imageResourcePath,
width: gridSize.width,
height: gridSize.height)
.shouldRerender { _, _ in false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class TankGameLakeStrategy: TankGameTerrainObjectStrategy {
func createObject(type: Int, location: CGPoint, size: CGSize,
zPos: Double, in ecsContext: ArkECSContext) -> Entity {
ecsContext.createEntity(with: [
BitmapImageRenderableComponent(imageResourcePath: TankGameImages.lake,
BitmapImageRenderableComponent(arkImageResourcePath: TankGameImages.lake,
width: size.width, height: size.height)
.zPosition(zPos)
.center(location)
Expand Down Expand Up @@ -90,7 +90,7 @@ class TankGameStoneStrategy: TankGameTerrainObjectStrategy {
func createObject(type: Int, location: CGPoint, size: CGSize,
zPos: Double, in ecsContext: ArkECSContext) -> Entity {
ecsContext.createEntity(with: [
BitmapImageRenderableComponent(imageResourcePath: stoneTypeToImageAsset[type] ?? .stones_1,
BitmapImageRenderableComponent(arkImageResourcePath: stoneTypeToImageAsset[type] ?? .stones_1,
width: size.width, height: size.height)
.zPosition(zPos)
.center(location),
Expand All @@ -113,7 +113,7 @@ class TankGameHealthPackStrategy: TankGameTerrainObjectStrategy {
func createObject(type: Int, location: CGPoint, size: CGSize,
zPos: Double, in ecsContext: ArkECSContext) -> Entity {
ecsContext.createEntity(with: [
BitmapImageRenderableComponent(imageResourcePath: TankGameImages.healthPack,
BitmapImageRenderableComponent(arkImageResourcePath: TankGameImages.healthPack,
width: size.width, height: size.height)
.zPosition(zPos)
.center(location),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ enum TankRaceGameEntityCreator {
in ecsContext: ArkECSContext,
zPosition: Double) -> Entity {
let tankEntity = ecsContext.createEntity(with: [
BitmapImageRenderableComponent(imageResourcePath: tankIndexToImageAsset[tankIndex] ?? .tank_1,
BitmapImageRenderableComponent(arkImageResourcePath: tankIndexToImageAsset[tankIndex] ?? .tank_1,
width: 80,
height: 100)
.center(position)
Expand Down Expand Up @@ -146,7 +146,7 @@ enum TankRaceGameEntityCreator {
let entities = positions.map {
ecsContext.createEntity(with: [
BitmapImageRenderableComponent(
imageResourcePath: TankRaceGameImages.finish_line,
arkImageResourcePath: TankRaceGameImages.finish_line,
width: canvasWidth / 3,
height: canvasWidth / 5
)
Expand Down
25 changes: 13 additions & 12 deletions ArkGameExample/pages/ArkDemoGameHostingPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@ import UIKit
protocol AbstractDemoGameHostingPage: UIViewController { }

class ArkDemoGameHostingPage<T: ArkExternalResources>: UIViewController {
// inject blueprint here
var arkBlueprint: ArkBlueprint<T>?
var ark: Ark<UIView, T>?
var shouldShowMultiplayerOptions = false

override func viewDidLoad() {
super.viewDidLoad()
guard let blueprint = arkBlueprint else {
return
}

// Example on how to inject views into ark blueprint after win/ termination
// arkBlueprint = arkBlueprint?.on(TerminateGameLoopEvent.self) { event, context in
// // add pop-up view here
// self.present(<#T##viewControllerToPresent: UIViewController##UIViewController#>, animated: <#T##Bool#>)
// }
if shouldShowMultiplayerOptions {
presentMultiplayerOptions()
}
}

// load blueprint
ark = Ark(rootView: self, blueprint: blueprint)
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
ark?.start()
}

private func presentMultiplayerOptions() {
let popover = ArkDemoMultiplayerPopover()
popover.modalPresentationStyle = .overFullScreen
self.present(popover, animated: true)
}
}

extension ArkDemoGameHostingPage: AbstractRootView {
Expand Down
11 changes: 8 additions & 3 deletions ArkGameExample/pages/ArkDemoHomePage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class ArkDemoHomePage: UIViewController {
button.setTitleColor(DARK_GRAY, for: .normal)

var buttonConfig = UIButton.Configuration.filled()
buttonConfig.contentInsets = NSDirectionalEdgeInsets(top: 4, leading: 16, bottom: 4, trailing: 16) // Padding
buttonConfig.contentInsets =
NSDirectionalEdgeInsets(top: 4, leading: 16, bottom: 4, trailing: 16) // Padding
buttonConfig.background.cornerRadius = 8

buttonConfig.background.backgroundColor = LIGHT_BLUE
Expand All @@ -70,7 +71,11 @@ class ArkDemoHomePage: UIViewController {
guard let gameOption = (sender.superview as? UIStackView)?.arrangedSubviews.firstIndex(of: sender) else {
return
}
let vc = GameHostingPageFactory.generateGameViewController(from: DemoGames.allCases[gameOption])
self.rootViewControllerDelegate?.pushViewController(vc, animated: false)

let gameType = DemoGames.allCases[gameOption]
if let parentDelegate = self.rootViewControllerDelegate {
GameHostingPageFactory.loadGame(from: gameType, with: parentDelegate,
sourceView: sender, sourceRect: sender.bounds)
}
}
}
Loading

0 comments on commit 113b50e

Please sign in to comment.