-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLifeBar.swift
More file actions
64 lines (52 loc) · 1.39 KB
/
LifeBar.swift
File metadata and controls
64 lines (52 loc) · 1.39 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
//
// LifeBar.swift
// Jikoku
//
// Created by Raphaël Calabro on 21/08/2017.
// Copyright © 2017 Raphaël Calabro. All rights reserved.
//
import Foundation
import Melisse
import GLKit
protocol HasLifePoints {
var lifePoints: Int { get }
var lifeBar: LifeBar? { get set }
}
class LifeBar {
var frame = Rectangle<GLfloat>() {
didSet {
update()
}
}
var value = 0 {
didSet {
update()
}
}
var maximum = 0 {
didSet {
update()
}
}
var progress: GLfloat {
return GLfloat(fence(0, value, maximum)) / max(GLfloat(maximum), 1)
}
var front: ColoredQuadrilateral
var background: ColoredQuadrilateral
init(plane: Plane) {
self.background = plane.coloredQuadrilateral()
self.background.color = .black
self.front = plane.coloredQuadrilateral()
self.front.color = .red
}
deinit {
self.front.color = nil
self.front.quadrilateral = nil
self.background.color = nil
self.background.quadrilateral = nil
}
fileprivate func update() {
background.quadrilateral = Quadrilateral(rectangle: frame)
front.quadrilateral = Quadrilateral(rectangle: Rectangle(left: frame.left, top: frame.top, width: frame.width * progress, height: frame.height))
}
}