Skip to content

Commit

Permalink
Fix bug where tower can be placed in enemy territory
Browse files Browse the repository at this point in the history
  • Loading branch information
zheng-ze committed Mar 30, 2024
1 parent 82a15a4 commit 8c8be62
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ class HomeSystem: TFSystem {
private func snap<T: TFEntity & PlayerSpawnable>(position: CGPoint, for type: T.Type) -> CGPoint {
if type is BaseUnit.Type {
return CGPoint(x: 0, y: position.y)
} else if type is BaseTower.Type && position.x > gridDelegate.playableBounds.midX {
return CGPoint(x: gridDelegate.playableBounds.midX, y: position.y)
} else if type is BaseTower.Type {
let maxX = gridDelegate.playableBounds.midX - gridDelegate.tileSize.width / 2
return position.x > maxX ? CGPoint(x: maxX, y: position.y) : position
}
return position
}
Expand Down
4 changes: 2 additions & 2 deletions TowerForge/TowerForge/LevelModule/Grid/Grid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class Grid: GridDelegate {
private var numCols: Int { Int(ceil(screenWidth / tileSize.width)) }
private var screenWidth: CGFloat { playableBounds.width }
private var screenHeight: CGFloat { playableBounds.height - UNIT_SELECTION_NODE_HEIGHT }
private var tileSize: CGSize { CGSize(width: screenHeight / CGFloat(numRows),
height: screenHeight / CGFloat(numRows)) }
var tileSize: CGSize { CGSize(width: screenHeight / CGFloat(numRows),
height: screenHeight / CGFloat(numRows)) }

init(screenSize: CGRect, numRows: Int = Grid.DEFAULT_NUM_ROWS) {
self.playableBounds = screenSize
Expand Down
1 change: 1 addition & 0 deletions TowerForge/TowerForge/LevelModule/Grid/GridDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ import QuartzCore
protocol GridDelegate: AnyObject {
var UNIT_SELECTION_NODE_HEIGHT: CGFloat { get }
var playableBounds: CGRect { get }
var tileSize: CGSize { get }
func snap(position: CGPoint) -> CGPoint
}

0 comments on commit 8c8be62

Please sign in to comment.