fantasy 0.1.5.1 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
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,21 @@
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
+ 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
@@ -15,7 +15,7 @@ module Global
15
15
 
16
16
  attr_accessor :game
17
17
  attr_reader :frame_time # delta_time
18
- attr_reader :pixel_font
18
+ attr_reader :pixel_fonts
19
19
  attr_reader :references
20
20
  attr_reader :camera
21
21
  attr_reader :game_state
@@ -28,15 +28,24 @@ 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
- @pixel_font = Gosu::Font.new(20, { name: "#{__dir__}/../../fonts/VT323-Regular.ttf" } )
34
+
35
+ @pixel_fonts = {}
36
+ @pixel_fonts["small"] = Gosu::Font.new(20, { name: "#{__dir__}/../../fonts/VT323-Regular.ttf" } )
37
+ @pixel_fonts["medium"] = Gosu::Font.new(40, { name: "#{__dir__}/../../fonts/VT323-Regular.ttf" } )
38
+ @pixel_fonts["big"] = Gosu::Font.new(60, { name: "#{__dir__}/../../fonts/VT323-Regular.ttf" } )
39
+ @pixel_fonts["huge"] = Gosu::Font.new(100, { name: "#{__dir__}/../../fonts/VT323-Regular.ttf" } )
40
+
34
41
  @d_key_pressed = false
35
42
  @references = OpenStruct.new
36
43
  @camera = Camera.new(position: Coordinates.zero)
37
44
  @game_state = Global.presentation_proc.nil? ? "game" : "presentation"
38
45
  @scene_started_at = Time.now
39
46
 
47
+ @background = Color.new(r: 0, g: 0, b: 0)
48
+
40
49
  if @presentation_proc.nil?
41
50
  on_presentation { Global.default_on_presentation }
42
51
  end
@@ -108,7 +117,23 @@ module Global
108
117
  @hud_images.clear
109
118
  @backgrounds.clear
110
119
  @tile_maps.clear
111
- @clocks.reject(&:persistent?).each(&:stop)
120
+ @shapes.clear
121
+ @camera.position = Coordinates.zero
122
+
123
+ @clocks.reject(&:persistent?).each do |clock|
124
+ clock.stop unless clock.thread == Thread.current # no stop current Thread
125
+ end
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
+
112
137
  @background = Color.new(r: 0, g: 0, b: 0)
113
138
  end
114
139
 
@@ -119,6 +144,7 @@ module Global
119
144
  def setup
120
145
  Sound.preload_sounds
121
146
  Image.preload_images
147
+ Music.preload_musics
122
148
  end
123
149
 
124
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
@@ -1,14 +1,16 @@
1
1
  class HudText
2
- attr_accessor :text, :size, :color, :visible, :layer, :in_world
2
+ attr_accessor :text, :size, :color, :background_color, :visible, :layer, :in_world, :position, :alignment
3
3
 
4
4
  def initialize(position:, text: "")
5
5
  @position = position
6
6
  @text = text
7
7
  @size = "medium"
8
- @color = Gosu::Color::WHITE
8
+ @color = Color.palette.white
9
+ @background_color = Color.palette.black
9
10
  @visible = true
10
11
  @layer = 100
11
12
  @in_world = false
13
+ @alignment = "top-left"
12
14
 
13
15
  Global.hud_texts.push(self)
14
16
  end
@@ -17,14 +19,48 @@ class HudText
17
19
 
18
20
  def draw
19
21
  if visible
20
- Global.pixel_font.draw_markup(text, screen_position.x + scale, screen_position.y - 20 + scale, 1, scale, scale, Gosu::Color::BLACK)
21
- Global.pixel_font.draw_markup(text, screen_position.x, screen_position.y - 20, 1, scale, scale, color)
22
+ unless @background_color.nil?
23
+ font.draw_markup_rel(text, screen_position.x + shadow_offset, screen_position.y + shadow_offset, 1, position_rel.x, position_rel.y, 1, 1, background_color)
24
+ end
25
+ font.draw_markup_rel(text, screen_position.x, screen_position.y, 1, position_rel.x, position_rel.y, 1, 1, color)
22
26
  end
