From b432f0c2ca506c1611808b5badf2062ab523c642 Mon Sep 17 00:00:00 2001 From: zheng-ze Date: Mon, 15 Apr 2024 17:28:18 +0800 Subject: [PATCH] Update GameScene to scale with iPad sizes --- TowerForge/TowerForge/GameModule/GameWorld.swift | 4 ++-- TowerForge/TowerForge/LevelModule/Grid/Grid.swift | 2 +- .../TowerForge/Networking/RoomNetwork/GameRoom.swift | 1 - TowerForge/TowerForge/Nodes/PowerUpNode.swift | 2 +- TowerForge/TowerForge/Nodes/UnitNode.swift | 6 +++++- TowerForge/TowerForge/Scenes/GameScene.swift | 4 ++++ TowerForge/TowerForge/TFCore/TFCameraNode.swift | 9 +++++---- TowerForge/TowerForge/TFCore/TFNode.swift | 3 +-- .../ViewControllers/GameWaitingRoomViewController.swift | 1 - 9 files changed, 19 insertions(+), 13 deletions(-) diff --git a/TowerForge/TowerForge/GameModule/GameWorld.swift b/TowerForge/TowerForge/GameModule/GameWorld.swift index d49f511e..5f07daa4 100644 --- a/TowerForge/TowerForge/GameModule/GameWorld.swift +++ b/TowerForge/TowerForge/GameModule/GameWorld.swift @@ -9,8 +9,8 @@ import QuartzCore import UIKit class GameWorld { - // Need to ensure that width is a multiple of 1024 - unit selection node height - static let worldSize = CGSize(width: 2_472, height: 1_024) + // Must subtract one else there will be an extra unintended tile for each row. + static let worldSize = CGSize(width: (1_024 * 0.8) * 3 - 1, height: 1_024) private var gameEngine: AbstractGameEngine private var gameMode: GameMode diff --git a/TowerForge/TowerForge/LevelModule/Grid/Grid.swift b/TowerForge/TowerForge/LevelModule/Grid/Grid.swift index f42ac05a..8b61f63e 100644 --- a/TowerForge/TowerForge/LevelModule/Grid/Grid.swift +++ b/TowerForge/TowerForge/LevelModule/Grid/Grid.swift @@ -9,7 +9,7 @@ import SpriteKit class Grid: GridDelegate { static let DEFAULT_NUM_ROWS = 5 - let UNIT_SELECTION_NODE_HEIGHT = CGFloat(200) + var UNIT_SELECTION_NODE_HEIGHT: CGFloat { GameWorld.worldSize.height / 5 } let playableBounds: CGRect private let numRows: Int diff --git a/TowerForge/TowerForge/Networking/RoomNetwork/GameRoom.swift b/TowerForge/TowerForge/Networking/RoomNetwork/GameRoom.swift index 5fa62836..a2e99ca7 100644 --- a/TowerForge/TowerForge/Networking/RoomNetwork/GameRoom.swift +++ b/TowerForge/TowerForge/Networking/RoomNetwork/GameRoom.swift @@ -56,7 +56,6 @@ class GameRoom { } deinit { - print("deinit") removeAllListeners() } diff --git a/TowerForge/TowerForge/Nodes/PowerUpNode.swift b/TowerForge/TowerForge/Nodes/PowerUpNode.swift index 660d8887..6c195592 100644 --- a/TowerForge/TowerForge/Nodes/PowerUpNode.swift +++ b/TowerForge/TowerForge/Nodes/PowerUpNode.swift @@ -27,7 +27,7 @@ protocol PowerUpNodeDelegate: AnyObject { } class PowerUpNode: TFEntity { - static let size = CGSize(width: 140, height: 200) + static let size = UnitNode.size let type: PowerUp var delegate: PowerUpNodeDelegate diff --git a/TowerForge/TowerForge/Nodes/UnitNode.swift b/TowerForge/TowerForge/Nodes/UnitNode.swift index 799c55da..9f49fc38 100644 --- a/TowerForge/TowerForge/Nodes/UnitNode.swift +++ b/TowerForge/TowerForge/Nodes/UnitNode.swift @@ -13,7 +13,11 @@ protocol UnitNodeDelegate: AnyObject { } class UnitNode: TFEntity { - static let size = CGSize(width: 140, height: 200) + static var size: CGSize { + let height = UIScreen.main.bounds.height / 5 + let width = height * 0.7 + return CGSize(width: width, height: height) + } let type: (TFEntity & PlayerSpawnable).Type weak var delegate: UnitNodeDelegate? diff --git a/TowerForge/TowerForge/Scenes/GameScene.swift b/TowerForge/TowerForge/Scenes/GameScene.swift index c86122f6..5594fc53 100644 --- a/TowerForge/TowerForge/Scenes/GameScene.swift +++ b/TowerForge/TowerForge/Scenes/GameScene.swift @@ -81,9 +81,13 @@ class GameScene: SKScene { private func setupCamera() { let cameraNode = TFCameraNode() + let scaleRatio = size.height / UIScreen.main.bounds.height self.cameraNode = cameraNode cameraNode.position = CGPoint(x: size.width / 2, y: size.height / 2) cameraNode.zPosition = 1_000 + cameraNode.xScale = scaleRatio + cameraNode.yScale = scaleRatio + addChild(cameraNode.node) camera = cameraNode.cameraNode } diff --git a/TowerForge/TowerForge/TFCore/TFCameraNode.swift b/TowerForge/TowerForge/TFCore/TFCameraNode.swift index 3f25f2d6..981482e3 100644 --- a/TowerForge/TowerForge/TFCore/TFCameraNode.swift +++ b/TowerForge/TowerForge/TFCore/TFCameraNode.swift @@ -32,10 +32,11 @@ class TFCameraNode: TFNode { func setBounds(_ bounds: CGRect) { let size = UIScreen.main.bounds.size - self.minX = bounds.minX + size.width / 2 - self.maxX = bounds.maxX - size.width / 2 - self.minY = bounds.minY + size.height / 2 - self.maxY = bounds.maxY - size.height / 2 + self.minX = (bounds.minX + size.width / 2) * self.xScale + self.maxX = (bounds.maxX - size.width / 2) - minX * (self.xScale - 1) / self.xScale + self.minY = (bounds.minY + size.height / 2) * self.yScale + self.maxY = (bounds.maxY - size.height / 2) - minY * (self.yScale - 1) / self.yScale + self.move(by: .zero) } override func move(by displacement: CGVector) { diff --git a/TowerForge/TowerForge/TFCore/TFNode.swift b/TowerForge/TowerForge/TFCore/TFNode.swift index 82431a31..1da6660e 100644 --- a/TowerForge/TowerForge/TFCore/TFNode.swift +++ b/TowerForge/TowerForge/TFCore/TFNode.swift @@ -10,7 +10,7 @@ import SpriteKit class TFNode { var staticOnScreen = false var node: SKNode - private var children: [String: TFNode] = [:] + var children: [String: TFNode] = [:] var name: String? { get { node.name } @@ -61,7 +61,6 @@ class TFNode { } func add(child: TFNode) { - // TODO: This enforces child to have name guard let name = child.name, children[name] == nil else { return } diff --git a/TowerForge/TowerForge/ViewControllers/GameWaitingRoomViewController.swift b/TowerForge/TowerForge/ViewControllers/GameWaitingRoomViewController.swift index bef43812..0faa42c8 100644 --- a/TowerForge/TowerForge/ViewControllers/GameWaitingRoomViewController.swift +++ b/TowerForge/TowerForge/ViewControllers/GameWaitingRoomViewController.swift @@ -17,7 +17,6 @@ class GameWaitingRoomViewController: UIViewController { gameRoom?.deleteRoom() gameRoom = nil currentPlayer = nil - print("deinit") } override func viewDidLoad() {