Skip to content

Book update #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .buildpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<buildpath>
<buildpathentry kind="src" path="01-hello"/>
<buildpathentry kind="src" path="02-warmup"/>
<buildpathentry kind="src" path="03-prototype"/>
<buildpathentry kind="src" path="04-prototype-optimized"/>
<buildpathentry kind="src" path="05-refactor"/>
<buildpathentry kind="src" path="06-physics"/>
<buildpathentry kind="src" path="07-damage"/>
<buildpathentry kind="src" path="08-ai"/>
<buildpathentry kind="src" path="09-polishing"/>
<buildpathentry kind="src" path="10-partitioning"/>
<buildpathentry kind="src" path="11-powerups"/>
<buildpathentry kind="src" path="12-stats"/>
<buildpathentry kind="src" path="13-advanced-ai"/>
<buildpathentry kind="con" path="org.eclipse.dltk.launching.INTERPRETER_CONTAINER"/>
</buildpath>
45 changes: 45 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Created by .ignore support plugin (hsz.mobi)
### Ruby template
*.gem
*.rbc
/.config
/coverage/
/InstalledFiles
/pkg/
/spec/reports/
/test/tmp/
/test/version_tmp/
/tmp/

## Specific to RubyMotion:
.dat*
.repl_history
build/

## Documentation cache and generated files:
/.yardoc/
/_yardoc/
/doc/
/rdoc/

## Environment normalisation:
/.bundle/
/vendor/bundle
/lib/bundler/man/

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
# .ruby-version
# .ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

# ignore idea stuff
*.iml
/.idea

# ignore editor backups
*~

17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ruby-gamedev-book-examples</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.dltk.core.scriptbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.dltk.ruby.core.nature</nature>
</natures>
</projectDescription>
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.3
2.2.3
4 changes: 2 additions & 2 deletions 01-hello/hello_sound.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def self.load_animation(window)

def self.load_sound(window)
Gosu::Sample.new(
window, media_path('explosion.mp3'))
window, media_path('explosion.ogg'))
end

def initialize(animation, sound, x, y)
Expand Down Expand Up @@ -71,7 +71,7 @@ def initialize(width=800, height=600, fullscreen=false)
@background = Gosu::Image.new(
self, BACKGROUND, false)
@music = Gosu::Song.new(
self, media_path('menu_music.mp3'))
self, media_path('menu_music.ogg'))
@music.volume = 0.5
@music.play(true)
@animation = Explosion.load_animation(self)
Expand Down
4 changes: 2 additions & 2 deletions 03-prototype/entities/bullet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(source_x, source_y, target_x, target_y)
if trajectory_length > MAX_DIST
@target_x, @target_y = point_at_distance(MAX_DIST)
end
sound.play
sound.play if sound
end

def draw
Expand Down Expand Up @@ -54,7 +54,7 @@ def fire(speed)

def sound
@@sound ||= Gosu::Sample.new(
$window, Game.media_path('fire.mp3'))
$window, Game.media_path('fire.ogg'))
end

def trajectory_length
Expand Down
4 changes: 2 additions & 2 deletions 03-prototype/entities/explosion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ def animation

def sound
@@sound ||= Gosu::Sample.new(
$window, Game.media_path('explosion.mp3'))
$window, Game.media_path('explosion.ogg'))
end

def initialize(x, y)
sound.play
sound.play if sound
@x, @y = x, y
@current_frame = 0
end
Expand Down
8 changes: 4 additions & 4 deletions 03-prototype/entities/tank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ def initialize(map)
@body_angle = 0.0
@gun_angle = 0.0
@last_shot = 0
sound.volume = 0.3
sound.volume = 0.3 if sound
end

def sound
@@sound ||= Gosu::Song.new(
$window, Game.media_path('tank_driving.mp3'))
$window, Game.media_path('tank_driving.ogg'))
end

def shoot(target_x, target_y)
Expand Down Expand Up @@ -47,9 +47,9 @@ def update(camera)
Gosu::KbW, Gosu::KbS, Gosu::KbA, Gosu::KbD)

if moving?
sound.play(true)
sound.play(true) if sound
else
sound.pause
sound.pause if sound
end

if $window.button_down?(Gosu::MsLeft)
Expand Down
14 changes: 9 additions & 5 deletions 03-prototype/states/menu_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ def initialize
end

def enter
music.play(true)
music.volume = 1
if music
music.play(true)
music.volume = 1
end
end

def leave
music.volume = 0
music.stop
if music
music.volume = 0
music.stop
end
end

def music
@@music ||= Gosu::Song.new(
$window, Game.media_path('menu_music.mp3'))
$window, Game.media_path('menu_music.ogg'))
end

def update
Expand Down
4 changes: 2 additions & 2 deletions 04-prototype-optimized/entities/bullet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(source_x, source_y, target_x, target_y)
if trajectory_length > MAX_DIST
@target_x, @target_y = point_at_distance(MAX_DIST)
end
sound.play
sound.play if sound
end

