fantasy 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 98083689a972e68c27f07ae5c9c8ef9818c1b1215fbf2baccc541233efca6827
4
- data.tar.gz: db32ef03fd54c4711b9b38b9f4e3f4ad587839fc4d78523982c6c69f85cb0ab4
3
+ metadata.gz: fe5958bf098b2f7a0190eecbd5a9914c5a232e38863b4e9a19280b44653d771a
4
+ data.tar.gz: fa8899b7f6f6c04cda1e3ee6d470e940e377395b403d6dd3cddcdbf39583cc30
5
5
  SHA512:
6
- metadata.gz: 5a6d2c503880c672d147909cd5ab5a88639ab4e3e019ea5b81601c4aa0cc967e03e5c8b48f1994309ab379e83012f5a103043ba6b64989ba8009d2c7e3950174
7
- data.tar.gz: 93f37ba3a6fde17d55ba7aa203a1ade6645e69af8d670d37b5a7d78442c5c1dc4bb115784520cbd5b6a9ef661b18e6dbc2b094dab4c2a54feca8d37e4c527303
6
+ metadata.gz: 7c7830aca33b0a07482782f644301c854f9017ed10bc4602435945a70901e60fdd01adae8ed757f4ecb3a07a51e0b4279a273dbe149c665472e75e7c32e80f28
7
+ data.tar.gz: 5fee0000ec447edd1849592ab3f5145214d4a84dcf983bf1fff9c05a5ac3655d99bf519d4760ebaba2fadba6194dbf697468cd2483d0b3a77e8ab1611210eb65
data/lib/fantasy/actor.rb CHANGED
@@ -80,13 +80,12 @@ class Actor
80
80
  @position = @position + (@direction * @speed * Global.frame_time)
81
81
 
82
82
  if solid?
83
- actual_collisions = collisions
84
- actual_collisions.each do |actor|
83
+ collisions.each do |actor|
85
84
  collision_with(actor)
86
85
  actor.collision_with(self)
87
86
  end
88
87
 
89
- @position = @last_position if actual_collisions.any?
88
+ @position = @last_position if collisions.any? # we don't cache collisions because position may be changed on collision callback
90
89
  end
91
90
  end
92
91
  end
@@ -0,0 +1,34 @@
1
+ class Background
2
+ attr_accessor :scale, :color, :visible, :position, :layer
3
+
4
+ def initialize(image_name:)
5
+ @image = Image.new(image_name)
6
+ @name = image_name
7
+ @position = Coordinates.zero
8
+ @scale = 1
9
+ @visible = true
10
+ @draggable_on_debug = true
11
+ @dragging = false
12
+ @layer = -100
13
+
14
+ Global.backgrounds.push(self)
15
+ end
16
+
17
+ def width
18
+ @image.width() * @scale
19
+ end
20
+
21
+ def height
22
+ @image.height() * @scale
23
+ end
24
+
25
+ def draw
26
+ if visible
27
+ @image.draw(x: @position.x, y: @position.y, scale: @scale)
28
+ end
29
+ end
30
+
31
+ def destroy
32
+ Global.backgrounds.delete(self)
33
+ end
34
+ end
data/lib/fantasy/clock.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  class Clock
2
+ attr_accessor :persistent
3
+
2
4
  def initialize(&block)
3
5
  @block = block
4
6
  @thread = nil
7
+ @persistent = false # if persistent, clock is not stopped when loading new scene
5
8
 
6
9
  Global.clocks << self
7
10
  end
@@ -28,12 +31,22 @@ class Clock
28
31
  while(times_executed < times)
29
32
  @block.call
30
33
  times_executed += 1;
31
- sleep(seconds)
34
+
35
+ seconds_to_sleep = seconds.is_a?(Range) ? rand(seconds) : seconds
36
+ sleep(seconds_to_sleep)
32
37
  end
33
38
  end
34
39
  end
35
40
 
36
41
  def stop
37
- Thread.kill(@thread)
42
+ Thread.kill(@thread) unless @thread.nil?
43
+ end
44
+
45
+ def started?
46
+ !@thread.nil?
47
+ end
48
+
49
+ def persistent?
50
+ @persistent
38
51
  end
39
52
  end
@@ -1,26 +1,17 @@
1
1
  module Draggable
2
2
  def drag
3
- puts "XXX: dragging 1 #{name}"
4
3
  mouse_position = Global.mouse_position
5
4
 
6
- puts "@draggable_on_debug: #{@draggable_on_debug}"
7
- puts "@dragging: #{@dragging}"
8
- puts "Gosu.button_down?(Gosu::MS_LEFT): #{Gosu.button_down?(Gosu::MS_LEFT)}"
9
- puts "collision: #{Utils.collision_at?(self, mouse_position.x, mouse_position.y)}"
10
-
11
5
  if @draggable_on_debug && !@dragging && Gosu.button_down?(Gosu::MS_LEFT) && Utils.collision_at?(self, mouse_position.x, mouse_position.y)
