💎 CRYSTAL GEM DIFFICULTY: Adept Behavioral

The State: The Elemental Form

Allowing an object to alter its behavior when its internal state changes. The object will appear to change its class.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: State
ARCANUM SCHOOL: Transmutation // Shapeshifting
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CRYSTAL PATTERN //
class Elemental
  property state : ElementalState

  def initialize(@state : ElementalState)
  end

  def attack
    @state.attack(self)
  end

  def shift
    @state.shift(self)
  end
end

abstract class ElementalState
  abstract def attack(elemental : Elemental)
  abstract def shift(elemental : Elemental)
end

class FireForm < ElementalState
  def attack(elemental : Elemental)
    puts "The Elemental hurls a searing fireball!"
  end

  def shift(elemental : Elemental)
    puts "The fire cools, hardening into Earth."
    elemental.state = EarthForm.new
  end
end

class EarthForm < ElementalState
  def attack(elemental : Elemental)
    puts "The Elemental smashes the ground, causing a tremor."
  end

  def shift(elemental : Elemental)
    puts "The earth superheats, bursting into Fire."
    elemental.state = FireForm.new
  end
end

elemental = Elemental.new(FireForm.new)
elemental.attack
elemental.shift
elemental.attack

An entity trapped in the Elemental Form constantly shifts its underlying structure. Instead of a colossal, tangled crystal of conditional logic, the State pattern encapsulates Fire and Earth into distinct classes, altering the creature’s entire attack paradigm at runtime.

Runic Tags: #behavioral #state #crystal #elemental-form