DrewKestell

RPG.rb

Jan 7th, 2015
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.16 KB | None | 0 0
  1. class NPC
  2.     attr_accessor :level, :name, :health, :mana, :strength, :dexterity, :agility, :intellect, :constitution, :wisdom, :charisma, :inventory
  3.  
  4.     def initialize(level, name, health, mana, strength, dexterity, agility, intellect, constitution, wisdom, charisma)
  5.         @level = level
  6.         @name = name
  7.         @health = health
  8.         @mana = mana
  9.         @strength = strength
  10.         @dexterity = dexterity
  11.         @agility = agility
  12.         @intellect = intellect
  13.         @constitution = constitution
  14.         @wisdom = wisdom
  15.         @charisma = charisma
  16.         @inventory = Inventory.new
  17.     end
  18. end
  19.  
  20. class Player < NPC
  21.     attr_accessor :level, :name, :health, :mana, :strength, :dexterity, :agility, :intellect, :constitution, :wisdom, :charisma, :inventory, :skills
  22.  
  23.     def initialize(level, name, health, mana, strength, dexterity, agility, intellect, constitution, wisdom, charisma, skills=Skills.new)
  24.         super(level, name, health, mana, strength, dexterity, agility, intellect, constitution, wisdom, charisma)
  25.         @skills = skills
  26.     end
  27.  
  28.     def survey
  29.         # skill: attempt to survey current tile to reveal tile info
  30.     end
  31.  
  32.     def harvest_metal
  33.         # skill: attempt to harvest metal from current tile
  34.     end
  35.  
  36.     def harvest_wood
  37.         # skill: attempt to harvest wood from current tile
  38.     end
  39.  
  40.     def harvest_animals
  41.         # skill: attempt to harvest game animals from current tile
  42.     end
  43.  
  44.     def harvest_foragables
  45.         # skill: attempt to harvest foragables from current tile
  46.     end
  47.  
  48. end
  49.  
  50. class Monster < NPC
  51.     attr_accessor :level, :name, :health, :mana, :strength, :dexterity, :agility, :intellect, :constitution, :wisdom, :charisma, :inventory
  52.  
  53.     def initialize(level, name, health, mana, strength, dexterity, agility, intellect, constitution, wisdom, charisma)
  54.         super(level, name, health, mana, strength, dexterity, agility, intellect, constitution, wisdom, charisma)
  55.     end
  56. end
  57.  
  58. class Item
  59.     attr_accessor :item_level, :name, :rarity
  60.  
  61.     def initialize(item_level, name, rarity=:normal)
  62.         @item_level = item_level
  63.         @name = name
  64.         @rarity = rarity
  65.     end
  66. end
  67.  
  68. class Weapon < Item
  69.     attr_accessor :item_level, :name, :rarity, :weapon_damage, :weapon_speed
  70.  
  71.     def initialize(item_level, name, rarity=:normal, weapon_damage, weapon_speed)
  72.         super(item_level, name, rarity)
  73.         @weapon_damage = weapon_damage
  74.         @weapon_speed = weapon_speed
  75.         @bonus_health = 0
  76.         @bonus_mana = 0
  77.  
  78.         if rarity == :magical
  79.             @bonus_health = 10
  80.             @bonus_mana = 10
  81.         end
  82.     end
  83. end
  84.  
  85. class Armor < Item
  86.     attr_accessor :item_level, :name, :rarity, :armor_value
  87.  
  88.     def initialize(item_level, name, rarity=:normal, armor_value)
  89.         super(item_level, name, rarity)
  90.         @armor_value = armor_value
  91.         @bonus_health = 0
  92.         @bonus_mana = 0
  93.  
  94.         if rarity == :magical
  95.             @bonus_health = 10
  96.             @bonus_mana = 10
  97.         end
  98.  
  99.         myGame.init_weapons
  100.         myGame.init_armor
  101.     end
  102. end
  103.  
  104. class Inventory
  105.     def initialize
  106.         @inventory = {}
  107.     end
  108.  
  109.     def get_item(name)
  110.         @inventory[name]
  111.     end
  112.  
  113.     def add_item(name, item)
  114.         @inventory[name] = item
  115.     end
  116.  
  117.     def remove_item(name)
  118.         @inventory.delete(name)
  119.     end
  120.  
  121.     def to_s
  122.         @inventory.inspect
  123.     end
  124. end
  125.  
  126. class Tile
  127.     attr_accessor :type, :danger_level, :wood, :metal, :animals, :foraging
  128.  
  129.     def initialize(type)
  130.         @type = type
  131.         @danger_level = rand(100)
  132.  
  133.         if type == :prairie
  134.             @wood = 0
  135.             @metal = rand(10)
  136.             @animals = rand(30)
  137.             @foraging = rand(30)
  138.  
  139.         elsif type == :swamp
  140.             @wood = rand(20)
  141.             @metal = rand(10)
  142.             @animals = rand(15)
  143.             @foraging = rand(15)
  144.  
  145.         elsif type == :desert
  146.             @wood = 0
  147.             @metal = rand(20)
  148.             @animals = rand(5)
  149.             @foraging = rand(5)
  150.  
  151.         elsif type == :mountain
  152.             @wood = rand(10)
  153.             @metal = rand(30)
  154.             @animals = rand(10)
  155.             @foraging = rand(10)
  156.  
  157.         elsif type == :forest
  158.             @wood = rand(30)
  159.             @metal = rand(5)
  160.             @animals = rand(15)
  161.             @foraging = rand(15)
  162.  
  163.         else
  164.             @wood = rand(20)
  165.             @metal = rand(10)
  166.             @animals = rand(20)
  167.             @foraging = rand(25)
  168.         end
  169.     end
  170.  
  171.     def to_s
  172.         puts "Tile type: #{type}"
  173.         puts "Danger level: #{danger_level}"
  174.         puts "Wood available: #{wood}"
  175.         puts "Metal available: #{metal}"
  176.         puts "Animals available: #{animals}"
  177.         puts "Foraging available: #{foraging}"
  178.     end
  179. end
  180.  
  181. class GameMap
  182.     attr_accessor :game_map, :tile_set, :player_col, :player_row
  183.  
  184.     def initialize
  185.         @game_map = Array.new(5) { Array.new(5, 0) }   
  186.         @tile_set = [:Prairie, :Swamp, :Desert, :Mountain, :Forest, :Jungle]
  187.         @player_row = 3
  188.         @player_col = 3
  189.     end
  190.  
  191.     def fill_game_map
  192.         (0..4).each do |i|
  193.             (0..4).each do |j|
  194.                 @game_map[i][j] = Tile.new(@tile_set[rand(5)])
  195.             end
  196.         end
  197.     end
  198.  
  199.     def is_valid_move(direction)
  200.         if direction == "n"
  201.             if @player_row == 0
  202.                 return false
  203.             else
  204.                 return true
  205.             end
  206.  
  207.         elsif direction == "s"
  208.             if @player_row == @game_map.size
  209.                 return false
  210.             else
  211.                 return true
  212.             end
  213.  
  214.         elsif direction == "e"
  215.             if @player_col == @game_map.size
  216.                 return false
  217.             else
  218.                 return true
  219.             end
  220.  
  221.         elsif direction == "w"
  222.             if @player_col == 0
  223.                 return false
  224.             else
  225.                 return true
  226.             end
  227.  
  228.         else
  229.             # do nothing
  230.         end
  231.     end
  232. end
  233.  
  234. class Skills
  235.     attr_accessor :skills
  236.  
  237.     def initialize
  238.         @skills = {"Piercing" => 0.0, "Slashing" => 0.0, "Mace Fighting" => 0.0, "Hand-to-Hand" => 0.0, "Mining" => 0.0,
  239.          "Lumberjacking" => 0.0, "Hunting" => 0.0, "Foraging" => 0.0}
  240.     end
  241.  
  242.     # all skills have a (100.0 - current skill value)% chance of increasing each time they're used
  243.     def skill_up(skill, value)
  244.         if rand(100.0) >= value
  245.             @skills[skill] += 0.1
  246.             puts "Your #{skill} skill has increased to #{@skills[skill]}!"
  247.         else
  248.             # no increase
  249.         end
  250.     end
  251. end
  252.  
  253.  
  254. class Game
  255.     attr_accessor :player, :game_map, :weapons, :armor
  256.  
  257.     def initialize(player=Player.new(1, "Bloog", 100, 100, 10, 10, 10, 10, 10, 10, 10), game_map=GameMap.new)
  258.         @player = player
  259.         @game_map = game_map
  260.     end
  261.  
  262.     def init_weapons
  263.         @weapons = []
  264.         @weapons.push(Weapon.new(1, "Longsword", 10, 30))
  265.         @weapons.push(Weapon.new(1, "Broadsword", 12, 20))
  266.         @weapons.push(Weapon.new(1, "Dagger", 6, 60))
  267.         @weapons.push(Weapon.new(1, "Mace", 14, 15))
  268.         @weapons.push(Weapon.new(1, "Axe", 16, 12))
  269.     end
  270.  
  271.     def init_armor
  272.         @armor = []
  273.         @armor.push(Armor.new(1, "Leather Tunic", 4))
  274.         @armor.push(Armor.new(1, "Leather Leggings", 4))
  275.         @armor.push(Armor.new(1, "Leather Cap", 2))
  276.         @armor.push(Armor.new(1, "Leather Gloves", 2))
  277.     end
  278.  
  279.     def move(direction)
  280.         if direction == "n"
  281.             if game_map.is_valid_move("n") == true
  282.                 game_map.player_row -= 1
  283.             else
  284.                 puts "That is not a valid move"
  285.             end
  286.  
  287.         elsif direction == "s"
  288.             if game_map.is_valid_move("s") == true
  289.                 game_map.player_row += 1
  290.             else
  291.                 puts "That is not a valid move"
  292.             end
  293.  
  294.         elsif direction == "e"
  295.             if game_map.is_valid_move("e") == true
  296.                 game_map.player_col += 1
  297.             else
  298.                 puts "That is not a valid move"
  299.             end
  300.  
  301.         elsif direction == "w"
  302.             if game_map.is_valid_move("w") == true
  303.                 game_map.player_col -= 1
  304.             else
  305.                 puts "That is not a valid move"
  306.             end
  307.  
  308.         else
  309.             # do nothing
  310.         end
  311.     end
  312.  
  313.     def print_map
  314.         (0..4).each do |i|
  315.             (0..4).each do |j|
  316.                 if i == @game_map.player_row && j == @game_map.player_col
  317.                     print "x"
  318.                 else
  319.                     print @game_map.game_map[i][j]
  320.                 end
  321.             end
  322.             print "\n"
  323.         end
  324.     end
  325.  
  326. end
Advertisement
Add Comment
Please, Sign In to add comment