or2d 0.0.5 → 0.0.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d549bc286d1ed32b29b3c09acb3490e373568eb8505895f6453287289a204261
4
- data.tar.gz: 0a031f2fe7d9f0316388952ddff86c0adc4dfaf06d00368432e6c30b73a46468
3
+ metadata.gz: 84ff6f80df77d3cdf1c7d68a9c807c6f91024150984ee4848f6852f7d1f1e73a
4
+ data.tar.gz: 1f3c4e7a6f0f88acea2ec63ad7c78f5c2a76e8417abbeba4b0fd5fbdbf7b8d53
5
5
  SHA512:
6
- metadata.gz: bb2eac9fbac591a5398821fda5dee6e94fb45f25cb8624999d2c0fe67feadee62efee3b1e02051655fc7d3a1c5ecdbf7ffd8a431ffbb38323bae81bc093ed1ce
7
- data.tar.gz: 3a6166a5d9ccbb2359bb3026efced1e30696c40506354a0725db510057b76f32efcb0733c96b6935c29aae4b28083b776b3b1c6c509c643c4ca7baffec70b16c
6
+ metadata.gz: 3c63db0c5fb39018dd3054d4fc83121ab697dca0326f5b46ee3d4ec0f2917df3de83be7bd2cbbdfc6e34ba420f7fa9032ba584caab150c73d6343e20904f9aa9
7
+ data.tar.gz: a24f90e8eb8749a3f92cc24120ffe1aa21b7906226cd1c110574ed1384839783dad6308664e98b5f8b3af38700a65e844f97fc79ec689cfa2cd44f79e1d5c8d8
File without changes
File without changes
File without changes
@@ -0,0 +1,87 @@
1
+ module OR2D
2
+ class Camera
3
+
4
+ # The target of the camera.
5
+ # @return [OR2D::Entity] the target of the camera
6
+ attr_reader :target
7
+
8
+ # The viewport of the camera.
9
+ # @return [OR2D::Entity] the viewport of the camera
10
+ attr_reader :viewport
11
+
12
+ # Constructs a new Camera object.
13
+ # @param viewport_width [Integer] the width of the viewport
14
+ # @param viewport_height [Integer] the height of the viewport
15
+ # @param target [OR2D::Entity] the target of the camera
16
+ def initialize(viewport_width = OR2D.game.screen_width,
17
+ viewport_height = OR2D.game.screen_height,
18
+ target = OR2D::Entity.new(:square, { size: 1, x: 0, y: 0, show: false }),
19
+ speed = 1)
20
+ @target = target
21
+ @speed = speed
22
+ @viewport = OR2D::Entity.new(:rectangle, {
23
+ x: (OR2D.game.screen_width / 2) - viewport_width,
24
+ y: (OR2D.game.screen_height / 2) + viewport_height,
25
+ width: viewport_width,
26
+ height: viewport_height,
27
+ show: false
28
+ })
29
+ end
30
+
31
+ def update; end
32
+
33
+ def scroll(direction, amount, target: false)
34
+ case direction
35
+ when :north then scroll_north(amount, target)
36
+ when :south then scroll_south(amount, target)
37
+ when :east then scroll_east(amount, target)
38
+ when :west then scroll_west(amount, target)
39
+ else raise ArgumentError, "Invalid direction: #{direction}"
40
+ end
41
+ end
42
+
43
+ # Simulates a northern camera scroll by moving all of the visible entities on the screen to the south.
44
+ # @param delta_y [Integer] the amount to scroll each entity
45
+ # @param target [Boolean] whether or not to scroll the target
46
+ def scroll_north(delta_y, target)
47
+ OR2D.game.entities.each_value do |entity|
48
+ next if !target && entity == @target
49
+
50
+ entity.screen_y += delta_y
51
+ end
52
+ end
53
+
54
+ # Simulates a southern camera scroll by moving all of the visible entities on the screen to the north.
55
+ # @param delta_y [Integer] the amount to scroll each entity
56
+ # @param target [Boolean] whether or not to scroll the target
57
+ def scroll_south(delta_y, target)
58
+ OR2D.game.entities.each_value do |entity|
59
+ next if !target && entity == @target
60
+
61
+ entity.screen_y -= delta_y
62
+ end
63
+ end
64
+
65
+ # Simulates an eastern camera scroll by moving all of the visible entities on the screen to the west.
66
+ # @param delta_x [Integer] the amount to scroll each entity
67
+ # @param target [Boolean] whether or not to scroll the target
68
+ def scroll_east(delta_x, target)
69
+ OR2D.game.entities.each_value do |entity|
70
+ next if !target && entity == @target
71
+
72
+ entity.screen_x -= delta_x
73
+ end
74
+ end
75
+
76
+ # Simulates a western camera scroll by moving all of the visible entities on the screen to the east.
77
+ # @param delta_x [Integer] the amount to scroll each entity
78
+ # @param target [Boolean] whether or not to scroll the target
79
+ def scroll_west(delta_x, target)
80
+ OR2D.game.entities.each_value do |entity|
81
+ next if !target && entity == @target
82
+
83
+ entity.screen_x += delta_x
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,39 @@
1
+ module OR2D::Cameras
2
+ class CenteredCamera < OR2D::Camera
3
+ def initialize(target = nil)
4
+ super(OR2D.game.screen_width, OR2D.game.screen_height, target, 1.scale)
5
+ orientate
6
+ @scrolling = false
7
+ @direction = nil
8
+ end
9
+
10
+ def update
11
+ orientate
12
+ return unless @scrolling
13
+
14
+ case @direction
15
+ when :north then scroll_north(@speed, false)
16
+ when :south then scroll_south(@speed, false)
17
+ when :east then scroll_east(@speed, false)
18
+ when :west then scroll_west(@speed, false)
19
+ end
20
+
21
+ reset_scrolling
22
+ end
23
+
24
+ def scroll(direction, amount)
25
+ @scrolling = true
26
+ @direction = direction
27
+ @speed = amount
28
+ end
29
+
30
+ def orientate
31
+ @target.screen_x = OR2D.game.screen_width / 2
32
+ @target.screen_y = OR2D.game.screen_height / 2
33
+ end
34
+
35
+ def reset_scrolling
36
+ @scrolling = false
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,65 @@
1
+ module OR2D::Cameras
2
+ # A PaddedCamera is a camera that has padding. When the target <OR2D::Entity> reaches the edge of the padding, the camera will scroll.
3
+ class PaddedCamera < OR2D::Camera
4
+ # Constructs a new PaddedCamera.
5
+ # @param target [OR2D::Entity] the target of the camera
6
+ # @param padding_width [Integer] the width of the padding
7
+ # @param padding_height [Integer] the height of the padding
8
+ def initialize(padding_width = 512, padding_height = 512, target = nil)
9
+ @padding_width = padding_width
10
+ @padding_height = padding_height
11
+ super(@padding_width, @padding_height, target, 1.scale)
12
+ orientate
13
+ end
14
+
15
+ # Updates the camera.
16
+ def update
17
+ scroll if should_scroll?
18
+ end
19
+
20
+ # Checks if the camera should scroll. This check is done by checking if the target <OR2D::Entity> is within the <OR2D::Cameras::PaddedCamera#boundary>.
21
+ # @return [Boolean] true if the camera should scroll, false otherwise
22
+ def should_scroll?
23
+ should_scroll_east? || should_scroll_west? || should_scroll_north? || should_scroll_south?
24
+ end
25
+
26
+ def should_scroll_east?
27
+ @target.screen_x >= (@viewport.screen_x + @viewport.width)
28
+ end
29
+
30
+ def should_scroll_west?
31
+ @target.screen_x <= (@viewport.screen_x - @viewport.width)
32
+ end
33
+
34
+ def should_scroll_north?
35
+ @target.screen_y <= (@viewport.screen_y - @viewport.height)
36
+ end
37
+
38
+ def should_scroll_south?
39
+ @target.screen_y >= (@viewport.screen_y + @viewport.height)
40
+ end
41
+
42
+ def scroll
43
+ if should_scroll_east?
44
+ super(:east, @speed, target: true)
45
+ @viewport.screen_x += @speed
46
+ elsif should_scroll_west?
47
+ super(:west, @speed, target: true)
48
+ @viewport.screen_x -= @speed
49
+ end
50
+
51
+ if should_scroll_north?
52
+ super(:north, @speed, target: true)
53
+ @viewport.screen_y -= @speed
54
+ elsif should_scroll_south?
55
+ super(:south, @speed, target: true)
56
+ @viewport.screen_y += @speed
57
+ end
58
+ end
59
+
60
+ def orientate
61
+ @target.screen_x = @viewport.screen_x + @viewport.width / 2
62
+ @target.screen_y = @viewport.screen_y + @viewport.height / 2
63
+ end
64
+ end
65
+ end
File without changes
File without changes
File without changes
File without changes
data/lib/or2d/console.rb CHANGED
File without changes
data/lib/or2d/entity.rb CHANGED
@@ -14,6 +14,14 @@ module OR2D
14
14
  # @return [Ruby2D::Image, Ruby2D::Sprite, Ruby2D::Text, Ruby2D::Triangle, Ruby2D::Quad, Ruby2D::Circle, Ruby2D::Square, Ruby2D::Line, Ruby2D::Rectangle] the entity's resource
