import SpriteKit
import GameplayKit
//let zombie = SKSpriteNode(imageNamed: “zombie1”)
//@available(iOS 11.2, *)
class GameScene: SKScene { //A single object called scene controls each scene, this object is represented by SKScene
//GameScene is a subclass of SKScene
let zombie = SKSpriteNode(imageNamed: “zombie1”)
var lastUpdateTime: TimeInterval = 0
var dt: TimeInterval = 0 // literally definition of “seconds.”
let zombieMovePointsPerSec: CGFloat = 480.0 //movement defined as a float.
var velocity = CGPoint.zero
override func didMove(to view: SKView) { //didMove is good for initial setup of some contents.
backgroundColor = SKColor.black
let background = SKSpriteNode(imageNamed: "background1")
background.position = CGPoint(x: size.width/2, y: size.height/2)//center of screen, CGPoint = position property with x and y
background.anchorPoint = CGPoint(x: 0.5, y:0.5) //This is 0,0 for start of sprite or bottom left. .5 = middle 1= topright
//background.position = CGPoint.zero //This is 0,0 for the position of the sprite as lower left hand of background picture
//background.zRotation = CGFloat.pi/8 // π/8 radians which is equal to 22.5 degrees to rotate
let mySize = background.size
print("Size: \(mySize)")
//checks size of png3
background.zPosition = -1
addChild(background)
let zombie = SKSpriteNode(imageNamed: "zombie1")
zombie.position = CGPoint(x:400, y:400)
//zombie.setScale(2) scales both x and y scale
addChild(zombie)
}
func sceneTouched(touchLocation:CGPoint) {
moveZombieToward(location: touchLocation)
}
override func touchesBegan(_ touches: Set<UITouch>,
with event: UIEvent?) {
guard let touch = touches.first else {
return
}
let touchLocation = touch.location(in: self)
sceneTouched(touchLocation: touchLocation)
}
override func touchesMoved(_ touches: Set<UITouch>,
with event: UIEvent?) {
guard let touch = touches.first else {
return
}
let touchLocation = touch.location(in: self)
sceneTouched(touchLocation: touchLocation)
} //this updates the zombies velocity direction so that it points wherever the user taps when time is taking its course
override func update(_ currentTime: CFTimeInterval) {
if lastUpdateTime > 0 {
dt = currentTime - lastUpdateTime // dt defined
} else {
dt = 0 }
lastUpdateTime = currentTime
print("\(dt*1000) milleseconds since last update")
move(sprite: zombie,
velocity: velocity)
}
func move(sprite: SKSpriteNode, velocity: CGPoint) {
// step 1
let amountToMove = CGPoint(x: velocity.x * CGFloat(dt),
y: velocity.y * CGFloat(dt))
print("Amount to move: \(amountToMove)")
// step 2
sprite.position = CGPoint(x: sprite.position.x + amountToMove.x,
y: sprite.position.y + amountToMove.y)
}
func moveZombieToward(location: CGPoint) {
let offset = CGPoint(x: location.x - zombie.position.x,
y: location.y - zombie.position.y) //creates how much they differr
let length = sqrt(
Double(offset.x * offset.x + offset.y * offset.y)) //A^2 + b^2 = c^2, c = √a^2 + b^2 // draws mathmatically vector direction line
//this is method of movement for touches, this is like a right triangle from zombie position to tap position this is the hypotenuse.
let direction = CGPoint(x: offset.x / CGFloat(length),
y: offset.y / CGFloat(length))
//offset in x ÷ distance, and y offset ÷ distance in math it would look like a÷√a^2+b^2 and b÷√a^2+b^2 finding comparison from x and y point to hypotenuse.
velocity = CGPoint(x: direction.x * zombieMovePointsPerSec,
y: direction.y * zombieMovePointsPerSec) //distance with a speed to make action.
//direction turned into velocity = (normalizing this vector by turning it into a vector unit of naturally 1)
}
}