-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionTowerDefense.swift
More file actions
49 lines (40 loc) · 1.36 KB
/
FunctionTowerDefense.swift
File metadata and controls
49 lines (40 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// FunctionTowerDefense.swift
// Swothie
//
// Created by Passavee Losripat on 19/2/2567 BE.
//
import Foundation
import SwiftUI
func randomOpportunityData() -> otStructure {
let amount = otData.count
return otData[Int.random(in: 0..<amount/2)]
}
func randomThreatData() -> otStructure {
let amount = otData.count
return otData[Int.random(in: amount/2..<amount)]
}
func randomSpawnPoint(max: CGFloat, min:CGFloat) -> CGFloat {
return CGFloat.random(in: 0...1) * (max-min) + min
}
func renderImage<V: View>(from view: V, size: CGSize) -> UIImage? {
let controller = UIHostingController(rootView: view)
controller.view.bounds = CGRect(origin: .zero, size: size)
controller.view.backgroundColor = .clear
let renderer = UIGraphicsImageRenderer(size: size)
return renderer.image { _ in
controller.view.drawHierarchy(in: controller.view.bounds, afterScreenUpdates: true)
}
}
func +(left: CGPoint, right: CGPoint) -> CGPoint {
return CGPoint(x: left.x + right.x, y: left.y + right.y)
}
func -(left: CGPoint, right: CGPoint) -> CGPoint {
return CGPoint(x: left.x - right.x, y: left.y - right.y)
}
func *(point: CGPoint, scalar: CGFloat) -> CGPoint {
return CGPoint(x: point.x * scalar, y: point.y * scalar)
}
func /(point: CGPoint, scalar: CGFloat) -> CGPoint {
return CGPoint(x: point.x / scalar, y: point.y / scalar)
}