-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
174 lines (151 loc) · 6.7 KB
/
utils.py
File metadata and controls
174 lines (151 loc) · 6.7 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
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
import arcade
from random import *
from arcade import AnimationKeyframe
def createSound(fileName):
snd = arcade.load_sound(fileName)
return snd
def drawText(params):
# retrieve parameters
x = params["x"]
y = params["y"]
message = params["message"]
size = 12 if "size" not in params else params["size" ]
color = (255,255,255,255) if "color" not in params else params["color" ]
alignH = "center" if "alignH" not in params else params["alignH"] # left, center, right
alignV = "center" if "alignV" not in params else params["alignV"] # top, center, bottom
angle = 0 if "angle" not in params else params["angle" ]
bold = False if "bold" not in params else params["bold" ]
italic = False if "italic" not in params else params["italic"]
# draw text according to configuration
arcade.draw_text(text=message,start_x=x,start_y=y,color=color,font_size=size,anchor_x=alignH,anchor_y=alignV,rotation=angle,bold=bold,italic=italic)
def createFixedSprite(params):
# retrieve parameters
filePath = params["filePath" ]
size = None if "size" not in params else params["size"]
filterColor = (255,255,255,255) if "filterColor" not in params else params["filterColor"]
isMaxRatio = False if "isMaxRatio" not in params else params["isMaxRatio"]
position = (0,0) if "position" not in params else params["position"]
# load texture for sprite
spr = arcade.AnimatedTimeSprite()
spr.color = filterColor
spr.append_texture(arcade.load_texture(filePath))
# set dimensions
spr.update_animation()
if size != None:
if isMaxRatio:
ratio = max(size[0] / spr.width, size[1] / spr.height)
else:
ratio = min(size[0]/spr.width, size[1]/spr.height)
spr.scale = ratio
# set position (init)
spr.center_x = position[0]
spr.center_y = position[1]
return spr
def createAnimatedSprite(params):
# retrieve parameters
filePath = params["filePath" ]
size = None if "size" not in params else params["size"]
filterColor = (255, 255, 255, 255) if "filterColor" not in params else params["filterColor"]
isMaxRatio = False if "isMaxRatio" not in params else params["isMaxRatio"]
position = (0, 0) if "position" not in params else params["position"]
spriteBox = params["spriteBox" ]
startIndex = params["startIndex"]
endIndex = params["endIndex" ]
frameduration = 1/60 if "frameDuration" not in params else params["frameDuration"]
flipH = False if "flipH" not in params else params["flipH"]
flipV = False if "flipv" not in params else params["flipV"]
# get sprite box (nb sprites X, nb Y, size X size Y)
nbX, nbY, szW, szH = spriteBox
# Instanciate sprite object
spr = arcade.AnimatedTimeSprite()
spr.color = filterColor
# Read Horizontal first, then vertical
for y in range(nbY):
for x in range(nbX):
index = x + y*nbX
# add index only if in range
if index >= startIndex and index <= endIndex:
tex = arcade.load_texture(filePath, x * szW, y * szH, szW, szH, flipped_horizontally=flipH, flipped_vertically=flipV)
spr.textures.append(tex)
# set dimensions
spr.update_animation()
spr.center_x = position[0]
spr.center_y = position[1]
if size != None:
if isMaxRatio:
ratio = max(size[0]/spr.width, size[1]/spr.height)
else:
ratio = min(size[0]/spr.width, size[1]/spr.height)
spr.scale = ratio
# set frame duration
spr.texture_change_frames = int(frameduration*60 + 0.5)
# return sprite object
return spr
def createParticleBurst(params):
# retrieve parameters
x0 = params["x0" ]
y0 = params["y0" ]
partSize = params["partSize" ]
partScale = params["partScale" ]
partSpeed = params["partSpeed" ]
color = params["color" ]
startAlpha = params["startAlpha" ]
endAlpha = params["endAlpha" ]
imagePath = None if "imagePath" not in params else params["imagePath"]
partInterval = params["partInterval" ]
totalDuration = params["totalDuration"]
# create particle emitter
e = arcade.Emitter(
center_xy=(x0, y0),
emit_controller=arcade.EmitterIntervalWithTime(partInterval, totalDuration),
particle_factory=lambda emitter: arcade.FadeParticle(
filename_or_texture=imagePath if imagePath is not None else arcade.make_circle_texture(partSize, color),
change_xy=arcade.rand_in_circle((0.0, 0.0), partSpeed),
scale=partScale,
lifetime=uniform(totalDuration/4, totalDuration),
start_alpha=startAlpha,
end_alpha=endAlpha,
),
)
# return result
return e
def createParticleEmitter(params):
# retrieve parameters
x0 = params["x0" ]
y0 = params["y0" ]
partSize = params["partSize" ]
partScale = params["partScale" ]
partSpeed = params["partSpeed" ]
color = params["color" ]
startAlpha = params["startAlpha" ]
endAlpha = params["endAlpha" ]
partNB = params["partNB" ]
maxLifeTime = params["maxLifeTime" ]
imagePath = None if "imagePath" not in params else params["imagePath"]
spriteBox = None if "spriteBox" not in params else params["spriteBox"]
spriteSelect = None if "spriteSelect" not in params else params["spriteSelect"]
flipH = False if "flipH" not in params else params["flipH"]
flipV = False if "flipv" not in params else params["flipV"]
# Prepare Texture
if imagePath == None:
tex = arcade.make_circle_texture(partSize, color)
else:
nbX, nbY, szW, szH = spriteBox
x, y = spriteSelect
tex = arcade.load_texture(imagePath, x * szW, y * szH, szW, szH,
flipped_horizontally=flipH,
flipped_vertically=flipV)
# Create emitter
e = arcade.Emitter(
center_xy = (x0, y0),
emit_controller = arcade.EmitMaintainCount(partNB),
particle_factory = lambda emitter: arcade.FadeParticle(
filename_or_texture = tex,
change_xy = arcade.rand_in_circle( (0.0,0.0), partSpeed),
lifetime = uniform(maxLifeTime/40,maxLifeTime),
scale = partScale,
start_alpha=startAlpha,
end_alpha=endAlpha,
),
)
return e