Class: Swarm::Tile

Inherits:
Object
  • Object
show all
Includes:
Console::Color
Defined in:
lib/swarm/tile.rb

Constant Summary

Constants included from Console::Color

Console::Color::BLACK, Console::Color::BLUE, Console::Color::GREEN, Console::Color::GREY, Console::Color::PURPLE, Console::Color::RED, Console::Color::SILVER, Console::Color::WHITE, Console::Color::YELLOW

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map_x, map_y, location) ⇒ Tile

Initialize a Tile with the x and both y positions (Console and Map) on the Console. Since tiles are 2 characters wide, two sets coordinates are stored: the position in the console (+@location+) and the position on the map (+@x+ and @y). All tiles start off as :empty.

Parameters:

  • map_x (Integer)
  • map_y (Integer)
  • console_x (Integer)

    default x

  • console_y (Integer)

    default y

[View source]

89
90
91
92
93
94
# File 'lib/swarm/tile.rb', line 89

def initialize(map_x, map_y, location)
  @location = location
  @x = map_x
  @y = map_y
  @age = 0
end

Instance Attribute Details

#ageInteger (readonly)

Returns:

  • (Integer)

78
79
80
# File 'lib/swarm/tile.rb', line 78

def age
  @age
end

#colorInteger (readonly)

Returns:

  • (Integer)

73
74
75
# File 'lib/swarm/tile.rb', line 73

def color
  @color
end

#iconString (readonly)

Returns:

  • (String)

70
71
72
# File 'lib/swarm/tile.rb', line 70

def icon
  @icon
end

#locationObject (readonly)

Returns the value of attribute location


75
76
77
# File 'lib/swarm/tile.rb', line 75

def location
  @location
end

#typeSymbol (readonly)

Returns:

  • (Symbol)

67
68
69
# File 'lib/swarm/tile.rb', line 67

def type
  @type
end

#xInteger (readonly)

Returns the position of the tile on the x axis.

Returns:

  • (Integer)

    the position of the tile on the x axis.


61
62
63
# File 'lib/swarm/tile.rb', line 61

def x
  @x
end

#yInteger (readonly)

Returns the position of the tile on the y axis.

Returns:

  • (Integer)

    the position of the tile on the y axis.


64
65
66
# File 'lib/swarm/tile.rb', line 64

def y
  @y
end

Class Method Details

.attr_type(type, icon: String.new, color: 0) ⇒ Object

Create methods for setting and testing the type value, change status and console color of every Tile.

[View source]

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/swarm/tile.rb', line 28

def attr_type(type, icon: String.new, color: 0)
  define_method(type) do |age|
    change do
      @type  = type
      @icon  = icon
      @color = color
      @age   = age
    end

    self
  end

  define_method("#{type}!") { send type, 0  }

  define_method("#{type}?") { @type == type }
end

Instance Method Details

#===(other) ⇒ Boolean

Returns:

  • (Boolean)
[View source]

101
102
103
# File 'lib/swarm/tile.rb', line 101

def ===(other)
  type == other.type
end

#age!Integer

Returns:

  • (Integer)
[View source]

111
112
113
# File 'lib/swarm/tile.rb', line 111

def age!
  @age += 1
end

#born!Integer

Returns:

  • (Integer)
[View source]

106
107
108
# File 'lib/swarm/tile.rb', line 106

def born!
  @age = 0
end

#changeVoid

Returns:

  • (Void)
[View source]

126
127
128
129
130
131
132
133
# File 'lib/swarm/tile.rb', line 126

def change
  Catalog.delete type, self

  yield

  Catalog.store type, self
  Catalog.store :changed, self
end

#change!Object

[View source]

139
140
141
# File 'lib/swarm/tile.rb', line 139

def change!
  Catalog.store :changed, self
end

#changed?Boolean

Returns:

  • (Boolean)
[View source]

135
136
137
# File 'lib/swarm/tile.rb', line 135

def changed?
  Catalog.fetch :changed, self, false
end

#destroy!Object

[View source]

143
144
145
146
147
# File 'lib/swarm/tile.rb', line 143

def destroy!
  Catalog.increment :destroyed, type

  empty!
end

#dirttrue

