fantasy 0.1.7 → 0.1.13

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.
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.new(0, 0)
12
- @direction = Coordinates.new(0, 0)
17
+ @position = Coordinates.zero
18
+ @direction = Coordinates.zero
13
19
  @speed = 0
14
20
  @scale = 1
15
- @on_after_move_callback = nil
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() * @scale
48
+ @image.width * @scale
34
49
  end
35
50
 
36
51
  def height
37
- @image.height() * @scale
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
@@ -61,6 +72,25 @@ class Actor
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,102 @@ class Actor
76
106
  if @dragging
77
107
  @position = mouse_position - @dragging_offset
78
108
  else
79
- calculate_direction_by_cursors if @moving_with_cursors
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
- if @direction != Coordinates.zero && !@speed.zero?
82
- @last_position = @position
83
- @position = @position + (@direction * @speed * Global.frame_time)
120
+ # Cursors moving
121
+ last_position = @position
122
+ move_by_cursors
84
123
 
85
- if solid?
86
- collisions.each do |actor|
87
- collision_with(actor)
88
- actor.collision_with(self)
89
- end
124
+ # Check collision after cursor moving
125
+ if @solid && @position != last_position
126
+ manage_collisions(last_position)
127
+ end
90
128
 
91
- @position = @last_position if collisions.any? # we don't cache collisions because position may be changed on collision callback
92
- end
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
93
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)
156
+ end
157
+ end
158
+
159
+ on_after_move_do
160
+ end
161
+
162
+ def manage_collisions(last_position)
163
+ @velocity ||= Coordinates.zero # In case it is not initialized yet
164
+
165
+ collisions.each do |other|
166
+ on_collision_do(other)
167
+ other.on_collision_do(self)
168
+
169
+ if other.position.y > (last_position.y + height)
170
+ on_floor_do unless @on_floor
171
+
172
+ @on_floor = true
173
+ @jumping = false
174
+ @velocity.y = 0
175
+ end
176
+
177
+ if other.position.y + other.height < last_position.y
178
+ @velocity.y = 0
179
+ end
180
+ end
181
+
182
+ # # Reset position pixel by pixel
183
+ # was_collision = false
184
+ # while collisions.any? && @position != last_position
185
+ # was_collision = true
186
+ # last_position_direction = last_position - @position
187
+ # @position += last_position_direction.normalize
188
+ # end
189
+ # was_collision
190
+
191
+ if collisions.any? # we don't cache collisions because position may be changed on collision callback
192
+ @position = last_position
193
+
194
+ return true
94
195
  end
95
196
 
96
- @on_after_move_callback.call unless @on_after_move_callback.nil?
197
+ false
97
198
  end
98
199
 
99
200
  def solid?
100
201
  @solid
101
202
  end
102
203
 
204
+ # Set callbacks
103
205
  def on_after_move(&block)
104
206
  @on_after_move_callback = block
105
207
  end
@@ -112,18 +214,50 @@ class Actor
112
214
  @on_destroy_callback = block
113
215
  end
114
216
 
115
- def collision_with(actor)
116
- @on_collision_callback.call(actor) unless @on_collision_callback.nil?
217
+ def on_jumping(&block)
218
+ @on_jumping_callback = block
117
219
  end
118
220
 
221
+ def on_floor(&block)
222
+ @on_floor_callback = block
223
+ end
224
+
225
+
226
+ # Execute callbacks
227
+ def on_after_move_do
228
+ instance_exec(&@on_after_move_callback) unless @on_after_move_callback.nil?
229
+ end
230
+
231
+ def on_collision_do(other)
232
+ instance_exec(other, &@on_collision_callback) unless @on_collision_callback.nil?
233
+ end
234
+
235
+ def on_destroy_do
236
+ instance_exec(&@on_destroy_callback) unless @on_destroy_callback.nil?
237
+ end
238
+
239
+ def on_jumping_do
240
+ instance_exec(&@on_jumping_callback) unless @on_jumping_callback.nil?
241
+ end
242
+
243
+ def on_floor_do
244
+ instance_exec(&@on_floor_callback) unless @on_floor_callback.nil?
245
+ end
246
+
247
+
119
248
  def collisions
