class Town

Attributes

parts_built[RW]
parts_needed[RW]
project[RW]

Public Class Methods

new(x, y, faction, map, infopane) click to toggle source
Calls superclass method Unit::new
# File lib/lib/units/town.rb, line 15
def initialize(x, y, faction, map, infopane)
  super
  dir_path = File.dirname(__FILE__)
  @image = Gosu::Image.new(dir_path + '/../../media/town.png')

  @armour_left = @armour_max = 1
  @moves_max = 0
  @cargo_max = 10

  @starting_project = Army unless @faction == 0 # used once at the game start
  @default_project = Army # used after capture
  @project = nil
  @parts_built = 0
  @parts_needed = 0

  set_function!(FUNCBUILD, @faction) unless @faction == 0
end

Public Instance Methods

build_info() click to toggle source

Tell the state of current build project

# File lib/lib/units/town.rb, line 42
def build_info
  "#{@parts_built}/#{@parts_needed}"
end
can_be_captured?() click to toggle source
# File lib/lib/units/town.rb, line 37
def can_be_captured?
  true
end
can_build?() click to toggle source
# File lib/lib/units/town.rb, line 33
def can_build?
  true
end
capture!(by_whom) click to toggle source

Process capture targeted at this town and reset build process

Calls superclass method Unit#capture!
# File lib/lib/units/town.rb, line 47
def capture!(by_whom)
  super

  # Reset build process
  # 1) remove old project so that it is not disclosed
  # 2) set default project so that there is always some set (remove old parts)
  # 3) offer change of the project to the new owner
  @function.func = FUNCNONE
  @project = nil
  set_project!(@default_project)
  set_function!(FUNCBUILD, @faction)
end
price_list() click to toggle source

Load all prices

# File lib/lib/units/town.rb, line 103
def price_list
  prices = Hash[
    [Army, Ship].collect { |ii| [ii, ii.price] } # TODO all subclasses of Unit which can_be_built?
  ]
end
set_function!(func, commanding_faction) click to toggle source

Set desired function and possibly also project

Calls superclass method Unit#set_function!
# File lib/lib/units/town.rb, line 61
def set_function!(func, commanding_faction)
  super

  if @faction != 0 and commanding_faction == @faction and func == FUNCBUILD
    # Set starting project once or ask player about next project
    if @starting_project
       set_project!(@starting_project)
       @starting_project = nil
    else
      GameState.switch!(BuildState.instance)
      BuildState.instance.unit = self
    end
  end
end
set_project!(desired_project) click to toggle source

Set desired project

# File lib/lib/units/town.rb, line 77
def set_project!(desired_project)
  unless price_list.key?(desired_project)
    abort("Town.set_project!(): Unknown project (#{desired_project})")
  end
  @parts_needed = price_list[desired_project]

  # Compare new setting with the old one
  if desired_project == @project
    puts PROMPT + to_s + ": project has already been set to #{@project.name} (#{build_info} done)"
  else
    previous_project = @project
    @project = desired_project
    lost_parts = @parts_built
    @parts_built = 0

    new_project_set_text = PROMPT + to_s + ": project set to #{@project.name} (#{build_info} done)"
    if previous_project and lost_parts > 0 # parts were lost but not due to capture
      puts new_project_set_text + ", losing #{lost_parts} " +
        "part#{ 's' unless lost_parts == 1 } of #{previous_project.name}"
    else
      puts new_project_set_text
    end
  end
end