-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationSystem.swift
More file actions
64 lines (56 loc) · 2.33 KB
/
NotificationSystem.swift
File metadata and controls
64 lines (56 loc) · 2.33 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// Created by Aleksandr Grin on 1/8/18.
// Copyright (c) 2018 AleksandrGrin. All rights reserved.
//
import Foundation
import SpriteKit
import UIKit
class NotificationSystem:NSObject {
private var actionToPlay:SKAction? = nil
private var nodeToRun:SKLabelNode? = nil
private var notificationQueue:Array<(text: String, color:SKColor?, font:String?)> = []
func setExecution(action: SKAction, node: SKLabelNode){
self.actionToPlay = action
self.nodeToRun = node
}
func addNew(notification: String, fontColor:SKColor?, fontName:String?){
if self.nodeToRun != nil {
if self.notificationQueue.count == 0{
self.notificationQueue.append((text: notification, color: fontColor, font: fontName))
self.runNextAction()
}else{
self.notificationQueue.append((text: notification, color: fontColor, font: fontName))
}
}else{
print("FATAL ERROR \(#function)")
return
}
}
private func runNextAction(){
if self.notificationQueue.count == 0 {
return
}else{
let notification = self.notificationQueue[0]
let gameNotificationIndicator = SKLabelNode(text: notification.text)
gameNotificationIndicator.fontSize = 36
gameNotificationIndicator.fontColor = notification.color ?? .black
gameNotificationIndicator.horizontalAlignmentMode = .center
gameNotificationIndicator.verticalAlignmentMode = .center
gameNotificationIndicator.fontName = notification.font ?? "Arial-BoldMT"
gameNotificationIndicator.alpha = 1
gameNotificationIndicator.isHidden = true
gameNotificationIndicator.position = CGPoint(x: 0 , y: 0)
gameNotificationIndicator.zPosition = 20
gameNotificationIndicator.name = "gameNotification"
gameNotificationIndicator.isUserInteractionEnabled = false
let message = gameNotificationIndicator.multilined()
let parentNode = self.nodeToRun!.parent!
self.nodeToRun = message
parentNode.addChild(message)
self.nodeToRun!.run(self.actionToPlay!){
self.notificationQueue.remove(at: 0)
self.runNextAction()
}
}
}
}