def draw
Expand Down Expand Up @@ -55,7 +55,7 @@ def fire(speed)

def sound
@@sound ||= Gosu::Sample.new(
$window, Game.media_path('fire.mp3'))
$window, Game.media_path('fire.ogg'))
end

def trajectory_length
Expand Down
4 changes: 2 additions & 2 deletions 04-prototype-optimized/entities/explosion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ def animation

def sound
@@sound ||= Gosu::Sample.new(
$window, Game.media_path('explosion.mp3'))
$window, Game.media_path('explosion.ogg'))
end

def initialize(x, y)
sound.play
sound.play if sound
@x, @y = x, y
@current_frame = 0
end
Expand Down
8 changes: 4 additions & 4 deletions 04-prototype-optimized/entities/tank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ def initialize(map)
@body_angle = 0.0
@gun_angle = 0.0
@last_shot = 0
sound.volume = 0.3
sound.volume = 0.3 if sound
end

def sound
@@sound ||= Gosu::Song.new(
$window, Game.media_path('tank_driving.mp3'))
$window, Game.media_path('tank_driving.ogg'))
end

def shoot(target_x, target_y)
Expand Down Expand Up @@ -48,9 +48,9 @@ def update(camera)
Gosu::KbW, Gosu::KbS, Gosu::KbA, Gosu::KbD)

if moving?
sound.play(true)
sound.play(true) if sound
else
sound.pause
sound.pause if sound
end

if $window.button_down?(Gosu::MsLeft)
Expand Down
14 changes: 9 additions & 5 deletions 04-prototype-optimized/states/menu_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ def initialize
end

def enter
music.play(true)
music.volume = 1
if music
music.play(true)
music.volume = 1
end
end

def leave
music.volume = 0
music.stop
if music
music.volume = 0
music.stop
end
end

def music
@@music ||= Gosu::Song.new(
$window, Game.media_path('menu_music.mp3'))
$window, Game.media_path('menu_music.ogg'))
end

def update
Expand Down
4 changes: 2 additions & 2 deletions 05-refactor/entities/components/bullet_sounds.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class BulletSounds
class << self
def play
sound.play
sound.play if sound
end

private

def sound
@@sound ||= Gosu::Sample.new(
$window, Utils.media_path('fire.mp3'))
$window, Utils.media_path('fire.ogg'))
end
end
end
4 changes: 2 additions & 2 deletions 05-refactor/entities/components/explosion_sounds.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class ExplosionSounds
class << self
def play
sound.play
sound.play if sound
end

private

def sound
@@sound ||= Gosu::Sample.new(
$window, Utils.media_path('explosion.mp3'))
$window, Utils.media_path('explosion.ogg'))
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions 05-refactor/entities/components/tank_sounds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def update
if @driving && @driving.paused?
@driving.resume
elsif @driving.nil?
@driving = driving_sound.play(1, 1, true)
@driving = driving_sound.play(1, 1, true) if driving_sound
end
else
if @driving && @driving.playing?
Expand All @@ -14,14 +14,14 @@ def update
end

def collide
crash_sound.play(1, 0.25, false)
crash_sound.play(1, 0.25, false) if crash_sound
end

private

def driving_sound
@@driving_sound ||= Gosu::Sample.new(
$window, Utils.media_path('tank_driving.mp3'))
$window, Utils.media_path('tank_driving.ogg'))
end

def crash_sound
Expand Down
4 changes: 2 additions & 2 deletions 06-physics/entities/components/bullet_sounds.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class BulletSounds
class << self
def play
sound.play
sound.play if sound
end

private

def sound
@@sound ||= Gosu::Sample.new(
$window, Utils.media_path('fire.mp3'))
$window, Utils.media_path('fire.ogg'))
end
end
end
4 changes: 2 additions & 2 deletions 06-physics/entities/components/explosion_sounds.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class ExplosionSounds
class << self
def play
sound.play
sound.play if sound
end

private

def sound
@@sound ||= Gosu::Sample.new(
$window, Utils.media_path('explosion.mp3'))
$window, Utils.media_path('explosion.ogg'))
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions 06-physics/entities/components/tank_sounds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def update
if @driving && @driving.paused?
@driving.resume
elsif @driving.nil?
@driving = driving_sound.play(0.3, 1, true)
@driving = driving_sound.play(0.3, 1, true) if driving_sound
end
else
if @driving && @driving.playing?
Expand All @@ -14,14 +14,14 @@ def update
end

def collide
crash_sound.play(0.3, 0.25, false)
crash_sound.play(0.3, 0.25, false) if crash_sound
end

private

def driving_sound
@@driving_sound ||= Gosu::Sample.new(
$window, Utils.media_path('tank_driving.mp3'))
$window, Utils.media_path('tank_driving.ogg'))
end

def crash_sound
Expand Down
Loading