fantasy 0.1.7 → 0.1.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +1,16 @@
1
1
  class HudText
2
- attr_accessor :text, :size, :color, :visible, :layer, :in_world, :position
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,8 +19,10 @@ class HudText
17
19
 
18
20
  def draw
19
21
  if visible
20
- font.draw_markup(text, screen_position.x + shadow_offset, screen_position.y - 20 + shadow_offset, 1, 1, 1, Gosu::Color::BLACK)
21
- font.draw_markup(text, screen_position.x, screen_position.y - 20, 1, 1, 1, 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
@@ -27,11 +31,37 @@ class HudText
27
31
  def font
28
32
  found_font = Global.pixel_fonts[@size]
29
33
  if found_font.nil?
30
- raise "HudText.size not valid '#{@size}'. Valid sizes: 'small, medium, big, huge'"
34
+ raise "HudText.size value not valid '#{@size}'. Valid values: 'small, medium, big, huge'"
31
35
  end
32
36
  found_font
33
37
  end
34
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 "top-center"
57
+ Coordinates.new(0.5, 0)
58
+ when "center"
59
+ Coordinates.new(0.5, 0.5)
60
+ else
61
+ raise "HudText.alignment value not valid '#{@alignment}'. Valid values: 'top-left, top-right, top-center, center'"
62
+ end
63
+ end
64
+
35
65
  def shadow_offset
36
66
  case @size
37
67
  when "small"
@@ -43,7 +73,7 @@ class HudText
43
73
  when "huge"
44
74
  4
45
75
  else
46
- raise "HudText.size not valid '#{@size}'. Valid sizes: 'small, medium, big, huge'"
76
+ raise "HudText.size value not valid '#{@size}'. Valid sizes: 'small, medium, big, huge'"
47
77
  end
48
78
  end
49
79
 
@@ -55,11 +85,7 @@ class HudText
55
85
  end
56
86
  end
57
87
 
58
- def destroy
59
- Global.hud_texts.delete(self)
60
- end
61
-
62
88
  def draw_debug
63
- Global.pixel_fonts["medium"].draw_text("#{@position.x.floor},#{@position.y.floor}", @position.x, @position.y, 1)
89
+ Global.pixel_fonts["medium"].draw_text("#{@position.x.floor},#{@position.y.floor}", screen_position.x, screen_position.y, 1)
64
90
  end
65
91
  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,24 @@
1
+ module Mover
2
+ def impulse(direction:, force:)
3
+ add_force(direction.normalize * force)
4
+ end
5
+
6
+ def add_force(force)
7
+ @acceleration ||= Coordinates.zero
8
+ @acceleration += force
9
+ end
10
+
11
+ def apply_forces(max_speed: Float::INFINITY)
12
+ @acceleration ||= Coordinates.zero
13
+ @velocity ||= Coordinates.zero
14
+
15
+ @velocity += @acceleration
16
+ @velocity.resize(max_speed) if @velocity.length > max_speed
17
+
18
+ unless @velocity.length.zero?
19
+ @position += @velocity * Global.frame_time
20
+ end
21
+
22
+ @acceleration = Coordinates.zero
23
+ end
24
+ 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
@@ -0,0 +1,53 @@
1
+ module Music
2
+ class << self
3
+ @@musics = {}
4
+ @@actual_song = nil
5
+
6
+ def play(music_name, volume: nil)
7
+ stop
8
+
9
+ @@actual_song = locate_music(music_name)
10
+ @@actual_song.play(true)
11
+
12
+ self.volume = volume unless volume.nil?
13
+ end
14
+
15
+ def stop
16
+ @@actual_song.stop unless @@actual_song.nil?
17
+ end
18
+
19
+ def volume
20
+ @@actual_song&.volume
21
+ end
22
+
23
+ def volume=(value)
24
+ @@actual_song.volume = value unless @@actual_song.nil?
25
+ end
26
+
27
+ def locate_music(music_name)
28
+ return @@musics[music_name] if @@musics[music_name]
29
+
30
+ puts "Initialize Music: '#{music_name}'"
31
+
32
+ file_name = Dir.entries(base_path).find { |e| e =~ /^#{music_name}($|\.)/ }
33
+
34
+ raise "Music file not found with name '#{music_name}'" if file_name.nil?
35
+
36
+ @@musics[music_name] = Gosu::Song.new("#{base_path}/#{file_name}")
37
+
38
+ return @@musics[music_name]
39
+ end
40
+
41
+ def preload_musics
42
+ return unless Dir.exist?(base_path)
43
+
44
+ Dir.each_child(base_path) do |file_name|
45
+ locate_music(file_name) unless file_name.start_with?(".")
46
+ end
47
+ end
48
+
49
+ def base_path
50
+ "#{Dir.pwd}/musics"
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,55 @@
1
+ class Shape
2
+ include UserInputs
3
+
4
+ attr_accessor :kind, :position, :width, :height, :stroke, :color, :fill, :stroke_color, :layer
5
+
6
+ def initialize(kind:, position:, width:, height:, stroke: 1, fill: true, color: Color.palette.black, stroke_color: nil)
7
+ @kind = kind
8
+ @position = position
9
+ @width = width
10
+ @height = height
11
+ @stroke = stroke
12
+ @color = color
13
+ @fill = fill
14
+ @stroke_color = stroke_color
15
+ @layer = 1
16
+
17
+ Global.shapes << self
18
+ end
19
+
20
+ def self.rectangle(position:, width:, height:, color: Color.palette.black)
21
+ Shape.new(kind: "rectangle", position: position, width: width, height: height)
22
+ end
23
+
24
+ def draw
25
+ case @kind
26
+ when "rectangle"
27
+ draw_rectangle
28
+ else
29
+ raise "Shape.kind not supported: '#{@kind}'. Supported kinds: 'rectangle'"
30
+ end
31
+ end
32
+
33
+ def destroy
34
+ Global.shapes.delete(self)
35
+ end
36
+
37
+ private
38
+
39
+ def draw_rectangle
40
+ if fill
41
+ Gosu.draw_rect(@position.x, @position.y, @width, @height, @color)
42
+ end
43
+
44
+ unless stroke.zero?
45
+ draw_frame(@position.x, @position.y, @width, @height, @stroke, @stroke_color || @color)
46
+ end
47
+ end
48
+
49
+ def draw_frame(x, y, width, height, stroke, color)
50
+ Gosu.draw_rect(x, y, width, stroke, color)
51
+ Gosu.draw_rect(width - stroke + x, y, stroke, height, color)
52
+ Gosu.draw_rect(x, height - stroke + y, width, stroke, color)
53
+ Gosu.draw_rect(x, y, stroke, height, color)
54
+ end
55
+ end
data/lib/fantasy/sound.rb CHANGED
@@ -2,8 +2,8 @@ module Sound
2
2
  class << self
3
3
  @@sounds = {}
4
4
 
5
- def play(sound_name)
6
- locate_sound(sound_name).play
5
+ def play(sound_name, volume: 1)
6
+ locate_sound(sound_name).play(volume)
7
7
  end
8
8
 
9
9
  def locate_sound(sound_name)
@@ -21,6 +21,8 @@ module Sound
21
21
  end
22
22
 
23
23
  def preload_sounds
24
+ return unless Dir.exist?(base_path)
25
+
24
26
  Dir.each_child(base_path) do |file_name|
25
27
  locate_sound(file_name) unless file_name.start_with?(".")
26
28
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fantasy
4
- VERSION = "0.1.7"
4
+ VERSION = "0.1.13"
5
5
  end
data/lib/fantasy.rb CHANGED
@@ -2,12 +2,21 @@
2
2
  require "gosu"
3
3
 
4
4
  require_relative "fantasy/version"
5
+
6
+ require_relative "fantasy/coordinates"
7
+ require_relative "fantasy/cursor"
8
+ require_relative "fantasy/mouse"
5
9
  require_relative "fantasy/includes/move_by_cursors"
10
+ require_relative "fantasy/includes/move_by_direction"
11
+ require_relative "fantasy/includes/mover"
12
+ require_relative "fantasy/includes/gravitier"
13
+ require_relative "fantasy/includes/jumper"
14
+ require_relative "fantasy/includes/user_inputs"
6
15
  require_relative "fantasy/tween"
7
16
  require_relative "fantasy/draggable"
8
17
  require_relative "fantasy/color"
18
+ require_relative "fantasy/shape"
9
19
  require_relative "fantasy/actor"
10
- require_relative "fantasy/coordinates"
11
20
  require_relative "fantasy/utils"
12
21
  require_relative "fantasy/global"
13
22
  require_relative "fantasy/clock"
@@ -16,7 +25,9 @@ require_relative "fantasy/hud_text"
16
25
  require_relative "fantasy/hud_image"
17
26
  require_relative "fantasy/background"
18
27
  require_relative "fantasy/sound"
28
+ require_relative "fantasy/music"
19
29
  require_relative "fantasy/camera"
20
30
  require_relative "fantasy/image"
21
31
  require_relative "fantasy/tilemap"
22
32
  require_relative "fantasy/base"
33
+ require_relative "fantasy/disk"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fantasy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fernando Guillen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-17 00:00:00.000000000 Z
11
+ date: 2022-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gosu
@@ -47,6 +47,7 @@ extra_rdoc_files: []
47
47
  files:
48
48
  - ".rubocop.yml"
49
49
  - CHANGELOG.md
50
+ - COLOR_PALETTE.md
50
51
  - Gemfile
51
52
  - LICENSE.txt
52
53
  - README.md
@@ -64,13 +65,23 @@ files:
64
65
  - lib/fantasy/clock.rb
65
66
  - lib/fantasy/color.rb
66
67
  - lib/fantasy/coordinates.rb
68
+ - lib/fantasy/cursor.rb
69
+ - lib/fantasy/disk.rb
67
70
  - lib/fantasy/draggable.rb
68
71
  - lib/fantasy/global.rb
69
72
  - lib/fantasy/hud_image.rb
70
73
  - lib/fantasy/hud_text.rb
71
74
  - lib/fantasy/image.rb
75
+ - lib/fantasy/includes/gravitier.rb
76
+ - lib/fantasy/includes/jumper.rb
72
77
  - lib/fantasy/includes/move_by_cursors.rb
78
+ - lib/fantasy/includes/move_by_direction.rb
79
+ - lib/fantasy/includes/mover.rb
80
+ - lib/fantasy/includes/user_inputs.rb
73
81
  - lib/fantasy/loop.rb
82
+ - lib/fantasy/mouse.rb
83
+ - lib/fantasy/music.rb
84
+ - lib/fantasy/shape.rb
74
85
  - lib/fantasy/sound.rb
75
86
  - lib/fantasy/tilemap.rb
76
87
  - lib/fantasy/tween.rb