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.
@@ -0,0 +1,51 @@
1
+ module Music
2
+ class << self
3
+ @@musics = {}
4
+ @@actual_song = nil
5
+
6
+ def play(music_name)
7
+ stop
8
+
9
+ @@actual_song = locate_music(music_name)
10
+ @@actual_song.play(true)
11
+ end
12
+
13
+ def stop
14
+ @@actual_song.stop unless @@actual_song.nil?
15
+ end
16
+
17
+ def volume
18
+ @@actual_song&.volume
19
+ end
20
+
21
+ def volume=(value)
22
+ @@actual_song.volume = value unless @@actual_song.nil?
23
+ end
24
+
25
+ def locate_music(music_name)
26
+ return @@musics[music_name] if @@musics[music_name]
27
+
28
+ puts "Initialize Music: '#{music_name}'"
29
+
30
+ file_name = Dir.entries(base_path).find { |e| e =~ /^#{music_name}($|\.)/ }
31
+
32
+ raise "Music file not found with name '#{music_name}'" if file_name.nil?
33
+
34
+ @@musics[music_name] = Gosu::Song.new("#{base_path}/#{file_name}")
35
+
36
+ return @@musics[music_name]
37
+ end
38
+
39
+ def preload_musics
40
+ return unless Dir.exist?(base_path)
41
+
42
+ Dir.each_child(base_path) do |file_name|
43
+ locate_music(file_name) unless file_name.start_with?(".")
44
+ end
45
+ end
46
+
47
+ def base_path
48
+ "#{Dir.pwd}/musics"
49
+ end
50
+ end
51
+ 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
@@ -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,4 +1,4 @@
1
- class TileMap
1
+ class Tilemap
2
2
  attr_accessor :position
3
3
 
4
4
  def initialize(map_name:, tiles:, tile_size: nil, tile_width: nil, tile_height: nil)
@@ -13,7 +13,7 @@ class TileMap
13
13
  @tiles = tiles
14
14
  @position = Coordinates.zero
15
15
 
16
- @grid = TileMap.load_grid(@map_name)
16
+ @grid = Tilemap.load_grid(@map_name)
17
17
  end
18
18
 
19
19
  def width
@@ -32,7 +32,13 @@ class TileMap
32
32
 
33
33
  line.each do |tile_index|
34
34
  if !tile_index.nil?
35
- actor = @tiles[tile_index].clone
35
+ actor_template = @tiles[tile_index]
36
+
37
+ if actor_template.nil?
38
+ raise "Tilemap config error. Not found Tile for index '#{tile_index}'"
39
+ end
40
+
41
+ actor = actor_template.clone
36
42
  actor.position.x = @position.x + (tile_position.x * @tile_width)
37
43
  actor.position.y = @position.y + (tile_position.y * @tile_height)
38
44
  end
@@ -50,7 +56,7 @@ class TileMap
50
56
  @@maps = {}
51
57
 
52
58
  def load_grid(map_name)
53
- File.readlines(TileMap.locate_map(map_name), chomp: true).map do |line|
59
+ File.readlines(Tilemap.locate_map(map_name), chomp: true).map do |line|
54
60
  line.each_char.map do |char|
55
61
  char == " " ? nil : char.to_i
56
62
  end
data/lib/fantasy/utils.rb CHANGED
@@ -27,4 +27,9 @@ module Utils
27
27
  Gosu.draw_rect(x, y + (height - stroke), width, stroke, color)
28
28
  Gosu.draw_rect(x, y, stroke, height, color)
29
29
  end
30
+
31
+ def self.remap(value:, from_ini:, from_end:, to_ini:, to_end:)
32
+ result = to_ini + (value - from_ini) * (to_end - to_ini) / (from_end - from_ini);
33
+ result
34
+ end
30
35
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fantasy
4
- VERSION = "0.1.5.1"
4
+ VERSION = "0.1.11"
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,8 @@ 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
- require_relative "fantasy/tile_map"
31
+ require_relative "fantasy/tilemap"
22
32
  require_relative "fantasy/base"
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.5.1
4
+ version: 0.1.11
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-16 00:00:00.000000000 Z
11
+ date: 2022-03-26 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,15 +65,24 @@ files:
64
65
  - lib/fantasy/clock.rb
65
66
  - lib/fantasy/color.rb
66
67
  - lib/fantasy/coordinates.rb
68
+ - lib/fantasy/cursor.rb
67
69
  - lib/fantasy/draggable.rb
68
70
  - lib/fantasy/global.rb
69
71
  - lib/fantasy/hud_image.rb
70
72
  - lib/fantasy/hud_text.rb
71
73
  - lib/fantasy/image.rb
74
+ - lib/fantasy/includes/gravitier.rb
75
+ - lib/fantasy/includes/jumper.rb
72
76
  - lib/fantasy/includes/move_by_cursors.rb
77
+ - lib/fantasy/includes/move_by_direction.rb
78
+ - lib/fantasy/includes/mover.rb
79
+ - lib/fantasy/includes/user_inputs.rb
73
80
  - lib/fantasy/loop.rb
81
+ - lib/fantasy/mouse.rb
82
+ - lib/fantasy/music.rb
83
+ - lib/fantasy/shape.rb
74
84
  - lib/fantasy/sound.rb
75
- - lib/fantasy/tile_map.rb
85
+ - lib/fantasy/tilemap.rb
76
86
  - lib/fantasy/tween.rb
77
87
  - lib/fantasy/utils.rb
78
88
  - lib/fantasy/version.rb