Skip to content

Commit

Permalink
Merge: Asteronits: add sounds and decouple app::audio from mnit
Browse files Browse the repository at this point in the history
Add some sound effects to Asteronits, when opening fire, on asteroid explosion and on collisions with the ship. There is no sound for the thruster because the pause feature is currently broken on Android and inexistant on GNU/Linux.

Also update app::soounds so they no longer depend on mnit and now rely only on app.nit.

Pull-Request: #1901
Reviewed-by: Jean Privat <jean@pryen.org>
  • Loading branch information
privat committed Dec 17, 2015
2 parents d4577ce + 85597b0 commit 4eb35f0
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 14 deletions.
4 changes: 2 additions & 2 deletions contrib/asteronits/Makefile
Expand Up @@ -26,10 +26,10 @@ check: bin/asteronits

android: bin/asteronits.apk
bin/asteronits.apk: $(shell ${NITLS} -M src/asteronits.nit android) ${NITC} res/drawable-hdpi/icon.png pre-build
${NITC} src/touch_ui.nit -m android -o $@
${NITC} src/android.nit -m android -o $@

android-release: $(shell ${NITLS} -M src/asteronits.nit android) ${NITC} res/drawable-hdpi/icon.png pre-build
${NITC} src/touch_ui.nit -m android -o bin/asteronits.apk --release
${NITC} src/android.nit -m android -o bin/asteronits.apk --release

res/drawable-hdpi/icon.png: art/icon.svg
make -C ../inkscape_tools/
Expand Down
4 changes: 3 additions & 1 deletion contrib/asteronits/README.md
Expand Up @@ -10,4 +10,6 @@ This projects is organized in 3 modules, one per concern:

# Art

Artwork created by Kenney.nl under CC0
* Graphics and laser sound created by Kenney.nl under CC0.
* Remote explosion sound created by NenadSimic under CC0.
* Close explosion sound created by dklon under CC-BY 3.0.
Binary file not shown.
Binary file not shown.
Binary file added contrib/asteronits/assets/sounds/fire.mp3
Binary file not shown.
35 changes: 35 additions & 0 deletions contrib/asteronits/src/android.nit
@@ -0,0 +1,35 @@
# This file is part of NIT ( http://www.nitlanguage.org ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import ::android::platform
import ::android::vibration

import asteronits
import touch_ui

redef class Ship
redef fun hit
do
super
app.vibrator.vibrate 20
end
end

redef class Asteroid
redef fun destroy
do
super
app.vibrator.vibrate 10
end
end
25 changes: 25 additions & 0 deletions contrib/asteronits/src/asteronits.nit
Expand Up @@ -26,6 +26,7 @@ import gamnit::simple_2d
import game_logic
import spritesheet
import app::audio
redef class Spritesheet
# Largest meteors, organized by color
Expand Down Expand Up @@ -59,6 +60,11 @@ redef class App
# Current world in play
var world = new World(12, 2, display.aspect_ratio) is lazy
# Sound effects
private var fx_fire = new Sound("sounds/fire.mp3")
private var fx_explosion_ship = new Sound("sounds/explosion_ship.wav")
private var fx_explosion_asteroids = new Sound("sounds/explosion_asteroids.wav")
redef fun on_create
do
super
Expand Down Expand Up @@ -147,6 +153,12 @@ redef class Asteroid
sprite = new Sprite(tex, center)
super
end
redef fun destroy
do
super
app.fx_explosion_asteroids.play
end
end
redef class Bullet
Expand Down Expand Up @@ -184,6 +196,7 @@ redef class Ship
# Show or hide the thrust sprite
if applied_thrust > 0.0 then
thrust_sprite.alpha = 1.0
else if thrust_sprite.alpha > 0.0 then
thrust_sprite.alpha -= dt*4.0
if thrust_sprite.alpha < 0.0 then thrust_sprite.alpha = 0.0
Expand All @@ -192,6 +205,18 @@ redef class Ship
# HACK, the "enemy" ship used for the player points downwards
sprite.rotation += pi
end
redef fun fire
do
super
app.fx_fire.play
end
redef fun hit
do
super
app.fx_explosion_ship.play
end
end
redef class KeyEvent
Expand Down
4 changes: 4 additions & 0 deletions contrib/asteronits/src/game_logic.nit
Expand Up @@ -83,6 +83,7 @@ class World
if object == ship then
# The ship is invincible
# TODO health and losing
ship.hit
else
object.destroy
end
Expand Down Expand Up @@ -197,6 +198,9 @@ class Ship
world.objects.add bullet
world.bullets.add bullet
end

# Something hits the ship
fun hit do end
end

# Asteroid, the main obstacle in this game
Expand Down
3 changes: 2 additions & 1 deletion lib/android/audio.nit
Expand Up @@ -208,10 +208,11 @@ end
# Used to play sound, best suited for sounds effects in apps or games
class SoundPool

# Error gestion
# Latest error on this sound pool
var error: nullable Error = null

private var nsoundpool: NativeSoundPool is noinit

# The maximum number of simultaneous streams for this SoundPool
var max_streams = 10 is writable

Expand Down
2 changes: 1 addition & 1 deletion lib/linux/audio.nit
Expand Up @@ -18,7 +18,7 @@
module audio

import app::audio
import mnit::linux::linux_assets
import linux

# Simple audio asset
redef class Sound
Expand Down
6 changes: 6 additions & 0 deletions lib/linux/linux.nit
Expand Up @@ -20,6 +20,12 @@ module linux
import app

redef class App
# Path to the expected location of the asset folder of this program
#
# The asset folder should be located relative to the executable at `../assets/`.
# This value can be redefined to change the expected location.
var assets_dir: String = sys.program_name.dirname / "../assets/" is lazy

redef fun setup
do
super
Expand Down
9 changes: 0 additions & 9 deletions lib/mnit/linux/linux_assets.nit
Expand Up @@ -20,15 +20,6 @@ import mnit
import linux_app

redef class App
var assets_dir: String

redef fun setup
do
assets_dir = sys.program_name.dirname + "/../assets/"

super
end

redef fun try_loading_asset( id )
do
var path = "{assets_dir}/{id}"
Expand Down

0 comments on commit 4eb35f0

Please sign in to comment.