23
27
 
24
28
  draw_debug if Global.debug
25
29
  end
26
30
 
27
- def scale
31
+ def font
32
+ found_font = Global.pixel_fonts[@size]
33
+ if found_font.nil?
34
+ raise "HudText.size value not valid '#{@size}'. Valid values: 'small, medium, big, huge'"
35
+ end
36
+ found_font
37
+ end
38
+
39
+ def destroy
40
+ Global.hud_texts.delete(self)
41
+ end
42
+
43
+ def width
44
+ font.markup_width(text, 1)
45
+ end
46
+
47
+
48
+ private
49
+
50
+ def position_rel
51
+ case @alignment
52
+ when "top-left"
53
+ Coordinates.new(0, 0)
54
+ when "top-right"
55
+ Coordinates.new(1, 0)
56
+ when "center"
57
+ Coordinates.new(0.5, 0.5)
58
+ else
59
+ raise "HudText.alignment value not valid '#{@alignment}'. Valid values: 'top-left, top-right, center'"
60
+ end
61
+ end
62
+
63
+ def shadow_offset
28
64
  case @size
29
65
  when "small"
30
66
  1
@@ -35,7 +71,7 @@ class HudText
35
71
  when "huge"
36
72
  4
37
73
  else
38
- raise "HudText.size not valid '#{@size}'. Valid sizes: 'small, medium, big, huge'"
74
+ raise "HudText.size value not valid '#{@size}'. Valid sizes: 'small, medium, big, huge'"
39
75
  end
40
76
  end
41
77
 
@@ -47,11 +83,7 @@ class HudText
47
83
  end
48
84
  end
49
85
 
50
- def destroy
51
- Global.hud_texts.delete(self)
52
- end
53
-
54
86
  def draw_debug
55
- Global.pixel_font.draw_text("#{@position.x.floor},#{@position.y.floor}", @position.x, @position.y, 1)
87
+ Global.pixel_fonts["medium"].draw_text("#{@position.x.floor},#{@position.y.floor}", screen_position.x, screen_position.y, 1)
56
88
  end
57
89
  end
data/lib/fantasy/image.rb CHANGED
@@ -23,6 +23,8 @@ class Image
23
23
  end
24
24
 
25
25
  def preload_images
26
+ return unless Dir.exist?(base_path)
27
+
26
28
  Dir.each_child(base_path) do |file_name|
27
29
  locate_image(file_name) unless file_name.start_with?(".")
28
30
  end
@@ -0,0 +1,5 @@
1
+ module Gravitier
2
+ def add_force_by_gravity
3
+ add_force(Coordinates.down * @gravity)
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ module Jumper
2
+ def jump
3
+ add_force(Coordinates.up * @jump_force)
4
+ @jumping = true
5
+ @on_floor = false
6
+ @final_vertical_position = @position.y - @jump_force
7
+
8
+ on_jumping_do
9
+ end
10
+ end
@@ -1,15 +1,43 @@
1
1
  module MoveByCursor
2
- def calculate_direction_by_cursors
3
- if Gosu.button_down?(Gosu::KB_DOWN)
4
- @direction = Coordinates.down
5
- elsif Gosu.button_down?(Gosu::KB_UP)
6
- @direction = Coordinates.up
7
- elsif Gosu.button_down?(Gosu::KB_RIGHT)
8
- @direction = Coordinates.right
9
- elsif Gosu.button_down?(Gosu::KB_LEFT)
10
- @direction = Coordinates.left
11
- else
12
- @direction = Coordinates.zero
2
+ def move_by_cursors
3
+ if Gosu.button_down?(Cursor.down) && @move_with_cursors_down
4
+ move_by(Coordinates.down)
13
5
  end