set the type to :dirt, the icon to ██, the color to color: SILVER, the age to and flags the tile to be updated by the Console attr_type, dirt, ██, color: SILVER, , , ,

Parameters:

  • age (Integer)

Returns:

  • (true)
[View source]

49
# File 'lib/swarm/tile.rb', line 49

attr_type :dirt,    icon: '██'.freeze, color: SILVER

#dirt!true

set the type to :dirt, the icon to '██', the color to color: SILVER, the age to 0, and flags the tile to be updated by the Console

Parameters:

  • age (Integer)

Returns:

  • (true)
[View source]

49
# File 'lib/swarm/tile.rb', line 49

attr_type :dirt,    icon: '██'.freeze, color: SILVER

#dirt?Boolean

test the value of type

Returns:

  • (Boolean)

    true if type is dirt, false if it is not

[View source]

49
# File 'lib/swarm/tile.rb', line 49

attr_type :dirt,    icon: '██'.freeze, color: SILVER

#eggtrue

set the type to :egg, the icon to oo, the color to color: YELLOW, the age to and flags the tile to be updated by the Console attr_type, egg, oo, color: YELLOW, , , ,

Parameters:

  • age (Integer)

Returns:

  • (true)
[View source]

54
# File 'lib/swarm/tile.rb', line 54

attr_type :egg,     icon: 'oo'.freeze, color: YELLOW

#egg!true

set the type to :egg, the icon to 'oo', the color to color: YELLOW, the age to 0, and flags the tile to be updated by the Console

Parameters:

  • age (Integer)

Returns:

  • (true)
[View source]

54
# File 'lib/swarm/tile.rb', line 54

attr_type :egg,     icon: 'oo'.freeze, color: YELLOW

#egg?Boolean

test the value of type

Returns:

  • (Boolean)

    true if type is egg, false if it is not

[View source]

54
# File 'lib/swarm/tile.rb', line 54

attr_type :egg,     icon: 'oo'.freeze, color: YELLOW

#emptytrue

set the type to :empty, the icon to + +, the color to color: BLACK, the age to and flags the tile to be updated by the Console attr_type, empty, , color: BLACK, , , ,

Parameters:

  • age (Integer)

Returns:

  • (true)
[View source]

48
# File 'lib/swarm/tile.rb', line 48

attr_type :empty,   icon: '  '.freeze, color: BLACK

#empty!true

set the type to :empty, the icon to ' ', the color to color: BLACK, the age to 0, and flags the tile to be updated by the Console

Parameters:

  • age (Integer)

Returns:

  • (true)
[View source]

48
# File 'lib/swarm/tile.rb', line 48

attr_type :empty,   icon: '  '.freeze, color: BLACK

#empty?Boolean

test the value of type

Returns:

  • (Boolean)

    true if type is empty, false if it is not

[View source]

48
# File 'lib/swarm/tile.rb', line 48

attr_type :empty,   icon: '  '.freeze, color: BLACK

#enemy?Boolean

Returns:

  • (Boolean)
[View source]

116
117
118
# File 'lib/swarm/tile.rb', line 116

def enemy?
  worker? || soldier? || queen?
end

#inspectObject

[View source]

96
97
98
# File 'lib/swarm/tile.rb', line 96

def inspect
  '#<%s:0x%014x @x=%p, @y=%p, @type=%p>' % [self.class, object_id << 1, @x, @y, @type]
end

#mover?Boolean

Returns:

  • (Boolean)
[View source]

121
122
123
# File 'lib/swarm/tile.rb', line 121

def mover?
  player? || queen?
end

#playertrue

set the type to :player, the icon to ◄▶, the color to color: GREEN, the age to and flags the tile to be updated by the Console attr_type, player, ◄▶, color: GREEN, , , ,

Parameters:

  • age (Integer)

Returns:

  • (true)
[View source]

55
# File 'lib/swarm/tile.rb', line 55

attr_type :player,  icon: '◄▶'.freeze, color: GREEN

#player!true

set the type to :player, the icon to '◄▶', the color to color: GREEN, the age to 0, and flags the tile to be updated by the Console

Parameters:

  • age (Integer)

Returns:

  • (true)
[View source]

55
# File 'lib/swarm/tile.rb', line 55

attr_type :player,  icon: '◄▶'.freeze, color: GREEN