120
- Global.actors.reject { |e| e == self }.select { |e| e.solid? }.select do |actor|
121
- Utils.collision? self, actor
249
+ Global.actors.reject { |e| e == self }.select { |e| e.solid? }.select do |other|
250
+ if(
251
+ (@collision_with == "all" || @collision_with.include?(other.name)) &&
252
+ (other.collision_with == "all" || other.collision_with.include?(self.name))
253
+ )
254
+ Utils.collision? self, other
255
+ end
122
256
  end
123
257
  end
124
258
 
125
259
  def destroy
126
- @on_destroy_callback.call unless @on_destroy_callback.nil?
260
+ on_destroy_do
127
261
  Global.actors.delete(self)
128
262
  end
129
263
 
@@ -133,30 +267,37 @@ class Actor
133
267
  actor.name = @name
134
268
  actor.position = @position.clone
135
269
  actor.direction = @direction.clone
270
+
136
271
  actor.speed = @speed
137
272
  actor.scale = @scale
138
273
  actor.moving_with_cursors if @moving_with_cursors
139
274
  actor.solid = @solid
140
275
  actor.layer = @layer
276
+ actor.gravity = @gravity
277
+ actor.jump_force = @jump_force
278
+ actor.collision_with = @collision_with
141
279
 
142
280
  actor.on_after_move_callback = @on_after_move_callback
143
281
  actor.on_collision_callback = @on_collision_callback
144
282
  actor.on_destroy_callback = @on_destroy_callback
283
+ actor.on_jumping_callback = @on_jumping_callback
284
+ actor.on_floor_callback = @on_floor_callback
145
285
 
146
- actor
147
- end
286
+ actor.on_cursor_down_callback = @on_cursor_down_callback
287
+ actor.on_cursor_up_callback = @on_cursor_up_callback
288
+ actor.on_cursor_left_callback = @on_cursor_left_callback
289
+ actor.on_cursor_right_callback = @on_cursor_right_callback
290
+ actor.on_space_bar_callback = @on_space_bar_callback
291
+ actor.on_mouse_button_left_callback = @on_mouse_button_left_callback
148
292
 
149
- protected
293
+ actor.on_click_callback = @on_click_callback
150
294
 
151
- def on_after_move_callback=(block)
152
- @on_after_move_callback = block
295
+ actor
153
296
  end
154
297
 
155
- def on_collision_callback=(block)
156
- @on_collision_callback = block
157
- end
298
+ protected
158
299
 
159
- def on_destroy_callback=(block)
160
- @on_destroy_callback = block
161
- end
300
+ attr_accessor :on_after_move_callback, :on_collision_callback, :on_destroy_callback, :on_jumping_callback, :on_floor_callback
301
+ 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
302
+ attr_accessor :on_click_callback
162
303
  end
@@ -1,5 +1,5 @@
1
1
  class Background
2
- attr_accessor :scale, :color, :visible, :position, :layer
2
+ attr_accessor :scale, :color, :visible, :position, :layer, :replicable
3
3
 
4
4
  def initialize(image_name:)
5
5
  @image = Image.new(image_name)
data/lib/fantasy/color.rb CHANGED
@@ -1,5 +1,186 @@
1
+ require "ostruct"
2
+
1
3
  class Color < Gosu::Color
2
- def initialize(r:, g:, b:, a: 255)
4
+ attr_reader :a, :r, :g, :b, :name
5
+
6
+ def initialize(r:, g:, b:, a: 255, name: nil)
3
7
  super(a, r, g, b)
8
+
9
+ @a = a
10
+ @r = r
11
+ @g = g
12
+ @b = b
13
+ @name = name
4
14
  end
