Class: Unit

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

Overview

All capturable game pieces

Direct Known Subclasses

Army, Ship, Town

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Unit.



8
9
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
# File 'lib/unit.rb', line 8

def initialize(x, y, faction, map, infopane)
  @x = x
  @y = y
  @faction = faction
  @map = map
  @infopane = infopane

  @name = 'unit'
  @value = 1
  @armor_max = 1
  @armor_left = @armor_max
  @moves_max = 1
  @moves_left = @moves_max
  @cargo = [] # transported units
  @cargo_max = 0
  @function = UnitFunction.new(FUNCNONE)



  # Store yourself
  coords_unit = @map.get_unit(@x, @y)
  if !coords_unit
    @map.set_unit(@x, @y, self)
  else # some town has just built you
    coords_unit.cargo.insert(-1, self) # -1 = to the end
  end
end

Instance Attribute Details

#cargoObject

Returns the value of attribute cargo.



5
6
7
# File 'lib/unit.rb', line 5

def cargo
  @cargo
end

#factionObject

Returns the value of attribute faction.



5
6
7
# File 'lib/unit.rb', line 5

def faction
  @faction
end

#functionObject

Returns the value of attribute function.



5
6
7
# File 'lib/unit.rb', line 5

def function
  @function
end

#xObject

Returns the value of attribute x.



5
6
7
# File 'lib/unit.rb', line 5

def x
  @x
end

#yObject

Returns the value of attribute y.



5
6
7
# File 'lib/unit.rb', line 5

def y
  @y
end

Instance Method Details

#can_build?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/unit.rb', line 171

def can_build?
  false
end

#can_fly?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/unit.rb', line 159

def can_fly?
  false
end

#can_moveObject



155
156
157
# File 'lib/unit.rb', line 155

def can_move
  (can_fly? || can_sail? || can_ride?) && @moves_left > 0
end

#can_ride?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/unit.rb', line 167

def can_ride?
  false
end

#can_sail?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/unit.rb', line 163

def can_sail?
  false
end

#can_transport?Boolean

Unit is able to both transport other units and currently has some space left

Returns:

  • (Boolean)


176
177
178
# File 'lib/unit.rb', line 176

def can_transport?
  @cargo_max > 0 && @cargo.size < @cargo_max
end

#capture(by_whom) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/unit.rb', line 43

def capture(by_whom)
  # add <value> to the capturing faction
  @infopane.add_score(by_whom - 1, @value)
  @faction = by_whom

  # if you are a town, start building units
  if can_build?
    set_function(FUNCBUILD)
  end
end

#check_movement(old_x, old_y) ⇒ Object

Processes move of unit and takes care of its (un)loading or attack



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/unit.rb', line 103

def check_movement(old_x, old_y)
  if @x != old_x || @y != old_y
    oldcoords_unit = @map.get_unit(old_x, old_y)
    newcoords_unit = @map.get_unit(@x, @y)

    # If there is nobody or is there friendly unit with some cargo space
    if !newcoords_unit || \
      (newcoords_unit.faction == @faction && newcoords_unit.can_transport?)

      # Leave old coordinates
      if oldcoords_unit == self 
        @map.set_unit(old_x, old_y, nil)
      else # if you have been transported
puts "oldcoords_unit.cargo = \"#{oldcoords_unit.cargo}\""
        oldcoords_unit.cargo.delete(self)
puts "oldcoords_unit.cargo = \"#{oldcoords_unit.cargo}\""
      end

      # Get to new coordinates
      if newcoords_unit == nil
        @map.set_unit(@x, @y, self)
      else # if you are going to be transported
        newcoords_unit.cargo.insert(-1, self) # -1 = to the end
      end

    else # if there already is somebody
        # Stay on your original tile
        @x = old_x
        @y = old_y

        # Don't capture allies
        if newcoords_unit.faction != @faction
          puts to_s + ' is capturing ' + newcoords_unit.to_s # TODO capture only towns, do combat otherwise
          newcoords_unit.capture(@faction)
        end      
    end
    @moves_left -= 1

    # Check if unit is on invalid type of tile
    if tile_check == false
      puts to_s + " found itself in a bad place"
      destroy
    end
  end
