-
Notifications
You must be signed in to change notification settings - Fork 2
/
GameScene.swift
47 lines (38 loc) · 1.33 KB
/
GameScene.swift
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
//
// GameScene.swift
// Raycast2dLinesVsObjects
//
// Created by Julian Arias Maetschl on 10-03-20.
// Copyright © 2020 Maetschl. All rights reserved.
//
import SpriteKit
import GameplayKit
class GameScene: SKScene {
private var player: SKSpriteNode!
override func didMove(to view: SKView) {
self.player = self.childNode(withName: "Player") as? SKSpriteNode
}
func touchDown(atPoint pos: CGPoint) {
let playerPosition = self.player.position
let clickPosition = pos
self.physicsWorld.enumerateBodies(alongRayStart: playerPosition, end: clickPosition) { body, point, vector, object in
if let node = body.node as? SKSpriteNode {
node.color = SKColor.red.withAlphaComponent(0.2)
let pointNode = SKSpriteNode(color: .cyan, size: CGSize(width: 5, height: 5))
pointNode.position = point
let path = CGMutablePath()
path.move(to: playerPosition)
path.addLine(to: clickPosition)
path.closeSubpath()
let line = SKShapeNode(path: path)
line.strokeColor = .green
line.fillColor = .green
self.addChild(line)
self.addChild(pointNode)
}
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchDown(atPoint: t.location(in: self)) }
}
}