Skip to content
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
Binary file added public/sounds/clear.wav
Binary file not shown.
Binary file added public/sounds/down.wav
Binary file not shown.
Binary file added public/sounds/drop.wav
Binary file not shown.
Binary file added public/sounds/left.wav
Binary file not shown.
Binary file added public/sounds/right.wav
Binary file not shown.
67 changes: 67 additions & 0 deletions src/tetromino-dom-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,27 @@ export class PlayingFieldDomView

THEMES = ['blue', 'orange', 'yellow']

soundContext: new AudioContext()
allSounds:
down:
url: 'sounds/down.wav'
volume: 1
drop:
url: 'sounds/drop.wav'
volume: 1
left:
url: 'sounds/left.wav'
volume: 1
right:
url: 'sounds/right.wav'
volume: 1
clear:
url: 'sounds/clear.wav'
volume: 3

constructor: (@fieldModel, @domId, options) ->
@isLocalPlayer = @fieldModel.isLocalPlayer()

@ordinal = options.ordinal
@blockHeight = 20
@blockWidth = 20
Expand Down Expand Up @@ -141,6 +161,53 @@ export class PlayingFieldDomView
else
$(@pausedSelector()).removeClass('visible')

# sound
if @isLocalPlayer
@initializeSounds()
decouple.on @fieldModel, 'afterAttachPiece', @, (caller, event) => console.log("attach")
decouple.on @fieldModel, 'onInput', @, (caller, event, arg) =>
if arg in ['down','left','right']
@playSound arg
decouple.on @fieldModel, 'clear', @, => @playSound('clear')
decouple.on @fieldModel, 'beforeDrop', @, => @playSound('drop')

initializeSounds: () -> @loadSound(key, sound) for key, sound of @allSounds
loadSound: (name, sound) =>
req = new XMLHttpRequest()
req.open('GET', sound.url, true)
req.responseType = 'arraybuffer'
req.onload = =>
Comment on lines +176 to +179
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the new fetch() API here?

@soundContext.decodeAudioData req.response,
(buff) =>
sound.buffer = buff
(err) ->
console.error('E: ', err)
req.send()


playSound: (name, options) =>
if not sound = @allSounds[name]
console.warn(name, ' entry not found in allSounds')
return

soundVolume = @allSounds[name].volume or 1
buffer = sound.buffer
if not buffer
console.warn(name, ' buffer not found in allSounds')
return
else
source = @soundContext.createBufferSource()
source.buffer = buffer
volume = @soundContext.createGain()
if options
if options.volume
volume.gain.value = soundVolume * options.volume
else
volume.gain.value = soundVolume
volume.connect @soundContext.destination
source.connect volume
source.start 0
return

leaveGame: (callback = null) =>
$(@fieldSelector()).fadeOut 'slow', =>
Expand Down
7 changes: 5 additions & 2 deletions src/tetromino-engine.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ export class PlayingField
@STATE_PAUSED = STATE_PAUSED = 1
@STATE_GAMEOVER_MESSAGE = STATE_GAMEOVER_MESSAGE = 2
@STATE_GAMEOVER_READY = STATE_GAMEOVER_READY = 3


isLocalPlayer: -> @viewType == 'local'

constructor: (game, options, @DEBUG = false) ->
@playerId = options.playerId if options.playerId?
@viewType = options.viewType
Expand Down Expand Up @@ -220,7 +222,7 @@ export class PlayingField

decouple.trigger(game, 'newPlayingFieldBeforeInit', @)

useDebugFill = @viewType == 'local' and @DEBUG
useDebugFill = @isLocalPlayer() and @DEBUG
if useDebugFill
for i in [0 ... @fieldHeight] when i > @fieldHeight - 5
for j in [0 ... @fieldWidth] when j != 0
Expand Down Expand Up @@ -270,6 +272,7 @@ export class PlayingField
when STATE_PLAYING
if event.action == 'escape' then @pause(); return true
return false unless @acceptingMoveInput()
decouple.trigger(@, 'onInput', event.action)
switch event.action
when 'left' then @moveLeft()
when 'right' then @moveRight()
Expand Down