Class: Town

Inherits:
Unit show all
Defined in:
lib/lib/units/town.rb

Instance Attribute Summary collapse

Attributes inherited from Unit

#armour_left, #cargo, #cargo_max, #faction, #function, #x, #y

Instance Method Summary collapse

Methods inherited from Unit

#attack!, #can_be_built?, #can_capture?, #can_fly?, #can_move?, #can_ride?, #can_sail?, #can_transport?, #check_movement, #destroy!, #draw, #engage!, #function!, #info, #is_full?, #is_terrain_suitable?, #is_transported?, #is_transporting?, #is_waiting_for_commands?, #reset_moves!, #to_s

Constructor Details

#initialize(x, y, faction, map, infopane) ⇒ Town

Returns a new instance of Town.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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

Instance Attribute Details

#parts_builtObject

Returns the value of attribute parts_built.



13
14
15
# File 'lib/lib/units/town.rb', line 13

def parts_built
  @parts_built
end

#parts_neededObject

Returns the value of attribute parts_needed.



13
14
15
# File 'lib/lib/units/town.rb', line 13

def parts_needed
  @parts_needed
end

#projectObject

Returns the value of attribute project.



13
14
15
# File 'lib/lib/units/town.rb', line 13

def project
  @project
end

Instance Method Details

#build_infoObject

Tell the state of current build project



42
43
44
# File 'lib/lib/units/town.rb', line 42

def build_info
  "#{@parts_built}/#{@parts_needed}"
end

#can_be_captured?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/lib/units/town.rb', line 37

def can_be_captured?
  true
end

#can_build?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/lib/units/town.rb', line 33

def can_build?
  true
end

#capture!(by_whom) ⇒ Object

Process capture targeted at this town and reset build process



47
48
49
50
51
52
53
54
55
56
57
58
# 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_listObject

Load all prices



103
104
105
106
107
# 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) ⇒ Object

Set desired function and possibly also project



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object

Set desired project



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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