Class: UnitFunction

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(function = FUNCNONE) ⇒ UnitFunction

Returns a new instance of UnitFunction.



6
7
8
# File 'lib/unitFunction.rb', line 6

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

Instance Attribute Details

#funcObject

Returns the value of attribute func.



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

def func
  @func
end

Instance Method Details

#func!(xx, yy, map, infopane) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/unitFunction.rb', line 10

def func!(xx, yy, map, infopane)
  ret = " didn't actually do anything (ERROR)"

  unit = map.get_unit(xx, yy)
  if unit == nil
    abort("unitFunction.func!(): Functioning unit not found at given coordinates (#{xx}-#{yy})")
  end

  case @func
  # Build given unit
  when FUNCBUILD
    if(unit.parts_built >= PARTS) # just == should be enough
      ret = " completed building of army"
      unit.parts_built = 0
      Army.new(unit.x, unit.y, unit.faction, map, infopane) # TODO allow setting what to build
    else
      ret = " built next part of army"
      unit.parts_built = unit.parts_built + 1
    end
  
  # Wake up when enemies are nearby
  when FUNCSENTRY
    units_around = map.all_units.select { |uu|
      (uu.x - xx).abs <= 1 &&
      (uu.y - yy).abs <= 1 &&
      uu.faction != unit.faction
    }
    if units_around.size > 0
      ret = " woke up"
      @func = FUNCNONE
    else
      ret = " was on sentry duty"
    end
  end

  ret
end