Skip to content

Commit

Permalink
Merge pull request #20 from felipemfp/dev
Browse files Browse the repository at this point in the history
Add new option
  • Loading branch information
felipemfp authored Aug 18, 2016
2 parents d315f4b + 482fc12 commit 9d00c82
Show file tree
Hide file tree
Showing 17 changed files with 56 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/data/data.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"high_scores":[0,0,0,0,0],"last_scenario":0,"current_car":0,"config":{"sound":true,"language":"en","current_difficulty":"normal","difficulty":{"easy":{"cars_wave":[1,1],"cars_move":false,"score_factor":225},"normal":{"cars_wave":[1,2],"cars_move":false,"score_factor":150},"hard":{"cars_wave":[1,2],"cars_move":true,"score_factor":50},"very_hard":{"cars_wave":[2,2],"cars_move":true,"score_factor":25}}}}
{"high_scores":[34,18,1,0,0],"last_scenario":0,"current_car":0,"config":{"sound":true,"language":"en","current_difficulty":"normal","countdown":true,"difficulty":{"easy":{"cars_wave":[1,1],"cars_move":false,"score_factor":225},"normal":{"cars_wave":[1,2],"cars_move":false,"score_factor":150},"hard":{"cars_wave":[1,2],"cars_move":true,"score_factor":50},"very_hard":{"cars_wave":[2,2],"cars_move":true,"score_factor":25}}}}
7 changes: 6 additions & 1 deletion src/lang/en.json → src/langs/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,17 @@
"options": [
"Language",
"Sound",
"Difficulty"
"Difficulty",
"Countdown"
],
"options_sound": [
"On",
"Off"
],
"options_countdown": [
"On",
"Off"
],
"pause_options": [
"Continue",
"Restart",
Expand Down
7 changes: 6 additions & 1 deletion src/lang/pt-br.json → src/langs/pt-br.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,17 @@
"options": [
"Idioma",
"Som",
"Dificuldade"
"Dificuldade",
"Contagem Regressiva"
],
"options_sound": [
"Ligado",
"Desligado"
],
"options_countdown": [
"Ligado",
"Desligado"
],
"pause_options": [
"Continuar",
"Reiniciar",
Expand Down
42 changes: 29 additions & 13 deletions src/main.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
require 'rubygems'
require 'gosu'
require 'json'
require_relative 'state'
require_relative 'player'
require_relative 'car'
require_relative 'road'
require_relative 'lang'
require_relative 'menuState'
require_relative 'gameState'
require_relative 'scenarioState'
require_relative 'oneWayGameState'
require_relative 'twoWayGameState'
require_relative 'highScoresState'
require_relative 'garageState'
require_relative 'optionsState'
require_relative 'models/state'
require_relative 'models/player'
require_relative 'models/car'
require_relative 'models/road'
require_relative 'utils/lang'
require_relative 'states/menuState'
require_relative 'states/gameState'
require_relative 'states/scenarioState'
require_relative 'states/oneWayGameState'
require_relative 'states/twoWayGameState'
require_relative 'states/highScoresState'
require_relative 'states/garageState'
require_relative 'states/optionsState'

WIDTH = 512
HEIGHT = 512
Expand Down Expand Up @@ -54,6 +54,7 @@ def initialize
@data = JSON.parse(File.read('src/data/data.json'))
@lang = Lang.new(main: self, lang: @data['config']['language'])
@is_sound_enable = @data['config']['sound']
@is_countdown_enable = @data['config']['countdown']

@current_state = @states[@state].new(main: self)
end
Expand Down Expand Up @@ -93,6 +94,11 @@ def toggle_music(_song = Nil, _loop = false)
end
end

def toggle_countdown
@is_countdown_enable = !@is_countdown_enable
@data['config']['countdown'] = @is_countdown_enable
end

def get_sound_label
if @is_sound_enable
return @lang.options_sound[0]
Expand All @@ -101,6 +107,14 @@ def get_sound_label
end
end

def get_countdown_label
if @is_countdown_enable
return @lang.options_countdown[0]
else
return @lang.options_countdown[1]
end
end

def current_difficulty
difficulty = @data['config']['difficulty'][@data['config']['current_difficulty']]
return {
Expand All @@ -116,6 +130,8 @@ def restart

def close
@data['config']['sound'] = @is_sound_enable
@data['config']['countdown'] = @is_countdown_enable

File.open('src/data/data.json', 'w') do |f|
f.write(@data.to_json)
end
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/gameState.rb → src/states/gameState.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def initialize(options = {})
@alive = true
@paused = false

@loading = true
@loading = @main.data['config']['countdown']
@loading_index = 0
@loading_texts = @main.lang.countdown
@loading_font = Gosu::Image.from_text(
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 6 additions & 4 deletions src/optionsState.rb → src/states/optionsState.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(options = {})
@languages = @main.lang.get_all
@difficulties = @main.data['config']['difficulty'].keys
@current_language = @languages.find { |l| l[1] == @main.data['config']['language'] }
@margins = [30, 30, HEIGHT - 45, 200]
@margins = [30, 30, HEIGHT - 45, 250]
@current_option = 0
@background = Gosu::Image.new('src/media/images/options-bg.jpg', tileable: true)
@option_sample = Gosu::Sample.new('src/media/sounds/menu-option.wav')
Expand All @@ -34,6 +34,7 @@ def draw
@option_font.draw(@current_language[0], @margins[3], @margins[0], ZOrder::UI)
@option_font.draw(@options_sound, @margins[3], @margins[0] * 2, ZOrder::UI)
@option_font.draw(@options_difficulty, @margins[3], @margins[0] * 3, ZOrder::UI)
@option_font.draw(@options_countdown, @margins[3], @margins[0] * 4, ZOrder::UI)
end

def button_down(id)
Expand All @@ -43,16 +44,16 @@ def button_down(id)
@main.state = 0
when 0
@current_language = @main.lang.switch
load_options
when 1
@main.toggle_music(@song, true)
load_options
when 2
next_difficulty = @difficulties.index(@main.data['config']['current_difficulty']) + 1
next_difficulty = 0 if next_difficulty == @difficulties.size
@main.data['config']['current_difficulty'] = @difficulties[next_difficulty]
load_options
when 3
@main.toggle_countdown
end
load_options
elsif id == Gosu::KbDown || id == Gosu::GpDown
@main.play_sound(@option_sample)
@current_option += 1
Expand All @@ -70,6 +71,7 @@ def load_options
@options = @main.lang.options
@options.push(@main.lang.back) if @options[@options.size - 1] != @main.lang.back
@options_sound = @main.get_sound_label
@options_countdown = @main.get_countdown_label
@options_difficulty = @main.lang.data[@main.data['config']['current_difficulty']]
end
end
File renamed without changes.
File renamed without changes.
10 changes: 7 additions & 3 deletions src/lang.rb → src/utils/lang.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ def initialize(options = {})
end

def get_lang(lang)
@path = 'src/lang/'+lang+'.json'
@path = 'src/langs/'+lang+'.json'
if !File.file?(@path)
@path = 'src/lang/en-us.json'
@path = 'src/langs/en.json'
end
return JSON.parse(File.read(@path))
end

def get_all
path = 'src/lang/'
path = 'src/langs/'
lang_files = Dir.entries(path).select {|f| !File.directory? f}
languages = []
lang_files.each do |language|
Expand Down Expand Up @@ -75,6 +75,10 @@ def options_sound
return @data['options_sound']
end

def options_countdown
return @data['options_countdown']
end

def pause_options
return @data['pause_options']
end
Expand Down

0 comments on commit 9d00c82

Please sign in to comment.