Created
December 19, 2012 18:31
-
-
Save anonymous/4339166 to your computer and use it in GitHub Desktop.
An exploration of DCI ideas in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
class Hero | |
attr_reader :name, :provenance, :hit_points | |
def initialize(name, prov, occupation_role, init_hit_points=nil) | |
@name = name | |
@provenance = prov | |
@occupation_role = occupation_role | |
@hit_points = init_hit_points || 1; | |
@attributes = {} | |
self.extend occupation_role | |
end | |
def occupation | |
@occupation_role | |
end | |
def increment_hit_points(amount=1) | |
@hit_points += 1 | |
end | |
def decrement_hit_points(amount=1) | |
@hit_points -= 1 | |
end | |
def add_attribute(attr) | |
@attributes.merge!({ attr => true }) | |
end | |
def attributes | |
@attributes.keys | |
end | |
def attributes_string | |
@attributes.keys.join(", ") + "[#{@hit_points}] <#{occupation}>" | |
end | |
def remove_attribute(attr) | |
@attributes.delete attr | |
end | |
def damage(point_loss=1, attributes=[:big, :firey]) | |
decrement_hit_points(point_loss) | |
attributes.each{|attr| remove_attribute(attr)} | |
end | |
end | |
module PlumberRole | |
def occupation | |
'Plumber' | |
end | |
def unclog_sink; end | |
def unblock_toilet; end | |
end | |
module SuperheroRole | |
def self.extended(klass) | |
end | |
def occupation | |
'Superhero' | |
end | |
def apply_powerup(aPowerup) | |
@current_powerup = aPowerup | |
aPowerup.apply(self) | |
end | |
def remove_powerup | |
@current_powerup.strip(self) | |
@current_powerup = nil | |
end | |
end | |
class Powerup; end | |
class FireflowerPowerup < Powerup; | |
def apply(host) | |
host.increment_hit_points | |
host.add_attribute :firey | |
host.add_attribute :big | |
end | |
def strip(host) | |
host.decrement_hit_points | |
host.remove_attribute :firey | |
host.remove_attribute :big | |
end | |
end | |
class MagicmushroomPowerup < Powerup; | |
def apply(host) | |
host.increment_hit_points | |
host.add_attribute :big | |
end | |
def strip(host) | |
host.decrement_hit_points | |
host.remove_attribute :big | |
end | |
end | |
class Game | |
def initialize(name, protagonist) | |
@name = name | |
@hero = protagonist.extend SuperheroRole | |
end | |
def story | |
backstory | |
setting | |
puts "\n" * 2 | |
end | |
def backstory | |
puts <<-EOS | |
There is a man named #{@hero.name} from #{@hero.provenance}. | |
He is a #{@hero.occupation}. | |
#{ method = :unclog_sink; @hero.respond_to?(method) ? ( "Yes, he can " + method.to_s) : ( 'No, he cannot ' + method.to_s ) } | |
He has #{@hero.hit_points} hit points | |
He is #{@hero.attributes_string} | |
EOS | |
end | |
def setting | |
puts <<-EOS | |
#{@hero.name} finds himself in the world of #{@name}. | |
Where he must rescue Princess Toadstool from Bowser, King of the Koopa, and his minions. | |
EOS | |
end | |
def hero_finds_powerup(aPowerup) | |
@powerup = aPowerup | |
@hero.apply_powerup aPowerup | |
end | |
def hero_loses_powerup | |
@hero.remove_powerup | |
end | |
def hero_status | |
puts @hero.attributes_string | |
end | |
def hero_takes_damage(enemy) | |
enemy.damage(@hero) | |
end | |
end | |
class Enemy; end | |
class Goomba < Enemy; | |
def damage(hero) | |
hero.damage(1) | |
end | |
end | |
game_name = "Super Mario Brothers" | |
man = Hero.new("Mario Mario", "Brooklyn, New York", PlumberRole) | |
man.add_attribute 'swarthy' | |
puts "He starts out like a: #{ man.attributes_string } and then begins the drama that is: #{game_name}\n\n" | |
smb = Game.new(game_name, man) | |
smb.story | |
puts "He beings the game like" | |
smb.hero_status | |
puts "He encounters a fire flower" | |
smb.hero_finds_powerup( FireflowerPowerup.new ) | |
puts "He is now: #{man.attributes_string}" | |
puts "He encounters a little Goomba" | |
smb.hero_takes_damage(Goomba.new) | |
puts "He is now: #{man.attributes_string}" | |
puts "He then encounters a Magic mushroom!" | |
smb.hero_finds_powerup MagicmushroomPowerup.new | |
puts "He is now: #{man.attributes_string}" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
He starts out like a: swarthy[1] <Plumber> and then begins the drama that is: Super Mario Brothers | |
There is a man named Mario Mario from Brooklyn, New York. | |
He is a Superhero. | |
Yes, he can unclog_sink | |
He has 1 hit points | |
He is swarthy[1] <Superhero> | |
Mario Mario finds himself in the world of Super Mario Brothers. | |
Where he must rescue Princess Toadstool from Bowser, King of the Koopa, and his minions. | |
He beings the game like | |
swarthy[1] <Superhero> | |
He encounters a fire flower | |
He is now: swarthy, firey, big[2] <Superhero> | |
He encounters a little Goomba | |
He is now: swarthy[1] <Superhero> | |
He then encounters a Magic mushroom! | |
He is now: swarthy, big[2] <Superhero> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment