-
-
Save Pasanpr/12deed324152234baab5 to your computer and use it in GitHub Desktop.
Code from Object Oriented Swift project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Point { | |
let x: Int | |
let y: Int | |
func pointsAroundMe(withRange range: Int) -> [Point] { | |
var results: [Point] = [] | |
for x in (self.x - range)...(self.x + range) { | |
for y in (self.y - 1)...(self.y + 1) { | |
let point = Point(x: x, y: y) | |
results.append(point) | |
} | |
} | |
return results | |
} | |
} | |
class Enemy { | |
var life: Int = 2 | |
let position: Point | |
init(position: Point) { | |
self.position = position | |
} | |
func decreaseHealth(factor: Int) { | |
life -= factor | |
} | |
} | |
class Tower { | |
let position: Point | |
var range: Int = 1 | |
var strength: Int = 1 | |
init(position: Point) { | |
self.position = position | |
} | |
func fireAtEnemy(enemy: Enemy) { | |
if inRange(self.position, range: self.range, target: enemy.position) { | |
while enemy.life >= 0 { | |
enemy.decreaseHealth(strength) | |
} | |
print("Enemy vanquished!") | |
} else { | |
print("Darn! Out of range!") | |
} | |
} | |
func inRange(position: Point, range: Int, target: Point) -> Bool { | |
let availablePositions = position.pointsAroundMe(withRange: range) | |
for point in availablePositions { | |
if point.x == target.x && point.y == target.y { | |
return true | |
} | |
} | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment