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
|
# Sprites
Sprites are the most important visual component of a game.
## Sample Apps Related to Sprite Usage (ordered by size of codebase increasing)
- 01_api_04_sprites
- 01_api_99_tech_demo (includes recording)
- 02_sprite_animation_and_keyboard_input (includes recording)
- 08_platformer_collisions_metroidvania
- 09_controller_analog_usage_advanced_sprites
- 99_sample_game_basic_gorillas (includes recording)
- 99_sample_game_dueling_starships (includes recording)
- 99_sample_game_flappy_dragon (includes recording)
- 99_sample_game_return_of_serenity
## Minimum Code
Sprites need to be under the `mygame` directory. It's recommended that you create a `sprites` folder
to keep things organized. All sprites must be `.png` files
Here is how you create an sprite with located at 100, 100, that is 32 pixels wide and 64 pixels tall.
In this example the sprite name is `player.png` and is located under a directory `mygame/sprites`.
```ruby
# X Y WIDTH HEIGHT PATH
args.outputs.sprites << [100, 100, 32, 64, "sprites/player.png"]
```
## Rotation / Angle
Unlike `solids` and `borders`, sprites can be rotated. This is how you rotate a sprite 90 degress.
Note: All angles in DragonRuby Game Toolkit are represented in degrees (not radians).
```ruby
# X Y WIDTH HEIGHT PATH ANGLE
args.outputs.sprites << [100, 100, 32, 64, "sprites/player.png", 90]
```
## Alpha
Sprites can also have a transparency associated with them. The transparency value must come after
the angle value and supports a number between 0 and 255.
This is how you would define a sprite with no rotation, and a 50% transparency.
```ruby
# X Y WIDTH HEIGHT PATH ANGLE ALPHA
args.outputs.sprites << [100, 100, 32, 64, "sprites/player.png", 0, 128]
```
## Color Saturations
A Sprite's color levels can be changed. The color saturations must come after `angle` and
`alpha` values.
This is a sprite with no rotation, fully opaque, and with a green tint.
```ruby
args.outputs.sprites << [100, # X
100, # Y
32, # W
64, # H
"sprites/player.png", # PATH
0, # ANGLE
255, # ALPHA
0, # RED_SATURATION
255, # GREEN_SATURATION
0] # BLUE_SATURATION
```
## Sprite Sub Division / Tile
You can render a portion of a sprite (a tile). The sub division of the sprite is denoted as a rectangle
directly related to the original size of the png.
This is a sprite scaled to 100 pixels where the source rectangle is located at the bottom left corner
within a 32 pixel square. The angle, opacity, and color levels of the tile are unaltered.
**For these advanced transforms, you should use a `Hash` instead of an `Array`.**
```ruby
args.outputs.sprites << {
x: 100,
y: 100,
w: 100,
h: 100,
path: "sprites/player.png",
source_x: 0,
source_y: 0,
source_w: 32,
source_h: 32,
}
```
## Flipping a Sprite Horizontally and Vertically
A sprite can be flipped horizontally and vertically.
This is a sprite that has been flipped horizontally. The sprites's angle, alpha, color saturations,
and source rectangls are unaltered.
**For these advanced transforms, you should use a `Hash` instead of an `Array`.**
```ruby
args.outputs.sprites << {
x: 100,
y: 100,
w: 100,
h: 100,
path: "sprites/player.png",
flip_horizontally: true,
}
```
This is a sprite that has been flipped vertically. The sprites's angle, alpha, color saturations,
and tile subdivision are unaltered.
```ruby
args.outputs.sprites << {
x: 100,
y: 100,
w: 100,
h: 100,
path: "sprites/player.png",
flip_vertically: true,
}
```
## Rotation Center
A sprites center of rotation can be altered.
This is a sprite that has its rotation center set to the top-middle. The sprites's angle, alpha, color saturations,
source rectangle subdivision, and projections are unaltered.
```ruby
args.outputs.sprites << {
x: 100,
y: 100,
w: 100,
h: 100,
path: "sprites/player.png",
angle: 0,
angle_anchor_x: 0.5,
angle_anchor_y: 1.0
}
```
## Hash
Here are all of the properites that are available on a sprite Hash.
Any parameters that are not specified will be given a default value. The keys of the hash can
be provided in any order.
```ruby
args.outputs.sprites << {
x: 100,
y: 100,
w: 100,
h: 100,
path: "sprites/player.png",
angle: 0,
a, 255,
r: 255,
g: 255,
b: 255,
source_x: 0,
source_y: 0,
source_w: -1,
source_h: -1,
flip_vertically: false,
flip_horizontally: false,
angle_anchor_x: 0.5,
angle_anchor_y: 1.0
}
```
## Duck Typing (Advanced)
You can also create a class with sprite properties and render it as a primitive,
using the `attr_sprite` helper.
Here is an example:
```ruby
class PlayerSprite
attr_sprite
# constructor
def initialize x, y, w, h
self.x = x
self.y = y
self.w = w
self.h = h
self.path = 'sprites/player.png'
end
end
#render player sprite
args.outputs.sprites << PlayerSprite.new(10, 10, 32, 64)
```
|