-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.rb
148 lines (120 loc) · 3.73 KB
/
Main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
GAMEROOT = File.dirname(File.expand_path($0))
require 'rubygems' unless RUBY_VERSION =~ /1\.9/
$: << File.join(GAMEROOT,"lib")
ENV['PATH'] = File.join(GAMEROOT,"lib") + ";" + ENV['PATH']
require 'chingu'
include Gosu
include Chingu
require 'rest_client'
require 'crack'
require_all File.join(GAMEROOT,"models")
exit if defined?(Ocra)
class Game < Chingu::Window
attr_reader :object_factor, :font, :metalfont, :scores
def initialize(width = 800, height = 600, fullscreen = false, update_interval = 16.666666)
super
load_settings
retrofy
@object_factor = 2.5 # set new objects factor to $window.factor when they initialize, see droid.rb
$window.caption = "Berzerk! By deps & ippa. [ #gosu @ freenode IRC ] ver. 1.01"
#
# TODO: Put user / password in a .gitignored file
#
@scores = OnlineHighScoreList.load(:game_id => 2,
:user => "berzerk",
:password => "droidlove",
:limit => 10)
# Make sure we have 10 scores to begin with (if the score file was missing or similar)
if @scores[0] == nil
10.times do |i|
@scores << { :name => "BZR", :score => (i+1)*50}
end
end
$last_score = 0
$player_name = nil
@sample_queue = []
@current_samples = []
@current_word = nil
@sample_speed = 1.0
@metalfont = Chingu::Animation.new(:file => "metalfont.png", :size => [32,32])
@directions_to_xy = { :north => [0, -1], :east => [1, 0], :south => [0, 1], :west => [-1, 0] }
@font_letters = ('A'..'Z').to_a + [' ','.'] + ('0'..'9').to_a + ['!','(',')',',','"','?','*','-']
# Normal font used elsewhere
@font = Font.new($window, default_font_name, 30)
push_game_state( IntroState )
end
#
# Takes a Hash (e.g. :north => true, :east => true } and returns [x, y]
#
def directions_to_xy(directions = nil)
x, y = 0, 0
return [x,y] unless directions
directions.each do |direction, boolean|
if boolean
x += @directions_to_xy[direction][0]
y += @directions_to_xy[direction][1]
end
end
return [x,y]
end
# This method turns a string into an array with the corrensponding index values for the
# bitmap font inside @metalfont
# See PlayState#draw_hud for an example of how to use it
def string_to_index(msg)
arr = []
msg.split("").each do |l|
i = @font_letters.index(l)
if i
arr << i
else
arr << 42
end
end
arr
end
# Load saved settings, or use default ones
def load_settings
# Default
$settings = {}
$settings['sound'] = 0.3
$settings['music'] = 0.1
$settings['robot'] = 0.7
end
def update
super
close if current_scope == self
end
def speak( message, pitch = 1.0 )
@sample_speed = pitch
words = message.split(" ")
samples = []
words.each do |w|
samples << "word_#{w}.wav"
end
@sample_queue << samples
end
def update_speech
if @current_samples.empty? and @current_word == nil
return if @sample_queue.length == 0
@current_samples = @sample_queue.shift
#@sample_speed = 0.90 + rand(0.20)
end
if @current_word == nil
@current_word = Sound[@current_samples.shift].play($settings['robot'], @sample_speed)
else
unless @current_word.playing?
@current_word = nil
end
end
end
def clear_speech( everything = true )
if everything
@current_word.stop if @current_word
@current_word = nil
@current_samples = []
end
@sample_queye = []
end
end
g = Game.new( 800,600, false )
g.show