class Demigod
Attributes
resources[R]
Public Class Methods
new(world, resources = GameData::STARTING_RESOURCES)
click to toggle source
Starts a new game - each demigod has one world to rule accepts different starting resources as a parameter
# File lib/demigodGame/Demigod.rb, line 21 def initialize (world, resources = GameData::STARTING_RESOURCES) @world = world @resources = resources end
Public Instance Methods
turn()
click to toggle source
Handles a turn, main function of the game
# File lib/demigodGame/Demigod.rb, line 27 def turn # calculates new amount of resources using the production function # world.production accepts a resources hash and returns a newly # calculated resource hash based on production @resources = @world.production(@resources) p @resources #do_event # ========= TODO ========= # # part 2 of every turn UiHandler.print_world(@world) UiHandler.print_turn_message # Asks the user for a tile to do his action on decision = UiHandler.get_decision() until (@world.valid?(decision) || decision == '') UiHandler.print_error(UiHandler::NO_TILE) UiHandler.print_turn_message decision = UiHandler.get_decision() end unless decision == '' tile = @world.get_tile decision # returns the tile at decision # checks for legality of move on tiles using tile.accepts? and tile.check_cost until tile.accepts?(decision) break if decision == '' UiHandler.print_tile_options(tile) decision = UiHandler.get_decision() if (!tile.accepts?(decision)) UiHandler.print_error(UiHandler::INVALID) elsif (!tile.check_cost(decision, @resources)) decision = nil UiHandler.print_error(UiHandler::RESOURCES) end end if decision != '' price = GameData.get_price(decision) @resources = reduce(price) @world.advance(tile, decision) end end # Clears the screen UiHandler.clear_messages() end
Private Instance Methods
do_event()
click to toggle source
calls and handles a new event # === TODO === #
# File lib/demigodGame/Demigod.rb, line 80 def do_event =begin event = Events.new(@resources[:luck]) # starts an event event.start decision = gets.chomp until event.accepts? decision # Requires valid input UiHandler.print_error(event.valid_range) decision = gets.chomp end # event.dispatch accepts player decision and affects the world # returning new number of resources @resources = event.dispatch(decision) =end end
reduce(amount)
click to toggle source
Reduces resources
# File lib/demigodGame/Demigod.rb, line 101 def reduce(amount) amount.each do |name, value| @resources[name] -= value end @resources end