#player?Boolean

test the value of type

Returns:

  • (Boolean)

    true if type is player, false if it is not

[View source]

55
# File 'lib/swarm/tile.rb', line 55

attr_type :player,  icon: '◄▶'.freeze, color: GREEN

#queentrue

set the type to :queen, the icon to ╬╬, the color to color: PURPLE, the age to and flags the tile to be updated by the Console attr_type, queen, ╬╬, color: PURPLE, , , ,

Parameters:

  • age (Integer)

Returns:

  • (true)
[View source]

53
# File 'lib/swarm/tile.rb', line 53

attr_type :queen,   icon: '╬╬'.freeze, color: PURPLE

#queen!true

set the type to :queen, the icon to '╬╬', the color to color: PURPLE, the age to 0, and flags the tile to be updated by the Console

Parameters:

  • age (Integer)

Returns:

  • (true)
[View source]

53
# File 'lib/swarm/tile.rb', line 53

attr_type :queen,   icon: '╬╬'.freeze, color: PURPLE

#queen?Boolean

test the value of type

Returns:

  • (Boolean)

    true if type is queen, false if it is not

[View source]

53
# File 'lib/swarm/tile.rb', line 53

attr_type :queen,   icon: '╬╬'.freeze, color: PURPLE

#rocktrue

set the type to :rock, the icon to ██, the color to color: WHITE, the age to and flags the tile to be updated by the Console attr_type, rock, ██, color: WHITE, , , ,

Parameters:

  • age (Integer)

Returns:

  • (true)
[View source]

50
# File 'lib/swarm/tile.rb', line 50

attr_type :rock,    icon: '██'.freeze, color: WHITE

#rock!true

set the type to :rock, the icon to '██', the color to color: WHITE, the age to 0, and flags the tile to be updated by the Console

Parameters:

  • age (Integer)

Returns:

  • (true)
[View source]

50
# File 'lib/swarm/tile.rb', line 50

attr_type :rock,    icon: '██'.freeze, color: WHITE

#rock?Boolean

test the value of type

Returns:

  • (Boolean)

    true if type is rock, false if it is not

[View source]

50
# File 'lib/swarm/tile.rb', line 50

attr_type :rock,    icon: '██'.freeze, color: WHITE

#soldiertrue

set the type to :soldier, the icon to ╟╢, the color to color: RED, the age to and flags the tile to be updated by the Console attr_type, soldier, ╟╢, color: RED, , , ,

Parameters:

  • age (Integer)

Returns:

  • (true)
[View source]

52
# File 'lib/swarm/tile.rb', line 52

attr_type :soldier, icon: '╟╢'.freeze, color: RED

#soldier!true

set the type to :soldier, the icon to '╟╢', the color to color: RED, the age to 0, and flags the tile to be updated by the Console

Parameters:

  • age (Integer)

Returns:

  • (true)
[View source]

52
# File 'lib/swarm/tile.rb', line 52

attr_type :soldier, icon: '╟╢'.freeze, color: RED

#soldier?Boolean

test the value of type

Returns:

  • (Boolean)

    true if type is soldier, false if it is not

[View source]

52
# File 'lib/swarm/tile.rb', line 52

attr_type :soldier, icon: '╟╢'.freeze, color: RED

#workertrue

set the type to :worker, the icon to ├┤, the color to color: BLUE, the age to and flags the tile to be updated by the Console attr_type, worker, ├┤, color: BLUE, , , ,

Parameters:

  • age (Integer)

Returns:

  • (true)
[View source]

51
# File 'lib/swarm/tile.rb', line 51

attr_type :worker,  icon: '├┤'.freeze, color: BLUE

#worker!true

set the type to :worker, the icon to '├┤', the color to color: BLUE, the age to 0, and flags the tile to be updated by the Console

Parameters:

  • age (Integer)

Returns:

  • (true)
[View source]

51
# File 'lib/swarm/tile.rb', line 51

attr_type :worker,  icon: '├┤'.freeze, color: BLUE

#worker?Boolean

test the value of type

Returns:

  • (Boolean)

    true if type is worker, false if it is not

[View source]

51
# File 'lib/swarm/tile.rb', line 51

attr_type :worker,  icon: '├┤'.freeze, color: BLUE