Class: Tile

Inherits:
Object
  • Object
show all
Defined in:
lib/terminal_hero/classes/tile.rb

Overview

Represents a terrain or entity tile on the map

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol: "?", color: :default, blocking: false, event: nil, entity: nil) ⇒ Tile

Returns a new instance of Tile.



6
7
8
9
10
11
12
# File 'lib/terminal_hero/classes/tile.rb', line 6

def initialize(symbol: "?", color: :default, blocking: false, event: nil, entity: nil)
  @symbol = symbol.colorize(color)
  @color = color
  @blocking = blocking
  @event = event
  @entity = entity
end

Instance Attribute Details

#blockingObject

Returns the value of attribute blocking.



3
4
5
# File 'lib/terminal_hero/classes/tile.rb', line 3

def blocking
  @blocking
end

#entityObject

Returns the value of attribute entity.



4
5
6
# File 'lib/terminal_hero/classes/tile.rb', line 4

def entity
  @entity
end

#eventObject

Returns the value of attribute event.



3
4
5
# File 'lib/terminal_hero/classes/tile.rb', line 3

def event
  @event
end

#symbolObject (readonly)

Returns the value of attribute symbol.



4
5
6
# File 'lib/terminal_hero/classes/tile.rb', line 4

def symbol
  @symbol
end

Instance Method Details

#exportObject

Export all values required for initialization to a hash, to be stored in a JSON save file



36
37
38
39
40
41
42
43
44
# File 'lib/terminal_hero/classes/tile.rb', line 36

def export
  return {
    symbol: @symbol,
    color: @color,
    blocking: @blocking,
    event: @event,
    entity: @entity.nil? || @entity.instance_of?(Player) ? nil : @entity.export
  }
end

#to_sObject

If the tile is unoccupied, return its display icon. Otherwise, return the occupant’s icon.



15
16
17
18
19
# File 'lib/terminal_hero/classes/tile.rb', line 15

def to_s
  return @symbol if @entity.nil?

  return @entity.avatar
end