12
- puts "XXX: dragging start #{name}"
13
6
  @dragging = true
14
7
  @dragging_offset = mouse_position - @position
15
8
  end
16
9
 
17
10
  if @dragging && !Gosu.button_down?(Gosu::MS_LEFT)
18
- puts "XXX: dragging end #{name}"
19
11
  @dragging = false
20
12
  end
21
13
 
22
14
  if @dragging
23
- puts "XXX: dragging 3 #{name}"
24
15
  @position = mouse_position - @dragging_offset
25
16
  end
26
17
  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, :clocks
5
+ attr_accessor :actors, :hud_texts, :hud_images, :backgrounds, :clocks
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
@@ -25,6 +25,7 @@ module Global
25
25
  @actors = []
26
26
  @hud_texts = []
27
27
  @hud_images = []
28
+ @backgrounds = []
28
29
  @clocks = []
29
30
  @last_frame_at = Time.now
30
31
  @debug = false
@@ -34,10 +35,17 @@ module Global
34
35
  @camera = Camera.new(position: Coordinates.zero)
35
36
  @game_state = Global.presentation_proc.nil? ? "game" : "presentation"
36
37
 
37
- @presentation_proc = Global.default_on_presentation if @presentation_proc.nil?
38
- @game_proc = Global.default_on_game if @game_proc.nil?
39
- @end_proc = Global.default_on_end if @end_proc.nil?
40
- @paused = false
38
+ if @presentation_proc.nil?
39
+ on_presentation { Global.default_on_presentation }
40
+ end
41
+
42
+ if @game_proc.nil?
43
+ on_game { Global.default_on_game }
44
+ end
45
+
46
+ if @end_proc.nil?
47
+ on_end { Global.default_on_end }
48
+ end
41
49
  end
42
50
 
43
51
  def update
@@ -74,7 +82,7 @@ module Global
74
82
  puts "Game stage 'presentation'"
75
83
 
76
84
  clear_state_elements
77
- presentation_proc.call
85
+ @presentation_proc.call
78
86
  end
79
87
 
80
88
  def go_to_game
@@ -95,7 +103,8 @@ module Global
95
103
  @actors.clear
96
104
  @hud_texts.clear
97
105
  @hud_images.clear
98
- @clocks.each(&:stop)
106
+ @backgrounds.clear
107
+ @clocks.reject(&:persistent?).each(&:stop)
99
108
  @background = Color.new(r: 0, g: 0, b: 0)
100
109
  end
101
110
 
data/lib/fantasy/loop.rb CHANGED
@@ -45,6 +45,7 @@ class Game < Gosu::Window
45
45
  Gosu.draw_rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, Global.background)
46
46
 
47
47
  (
48
+ Global.backgrounds +
48
49
  Global.actors +
49
50
  Global.hud_texts +
50
51
  Global.hud_images
data/lib/fantasy/utils.rb CHANGED
@@ -1,12 +1,15 @@
1
1
  module Utils
2
2
  # https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection
3
3
  def self.collision?(actor_1, actor_2)
4
- (
5
- actor_1.position.x < (actor_2.position.x + actor_2.width) &&
6
- (actor_1.position.x + actor_1.width) > actor_2.position.x &&
7
- actor_1.position.y < (actor_2.position.y + actor_2.height) &&
8
- actor_1.position.y + actor_1.height > actor_2.position.y
9
- )
4
+ result =
5
+ (
6
+ actor_1.position.x < (actor_2.position.x + actor_2.width) &&
7
+ (actor_1.position.x + actor_1.width) > actor_2.position.x &&
8
+ actor_1.position.y < (actor_2.position.y + actor_2.height) &&
9
+ actor_1.position.y + actor_1.height > actor_2.position.y
10
+ )
11
+
12
+ result
10
13
  end
11
14
 
12
15
  def self.collision_at?(actor, x, y)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fantasy
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/fantasy.rb CHANGED
@@ -12,6 +12,7 @@ require_relative "fantasy/clock"
12
12
  require_relative "fantasy/loop"
13
13
  require_relative "fantasy/hud_text"
14
14
  require_relative "fantasy/hud_image"
15
+ require_relative "fantasy/background"
15
16
  require_relative "fantasy/sound"
16
17
  require_relative "fantasy/camera"
17
18
  require_relative "fantasy/image"
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.1
4
+ version: 0.1.3
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-11 00:00:00.000000000 Z
11
+ date: 2022-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gosu
@@ -58,6 +58,7 @@ files:
58
58
  - fonts/VT323-Regular.ttf
59
59
  - lib/fantasy.rb
60
60
  - lib/fantasy/actor.rb
61
+ - lib/fantasy/background.rb
61
62
  - lib/fantasy/base.rb
62
63
  - lib/fantasy/camera.rb
63
64
  - lib/fantasy/clock.rb