15
+
16
+ def hex
17
+ [@r, @g, @b].map do |e|
18
+ e.to_s(16).rjust(2, "0")
19
+ end.join
20
+ end
21
+
22
+ def to_s
23
+ result = "r:#{@r}, g:#{@g}, b:#{@b}, a:#{@a}, hex:#{hex}"
24
+ result += " (#{name})" unless name.nil?
25
+
26
+ result
27
+ end
28
+
29
+ class << self
30
+ attr_reader :palette
31
+ end
32
+
33
+ @palette = OpenStruct.new({
34
+ "orange" => Color.new(r:255, g:165, b:0, a:255, name: "orange"),
35
+ "red" => Color.new(r:255, g:0, b:0, a:255, name: "red"),
36
+ "orange_red" => Color.new(r:255, g:69, b:0, a:255, name: "orange_red"),
37
+ "tomato" => Color.new(r:255, g:99, b:71, a:255, name: "tomato"),
38
+ "dark_red" => Color.new(r:139, g:0, b:0, a:255, name: "dark_red"),
39
+ "fire_brick" => Color.new(r:178, g:34, b:34, a:255, name: "fire_brick"),
40
+ "crimson" => Color.new(r:220, g:20, b:60, a:255, name: "crimson"),
41
+ "deep_pink" => Color.new(r:255, g:20, b:147, a:255, name: "deep_pink"),
42
+ "maroon" => Color.new(r:176, g:48, b:96, a:255, name: "maroon"),
43
+ "indian_red" => Color.new(r:205, g:92, b:92, a:255, name: "indian_red"),
44
+ "medium_violet_red" => Color.new(r:199, g:21, b:133, a:255, name: "medium_violet_red"),
45
+ "violet_red" => Color.new(r:208, g:32, b:144, a:255, name: "violet_red"),
46
+ "light_coral" => Color.new(r:240, g:128, b:128, a:255, name: "light_coral"),
47
+ "hot_pink" => Color.new(r:255, g:105, b:180, a:255, name: "hot_pink"),
48
+ "pale_violet_red" => Color.new(r:219, g:112, b:147, a:255, name: "pale_violet_red"),
49
+ "light_pink" => Color.new(r:255, g:182, b:193, a:255, name: "light_pink"),
50
+ "rosy_brown" => Color.new(r:188, g:143, b:143, a:255, name: "rosy_brown"),
51
+ "pink" => Color.new(r:255, g:192, b:203, a:255, name: "pink"),
52
+ "orchid" => Color.new(r:218, g:112, b:214, a:255, name: "orchid"),
53
+ "lavender_blush" => Color.new(r:255, g:240, b:245, a:255, name: "lavender_blush"),
54
+ "snow" => Color.new(r:255, g:250, b:250, a:255, name: "snow"),
55
+ "chocolate" => Color.new(r:210, g:105, b:30, a:255, name: "chocolate"),
56
+ "saddle_brown" => Color.new(r:139, g:69, b:19, a:255, name: "saddle_brown"),
57
+ "brown" => Color.new(r:132, g:60, b:36, a:255, name: "brown"),
58
+ "dark_orange" => Color.new(r:255, g:140, b:0, a:255, name: "dark_orange"),
59
+ "coral" => Color.new(r:255, g:127, b:80, a:255, name: "coral"),
60
+ "sienna" => Color.new(r:160, g:82, b:45, a:255, name: "sienna"),
61
+ "salmon" => Color.new(r:250, g:128, b:114, a:255, name: "salmon"),
62
+ "peru" => Color.new(r:205, g:133, b:63, a:255, name: "peru"),
63
+ "dark_goldenrod" => Color.new(r:184, g:134, b:11, a:255, name: "dark_goldenrod"),
64
+ "goldenrod" => Color.new(r:218, g:165, b:32, a:255, name: "goldenrod"),
65
+ "sandy_brown" => Color.new(r:244, g:164, b:96, a:255, name: "sandy_brown"),
66
+ "light_salmon" => Color.new(r:255, g:160, b:122, a:255, name: "light_salmon"),
67
+ "dark_salmon" => Color.new(r:233, g:150, b:122, a:255, name: "dark_salmon"),
68
+ "gold" => Color.new(r:255, g:215, b:0, a:255, name: "gold"),
69
+ "yellow" => Color.new(r:255, g:255, b:0, a:255, name: "yellow"),
70
+ "olive" => Color.new(r:128, g:128, b:0, a:255, name: "olive"),
71
+ "burlywood" => Color.new(r:222, g:184, b:135, a:255, name: "burlywood"),
72
+ "tan" => Color.new(r:210, g:180, b:140, a:255, name: "tan"),
73
+ "navajo_white" => Color.new(r:255, g:222, b:173, a:255, name: "navajo_white"),
74
+ "peach_puff" => Color.new(r:255, g:218, b:185, a:255, name: "peach_puff"),
75
+ "khaki" => Color.new(r:240, g:230, b:140, a:255, name: "khaki"),
76
+ "dark_khaki" => Color.new(r:189, g:183, b:107, a:255, name: "dark_khaki"),
77
+ "moccasin" => Color.new(r:255, g:228, b:181, a:255, name: "moccasin"),
78
+ "wheat" => Color.new(r:245, g:222, b:179, a:255, name: "wheat"),
79
+ "bisque" => Color.new(r:255, g:228, b:196, a:255, name: "bisque"),
80
+ "pale_goldenrod" => Color.new(r:238, g:232, b:170, a:255, name: "pale_goldenrod"),
81
+ "blanched_almond" => Color.new(r:255, g:235, b:205, a:255, name: "blanched_almond"),
82
+ "medium_goldenrod" => Color.new(r:234, g:234, b:173, a:255, name: "medium_goldenrod"),
83
+ "papaya_whip" => Color.new(r:255, g:239, b:213, a:255, name: "papaya_whip"),
84
+ "misty_rose" => Color.new(r:255, g:228, b:225, a:255, name: "misty_rose"),
85
+ "lemon_chiffon" => Color.new(r:255, g:250, b:205, a:255, name: "lemon_chiffon"),
86
+ "antique_white" => Color.new(r:250, g:235, b:215, a:255, name: "antique_white"),
87
+ "cornsilk" => Color.new(r:255, g:248, b:220, a:255, name: "cornsilk"),
88
+ "light_goldenrod_yellow" => Color.new(r:250, g:250, b:210, a:255, name: "light_goldenrod_yellow"),
89
+ "old_lace" => Color.new(r:253, g:245, b:230, a:255, name: "old_lace"),
90
+ "linen" => Color.new(r:250, g:240, b:230, a:255, name: "linen"),
91
+ "light_yellow" => Color.new(r:255, g:255, b:224, a:255, name: "light_yellow"),
92
+ "seashell" => Color.new(r:255, g:245, b:238, a:255, name: "seashell"),
93
+ "beige" => Color.new(r:245, g:245, b:220, a:255, name: "beige"),
94
+ "floral_white" => Color.new(r:255, g:250, b:240, a:255, name: "floral_white"),
95
+ "ivory" => Color.new(r:255, g:255, b:240, a:255, name: "ivory"),
96
+ "green" => Color.new(r:0, g:255, b:0, a:255, name: "green"),
97
+ "lawn_green" => Color.new(r:124, g:252, b:0, a:255, name: "lawn_green"),
98
+ "chartreuse" => Color.new(r:127, g:255, b:0, a:255, name: "chartreuse"),
99
+ "green_yellow" => Color.new(r:173, g:255, b:47, a:255, name: "green_yellow"),
100
+ "yellow_green" => Color.new(r:154, g:205, b:50, a:255, name: "yellow_green"),
101
+ "medium_forest_green" => Color.new(r:107, g:142, b:35, a:255, name: "medium_forest_green"),
102
+ "olive_drab" => Color.new(r:107, g:142, b:35, a:255, name: "olive_drab"),
103
+ "dark_olive_green" => Color.new(r:85, g:107, b:47, a:255, name: "dark_olive_green"),
104
+ "dark_sea_green" => Color.new(r:143, g:188, b:139, a:255, name: "dark_sea_green"),
105
+ "lime" => Color.new(r:0, g:255, b:0, a:255, name: "lime"),
106
+ "dark_green" => Color.new(r:0, g:100, b:0, a:255, name: "dark_green"),
107
+ "lime_green" => Color.new(r:50, g:205, b:50, a:255, name: "lime_green"),
108
+ "forest_green" => Color.new(r:34, g:139, b:34, a:255, name: "forest_green"),
109
+ "spring_green" => Color.new(r:0, g:255, b:127, a:255, name: "spring_green"),
110
+ "medium_spring_green" => Color.new(r:0, g:250, b:154, a:255, name: "medium_spring_green"),
111
+ "sea_green" => Color.new(r:46, g:139, b:87, a:255, name: "sea_green"),
112
+ "medium_sea_green" => Color.new(r:60, g:179, b:113, a:255, name: "medium_sea_green"),
113
+ "aquamarine" => Color.new(r:112, g:216, b:144, a:255, name: "aquamarine"),
114
+ "light_green" => Color.new(r:144, g:238, b:144, a:255, name: "light_green"),
115
+ "pale_green" => Color.new(r:152, g:251, b:152, a:255, name: "pale_green"),
116
+ "medium_aquamarine" => Color.new(r:102, g:205, b:170, a:255, name: "medium_aquamarine"),
117
+ "turquoise" => Color.new(r:64, g:224, b:208, a:255, name: "turquoise"),
118
+ "light_sea_green" => Color.new(r:32, g:178, b:170, a:255, name: "light_sea_green"),
119
+ "medium_turquoise" => Color.new(r:72, g:209, b:204, a:255, name: "medium_turquoise"),
120
+ "honeydew" => Color.new(r:240, g:255, b:240, a:255, name: "honeydew"),
121
+ "mint_cream" => Color.new(r:245, g:255, b:250, a:255, name: "mint_cream"),
122
+ "royal_blue" => Color.new(r:65, g:105, b:225, a:255, name: "royal_blue"),
123
+ "dodger_blue" => Color.new(r:30, g:144, b:255, a:255, name: "dodger_blue"),
124
+ "deep_sky_blue" => Color.new(r:0, g:191, b:255, a:255, name: "deep_sky_blue"),
125
+ "cornflower_blue" => Color.new(r:100, g:149, b:237, a:255, name: "cornflower_blue"),
126
+ "steel_blue" => Color.new(r:70, g:130, b:180, a:255, name: "steel_blue"),
127
+ "light_sky_blue" => Color.new(r:135, g:206, b:250, a:255, name: "light_sky_blue"),
128
+ "dark_turquoise" => Color.new(r:0, g:206, b:209, a:255, name: "dark_turquoise"),
129
+ "cyan" => Color.new(r:0, g:255, b:255, a:255, name: "cyan"),
130
+ "aqua" => Color.new(r:0, g:255, b:255, a:255, name: "aqua"),
131
+ "dark_cyan" => Color.new(r:0, g:139, b:139, a:255, name: "dark_cyan"),
132
+ "teal" => Color.new(r:0, g:128, b:128, a:255, name: "teal"),
133
+ "sky_blue" => Color.new(r:135, g:206, b:235, a:255, name: "sky_blue"),
134
+ "cadet_blue" => Color.new(r:95, g:158, b:160, a:255, name: "cadet_blue"),
135
+ "dark_slate_gray" => Color.new(r:47, g:79, b:79, a:255, name: "dark_slate_gray"),
136
+ "dark_slate_grey" => Color.new(r:47, g:79, b:79, a:255, name: "dark_slate_grey"),
137
+ "light_slate_gray" => Color.new(r:119, g:136, b:153, a:255, name: "light_slate_gray"),
138
+ "light_slate_grey" => Color.new(r:119, g:136, b:153, a:255, name: "light_slate_grey"),
139
+ "slate_gray" => Color.new(r:112, g:128, b:144, a:255, name: "slate_gray"),
140
+ "slate_grey" => Color.new(r:112, g:128, b:144, a:255, name: "slate_grey"),
141
+ "light_steel_blue" => Color.new(r:176, g:196, b:222, a:255, name: "light_steel_blue"),
142
+ "light_blue" => Color.new(r:173, g:216, b:230, a:255, name: "light_blue"),
143
+ "powder_blue" => Color.new(r:176, g:224, b:230, a:255, name: "powder_blue"),
144
+ "pale_turquoise" => Color.new(r:175, g:238, b:238, a:255, name: "pale_turquoise"),
145
+ "light_cyan" => Color.new(r:224, g:255, b:255, a:255, name: "light_cyan"),
146
+ "alice_blue" => Color.new(r:240, g:248, b:255, a:255, name: "alice_blue"),
147
+ "azure" => Color.new(r:240, g:255, b:255, a:255, name: "azure"),
148
+ "medium_blue" => Color.new(r:0, g:0, b:205, a:255, name: "medium_blue"),
149
+ "dark_blue" => Color.new(r:0, g:0, b:139, a:255, name: "dark_blue"),
150
+ "midnight_blue" => Color.new(r:25, g:25, b:112, a:255, name: "midnight_blue"),
151
+ "navy" => Color.new(r:36, g:36, b:140, a:255, name: "navy"),
152
+ "blue" => Color.new(r:0, g:0, b:255, a:255, name: "blue"),
153
+ "indigo" => Color.new(r:75, g:0, b:130, a:255, name: "indigo"),
154
+ "blue_violet" => Color.new(r:138, g:43, b:226, a:255, name: "blue_violet"),
155
+ "medium_slate_blue" => Color.new(r:123, g:104, b:238, a:255, name: "medium_slate_blue"),
156
+ "slate_blue" => Color.new(r:106, g:90, b:205, a:255, name: "slate_blue"),
157
+ "purple" => Color.new(r:160, g:32, b:240, a:255, name: "purple"),
158
+ "dark_slate_blue" => Color.new(r:72, g:61, b:139, a:255, name: "dark_slate_blue"),
159
+ "dark_violet" => Color.new(r:148, g:0, b:211, a:255, name: "dark_violet"),
160
+ "dark_orchid" => Color.new(r:153, g:50, b:204, a:255, name: "dark_orchid"),
161
+ "medium_purple" => Color.new(r:147, g:112, b:219, a:255, name: "medium_purple"),
162
+ "medium_orchid" => Color.new(r:186, g:85, b:211, a:255, name: "medium_orchid"),
163
+ "magenta" => Color.new(r:255, g:0, b:255, a:255, name: "magenta"),
164
+ "fuchsia" => Color.new(r:255, g:0, b:255, a:255, name: "fuchsia"),
165
+ "dark_magenta" => Color.new(r:139, g:0, b:139, a:255, name: "dark_magenta"),
166
+ "violet" => Color.new(r:238, g:130, b:238, a:255, name: "violet"),
167
+ "plum" => Color.new(r:221, g:160, b:221, a:255, name: "plum"),
168
+ "lavender" => Color.new(r:230, g:230, b:250, a:255, name: "lavender"),
169
+ "thistle" => Color.new(r:216, g:191, b:216, a:255, name: "thistle"),
170
+ "ghost_white" => Color.new(r:248, g:248, b:255, a:255, name: "ghost_white"),
171
+ "white" => Color.new(r:255, g:255, b:255, a:255, name: "white"),
172
+ "white_smoke" => Color.new(r:245, g:245, b:245, a:255, name: "white_smoke"),
173
+ "gainsboro" => Color.new(r:220, g:220, b:220, a:255, name: "gainsboro"),
174
+ "light_gray" => Color.new(r:211, g:211, b:211, a:255, name: "light_gray"),
175
+ "light_grey" => Color.new(r:211, g:211, b:211, a:255, name: "light_grey"),
176
+ "silver" => Color.new(r:192, g:192, b:192, a:255, name: "silver"),
177
+ "gray" => Color.new(r:190, g:190, b:190, a:255, name: "gray"),
178
+ "grey" => Color.new(r:190, g:190, b:190, a:255, name: "grey"),
179
+ "dark_gray" => Color.new(r:169, g:169, b:169, a:255, name: "dark_gray"),
180
+ "dark_grey" => Color.new(r:169, g:169, b:169, a:255, name: "dark_grey"),
181
+ "dim_gray" => Color.new(r:105, g:105, b:105, a:255, name: "dim_gray"),
182
+ "dim_grey" => Color.new(r:105, g:105, b:105, a:255, name: "dim_grey"),
183
+ "black" => Color.new(r:0, g:0, b:0, a:255, name: "black"),
184
+ "transparent" => Color.new(r:0, g:0, b:0, a:0, name: "transparent")
185
+ })
5
186
  end