15
15
  attr_reader :resource
16
16
 
17
+ # @!attribute [r] previous_x
18
+ # @return [Integer] the entity's previous x coordinate
19
+ attr_reader :previous_screen_x
20
+
21
+ # @!attribute [r] previous_y
22
+ # @return [Integer] the entity's previous y coordinate
23
+ attr_reader :previous_screen_y
24
+
17
25
  # Constructs a new Entity object.
18
26
  # @param type [Symbol] the type of entity to create
19
27
  # @param options [Hash] the options to create the entity with
@@ -28,6 +36,8 @@ module OR2D
28
36
  @resource = options[:resource] || OR2D::Resource.create(type, options)
29
37
  @width = @resource.width if @resource.respond_to?(:width)
30
38
  @height = @resource.height if @resource.respond_to?(:height)
39
+ @previous_screen_x = options[:x] || 0
40
+ @previous_screen_y = options[:y] || 0
31
41
  show if options[:show]
32
42
  ensure
33
43
  OR2D.game.add_entity(self)
@@ -36,6 +46,7 @@ module OR2D
36
46
  # Set the Entity x coordinate.
37
47
  # @param x_coordinate [Integer] the x coordinate of the Entity on the screen
38
48
  def x=(x_coordinate)
49
+ @previous_screen_x = @x
39
50
  super(x_coordinate)
