-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.wren
226 lines (194 loc) · 4.55 KB
/
api.wren
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import "graphics" for ImageData, Point, Canvas, Color
import "audio" for AudioEngine
// A set of common classes that we can use to make life easier
class Sprite {
// An image that can be displayed on the screen.
construct new(filename, center) {
_center = center
sprite = filename
}
construct new(filename) {
_center = false
sprite = filename
}
sprite=(filename) { _sprite = ImageData.loadFromFile(filename) }
width { _sprite.width }
height { _sprite.height }
draw (x, y) {
if (_center) {
x = x - (_sprite.width / 2)
y = y - (_sprite.height / 2)
}
_sprite.draw(x, y)
// Store this so that we know the co-ordinates that refer to this sprite
_loc = Point.new(x, y)
}
getSize() {
if (_loc != null) {
return [_loc, Point.new(_loc.x + _sprite.width, _loc.y + _sprite.height)]
}
}
}
class Button is Sprite {
// An interactable button, including a sprite.
construct new(image, action, center) {
super(image, center)
this.action = action
}
construct new(image, action) {
super(image, false)
this.action = action
}
action=(function) {
if (function is Fn) {
_action = function
} else {
_action = Fn.new { System.print(function) }
}
}
onClick() {
_action.call()
}
hover=(func) {
_hover = func
}
onHover() {
if (_hover != null) {
_hover.call()
} else {
System.print("Hovering!")
}
}
}
class Scene {
// The core class for scenes. Will do little on its own, other than managing the
// draw loop.
// Subclass to create a scene.
construct init() {}
static run() {
// We use the static function run for all scenes instead of their constructors,
// as this means that when we're done with the scene all memory associated with
// it is freed automatically.
return init()
}
update() {
_tempDraw = []
}
draw(dt) {
Canvas.cls()
for (item in _toDraw) {
item[0].draw(item[1].x, item[1].y)
}
for (item in _tempDraw) {
item[0].draw(item[1].x, item[1].y)
}
}
setupDrawLoop() {
_toDraw = []
_tempDraw = []
}
addCanvasItem(item, x, y) { _toDraw.add([item, Point.new(x,y)]) }
addTempCanvasItem(item, x, y) { _tempDraw.add([item, Point.new(x,y)]) }
clearCanvasItems() { setupDrawLoop() } // Really just a convenience.
mouseHandler() {}
keyboardHandler() {}
gamepadHandler() {}
}
class CanvasString {
construct new(string) {
_string = string
_center = false
}
construct new(string, center) {
_string = string
_center = center
}
draw(x, y) {
if (_center) {
x = x - (4 * _string.count)
// Imperfect, but we shouldn't be using any multi-byte characters
}
Canvas.print(_string, x, y, Color.white)
}
}
class Fading {
static init() {
AudioEngine.load("bluebeat", "res/bluebeat.ogg")
AudioEngine.load("cyberrunner", "res/cyberrunner.ogg")
__fadeIn = []
__fadeOut = []
}
static play(trackname) {
var channelID = AudioEngine.play(trackname, 0, true)
System.print("Now fading in %(trackname) on channel %(channelID)")
__fadeIn.add(channelID)
__fadeIn.add(0)
return __fadeIn[0]
}
static stop(channelID) {
__fadeOut.add(channelID)
if (__fadeIn.count > 0 && __fadeIn[0] == channelID) {
__fadeOut.add(__fadeIn[1])
__fadeIn = []
} else {
__fadeOut.add(1)
}
}
static update() {
if (__fadeIn.count > 0) {
__fadeIn[1] = __fadeIn[1] + 0.01
AudioEngine.setChannelVolume(__fadeIn[0], __fadeIn[1])
if (__fadeIn[1] >= 1.0) {
__fadeIn = []
}
}
if (__fadeOut.count > 0) {
__fadeOut[1] = __fadeOut[1] - 0.01
AudioEngine.setChannelVolume(__fadeOut[0], __fadeOut[1])
if (__fadeOut[1] <= 0) {
AudioEngine.stopChannel(__fadeOut[0])
__fadeOut = []
}
}
}
static isPlaying(channelID) { AudioEngine.isPlaying(channelID) }
static stopAllChannels() { AudioEngine.stopAllChannels() }
}
class AnimatedSprite is Sprite {
construct new(filenames, center) {
_center = center
setAnimation(filenames)
}
construct new(filenames) {
_center = false
setAnimation(filenames)
}
setAnimation(filenames) {
_count = 0
speed = 5
_frameNumber = 0
_animationFrames = []
for (file in filenames) {
_animationFrames.add(Sprite.new(file, _center))
}
}
draw(x,y) {
_animationFrames[_frameNumber].draw(x,y)
_loc = Point.new(x, y)
}
speed=(speed) { _speed = speed }
getSpeed { _speed }
update() {
_count = _count + 1
if (_count == getSpeed) {
_frameNumber=_frameNumber+1
if (_frameNumber == _animationFrames.count) { _frameNumber = 0 }
_count = 0
}
}
getSize() {
if (_loc != null) {
return [_loc, Point.new(_loc.x + _animationFrames[_frameNumber].width, _loc.y + _animationFrames[_frameNumber].height)]
}
}
}