fantasy 0.1.5.1 → 0.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +32 -1
- data/COLOR_PALETTE.md +159 -0
- data/README.md +59 -9
- data/lib/fantasy/actor.rb +170 -40
- data/lib/fantasy/clock.rb +1 -0
- data/lib/fantasy/color.rb +182 -1
- data/lib/fantasy/coordinates.rb +4 -0
- data/lib/fantasy/cursor.rb +21 -0
- data/lib/fantasy/global.rb +30 -4
- data/lib/fantasy/hud_image.rb +1 -1
- data/lib/fantasy/hud_text.rb +43 -11
- data/lib/fantasy/image.rb +2 -0
- data/lib/fantasy/includes/gravitier.rb +5 -0
- data/lib/fantasy/includes/jumper.rb +10 -0
- data/lib/fantasy/includes/move_by_cursors.rb +39 -11
- data/lib/fantasy/includes/move_by_direction.rb +5 -0
- data/lib/fantasy/includes/mover.rb +20 -0
- data/lib/fantasy/includes/user_inputs.rb +60 -0
- data/lib/fantasy/loop.rb +58 -8
- data/lib/fantasy/mouse.rb +9 -0
- data/lib/fantasy/music.rb +51 -0
- data/lib/fantasy/shape.rb +55 -0
- data/lib/fantasy/sound.rb +2 -0
- data/lib/fantasy/{tile_map.rb → tilemap.rb} +10 -4
- data/lib/fantasy/utils.rb +5 -0
- data/lib/fantasy/version.rb +1 -1
- data/lib/fantasy.rb +12 -2
- metadata +13 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb68d8561352d746b2ccdbec94196c568436a0164619f325f0762c200d9d4a71
|
4
|
+
data.tar.gz: a11a5b19442edc60fc11d3c803ec40df543c9f1a176f7f7d6a3b11ad4dcf2eed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e705ad65f134cf775e3bf2ef847643bbd8a2efcc49630837f555f57df019573c9356190db394cddb647ed95420441ea0ce8d42099ba43f006c89e05b63452fa9
|
7
|
+
data.tar.gz: c6e6506581053c11940b0fd3bed2205ae4b003a2efd35016a84e04640cc75ad9300a8276ff8562ec8f33eba229b3017c7ba4f38306d165271d637e7c0e51b66a
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,36 @@
|
|
1
|
-
## [
|
1
|
+
## [0.1.11] - 2022-03-26
|
2
|
+
|
3
|
+
- Better jumping and gravity physics
|
4
|
+
- Support Shape.rectangle
|
5
|
+
- Support on_click callback on Shapes and Actors
|
6
|
+
- Support on_cursor_* callbacks on Shapes and Actors
|
7
|
+
- Support Color.palette with 150 colors + transparent
|
8
|
+
|
9
|
+
|
10
|
+
## [0.1.9] - 2022-03-25
|
11
|
+
|
12
|
+
- Adding platformer support
|
13
|
+
- Jump
|
14
|
+
- Gravity
|
15
|
+
- Music background support
|
16
|
+
|
17
|
+
|
18
|
+
## [0.1.7] - 2022-03-17
|
19
|
+
|
20
|
+
- HudText accepts alignment property. Valid values: 'top-left, top-right, center'
|
21
|
+
|
22
|
+
|
23
|
+
## [0.1.5] - 2022-03-16
|
24
|
+
|
25
|
+
- Background color or image
|
26
|
+
- Tilemap
|
27
|
+
- Camera can be controlled by cursors
|
28
|
+
- Background image can be replicable
|
29
|
+
|
2
30
|
|
3
31
|
## [0.1.0] - 2022-03-11
|
4
32
|
|
5
33
|
- Initial release
|
34
|
+
|
35
|
+
|
36
|
+
## [Unreleased]
|
data/COLOR_PALETTE.md
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
# Color.palette
|
2
|
+
|
3
|
+
```ruby
|
4
|
+
Color.palette.orange
|
5
|
+
Color.palette.crimson
|
6
|
+
Color.palette.transparent
|
7
|
+
[...]
|
8
|
+
```
|
9
|
+
|
10
|
+
-  orange
|
11
|
+
-  red
|
12
|
+
-  orange_red
|
13
|
+
-  tomato
|
14
|
+
-  dark_red
|
15
|
+
-  fire_brick
|
16
|
+
-  crimson
|
17
|
+
-  deep_pink
|
18
|
+
-  maroon
|
19
|
+
-  indian_red
|
20
|
+
-  medium_violet_red
|
21
|
+
-  violet_red
|
22
|
+
-  light_coral
|
23
|
+
-  hot_pink
|
24
|
+
-  pale_violet_red
|
25
|
+
-  light_pink
|
26
|
+
-  rosy_brown
|
27
|
+
-  pink
|
28
|
+
-  orchid
|
29
|
+
-  lavender_blush
|
30
|
+
-  snow
|
31
|
+
-  chocolate
|
32
|
+
-  saddle_brown
|
33
|
+
-  brown
|
34
|
+
-  dark_orange
|
35
|
+
-  coral
|
36
|
+
-  sienna
|
37
|
+
-  salmon
|
38
|
+
-  peru
|
39
|
+
-  dark_goldenrod
|
40
|
+
-  goldenrod
|
41
|
+
-  sandy_brown
|
42
|
+
-  light_salmon
|
43
|
+
-  dark_salmon
|
44
|
+
-  gold
|
45
|
+
-  yellow
|
46
|
+
-  olive
|
47
|
+
-  burlywood
|
48
|
+
-  tan
|
49
|
+
-  navajo_white
|
50
|
+
-  peach_puff
|
51
|
+
-  khaki
|
52
|
+
-  dark_khaki
|
53
|
+
-  moccasin
|
54
|
+
-  wheat
|
55
|
+
-  bisque
|
56
|
+
-  pale_goldenrod
|
57
|
+
-  blanched_almond
|
58
|
+
-  medium_goldenrod
|
59
|
+
-  papaya_whip
|
60
|
+
-  misty_rose
|
61
|
+
-  lemon_chiffon
|
62
|
+
-  antique_white
|
63
|
+
-  cornsilk
|
64
|
+
-  light_goldenrod_yellow
|
65
|
+
-  old_lace
|
66
|
+
-  linen
|
67
|
+
-  light_yellow
|
68
|
+
-  seashell
|
69
|
+
-  beige
|
70
|
+
-  floral_white
|
71
|
+
-  ivory
|
72
|
+
-  green
|
73
|
+
-  lawn_green
|
74
|
+
-  chartreuse
|
75
|
+
-  green_yellow
|
76
|
+
-  yellow_green
|
77
|
+
-  medium_forest_green
|
78
|
+
-  olive_drab
|
79
|
+
-  dark_olive_green
|
80
|
+
-  dark_sea_green
|
81
|
+
-  lime
|
82
|
+
-  dark_green
|
83
|
+
-  lime_green
|
84
|
+
-  forest_green
|
85
|
+
-  spring_green
|
86
|
+
-  medium_spring_green
|
87
|
+
-  sea_green
|
88
|
+
-  medium_sea_green
|
89
|
+
-  aquamarine
|
90
|
+
-  light_green
|
91
|
+
-  pale_green
|
92
|
+
-  medium_aquamarine
|
93
|
+
-  turquoise
|
94
|
+
-  light_sea_green
|
95
|
+
-  medium_turquoise
|
96
|
+
-  honeydew
|
97
|
+
-  mint_cream
|
98
|
+
-  royal_blue
|
99
|
+
-  dodger_blue
|
100
|
+
-  deep_sky_blue
|
101
|
+
-  cornflower_blue
|
102
|
+
-  steel_blue
|
103
|
+
-  light_sky_blue
|
104
|
+
-  dark_turquoise
|
105
|
+
-  cyan
|
106
|
+
-  aqua
|
107
|
+
-  dark_cyan
|
108
|
+
-  teal
|
109
|
+
-  sky_blue
|
110
|
+
-  cadet_blue
|
111
|
+
-  dark_slate_gray
|
112
|
+
-  dark_slate_grey
|
113
|
+
-  light_slate_gray
|
114
|
+
-  light_slate_grey
|
115
|
+
-  slate_gray
|
116
|
+
-  slate_grey
|
117
|
+
-  light_steel_blue
|
118
|
+
-  light_blue
|
119
|
+
-  powder_blue
|
120
|
+
-  pale_turquoise
|
121
|
+
-  light_cyan
|
122
|
+
-  alice_blue
|
123
|
+
-  azure
|
124
|
+
-  medium_blue
|
125
|
+
-  dark_blue
|
126
|
+
-  midnight_blue
|
127
|
+
-  navy
|
128
|
+
-  blue
|
129
|
+
-  indigo
|
130
|
+
-  blue_violet
|
131
|
+
-  medium_slate_blue
|
132
|
+
-  slate_blue
|
133
|
+
-  purple
|
134
|
+
-  dark_slate_blue
|
135
|
+
-  dark_violet
|
136
|
+
-  dark_orchid
|
137
|
+
-  medium_purple
|
138
|
+
-  medium_orchid
|
139
|
+
-  magenta
|
140
|
+
-  fuchsia
|
141
|
+
-  dark_magenta
|
142
|
+
-  violet
|
143
|
+
-  plum
|
144
|
+
-  lavender
|
145
|
+
-  thistle
|
146
|
+
-  ghost_white
|
147
|
+
-  white
|
148
|
+
-  white_smoke
|
149
|
+
-  gainsboro
|
150
|
+
-  light_gray
|
151
|
+
-  light_grey
|
152
|
+
-  silver
|
153
|
+
-  gray
|
154
|
+
-  grey
|
155
|
+
-  dark_gray
|
156
|
+
-  dark_grey
|
157
|
+
-  dim_gray
|
158
|
+
-  dim_grey
|
159
|
+
-  black
|
data/README.md
CHANGED
@@ -116,12 +116,13 @@ Managing game elements which have (optionally) image, movement and collision
|
|
116
116
|
- Easy to set an `image`
|
117
117
|
- Managing movement through `direction` and `speed`
|
118
118
|
- Built-in movement control through cursors
|
119
|
-
- Collision detection, OnCollision callback
|
120
|
-
- Jump
|
121
|
-
- Gravity
|
119
|
+
- Collision detection, OnCollision callback, Collision matrix
|
120
|
+
- Jump
|
121
|
+
- Gravity
|
122
122
|
- Animations (TODO)
|
123
123
|
- Possibility to extend Actor class or instantiate it directly for simple characters
|
124
124
|
- Allowing magic instance properties (Like in OpenStruct). So programmer can do `actor.stuff = 1` and it is valid (TODO)
|
125
|
+
- Alignment "center" and "top-left" (TODO)
|
125
126
|
|
126
127
|
### Clock
|
127
128
|
|
@@ -181,9 +182,9 @@ Actors in the game will be rendered in the relative position to this camera.
|
|
181
182
|
- Allow camera move
|
182
183
|
- Easy to follow one actor
|
183
184
|
|
184
|
-
### Colors palette
|
185
|
+
### Colors palette
|
185
186
|
|
186
|
-
- Not deal with RGB or anything, just a list of colors (
|
187
|
+
- Not deal with RGB or anything, just a list of colors. See [Color.palette](COLOR_PALETTE.md)
|
187
188
|
|
188
189
|
|
189
190
|
### Pause Game (TODO)
|
@@ -196,7 +197,9 @@ Move the core functions to the top level hierarchy so I don't need to create a `
|
|
196
197
|
|
197
198
|
### Sound
|
198
199
|
|
199
|
-
Direct and easy way to play a sound
|
200
|
+
- Direct and easy way to play a sound
|
201
|
+
- Direct and easy way to play a background music
|
202
|
+
|
200
203
|
|
201
204
|
### Background
|
202
205
|
|
@@ -219,12 +222,13 @@ Easy access to keyboard and mouse inputs on any part of the code. Specially in t
|
|
219
222
|
- Remove "on_space_bar" when changing scene (TODO)
|
220
223
|
- Detect when key/mouse button is pressed in the actual frame in any part of the code (TODO)
|
221
224
|
|
222
|
-
###
|
225
|
+
### Tilemap (TODO)
|
223
226
|
|
224
227
|
For easy creation of:
|
225
228
|
|
226
|
-
- Top-down map levels
|
229
|
+
- Top-down map levels
|
227
230
|
- Platformer map levels (TODO)
|
231
|
+
- Allow specific map keys like a=>actor_1, b=>actor_2 (TODO)
|
228
232
|
|
229
233
|
### Tweens (TODO)
|
230
234
|
|
@@ -301,6 +305,7 @@ player.solid = true
|
|
301
305
|
player.speed = 200
|
302
306
|
player.layer = 1
|
303
307
|
player.move_with_cursors
|
308
|
+
player.collision_with = ["enemy", "bullets"] # default "all"
|
304
309
|
|
305
310
|
player.on_collision do |other|
|
306
311
|
if other.name == "enemy"
|
@@ -330,6 +335,7 @@ class Player < Actor
|
|
330
335
|
@speed = 200
|
331
336
|
@layer = 1
|
332
337
|
@direction = Coordinates.zero
|
338
|
+
@collision_with = ["enemy", "bullets"] # default "all"
|
333
339
|
move_with_cursors
|
334
340
|
end
|
335
341
|
|
@@ -351,6 +357,25 @@ class Player < Actor
|
|
351
357
|
end
|
352
358
|
```
|
353
359
|
|
360
|
+
Actor settings for a platformer:
|
361
|
+
|
362
|
+
```ruby
|
363
|
+
player = Actor.new("warrior") # ./images/warrior.png
|
364
|
+
player.solid = true
|
365
|
+
player.jump = 150
|
366
|
+
player.gravity = 200
|
367
|
+
player.move_with_cursors(left: true, right: true, up: false, down: false, jump: true)
|
368
|
+
|
369
|
+
player.on_jumping do
|
370
|
+
Sound.play("jump")
|
371
|
+
player.image = "warrior_jump")
|
372
|
+
end
|
373
|
+
|
374
|
+
player.on_floor do
|
375
|
+
player.image = "warrior")
|
376
|
+
end
|
377
|
+
```
|
378
|
+
|
354
379
|
### Clock
|
355
380
|
|
356
381
|
```ruby
|
@@ -397,18 +422,43 @@ end
|
|
397
422
|
|
398
423
|
### Sound
|
399
424
|
|
425
|
+
Plays only once:
|
426
|
+
|
400
427
|
```ruby
|
401
428
|
Sound.play("shoot") # ./sounds/shoot.wav
|
402
429
|
```
|
403
430
|
|
431
|
+
Plays in loop:
|
432
|
+
|
433
|
+
```ruby
|
434
|
+
Music.play("music") # ./musics/music.mp3
|
435
|
+
Music.volume = 0.5
|
436
|
+
Music.stop
|
437
|
+
```
|
438
|
+
|
439
|
+
### Tilemap
|
440
|
+
|
441
|
+
```ruby
|
442
|
+
# ./maps/sky.txt
|
443
|
+
#0 0 0
|
444
|
+
# 1 0
|
445
|
+
#
|
446
|
+
#0 0 0
|
447
|
+
|
448
|
+
planet = Actor.new("planet")
|
449
|
+
star = Actor.new("star")
|
450
|
+
map = Tilemap.new(map_name: "sky", tiles: [planet, star], tile_size: 30)
|
451
|
+
map.spawn
|
452
|
+
```
|
404
453
|
|
405
454
|
### UI
|
406
455
|
|
407
456
|
#### HUD Text
|
408
457
|
|
409
458
|
```ruby
|
410
|
-
timer = HudText.new(position: Coordinates.new(
|
459
|
+
timer = HudText.new(position: Coordinates.new(SCREEN_WIDTH / 2, 10))
|
411
460
|
timer.text = 0
|
461
|
+
timer.alignment = "center"
|
412
462
|
timer.size = "big"
|
413
463
|
|
414
464
|
Clock.new { timer.text += 1 }.repeat(seconds: 1)
|
data/lib/fantasy/actor.rb
CHANGED
@@ -1,26 +1,41 @@
|
|
1
1
|
class Actor
|
2
2
|
include MoveByCursor
|
3
|
+
include MoveByDirection
|
4
|
+
include Mover
|
5
|
+
include Gravitier
|
6
|
+
include Jumper
|
7
|
+
include UserInputs
|
3
8
|
|
4
9
|
attr_reader :image, :moving_with_cursors
|
5
|
-
attr_accessor :image_name, :position, :direction, :speed, :solid, :scale, :name, :layer
|
10
|
+
attr_accessor :image_name, :position, :direction, :speed, :jump_force, :gravity, :solid, :scale, :name, :layer
|
11
|
+
attr_accessor :collision_with
|
6
12
|
|
7
13
|
def initialize(image_name)
|
8
14
|
@image_name = image_name
|
9
15
|
@image = Image.new(image_name)
|
10
16
|
@name = image_name
|
11
|
-
@position = Coordinates.
|
12
|
-
@direction = Coordinates.
|
17
|
+
@position = Coordinates.zero
|
18
|
+
@direction = Coordinates.zero
|
13
19
|
@speed = 0
|
14
20
|
@scale = 1
|
15
|
-
|
21
|
+
|
16
22
|
@moving_with_cursors = false
|
17
23
|
@solid = false
|
18
24
|
@draggable_on_debug = true
|
19
25
|
@dragging = false
|
20
26
|
@dragging_offset = nil
|
21
27
|
@layer = 0
|
28
|
+
@gravity = 0
|
29
|
+
@jump_force = 0
|
30
|
+
@collision_with = "all"
|
22
31
|
|
32
|
+
@on_floor = false
|
33
|
+
|
34
|
+
@on_after_move_callback = nil
|
23
35
|
@on_collision_callback = nil
|
36
|
+
@on_destroy_callback = nil
|
37
|
+
@on_jumping_callback = nil
|
38
|
+
@on_floor_callback = nil
|
24
39
|
|
25
40
|
Global.actors << self
|
26
41
|
end
|
@@ -30,20 +45,16 @@ class Actor
|
|
30
45
|
end
|
31
46
|
|
32
47
|
def width
|
33
|
-
@image.width
|
48
|
+
@image.width * @scale
|
34
49
|
end
|
35
50
|
|
36
51
|
def height
|
37
|
-
@image.height
|
38
|
-
end
|
39
|
-
|
40
|
-
def move_with_cursors
|
41
|
-
@moving_with_cursors = true
|
52
|
+
@image.height * @scale
|
42
53
|
end
|
43
54
|
|
44
55
|
def direction=(value)
|
45
56
|
@direction = value
|
46
|
-
@direction = @direction.normalize
|
57
|
+
@direction = @direction.normalize unless @direction.zero?
|
47
58
|
end
|
48
59
|
|
49
60
|
def draw
|
@@ -54,13 +65,32 @@ class Actor
|
|
54
65
|
|
55
66
|
def draw_debug
|
56
67
|
Utils.draw_frame(position_in_camera.x, position_in_camera.y, width, height, 1, Gosu::Color::RED) if solid
|
57
|
-
Global.
|
68
|
+
Global.pixel_fonts["medium"].draw_text("#{@position.x.floor},#{@position.y.floor}", position_in_camera.x, position_in_camera.y - 20, 1)
|
58
69
|
end
|
59
70
|
|
60
71
|
def position_in_camera
|
61
72
|
@position - Global.camera.position
|
62
73
|
end
|
63
74
|
|
75
|
+
# TODO: make this work optimized
|
76
|
+
# def position_top_left
|
77
|
+
# @position - position_delta
|
78
|
+
# end
|
79
|
+
|
80
|
+
# def position_delta
|
81
|
+
# case @alignment
|
82
|
+
# when "top-left"
|
83
|
+
# Coordinates.zero
|
84
|
+
# when "center"
|
85
|
+
# Coordinates.new(width/2, height/2)
|
86
|
+
# else
|
87
|
+
# raise "Actor.alignment value not valid '#{@alignment}'. Valid values: 'top-left, center'"
|
88
|
+
# end
|
89
|
+
# end
|
90
|
+
|
91
|
+
# TODO: I made more of this code while I was with Covid
|
92
|
+
# It looks horrible and it is crap
|
93
|
+
# I'll improve it some day :)
|
64
94
|
def move
|
65
95
|
mouse_position = Global.mouse_position + Global.camera.position
|
66
96
|
|
@@ -76,30 +106,91 @@ class Actor
|
|
76
106
|
if @dragging
|
77
107
|
@position = mouse_position - @dragging_offset
|
78
108
|
else
|
79
|
-
|
109
|
+
# Direction moving
|
110
|
+
unless @direction.zero?
|
111
|
+
last_position = @position
|
112
|
+
move_by_direction
|
113
|
+
|
114
|
+
# Check collision after cursor moving
|
115
|
+
if @solid && @position != last_position
|
116
|
+
manage_collisions(last_position)
|
117
|
+
end
|
118
|
+
end
|
80
119
|
|
81
|
-
|
82
|
-
|
83
|
-
|
120
|
+
# Cursors moving
|
121
|
+
last_position = @position
|
122
|
+
move_by_cursors
|
84
123
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
end
|
124
|
+
# Check collision after cursor moving
|
125
|
+
if @solid && @position != last_position
|
126
|
+
manage_collisions(last_position)
|
127
|
+
end
|
90
128
|
|
91
|
-
|
92
|
-
|
129
|
+
# # Jump moving
|
130
|
+
# unless @jump_force.zero?
|
131
|
+
# last_position = @position
|
132
|
+
# add_force_by_jump
|
133
|
+
# apply_forces(max_speed: @jump_speed || @jump_force)
|
134
|
+
|
135
|
+
# # Check collision after jump moving
|
136
|
+
# if @solid && @position != last_position
|
137
|
+
# if manage_collisions(last_position)
|
138
|
+
# @jumping = false
|
139
|
+
# end
|
140
|
+
# end
|
141
|
+
# end
|
142
|
+
|
143
|
+
# Gravity force
|
144
|
+
if !@gravity.zero?
|
145
|
+
add_force_by_gravity
|
146
|
+
end
|
147
|
+
|
148
|
+
# Apply forces
|
149
|
+
last_position = @position
|
150
|
+
apply_forces(max_speed: @speed)
|
151
|
+
|
152
|
+
# Check collision after gravity moving
|
153
|
+
if @solid && @position != last_position
|
154
|
+
@on_floor = false
|
155
|
+
manage_collisions(last_position)
|
93
156
|
end
|
94
157
|
end
|
95
158
|
|
96
|
-
|
159
|
+
on_after_move_do
|
160
|
+
end
|
161
|
+
|
162
|
+
def manage_collisions(last_position)
|
163
|
+
collisions.each do |other|
|
164
|
+
on_collision_do(other)
|
165
|
+
other.on_collision_do(self)
|
166
|
+
|
167
|
+
if other.position.y > (last_position.y + height)
|
168
|
+
on_floor_do unless @on_floor
|
169
|
+
|
170
|
+
@on_floor = true
|
171
|
+
@jumping = false
|
172
|
+
@velocity.y = 0
|
173
|
+
end
|
174
|
+
|
175
|
+
if other.position.y + other.height < last_position.y
|
176
|
+
@velocity.y = 0
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
if collisions.any? # we don't cache collisions because position may be changed on collision callback
|
181
|
+
@position = last_position
|
182
|
+
|
183
|
+
return true
|
184
|
+
end
|
185
|
+
|
186
|
+
false
|
97
187
|
end
|
98
188
|
|
99
189
|
def solid?
|
100
190
|
@solid
|
101
191
|
end
|
102
192
|
|
193
|
+
# Set callbacks
|
103
194
|
def on_after_move(&block)
|
104
195
|
@on_after_move_callback = block
|
105
196
|
end
|
@@ -112,18 +203,50 @@ class Actor
|
|
112
203
|
@on_destroy_callback = block
|
113
204
|
end
|
114
205
|
|
115
|
-
def
|
116
|
-
@
|
206
|
+
def on_jumping(&block)
|
207
|
+
@on_jumping_callback = block
|
117
208
|
end
|
118
209
|
|
210
|
+
def on_floor(&block)
|
211
|
+
@on_floor_callback = block
|
212
|
+
end
|
213
|
+
|
214
|
+
|
215
|
+
# Execute callbacks
|
216
|
+
def on_after_move_do
|
217
|
+
instance_exec(&@on_after_move_callback) unless @on_after_move_callback.nil?
|
218
|
+
end
|
219
|
+
|
220
|
+
def on_collision_do(other)
|
221
|
+
instance_exec(other, &@on_collision_callback) unless @on_collision_callback.nil?
|
222
|
+
end
|
223
|
+
|
224
|
+
def on_destroy_do
|
225
|
+
instance_exec(&@on_destroy_callback) unless @on_destroy_callback.nil?
|
226
|
+
end
|
227
|
+
|
228
|
+
def on_jumping_do
|
229
|
+
instance_exec(&@on_jumping_callback) unless @on_jumping_callback.nil?
|
230
|
+
end
|
231
|
+
|
232
|
+
def on_floor_do
|
233
|
+
instance_exec(&@on_floor_callback) unless @on_floor_callback.nil?
|
234
|
+
end
|
235
|
+
|
236
|
+
|
119
237
|
def collisions
|
120
|
-
Global.actors.reject { |e| e == self }.select { |e| e.solid? }.select do |
|
121
|
-
|
238
|
+
Global.actors.reject { |e| e == self }.select { |e| e.solid? }.select do |other|
|
239
|
+
if(
|
240
|
+
(@collision_with == "all" || @collision_with.include?(other.name)) &&
|
241
|
+
(other.collision_with == "all" || other.collision_with.include?(self.name))
|
242
|
+
)
|
243
|
+
Utils.collision? self, other
|
244
|
+
end
|
122
245
|
end
|
123
246
|
end
|
124
247
|
|
125
248
|
def destroy
|
126
|
-
|
249
|
+
on_destroy_do
|
127
250
|
Global.actors.delete(self)
|
128
251
|
end
|
129
252
|
|
@@ -133,30 +256,37 @@ class Actor
|
|
133
256
|
actor.name = @name
|
134
257
|
actor.position = @position.clone
|
135
258
|
actor.direction = @direction.clone
|
259
|
+
|
136
260
|
actor.speed = @speed
|
137
261
|
actor.scale = @scale
|
138
262
|
actor.moving_with_cursors if @moving_with_cursors
|
139
263
|
actor.solid = @solid
|
140
264
|
actor.layer = @layer
|
265
|
+
actor.gravity = @gravity
|
266
|
+
actor.jump_force = @jump_force
|
267
|
+
actor.collision_with = @collision_with
|
141
268
|
|
142
269
|
actor.on_after_move_callback = @on_after_move_callback
|
143
270
|
actor.on_collision_callback = @on_collision_callback
|
144
271
|
actor.on_destroy_callback = @on_destroy_callback
|
272
|
+
actor.on_jumping_callback = @on_jumping_callback
|
273
|
+
actor.on_floor_callback = @on_floor_callback
|
145
274
|
|
146
|
-
actor
|
147
|
-
|
275
|
+
actor.on_cursor_down_callback = @on_cursor_down_callback
|
276
|
+
actor.on_cursor_up_callback = @on_cursor_up_callback
|
277
|
+
actor.on_cursor_left_callback = @on_cursor_left_callback
|
278
|
+
actor.on_cursor_right_callback = @on_cursor_right_callback
|
279
|
+
actor.on_space_bar_callback = @on_space_bar_callback
|
280
|
+
actor.on_mouse_button_left_callback = @on_mouse_button_left_callback
|
148
281
|
|
149
|
-
|
282
|
+
actor.on_click_callback = @on_click_callback
|
150
283
|
|
151
|
-
|
152
|
-
@on_after_move_callback = block
|
284
|
+
actor
|
153
285
|
end
|
154
286
|
|
155
|
-
|
156
|
-
@on_collision_callback = block
|
157
|
-
end
|
287
|
+
protected
|
158
288
|
|
159
|
-
|
160
|
-
|
161
|
-
|
289
|
+
attr_accessor :on_after_move_callback, :on_collision_callback, :on_destroy_callback, :on_jumping_callback, :on_floor_callback
|
290
|
+
attr_accessor :on_cursor_down_callback, :on_cursor_up_callback, :on_cursor_left_callback, :on_cursor_right_callback, :on_space_bar_callback, :on_mouse_button_left_callback
|
291
|
+
attr_accessor :on_click_callback
|
162
292
|
end
|