Class: Cursor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, map, infopane) ⇒ Cursor

Returns a new instance of Cursor.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/lib/user_interface/cursor.rb', line 4

def initialize(x, y, map, infopane)
  dir_path = File.dirname(__FILE__)

  @x = x
  @y = y
  @map = map
  @infopane = infopane

  @image = Gosu::Image.new(dir_path + '/../../media/cursor.png')
  @freeroam = false
  @info_stopped = false

  # Give to Infopane link to yourself (for freeroam marker)
  if !@infopane
    abort("Cursor.initialize!(): Infopane is not set")
  end
  @infopane.cursor = self
end

Instance Attribute Details

#freeroamObject

Returns the value of attribute freeroam.



2
3
4
# File 'lib/lib/user_interface/cursor.rb', line 2

def freeroam
  @freeroam
end

#info_stoppedObject

Returns the value of attribute info_stopped.



2
3
4
# File 'lib/lib/user_interface/cursor.rb', line 2

def info_stopped
  @info_stopped
end

Instance Method Details

#check_unitObject

When cursor is in locked mode there needs to be a local unit it is locked to When cursor is in freeroam mode the local unit should still be loaded



154
155
156
157
158
159
# File 'lib/lib/user_interface/cursor.rb', line 154

def check_unit
  if !@freeroam and !@local_unit
    abort("cursor.set_function_to_unit(): Cursor is in locked mode " \
          "but there is no unit it is locked to (at #{@x}-#{@y})")
  end
end

#drawObject



64
65
66
# File 'lib/lib/user_interface/cursor.rb', line 64

def draw
  @image.draw(@x * TILESIZE, (@y + 1) * TILESIZE, ZCURSOR)
end

#infoObject

Find some info about units on the current tile



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/lib/user_interface/cursor.rb', line 175

def info
  check_unit

  if @local_unit
    @infopane.text = @local_unit.info
    puts @local_unit.info

    if @local_unit.is_transporting?
      @local_unit.cargo.each { |uu| puts '- cargo: ' + uu.info }
    end
  else
    @infopane.text = ''
    puts "no unit to show info of (at #{@x}-#{@y})"
  end
end

#move!(xx, yy) ⇒ Object

Move by given change of coordinates



76
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/user_interface/cursor.rb', line 76

def move!(xx, yy)
  if freeroam
    @x += xx
    @y += yy
    @local_unit = @map.get_unit(@x, @y)
    # TODO show some basic tile info in infopane
  else
    check_unit

    # Move the unit first
    @locked_to.x += xx
    @locked_to.y += yy
    @locked_to.check_movement(@x, @y) # cursor coordinates work like old_x, old_y

    # Is the unit still alive?
    if @locked_to.armour_left > 0
      warp_to_locked! # whether it moved or not
    else
      # It got destroyed so clear last links then so that (object of)
      # given unit can be truly destroyed
      @local_unit = nil
      @locked_to = nil
    end
  end
end

#reset!Object

Reset to locked mode



119
120
121
122
123
124
125
# File 'lib/lib/user_interface/cursor.rb', line 119

def reset!
  @freeroam = true
  switch_freeroam!
  @local_unit = nil
  @locked_to = nil
  to_next_unit!
end

#set_function_to_unit(func) ⇒ Object

Tries to set function <func> to local unit



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/lib/user_interface/cursor.rb', line 103

def set_function_to_unit(func)
  check_unit

  if @local_unit
    @local_unit.set_function!(func, @infopane.faction)

    # Update infopane with the new (possibly changed) state
    # (visible only in freeroam mode as in locked one the infopane is
    # overwritten as cursor either jumps away or switches to freeroam mode)
    @infopane.text = @local_unit.info
  else
    puts "no unit to set that function to (at #{@x}-#{@y})"
  end
end

#switch_freeroam!Object

Switch between being attached to unit and being able to freeroam



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/lib/user_interface/cursor.rb', line 162

def switch_freeroam!
  if freeroam
    @infopane.text = 'freeroam disabled'
    puts 'freeroam disabled'
    @freeroam = false
  else
    @infopane.text = 'freeroam enabled'
    puts 'freeroam enabled'
    @freeroam = true
  end
end

#to_next_unit!Object

Find next unit which is still waiting for commands and lock to it (local -> last locked to -> next waiting) or switch (back) to freeroaming



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/lib/user_interface/cursor.rb', line 129

def to_next_unit!
  if @local_unit and @local_unit.is_waiting_for_commands?
    # Lock to such unit (though it may have already been locked)
    @locked_to = @local_unit
  else
    unless @locked_to and @locked_to.is_waiting_for_commands?
      waiting = @map.all_units.select { |uu| uu.is_waiting_for_commands? }

      # Are there still some units of active faction waiting for commands?
      if waiting.size <= 0 # == would be enough
        puts 'all movable units without functions moved'
        switch_freeroam!
        return
      end

      @locked_to = waiting[0] # newly selected one
    end
  end

  warp_to_locked! # stay at old or go to new
  info unless @info_stopped # due to switching out of the play game state
end

#update(button) ⇒ Object



23
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/user_interface/cursor.rb', line 23

def update(button)
  case button
  # Cardinal directions
  when Gosu::KbLeft, Gosu::KbA, Gosu::KB_NUMPAD_4 then
    move!(-1, 0) unless @x <= 0
  when Gosu::KbRight, Gosu::KbD, Gosu::KB_NUMPAD_6 then
    move!(1, 0) unless @x >= MAPX
  when Gosu::KbUp, Gosu::KbW, Gosu::KB_NUMPAD_8 then
    move!(0, -1) unless @y <= 0
  when Gosu::KbDown, Gosu::KbX, Gosu::KB_NUMPAD_2 then
    move!(0, 1) unless @y >= MAPY

  # Intercardinal directions
  when Gosu::KbQ, Gosu::KB_NUMPAD_7 then
    move!(-1, -1) unless @x <= 0 || @y <= 0
  when Gosu::KbE, Gosu::KB_NUMPAD_9 then
    move!(1, -1) unless @x >= MAPX || @y <= 0
  when Gosu::KbZ, Gosu::KB_NUMPAD_1 then
    move!(-1, 1) unless @x <= 0 || @y >= MAPY
  when Gosu::KbC, Gosu::KB_NUMPAD_3 then
    move!(1, 1) unless @x >= MAPX || @y >= MAPY

  # Functions
  when Gosu::KbS then
    set_function_to_unit(FUNCSENTRY)
  when Gosu::KbB then
    set_function_to_unit(FUNCBUILD)
  when Gosu::KbN then
    set_function_to_unit(FUNCNONE)

  # The rest
  when Gosu::KbJ, Gosu::KB_NUMPAD_0 then
    switch_freeroam!
  when Gosu::KbReturn, Gosu::KB_NUMPAD_5 then
    info
  end

  # If in locked mode, stay at current/jump to next movable unit
  to_next_unit! unless @freeroam
end

#warp_to_locked!Object

Move to coordinates of unit the cursor is locked to



69
70
71
72
73
# File 'lib/lib/user_interface/cursor.rb', line 69

def warp_to_locked!()
  @x = @locked_to.x
  @y = @locked_to.y
  @local_unit = @locked_to
end