end

#destroyObject

Add <value> to the other faction



37
38
39
40
41
# File 'lib/unit.rb', line 37

def destroy
  @infopane.add_score(3 - @faction - 1, @value) # TODO more factions?
  # TODO is this deleted enough?
  @map.set_unit(@x, @y, nil)
end

#drawObject



150
151
152
153
# File 'lib/unit.rb', line 150

def draw
  @image.draw(@x * TILESIZE, (@y + 1) * TILESIZE, ZUNIT,
              scale_x = 1, scale_y = 1, color = COLOUR[@faction])
end

#function!Object



214
215
216
217
# File 'lib/unit.rb', line 214

def function!
  ret = @function.func!(@x, @y, @map, @infopane)
  puts to_s + ret
end

#infoObject

Set long info string: short info string, armor, moves, function, cargo



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/unit.rb', line 225

def info
  ret = to_s + ": armor #{@armor_left}/#{@armor_max}"
   
  if can_move
    ret = ret + ", moves #{@moves_left}/#{@moves_max}"
  end

  ret = ret + ", func #{@function.func}"

  if @cargo.size > 0
    ret = ret + ", transports #{@cargo.size} units"
  end

  ret
end

#is_transporting?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/unit.rb', line 180

def is_transporting?
  @cargo.size > 0
end

#reset_movesObject



241
242
243
# File 'lib/unit.rb', line 241

def reset_moves
  @moves_left = @moves_max
end

#set_function(func) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/unit.rb', line 198

def set_function(func)
  unless @faction == 0 # neutral faction doesn't need functions
    if (func == FUNCBUILD && !can_build?)
      puts to_s + ": this unit can't build other units"
    end

    if @function == func
      puts to_s + ": function already set to #{@function.func}"
    else
      @function.func = func
      puts to_s + ": function set to #{@function.func}"
    end
  end
end

#tile_checkObject



184
185
186
187
188
189
190
191
192
# File 'lib/unit.rb', line 184

def tile_check
  case @map.tile(@x, @y)
  when TILE_SEA
    return can_fly? || can_sail?
  when TILE_GROUND
    return can_fly? || can_ride?
  end
  true
end

#to_sObject

Set short info string: type, faction, coordinates



220
221
222
# File 'lib/unit.rb', line 220

def to_s
  "#{@name} (FAC#{@faction} #{@x}-#{@y})"
end

#update(key) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/unit.rb', line 54

def update(key)
  old_x = @x
  old_y = @y

  case key
  # Cardinal directions
  when Gosu::KbLeft, Gosu::KbA then
    @x -= 1 unless @x <= 0
  when Gosu::KbRight, Gosu::KbD then
    @x += 1 unless @x >= MAPX
  when Gosu::KbUp, Gosu::KbW then
    @y -= 1 unless @y <= 0
  when Gosu::KbDown, Gosu::KbX then
    @y += 1 unless @y >= MAPY

  # Intercardinal directions
  when Gosu::KbQ then
    unless @x <= 0 || @y <= 0
      @x -= 1
      @y -= 1
    end
  when Gosu::KbE then
    unless @x >= MAPX || @y <= 0
      @x += 1
      @y -= 1
    end
  when Gosu::KbZ then
    unless @x <= 0 || @y >= MAPY
      @x -= 1
      @y += 1
    end
  when Gosu::KbC then
    unless @x >= MAPX || @y >= MAPY
      @x += 1
      @y += 1
    end

  # Functions
  when Gosu::KbS then
    set_function(FUNCSENTRY)
  when Gosu::KbB then
    set_function(FUNCBUILD)
  end

  check_movement(old_x, old_y)
end