Class: Amaterasu::GameBoy::Oam::Sprite

Inherits:
Object
  • Object
show all
Defined in:
lib/amaterasu/game_boy/oam/sprite.rb

Overview

Models each Sprite that is stored inside the Object Attribute Memory (OAM).

Constant Summary collapse

SIZE_IN_BYTES =
4
BIT_MASK_BG_WIN_PRIORITY_SET =
1 << 7
BIT_MASK_OBJ_Y_FLIPPED =
1 << 6
BIT_MASK_OBJ_X_FLIPPED =
1 << 5
BIT_MASK_USE_OBP1_PALETTE =
1 << 4

Instance Method Summary collapse

Constructor Details

#initialize(oam_data:, index:) ⇒ Sprite

Returns a new instance of Sprite.

Parameters:

  • oam_data (Array)

    Reference to the original OAM @data.

  • index (Integer)

    Sprite index within OAM (0 - 39).



17
18
19
20
# File 'lib/amaterasu/game_boy/oam/sprite.rb', line 17

def initialize(oam_data:, index:)
  @oam_data    = oam_data
  @base_offset = index * SIZE_IN_BYTES
end

Instance Method Details

#attributesInteger

Returns Byte 3 of the Sprite.

Returns:

  • (Integer)

    Byte 3 of the Sprite.



65
66
67
# File 'lib/amaterasu/game_boy/oam/sprite.rb', line 65

def attributes
  @oam_data[@base_offset + 3]
end

#bg_win_priority_set?Boolean

If this bit is set in the Sprite attributes it means that the Background or Window should be displayed on top of the Sprite UNLESS the Background/Window pixel has color_id = 0b00, in which case the Sprite pixel is shown.

Returns:

  • (Boolean)


76
77
78
# File 'lib/amaterasu/game_boy/oam/sprite.rb', line 76

def bg_win_priority_set?
  (attributes & BIT_MASK_BG_WIN_PRIORITY_SET) != 0
end

#bottom_halfObject



60
61
62
# File 'lib/amaterasu/game_boy/oam/sprite.rb', line 60

def bottom_half
  @oam_data[@base_offset + 2] | 0x01
end

#inspectString

Returns Custom inspect for easier debugging.

Returns:

  • (String)

    Custom inspect for easier debugging.



96
97
98
99
100
101
102
# File 'lib/amaterasu/game_boy/oam/sprite.rb', line 96

def inspect
  '#<Sprite ' \
    "y_pos=$#{format('%02X', y)} " \
    "x_pos=$#{format('%02X', x)} " \
    "tile_index=$#{format('%02X', top_half)} " \
    "attributes=#{format('%08b', attributes)}>"
end

#tile_index(obj_size_8x16, y_flipped, current_obj_y) ⇒ Integer

Returns Byte 2 of the Sprite.

Returns:

  • (Integer)

    Byte 2 of the Sprite.



46
47
48
49
50
51
52
53
54
# File 'lib/amaterasu/game_boy/oam/sprite.rb', line 46

def tile_index(obj_size_8x16, y_flipped, current_obj_y)
  return @oam_data[@base_offset + 2] unless obj_size_8x16

  if current_obj_y >= 0 && current_obj_y < 8
    y_flipped ? bottom_half : top_half
  else
    y_flipped ? top_half : bottom_half
  end
end

#top_halfObject



56
57
58
# File 'lib/amaterasu/game_boy/oam/sprite.rb', line 56

def top_half
  @oam_data[@base_offset + 2] & 0xFE
end

#use_obp1_palette?Boolean

Returns Selects which Color Palette to use for the Sprite pixels.

Returns:

  • (Boolean)

    Selects which Color Palette to use for the Sprite pixels.



91
92
93
# File 'lib/amaterasu/game_boy/oam/sprite.rb', line 91

def use_obp1_palette?
  (attributes & BIT_MASK_USE_OBP1_PALETTE) != 0
end

#xInteger

Returns Byte 1 of the Sprite.

Returns:

  • (Integer)

    Byte 1 of the Sprite.



37
38
39
# File 'lib/amaterasu/game_boy/oam/sprite.rb', line 37

def x
  @oam_data[@base_offset + 1]
end

#x_flipped?Boolean

Returns Whether or not the Sprite is flipped vertically.

Returns:

  • (Boolean)

    Whether or not the Sprite is flipped vertically.



86
87
88
# File 'lib/amaterasu/game_boy/oam/sprite.rb', line 86

def x_flipped?
  (attributes & BIT_MASK_OBJ_X_FLIPPED) != 0
end

#x_screen_posObject



41
42
43
# File 'lib/amaterasu/game_boy/oam/sprite.rb', line 41

def x_screen_pos
  x - 8
end

#yInteger

Screen Y position + 16, this means if Y = 0 the object top pixel is offscreen at (- 16).

Returns:

  • (Integer)

    First byte of the Sprite in OAM.



26
27
28
# File 'lib/amaterasu/game_boy/oam/sprite.rb', line 26

def y
  @oam_data[@base_offset]
end

#y_flipped?Boolean

Returns Whether or not the Sprite is flipped horizontally.

Returns:

  • (Boolean)

    Whether or not the Sprite is flipped horizontally.



81
82
83
# File 'lib/amaterasu/game_boy/oam/sprite.rb', line 81

def y_flipped?
  (attributes & BIT_MASK_OBJ_Y_FLIPPED) != 0
end

#y_screen_posObject

Where the top-most pixel starts at (top row).



32
33
34
# File 'lib/amaterasu/game_boy/oam/sprite.rb', line 32

def y_screen_pos
  y - 16
end