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
# 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
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

#unitObject

Returns the value of attribute unit.



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

def unit
  @unit
end

#xObject

Returns the value of attribute x.



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

def x
  @x
end

#yObject

Returns the value of attribute y.



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

def y
  @y
end

Instance Method Details

#drawObject



58
59
60
# File 'lib/lib/user_interface/cursor.rb', line 58

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

#infoObject

Find some info about units on the current tile



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/lib/user_interface/cursor.rb', line 146

def info
  if freeroam
    uu = @map.get_unit(@x, @y)
  else
    if !@unit
      abort("cursor.info(): Cursor is in locked mode but there is no unit it is locked to (at #{@x} - #{@y})")
    end
    uu = @unit
  end

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

    if uu.is_transporting?
      uu.cargo.each { |uu| puts '- cargo: ' + uu.info }
    end
  else
    @infopane.text = ''
    puts 'nothing here'
  end
end

#move!(xx, yy) ⇒ Object

Move by given change of coordinates



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
# File 'lib/lib/user_interface/cursor.rb', line 70

def move!(xx, yy)
  if freeroam
    @x += xx
    @y += yy
    @unit = @map.get_unit(@x, @y)
    return
  end

  # If in locked mode
  if !@unit
    abort("cursor.move!(): Cursor is in locked mode but there is no unit it is locked to (at #{@x} - #{@y})")
  end

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

  uu = @map.get_unit(@unit.x, @unit.y)
  if !uu # it got destroyed
    @unit = nil # clear the last links so that (object of) given unit can be truly destroyed
    return
  end

  warp_to!(@unit) # whether it moved or not, unless it got destroyed
end

#set_function_to_unit(func) ⇒ Object

Tries to set function <func> to currently selected unit



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/lib/user_interface/cursor.rb', line 97

def set_function_to_unit(func)
    if freeroam
      uu = @map.get_unit(@x, @y)
    else # in locked mode
      if !@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
      uu = @unit
    end

    uu.set_function!(func) unless !uu
end

#switch_freeroam!Object

Switch between being attached to unit and being able to freeroam



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/lib/user_interface/cursor.rb', line 131

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

#to_next_unit!Object

Find next unit to target with cursor



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/lib/user_interface/cursor.rb', line 111

def to_next_unit!
  unless @unit && @unit.can_move? && @unit.function == FUNCNONE # unless the current one is still movable
    movable_units = @map.all_units.select { |uu| uu.can_move? && uu.function == FUNCNONE}

    # If there are no more movable units without function, switch to freeroam mode
    if movable_units.size <= 0 # == would be enough
      puts 'all movable units without functions moved'

      switch_freeroam!
      return
    end

    @unit = movable_units[0] # newly selected one
  end

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

#update(button) ⇒ Object



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
47
48
49
50
51
52
53
54
55
56
# File 'lib/lib/user_interface/cursor.rb', line 17

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!(uu) ⇒ Object

Move to coordinates of given unit



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

def warp_to!(uu)
  @x = uu.x
  @y = uu.y
  @unit = uu
end