Class: Chingu::GameStates::EnterName

Inherits:
Chingu::GameState show all
Defined in:
lib/chingu/game_states/enter_name.rb

Overview

A Chingu::GameState for showing a classic arcade “enter name” screen. Will let the user enter his alias or name with the keyboard or game_pad Intended to be minimalistic. If you wan't something flashy you probably want to do this class yourself.

Instance Attribute Summary

Attributes inherited from Chingu::GameState

#game_objects, #game_state_manager, #options, #previous_game_state

Attributes included from Helpers::InputDispatcher

#input_clients

Attributes included from Helpers::GameObject

#game_objects

Instance Method Summary (collapse)

Methods inherited from Chingu::GameState

#button_down, #button_up, #close, #close_game, #draw, #draw_trait, #filename, #setup, #setup_trait, #to_s, #to_sym, trait, #trait_options, traits, #update, #update_trait

Methods included from Helpers::ClassInheritableAccessor

included

Methods included from Helpers::InputClient

#add_inputs, #holding?, #holding_all?, #holding_any?, #input, #input=, #on_input

Methods included from Helpers::InputDispatcher

#add_input_client, #dispatch_button_down, #dispatch_button_up, #dispatch_input_for, #remove_input_client

Methods included from Helpers::GameObject

#game_objects_of_class, #load_game_objects, #save_game_objects

Methods included from Helpers::GFX

#draw_arc, #draw_circle, #draw_rect, #fill, #fill_arc, #fill_circle, #fill_gradient, #fill_rect

Constructor Details

- (EnterName) initialize(options = {})



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
63
64
65
66
67
68
# File 'lib/chingu/game_states/enter_name.rb', line 31

def initialize(options = {})
  super
  
  #@title = options[:title] || "<u>Please enter your name</u>"
  #Text.create(@title, :rotation_center => :top_center, :x => $window.width/2, :y => 10, :size => 40)

  on_input([:holding_up, :holding_w, :holding_gamepad_up], :up)
  on_input([:holding_down, :holding_s, :holding_gamepad_down], :down)
  on_input([:holding_left, :holding_a, :holding_gamepad_left], :left)
  on_input([:holding_right, :holding_d, :holding_gamepad_right], :right)
  on_input([:space, :x, :enter, :gamepad_button_1, :return], :action)
  on_input(:esc, :pop_game_state)
  
  @callback = options[:callback]
  @columns = options[:columns] || 14
  
  @string = []
  @texts = []
  @index = 0
  @letter_size = 30
  @letters = %w[ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ! " # % & $ ( ) [ ] / \\ - + = * . 1 2 3 4 5 6 7 8 9 0 SPACE DEL ENTER ]

  @y = 140
  @x = ($window.width - 600)/2
  
  @letters.each_with_index do |letter, index|
    @texts << Text.create(letter, :x => @x, :y => @y, :size => @letter_size)
    @x += @texts.last.width + 20
    
    if (index+1) % @columns == 0
      @y += @letter_size
      @x = @texts.first.x
    end
  end

  @texts[@index].color = ::Gosu::Color::RED
  @name = Text.create("", :rotaion_center => :top_center, :x => $window.width/2, :y => 60, :size => 80)
end

Instance Method Details

- (Object) action



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/chingu/game_states/enter_name.rb', line 103

def action
  case @letters[@index]
    when "DEL"      then  @string.pop
    when "SPACE"    then  @string << " "
    when "ENTER"    then  go
    else            @string << @letters[@index]
  end
  
  @name.text = @string.join
  @name.x = $window.width/2 - @name.width/2
end

- (Object) down

Move cursor 1 step to the right



80
# File 'lib/chingu/game_states/enter_name.rb', line 80

def down; move_cursor(@columns); end

- (Object) go



115
116
117
118
# File 'lib/chingu/game_states/enter_name.rb', line 115

def go
  @callback.call(@name.text)
  pop_game_state
end

- (Object) left

Move cursor 1 step to the left



71
# File 'lib/chingu/game_states/enter_name.rb', line 71

def left; move_cursor(-1); end

- (Object) move_cursor(amount = 1)

Move cursor any given value (positive or negative). Used by left() and right()



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/chingu/game_states/enter_name.rb', line 83

def move_cursor(amount = 1)
  #
  # Curser will wrap
  #
  #@index += amount
  #@index = 0                if @index >= @letters.size
  #@index = @letters.size-1  if @index < 0
  
  #
  # Cursor won't wrap
  #
  new_value = @index + amount
  @index = new_value  if new_value < @letters.size && new_value >= 0
  
  @texts.each { |text| text.color = ::Gosu::Color::WHITE }
  @texts[@index].color = ::Gosu::Color::RED
  
  sleep(0.15)
end

- (Object) right

Move cursor 1 step to the right



74
# File 'lib/chingu/game_states/enter_name.rb', line 74

def right; move_cursor(1); end

- (Object) up

Move cursor 1 step to the left



77
# File 'lib/chingu/game_states/enter_name.rb', line 77

def up; move_cursor(-@columns); end