6
+
7
+ if Gosu.button_down?(Cursor.up) && @move_with_cursors_up
8
+ move_by(Coordinates.up)
9
+ end
10
+
11
+ if Gosu.button_down?(Cursor.right) && @move_with_cursors_right
12
+ move_by(Coordinates.right)
13
+ end
14
+
15
+ if Gosu.button_down?(Cursor.left) && @move_with_cursors_left
16
+ move_by(Coordinates.left)
17
+ end
18
+
19
+ if Gosu.button_down?(Cursor.space_bar) && !@jumping && @on_floor && @move_with_cursors_jump
20
+ jump
21
+ end
22
+ end
23
+
24
+ def move_with_cursors(down: nil, up: nil, left: nil, right: nil, jump: false)
25
+ puts "#{@name}: move_with_cursors(down: #{down}, up: #{up}, left: #{left}, right: #{right}), jump: #{jump}"
26
+
27
+ if down.nil? and up.nil? and left.nil? and right.nil?
28
+ down = true
29
+ up = true
30
+ left = true
31
+ right = true
32
+ end
33
+ @move_with_cursors_down = down || false
34
+ @move_with_cursors_up = up || false
35
+ @move_with_cursors_left = left || false
36
+ @move_with_cursors_right = right || false
37
+ @move_with_cursors_jump = jump || false
38
+ end
39
+
40
+ def move_by(direction)
41
+ @position += direction * @speed * Global.frame_time
14
42
  end
15
43
  end
@@ -0,0 +1,5 @@
1
+ module MoveByDirection
2
+ def move_by_direction
3
+ @position += @direction * @speed * Global.frame_time
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ module Mover
2
+ def add_force(force)
3
+ @acceleration ||= Coordinates.zero
4
+ @acceleration += force
5
+ end
6
+
7
+ def apply_forces(max_speed: Float::INFINITY)
8
+ @acceleration ||= Coordinates.zero
9
+ @velocity ||= Coordinates.zero
10
+
11
+ @velocity += @acceleration
12
+ @velocity.resize(max_speed) if @velocity.length > max_speed
13
+
14
+ unless @velocity.length.zero?
15
+ @position += @velocity * Global.frame_time
16
+ end
17
+
18
+ @acceleration = Coordinates.zero
19
+ end
20
+ end
@@ -0,0 +1,60 @@
1
+ module UserInputs
2
+ # Set callbacks
3
+ def on_cursor_down(&block)
4
+ @on_cursor_down_callback = block
5
+ end
6
+
7
+ def on_cursor_up(&block)
8
+ @on_cursor_up_callback = block
9
+ end
10
+
11
+ def on_cursor_left(&block)
12
+ @on_cursor_left_callback = block
13
+ end
14
+
15
+ def on_cursor_right(&block)
16
+ @on_cursor_right_callback = block
17
+ end
18
+
19
+ def on_space_bar(&block)
20
+ @on_space_bar_callback = block
21
+ end
22
+
23
+ def on_mouse_button_left(&block)
24
+ @on_mouse_button_left_callback = block
25
+ end
26
+
27
+ def on_click(&block)
28
+ @on_click_callback = block
29
+ end
30
+
31
+ # Execute callbacks
32
+ def on_cursor_down_do
33
+ instance_exec(&@on_cursor_down_callback) unless @on_cursor_down_callback.nil?
34
+ end
35
+
36
+ def on_cursor_up_do
37
+ instance_exec(&@on_cursor_up_callback) unless @on_cursor_up_callback.nil?
38
+ end
39
+
40
+ def on_cursor_left_do
41
+ instance_exec(&@on_cursor_left_callback) unless @on_cursor_left_callback.nil?
42
+ end
43
+
44
+ def on_cursor_right_do
45
+ instance_exec(&@on_cursor_right_callback) unless @on_cursor_right_callback.nil?
46
+ end
47
+
48
+ def on_space_bar_do
49
+ instance_exec(&@on_space_bar_callback) unless @on_space_bar_callback.nil?
50
+ end
51
+
52
+ def on_mouse_button_left_do
53
+ instance_exec(&@on_mouse_button_left_callback) unless @on_mouse_button_left_callback.nil?
54
+ end
55
+
56
+ def on_click_do
57
+ puts "XXX: on_click_do: #{@on_click_callback}"
58
+ instance_exec(&@on_click_callback) unless @on_click_callback.nil?
59
+ end
60
+ end
data/lib/fantasy/loop.rb CHANGED
@@ -9,13 +9,13 @@ class Game < Gosu::Window
9
9
 