40
51
  @resource.x = x_coordinate if @resource.respond_to?(:x=)
41
52
  end
@@ -43,6 +54,7 @@ module OR2D
43
54
  # Set the Entity y coordinate.
44
55
  # @param y_coordinate [Integer] the y coordinate of the Entity on the screen
45
56
  def y=(y_coordinate)
57
+ @previous_screen_x = @y
46
58
  super(y_coordinate)
47
59
  @resource.y = y_coordinate if @resource.respond_to?(:y=)
48
60
  end
@@ -78,5 +90,12 @@ module OR2D
78
90
  OR2D.game.window.remove(@resource) if @resource
79
91
  OR2D.game.remove_entity(@id)
80
92
  end
93
+
94
+ alias screen_x x
95
+ alias screen_y y
96
+ alias screen_z z
97
+ alias screen_x= x=
98
+ alias screen_y= y=
99
+ alias screen_z= z=
81
100
  end
82
101
  end
data/lib/or2d/instance.rb CHANGED
@@ -88,6 +88,18 @@ module OR2D
88
88
  @window.close
89
89
  end
90
90
 
91
+ # The width of the game window.
92
+ # @return [Integer] the width of the game window
93
+ def screen_width
94
+ @window.get(:width)
95
+ end
96
+
97
+ # The height of the game window.
98
+ # @return [Integer] the height of the game window
99
+ def screen_height
100
+ @window.get(:height)
101
+ end
102
+
91
103
  private
