Class: GameWindow

Inherits:
Gosu::Window
  • Object
show all
Defined in:
lib/empi.rb

Overview

Main class

Instance Method Summary collapse

Constructor Details

#initialize(width = (MAPX + 1) * TILESIZE, height = (MAPY + 2) * TILESIZE, fullscreen = false) ⇒ GameWindow

Returns a new instance of GameWindow.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/empi.rb', line 32

def initialize(width = (MAPX + 1) * TILESIZE, \
               height = (MAPY + 2) * TILESIZE, \
               fullscreen = false)
  super
  self.caption = 'Empi: Ruby Edition 0.16 dev'

  @infopane = Infopane.new
  @map = Map.new(@infopane)
  @cursor = Cursor.new(5, 5, @map, @infopane)

  new_turn
end

Instance Method Details

#button_up(key) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/empi.rb', line 45

def button_up(key)
  @button = key

  case(key)
  when Gosu::KbEscape then
    close
  when Gosu::KbPeriod then
    new_turn
  when Gosu::KbH then
    help
  when Gosu::KbJ then
    unit_to_move = 0
    @cursor.switch_freeroam
  end
end

#drawObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/empi.rb', line 104

def draw
  @button = -1 # use each button just once

  # Draw map tiles and units
  @map.draw_tiles
  @map.draw_units
# DEBUG    @map.all_units.each { |uu| puts uu.info}

  @cursor.draw
  @infopane.draw

  # @message = Gosu::Image.from_text(
  # self, "Empire", Gosu.default_font_name, 20)
  # @message.draw(10, 10, 2)
end

#helpObject



132
133
134
135
136
137
# File 'lib/empi.rb', line 132

def help
  puts "----------\n" \
       "QWEADZXC movement, h help, Enter info, j switch freeroam\n" \
       "functions: s sentry, n none\n" \
       "----------\n"
end

#needs_redraw?Boolean

Draw only after some button was released and in the start

Returns:

  • (Boolean)


100
101
102
# File 'lib/empi.rb', line 100

def needs_redraw?
  @button != -1
end

#new_turnObject

End current turn and start next one



121
122
123
124
125
126
127
128
129
130
# File 'lib/empi.rb', line 121

def new_turn
  @cursor.freeroam = true
  @cursor.switch_freeroam # so freeroam = false, with messages

  functionable_units = @map.all_units.select { |uu| uu.function != FUNCNONE}
  functionable_units.each { |uu| uu.function! }

  @map.all_units.each { |uu| uu.reset_moves}
  @infopane.next_turn
end

#updateObject

Process given button to cursor



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
# File 'lib/empi.rb', line 62

def update
  if @button == -1 then return end

  movable_units = @map.all_units.select { |uu| uu.can_move && uu.function == FUNCNONE}
  # If there are any movable units without functions select one of them with the cursor
  if @cursor.freeroam == false && movable_units.size > 0
    unit_to_move = movable_units[0]
    unit_to_move.update(@button)

    # Can it still move?
    if unit_to_move.can_move && unit_to_move.function == FUNCNONE
     # Move the cursor to it unless it is already there
      if (@cursor.x != unit_to_move.x \
          || @cursor.y != unit_to_move.y)
        @cursor.warp(unit_to_move.x, unit_to_move.y)
        @cursor.info
      end
    else
      # Was that the last currently avaiable non-function move?
      movable_units = @map.all_units.select { |uu| uu.can_move && uu.function == FUNCNONE}
      if movable_units.size == 0
        puts 'all movable units without functions moved'
        @cursor.switch_freeroam
      else
        # Move cursor to next avaiable unit
        unit_to_move = movable_units[0]
        @cursor.warp(unit_to_move.x, unit_to_move.y)
        @cursor.info
      end
    end

  else
    # Cursor in freeroam mode
    @cursor.update(@button) 
  end
end