10
10
  def button_down(button_id)
11
11
  case button_id
12
- when Gosu::KB_DOWN then Global.cursor_down_proc.call unless Global.cursor_down_proc.nil?
13
- when Gosu::KB_UP then Global.cursor_up_proc.call unless Global.cursor_up_proc.nil?
14
- when Gosu::KB_LEFT then Global.cursor_left_proc.call unless Global.cursor_left_proc.nil?
15
- when Gosu::KB_RIGHT then Global.cursor_right_proc.call unless Global.cursor_right_proc.nil?
16
- when Gosu::MS_LEFT then Global.mouse_button_left_proc.call unless Global.mouse_button_left_proc.nil?
17
- when Gosu::MS_RIGHT then Global.mouse_button_right_proc.call unless Global.mouse_button_right_proc.nil?
18
- when Gosu::KB_SPACE then Global.space_bar_proc.call unless Global.space_bar_proc.nil?
12
+ when Cursor.down then cursor_down_pressed
13
+ when Cursor.up then cursor_up_pressed
14
+ when Cursor.left then cursor_left_pressed
15
+ when Cursor.right then cursor_right_pressed
16
+ when Cursor.space_bar then space_bar_pressed
17
+
18
+ when Mouse.left then mouse_button_left_pressed
19
19
  end
20
20
 
21
21
  Global.button_proc.call(button_id) unless Global.button_proc.nil?
@@ -23,6 +23,46 @@ class Game < Gosu::Window
23
23
  super
24
24
  end
25
25
 
26
+ def cursor_down_pressed
27
+ Global.cursor_down_proc.call unless Global.cursor_down_proc.nil?
28
+ invoke_input_method("on_cursor_down_do")
29
+ end
30
+
31
+ def cursor_up_pressed
32
+ Global.cursor_up_proc.call unless Global.cursor_up_proc.nil?
33
+ invoke_input_method("on_cursor_up_do")
34
+ end
35
+
36
+ def cursor_left_pressed
37
+ Global.cursor_left_proc.call unless Global.cursor_left_proc.nil?
38
+ invoke_input_method("on_cursor_left_do")
39
+ end
40
+
41
+ def cursor_right_pressed
42
+ Global.cursor_right_proc.call unless Global.cursor_right_proc.nil?
43
+ invoke_input_method("on_cursor_right_do")
44
+ end
45
+
46
+ def space_bar_pressed
47
+ Global.space_bar_proc.call unless Global.space_bar_proc.nil?
48
+ invoke_input_method("on_space_bar_do")
49
+ end
50
+
51
+ def mouse_button_left_pressed
52
+ Global.mouse_button_left_proc.call unless Global.mouse_button_left_proc.nil?
53
+
54
+ check_click
55
+ end
56
+
57
+ def invoke_input_method(input_method_name)
58
+ (
59
+ Global.actors +
60
+ Global.shapes
61
+ ).sort_by(&:layer).each do |e|
62
+ e.send(input_method_name)
63
+ end
64
+ end
65
+
26
66
  def update
27
67
  Global.update
28
68
 
@@ -51,9 +91,19 @@ class Game < Gosu::Window
51
91
  Global.tile_maps +
52
92
  Global.actors +
53
93
  Global.hud_texts +
54
- Global.hud_images
94
+ Global.hud_images +
95
+ Global.shapes
55
96
  ).sort_by(&:layer).each do |e|
56
97
  e.draw
57
98
  end
58
99
  end
100
+
101
+ def check_click
102
+ (
103
+ Global.actors +
104
+ Global.shapes
105
+ ).sort_by(&:layer).each do |e|
106
+ e.on_click_do if Utils.collision_at?(e, mouse_x, mouse_y)
107
+ end
108
+ end
59
109
  end
@@ -0,0 +1,9 @@
1
+ module Mouse
2
+ def self.left
3
+ Gosu::MS_LEFT
4
+ end
5
+
6
+ def self.right
7
+ Gosu::MS_RIGHT
8
+ end
9
+ end