@@ -32,4 +32,8 @@ class Coordinates < Vector2d
32
32
  def clone
33
33
  Coordinates.new(@x, @y)
34
34
  end
35
+
36
+ def zero?
37
+ @x == 0 && @y == 0
38
+ end
35
39
  end
@@ -0,0 +1,42 @@
1
+ module Cursor
2
+ def self.left
3
+ Gosu::KB_LEFT
4
+ end
5
+
6
+ def self.right
7
+ Gosu::KB_RIGHT
8
+ end
9
+
10
+ def self.up
11
+ Gosu::KB_UP
12
+ end
13
+
14
+ def self.down
15
+ Gosu::KB_DOWN
16
+ end
17
+
18
+ def self.space_bar
19
+ Gosu::KB_SPACE
20
+ end
21
+
22
+
23
+ def self.left?
24
+ Gosu.button_down?(Cursor.left)
25
+ end
26
+
27
+ def self.right?
28
+ Gosu.button_down?(Cursor.right)
29
+ end
30
+
31
+ def self.up?
32
+ Gosu.button_down?(Cursor.up)
33
+ end
34
+
35
+ def self.down?
36
+ Gosu.button_down?(Cursor.down)
37
+ end
38
+
39
+ def self.space_bar?
40
+ Gosu.button_down?(Cursor.space_bar)
41
+ end
42
+ end
@@ -0,0 +1,31 @@
1
+ require "json"
2
+ require "ostruct"
3
+ require "fileutils"
4
+
5
+ module Disk
6
+ @@data_path = "#{Dir.pwd}/disk/data.json"
7
+ @@data = nil
8
+
9
+ def self.data
10
+ @@data ||= OpenStruct.new(load)
11
+ end
12
+
13
+ def self.save
14
+ dirname = File.dirname(@@data_path)
15
+ unless File.directory?(dirname)
16
+ FileUtils.mkdir_p(dirname)
17
+ end
18
+
19
+ File.open(@@data_path, "w") { |f| f.write JSON.pretty_generate(@@data.to_h) }
20
+ end
21
+
22
+ private
23
+
24
+ def self.load
25
+ if File.file?(@@data_path)
26
+ JSON.parse(File.read(@@data_path))
27
+ else
28
+ {}
29
+ end
30
+ end
31
+ end
@@ -2,7 +2,7 @@ require "ostruct"
2
2
 
