Class: UnitFunction

Inherits:
Object
  • Object
show all
Defined in:
lib/lib/units/unitFunction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(function = FUNCNONE, unit) ⇒ UnitFunction

Returns a new instance of UnitFunction.



4
5
6
7
# File 'lib/lib/units/unitFunction.rb', line 4

def initialize(function = FUNCNONE, unit)
  @func = function
  @unit = unit
end

Instance Attribute Details

#funcObject

Returns the value of attribute func.



2
3
4
# File 'lib/lib/units/unitFunction.rb', line 2

def func
  @func
end

Instance Method Details

#func!(map, infopane) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lib/units/unitFunction.rb', line 24

def func!(map, infopane)
  case @func
  # Build given unit
  when FUNCBUILD
    unless @unit.project
      abort("unitFunction.func!(): No project set (" + @unit.to_s + ")")
    end

    @unit.parts_built += 1

    unless @unit.parts_built >= @unit.parts_needed # just == should be enough
      ret = " built next part of #{@unit.project.name} (#{@unit.build_info} done)"
    else
      ret = " completed building of #{@unit.project.name}"
      @unit.parts_built = 0
      @unit.project.new(@unit.x, @unit.y, @unit.faction, map, infopane)
    end

  # Wake when enemies are nearby
  when FUNCSENTRY
    units_around = map.all_units.select { |uu|
      (uu.x - @unit.x).abs <= 1 &&
      (uu.y - @unit.y).abs <= 1 &&
      uu.faction != @unit.faction
    }
    if units_around.size > 0
      ret = " woke up"
      @func = FUNCNONE
    else
      ret = " was on sentry duty"
    end
  end

  unless ret
    abort("unitFunction.func!(): Functionable unit didn't function (" + @unit.to_s + ")")
  end

  ret
end

#infoObject

Set function part of long info string of unit



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/lib/units/unitFunction.rb', line 10

def info
  case @func
  when FUNCNONE
    "no function"
  when FUNCBUILD
    unless @unit.project
      abort("unitFunction.info(): No project set (" + @unit.to_s + ")")
    end
    "building #{@unit.project.name} (#{@unit.build_info})"
  when FUNCSENTRY
    "sentrying"
  end
end