Class: Lich::Gemstone::Messaging

Inherits:
Object
  • Object
show all
Defined in:
documented/gemstone/creature.rb

Overview

Creature messaging and flavor text

Stores creature-specific messages with placeholder support. Messages can contain placeholders like Pronoun, direction, weapon that are replaced at runtime.

Constant Summary collapse

PLACEHOLDER_MAP =
{
  Pronoun: %w[He Her His It She],
  pronoun: %w[he her his it she],
  direction: %w[north south east west up down northeast northwest southeast southwest],
  weapon: %w[RAW:.+?]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Messaging

Returns a new instance of Messaging.



764
765
766
767
768
# File 'documented/gemstone/creature.rb', line 764

def initialize(data)
  data.each do |key, value|
    instance_variable_set("@#{key}", normalize(value))
  end
end

Instance Attribute Details

#arrivalObject

Returns the value of attribute arrival.



753
754
755
# File 'documented/gemstone/creature.rb', line 753

def arrival
  @arrival
end

#attackObject

Returns the value of attribute attack.



753
754
755
# File 'documented/gemstone/creature.rb', line 753

def attack
  @attack
end

#biteObject

Returns the value of attribute bite.



753
754
755
# File 'documented/gemstone/creature.rb', line 753

def bite
  @bite
end

#clawObject

Returns the value of attribute claw.



753
754
755
# File 'documented/gemstone/creature.rb', line 753

def claw
  @claw
end

#deathObject

Returns the value of attribute death.



753
754
755
# File 'documented/gemstone/creature.rb', line 753

def death
  @death
end

#descriptionObject

Returns the value of attribute description.



753
754
755
# File 'documented/gemstone/creature.rb', line 753

def description
  @description
end

#enrageObject

Returns the value of attribute enrage.



753
754
755
# File 'documented/gemstone/creature.rb', line 753

def enrage
  @enrage
end

#fleeObject

Returns the value of attribute flee.



753
754
755
# File 'documented/gemstone/creature.rb', line 753

def flee
  @flee
end

#frenzyObject

Returns the value of attribute frenzy.



753
754
755
# File 'documented/gemstone/creature.rb', line 753

def frenzy
  @frenzy
end

#mstrikeObject

Returns the value of attribute mstrike.



753
754
755
# File 'documented/gemstone/creature.rb', line 753

def mstrike
  @mstrike
end

#spell_prepObject

Returns the value of attribute spell_prep.



753
754
755
# File 'documented/gemstone/creature.rb', line 753

def spell_prep
  @spell_prep
end

#sympathyObject

Returns the value of attribute sympathy.



753
754
755
# File 'documented/gemstone/creature.rb', line 753

def sympathy
  @sympathy
end

Instance Method Details

#display(field, subs = {}) ⇒ Object



782
783
784
785
786
787
788
789
790
791
# File 'documented/gemstone/creature.rb', line 782

def display(field, subs = {})
  msg = send(field)
  if msg.is_a?(Array)
    msg.map { |m| m.is_a?(PlaceholderTemplate) ? m.to_display(subs) : m }.join("\n")
  elsif msg.is_a?(PlaceholderTemplate)
    msg.to_display(subs)
  else
    msg
  end
end

#match(field, str) ⇒ Object



793
794
795
796
797
798
799
800
# File 'documented/gemstone/creature.rb', line 793

def match(field, str)
  msg = send(field)
  if msg.is_a?(PlaceholderTemplate)
    msg.match(str)
  else
    msg == str ? {} : nil
  end
end

#normalize(value) ⇒ Object



770
771
772
773
774
775
776
777
778
779
780
# File 'documented/gemstone/creature.rb', line 770

def normalize(value)
  if value.is_a?(Array)
    value.map { |v| normalize(v) }
  elsif value.is_a?(String) && value.match?(/\{[a-zA-Z_]+\}/)
    phs = value.scan(/\{([a-zA-Z_]+)\}/).flatten.map(&:to_sym)
    placeholders = phs.map { |ph| [ph, PLACEHOLDER_MAP[ph] || []] }.to_h
    PlaceholderTemplate.new(value, placeholders)
  else
    value
  end
end