3
3
  module Global
4
4
  class << self
5
- attr_accessor :actors, :hud_texts, :hud_images, :backgrounds, :tile_maps, :clocks
5
+ attr_accessor :actors, :hud_texts, :hud_images, :backgrounds, :tile_maps, :clocks, :shapes
6
6
  attr_accessor :debug
7
7
  attr_accessor :setup_proc, :loop_proc, :button_proc
8
8
  attr_accessor :presentation_proc, :game_proc, :end_proc
@@ -28,6 +28,7 @@ module Global
28
28
  @backgrounds = []
29
29
  @tile_maps = []
30
30
  @clocks = []
31
+ @shapes = []
31
32
  @last_frame_at = Time.now
32
33
  @debug = false
33
34
 
@@ -116,11 +117,23 @@ module Global
116
117
  @hud_images.clear
117
118
  @backgrounds.clear
118
119
  @tile_maps.clear
120
+ @shapes.clear
121
+ @camera.position = Coordinates.zero
119
122
 
120
123
  @clocks.reject(&:persistent?).each do |clock|
121
124
  clock.stop unless clock.thread == Thread.current # no stop current Thread
122
125
  end
123
126
 
127
+ # clear callbacks
128
+ @button_proc = nil
129
+ @space_bar_proc = nil
130
+ @cursor_up_proc = nil
131
+ @cursor_down_proc = nil
132
+ @cursor_left_proc = nil
133
+ @cursor_right_proc = nil
134
+ @mouse_button_left_proc = nil
135
+ @mouse_button_right_proc = nil
136
+
124
137
  @background = Color.new(r: 0, g: 0, b: 0)
125
138
  end
126
139
 
@@ -131,6 +144,7 @@ module Global
131
144
  def setup
132
145
  Sound.preload_sounds
133
146
  Image.preload_images
147
+ Music.preload_musics
134
148
  end
135
149
 
136
150
  def seconds_in_scene
@@ -37,7 +37,7 @@ class HudImage
37
37
  end
38
38
 
39
39
  def draw_debug
40
- Global.pixel_font.draw_text("#{@position.x.floor},#{@position.y.floor}", @position.x, @position.y - 20, 1)
40
+ Global.pixel_fonts["medium"].draw_text("#{@position.x.floor},#{@position.y.floor}", @position.x, @position.y - 20, 1)
41
41
  end
42
42
 
43
43
  def destroy