Class: Background

Inherits:
Object
  • Object
show all
Defined in:
lib/fantasy/background.rb

Overview

Represents on static image that will be rendered on every frame. Replicable (by default). The position is relative to Camera.main.

on_game do
  background = Background.new(image_name: "beach")
  # background.replicable = false # if you don't want the image to replicate
  background.scale = 6
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image_name:) ⇒ Background

Generates an Background with all the below default values:

  • position: Coordinates.zero.
  • scale: 1.
  • draggable_on_debug: true.
  • layer: -100.
  • image: The Image generated by image_name
  • name: Same as image_name
  • visible: true
  • replicable: true

Examples:

Instantiate a new Background

background = Background.new("background")

Parameters:

  • image_name (string)

    the name of the image file from ./images/*



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fantasy/background.rb', line 86

def initialize(image_name:)
  @image = Image.new(image_name)
  @position = Coordinates.zero
  @scale = 1
  @visible = true
  @draggable_on_debug = true
  @dragging = false
  @layer = -100
  @replicable = true

  Global.backgrounds.push(self)
end

Instance Attribute Details

#layerObject

In which layer the image of the Background is rendered. Smaller numbers are rendered behind higher numbers.

Default -100.

Examples:

Set layer

background = Background.new("image")
background.layer = -50


23
24
25
# File 'lib/fantasy/background.rb', line 23

def layer
  @layer
end

#positionObject

Coordinates object where x and y represent the position of the Background in the World (no necessarily in the Screen).

Default Coordinates.zero.

Examples:

Setting position

background = Background.new("image")
background.position = Coordinates.new(10, 20)
background.position.x # => 10

Modify position

background.position.x +=  1
background.position.x # => 11


39
40
41
# File 'lib/fantasy/background.rb', line 39

def position
  @position
end

#replicableObject

[Boolean] When true the image will replicate itself to cover all the screen. Default true.

Default true.

Examples:

Setting replicable

background = Background.new("image")
background.replicable = false


48
49
50
# File 'lib/fantasy/background.rb', line 48

def replicable
  @replicable
end

#scaleObject

Note:

this value affects the attributes width and height

The value to scale the image of the Background when drawn. If the value is 2 the image will rendered at double of size. If the value is 0.5 the image will rendered at half of size.

Default 1.

Examples:

Set scale

background = Background.new("image")
background.scale = 6


61
62
63
# File 'lib/fantasy/background.rb', line 61

def scale
  @scale
end

#visibleObject

When false the Background won't be rendered in the next frame.

Default true.

Examples:

Set visible

background = Background.new("image")
background.visible = false


70
71
72
# File 'lib/fantasy/background.rb', line 70

def visible
  @visible
end

Instance Method Details

#destroyObject

Destroy this Background and it will not longer be rendered

Examples:

Destroy a Background

background = Background.new("background")
background.destroy


128
129
130
# File 'lib/fantasy/background.rb', line 128

def destroy
  Global.backgrounds.delete(self)
end

#heightFixnum

Returns the Background height in pixels.

Returns:

  • (Fixnum)

    the Background height in pixels



105
106
107
# File 'lib/fantasy/background.rb', line 105

def height
  @image.height * @scale
end

#widthFixnum

Returns the Background width in pixels.

Returns:

  • (Fixnum)

    the Background width in pixels



100
101
102
# File 'lib/fantasy/background.rb', line 100

def width
  @image.width * @scale
end