Class: GameWindow

Inherits:
Gosu::Window
  • Object
show all
Includes:
GameClient
Defined in:
lib/game_2d/game_window.rb

Overview

The Gosu::Window is always the “environment” of our game It also provides the pulse of our game

Constant Summary

Constants included from GameClient

GameClient::DEFAULT_KEY_SIZE, GameClient::DEFAULT_PORT, GameClient::SCREEN_HEIGHT, GameClient::SCREEN_WIDTH

Constants included from EntityConstants

EntityConstants::CELL_WIDTH_IN_PIXELS, EntityConstants::MAX_VELOCITY, EntityConstants::PIXEL_WIDTH, EntityConstants::WIDTH

Instance Attribute Summary

Attributes included from GameClient

#animation, #font, #player_id, #player_name, #top_menu

Instance Method Summary collapse

Methods included from GameClient

#_make_client_connection, #adjust_angle, #build_top_menu, #button_down, #clear_message, #display_message, #display_message!, #generate_move_from_click, #grab_specific, #handle_input, #initialize_from_hash, #main_menu, #make_base_npc_proc, #make_block_npc_proc, #make_destination_npc_proc, #make_grab_destination_proc, #make_hole_npc_proc, #make_simple_npc_proc, #make_slime_npc_proc, #make_teleporter_npc_proc, #media, #message_drawn?, #mouse_coords, #mouse_entity_location, #move_for_keypress, #move_grabbed_entity, #object_creation_menu, #object_type_menu, #object_type_submenus, #player, #rotate_left, #rotate_right, #selected_object, #send_create_npc, #send_delete_entity, #shutdown, #space, #tick, #toggle_grab, #update

Constructor Details

#initialize(opts = {}) ⇒ GameWindow

Returns a new instance of GameWindow.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/game_2d/game_window.rb', line 15

def initialize(opts = {})
  super(SCREEN_WIDTH, SCREEN_HEIGHT, false, 16)

  @background_image = Gosu::Image.new(self, media("Space.png"), true)
  @animation = Hash.new do |h, k|
    h[k] = Gosu::Image::load_tiles(
      self, k, CELL_WIDTH_IN_PIXELS, CELL_WIDTH_IN_PIXELS, false)
  end

  @cursor_anim = @animation[media("crosshair.gif")]

  @beep = Gosu::Sample.new(self, media("Beep.wav")) # not used yet

  @font = Gosu::Font.new(self, Gosu::default_font_name, 20)

  initialize_from_hash(opts)
end

Instance Method Details

#drawObject



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/game_2d/game_window.rb', line 33

def draw
  @background_image.draw(0, 0, ZOrder::Background)
  @dialog.draw if @dialog
  @message.draw if @message
  @menu.draw if @menu

  cursor_img = @cursor_anim[Gosu::milliseconds / 50 % @cursor_anim.size]
  cursor_img.draw(
    mouse_x - cursor_img.width / 2.0,
    mouse_y - cursor_img.height / 2.0,
    ZOrder::Cursor,
    1, 1, Gosu::Color::WHITE, :add)

  return unless p = player

  @camera_x, @camera_y = space.good_camera_position_for(p, SCREEN_WIDTH, SCREEN_HEIGHT)
  translate(-@camera_x, -@camera_y) do
    (space.players + space.npcs).each {|entity| entity.draw(self) }
  end

  space.entities_at_point(*mouse_coords).each_with_index do |entity, line|
    @font.draw(entity.to_s, 10, 10 * (line * 2 + 1), ZOrder::Text, 1.0, 1.0, Gosu::Color::YELLOW)
  end
end

#draw_box_at(x1, y1, x2, y2, c) ⇒ Object



58
59
60
# File 'lib/game_2d/game_window.rb', line 58

def draw_box_at(x1, y1, x2, y2, c)
  draw_quad(x1, y1, c, x2, y1, c, x2, y2, c, x1, y2, c, ZOrder::Highlight)
end