92
104
 
93
105
  def setup
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
data/lib/or2d/resource.rb CHANGED
File without changes
data/lib/or2d/scene.rb CHANGED
File without changes
File without changes
data/lib/or2d.rb CHANGED
@@ -19,30 +19,37 @@ Dir["#{File.dirname(__FILE__)}/or2d/patches/*.rb"].each { |file| load(file) }
19
19
  # @author Patrick W.
20
20
  # @since 2023-04-26
21
21
  module OR2D
22
- autoload :Animation, 'or2d/animation'
23
- autoload :Composite, 'or2d/composite'
24
- autoload :Console, 'or2d/console'
25
- autoload :Entity, 'or2d/entity'
26
- autoload :Instance, 'or2d/instance'
27
- autoload :Resource, 'or2d/resource'
28
- autoload :Scene, 'or2d/scene'
22
+ autoload :Animation, 'or2d/animation'
23
+ autoload :Composite, 'or2d/composite'
24
+ autoload :Console, 'or2d/console'
25
+ autoload :Camera, 'or2d/camera'
26
+ autoload :Entity, 'or2d/entity'
27
+ autoload :Instance, 'or2d/instance'
28
+ autoload :Resource, 'or2d/resource'
29
+ autoload :Scene, 'or2d/scene'
29
30
 
30
31
  # The Animations module contains modules and classes for animating objects.
31
32
  module Animations
32
- autoload :CompositeAnimations, 'or2d/animations/composite_animations'
33
- autoload :EntityAnimations, 'or2d/animations/entity_animations'
33
+ autoload :CompositeAnimations, 'or2d/animations/composite_animations'
34
+ autoload :EntityAnimations, 'or2d/animations/entity_animations'
35
+ end
36
+
37
+ # The Cameras module contains all of the camera implementations for the OR2D gem.
38
+ module Cameras
39
+ autoload :CenteredCamera, 'or2d/cameras/centered'
40
+ autoload :PaddedCamera, 'or2d/cameras/padded'
34
41
  end
35
42
 
36
43
  # The Composites module contains all of the composite entities for the OR2D gem.
37
44
  module Composites
38
- autoload :Projectile, 'or2d/composites/projectile'
39
- autoload :Sprite, 'or2d/composites/sprite'
40
- autoload :Text, 'or2d/composites/text'
45
+ autoload :Projectile, 'or2d/composites/projectile'
46
+ autoload :Sprite, 'or2d/composites/sprite'
47
+ autoload :Text, 'or2d/composites/text'
41
48
  end
42
49
 
43
50
  # The Scenes module contains all of the scenes for the OR2D gem.
44
51
  module Scenes
45
- autoload :PlaceholderScene, 'or2d/scenes/placeholder_scene'
52
+ autoload :PlaceholderScene, 'or2d/scenes/placeholder_scene'
46
53
  end
47
54
 
48
55
  # Is debug mode enabled?
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: or2d
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick W.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-10 00:00:00.000000000 Z
11
+ date: 2023-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dotenv
@@ -138,6 +138,9 @@ files:
138
138
  - lib/or2d/animation.rb
139
139
  - lib/or2d/animations/composite_animations.rb
140
140
  - lib/or2d/animations/entity_animations.rb
141
+ - lib/or2d/camera.rb
142
+ - lib/or2d/cameras/centered.rb
143
+ - lib/or2d/cameras/padded.rb
141
144
  - lib/or2d/composite.rb
142
145
  - lib/or2d/composites/projectile.rb
143
146
  - lib/or2d/composites/sprite.rb