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.

Constant Summary

Constants included from Helpers::GFX

Helpers::GFX::CIRCLE_STEP

Instance Attribute Summary

Attributes inherited from Chingu::GameState

#game_objects, #game_state_manager, #options, #previous_game_state

Attributes included from Helpers::InputDispatcher

#input_clients

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

#add_game_object, #game_objects, #game_objects_of_class, #hide_game_object, #load_game_objects, #pause_game_object, #remove_game_object, #save_game_objects, #show_game_object, #unpause_game_object

Methods included from Helpers::GameState

#clear_game_states, #current_game_state, #game_states, #pop_game_state, #push_game_state, #switch_game_state, #transitional_game_state

Methods included from Helpers::GFX

#draw_circle, #draw_rect, #fill, #fill_gradient, #fill_rect

Constructor Details

#initialize(options = {}) ⇒ EnterName

Returns a new instance of EnterName.



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
# File 'lib/chingu/game_states/enter_name.rb', line 31

def initialize(options = {})
  super
    
  Text.create("<u>Please enter your name</u>", :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] || 10
  
  @string = []
  @texts = []
  @index = 1
  @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 ! " # % & ( ) [ ] / \\ - = * SPACE DEL ENTER ]

  @y = 140
  @x = ($window.width - 350)/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

  @selected_color = Color::RED
  @name = Text.create("", :rotaion_center => :top_center, :x => $window.width/2, :y => 60, :size => 80)
end

Instance Method Details

#actionObject



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/chingu/game_states/enter_name.rb', line 93

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

#downObject

Move cursor 1 step to the right



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

def down; move_cursor(@columns); end

#goObject



105
106
107
108
# File 'lib/chingu/game_states/enter_name.rb', line 105

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

#leftObject

Move cursor 1 step to the left



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

def left; move_cursor(-1); end

#move_cursor(amount = 1) ⇒ Object

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



82
83
84
85
86
87
88
89
90
91
# File 'lib/chingu/game_states/enter_name.rb', line 82

def move_cursor(amount = 1)
  @index += amount
  @index = 0                if @index >= @letters.size
  @index = @letters.size-1  if @index < 0
  
  @texts.each { |text| text.color = Color::WHITE }
  @texts[@index].color = Color::RED
  
  sleep(0.1)
end

#rightObject

Move cursor 1 step to the right



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

def right; move_cursor(1); end

#upObject

Move cursor 1 step to the left



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

def up; move_cursor(-@columns); end