Skip to content

Commit a824e82

Browse files
committed
make independet volume for sfx
1 parent 7e976b1 commit a824e82

File tree

7 files changed

+29
-22
lines changed

7 files changed

+29
-22
lines changed

engine/game.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const ctxSight = document.getElementById('sight').getContext('2d')
33

44
// eslint-disable-next-line no-unused-vars
5-
const [drawCollitions, noShadows] = [false, false]
5+
const [drawCollitions, noShadows, noSounds] = [true, true, true]
66
// eslint-disable-next-line no-unused-vars
77
const game = {
88
objects: {},
@@ -13,10 +13,13 @@ const game = {
1313
ticker () {
1414
if (this.stepValue === 10) this.stepValue = 0
1515
this.stepValue += 1
16-
const stepSound = ['F2', 'C2'][this.stepValue % 2]
17-
playNote(createSoundMap([stepSound], [400]))
16+
if (!noSounds) {
17+
const stepSound = ['F2', 'C2'][this.stepValue % 2]
18+
playNote(createSoundMap([stepSound], [400]))
19+
}
1820
if (this.timer) clearInterval(this.timer)
1921
Vigilance.ctx.clearRect(0, 0, 512, 512)
22+
Vigilance.ctxSight.clearRect(0, 0, 512, 512)
2023
Vigilance.instances
2124
.filter(vg => vg.props.start)
2225
.forEach(vg => {
@@ -156,14 +159,14 @@ const game = {
156159
}
157160
}
158161

159-
Vigilance.ctx.beginPath()
162+
Vigilance.ctxSight.beginPath()
160163
sombra.forEach((val, index) => {
161164
if (val === 0) return
162-
Vigilance.ctx.rect((index % 32) * 16, Math.floor(index / 32) * 16, 16, 16)
165+
Vigilance.ctxSight.rect((index % 32) * 16, Math.floor(index / 32) * 16, 16, 16)
163166
})
164-
Vigilance.ctx.fillStyle = `${this.palette[11]}66`
165-
Vigilance.ctx.fill()
166-
Vigilance.ctx.closePath()
167+
Vigilance.ctxSight.fillStyle = `${this.palette[11]}66`
168+
Vigilance.ctxSight.fill()
169+
Vigilance.ctxSight.closePath()
167170
},
168171
setSight () {
169172
if (noShadows) return

engine/objects.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class Obj {
152152
this.combination = [
153153
...new Array(Math.round(Math.random() * 5) + 5)
154154
].map(_ => Math.round(Math.random() * 5) + 1)
155+
this.combination[0] = Math.max(2, this.combination[0])
155156
}
156157
if (this.combinationStep === 0) {
157158
this.combinationStep += 1
@@ -173,21 +174,21 @@ class Obj {
173174
} else {
174175
this.combinationFailed = true
175176
this.combinationSuccess = false
176-
playNote(createSoundMap(['G4'], [60]))
177+
playNote(createSoundMap(['E4', '', 'A3'], [60, 200, 250]), 0.4)
177178
return
178179
}
179180

180181
if (this.combination[this.combinationStep] - this.combinationTicks === 0) {
181182
this.combinationStep += 1
182183
this.combinationTicks = 0
183-
if (this.combination[this.combinationStep]) playNote(createSoundMap(['A3'], [60]))
184+
if (this.combination[this.combinationStep]) playNote(createSoundMap(['A3'], [60]), 0.4)
184185
} else {
185-
playNote(createSoundMap(['A2'], [60]))
186+
playNote(createSoundMap(['A2'], [60]), 0.4)
186187
}
187188

188189
if (!this.combination[this.combinationStep]) {
189190
this.combinationSuccess = true
190-
playNote(createSoundMap(['G5', ' ', 'A5'], [50, 50, 50]))
191+
playNote(createSoundMap(['G5', ' ', 'A5'], [50, 50, 50]), 0.4)
191192
}
192193
}
193194
}

engine/sfx.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ const tableOfFreq = {
109109
B8: 7902.13
110110
}
111111
const audioCtx = new (window.AudioContext || window.webkitAudioContext || window.audioContext)()
112-
const beep = (duration, frequency, type = 'triangle') => {
112+
const beep = (duration, frequency, type = 'triangle', volume) => {
113113
const noteLength = duration / 1000
114114
const oscillator = audioCtx.createOscillator()
115115
const gainNode = audioCtx.createGain()
116116
gainNode.connect(audioCtx.destination)
117117
const attackTime = 0.01
118-
const sustainLevel = 0.1
118+
const sustainLevel = volume
119119
const releaseTime = 0.4
120120
gainNode.gain.setValueAtTime(0, 0)
121121
gainNode.gain.linearRampToValueAtTime(sustainLevel, audioCtx.currentTime + noteLength * attackTime)
@@ -129,10 +129,10 @@ const beep = (duration, frequency, type = 'triangle') => {
129129
}
130130

131131
// eslint-disable-next-line no-unused-vars
132-
const playNote = ([note, ...notes]) => {
132+
const playNote = ([note, ...notes], volume = 0.1) => {
133133
if (!note) return
134134
const [duration, frequency] = note
135-
beep(duration, frequency)
135+
beep(duration, frequency, 'triangle', volume)
136136
const timer = setTimeout(() => {
137137
clearTimeout(timer)
138138
playNote(notes)

engine/user.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ const setPalette = tileset => {
106106
}
107107

108108
const preload = level => {
109-
// Player.create(ctxPlayer1, 280, 160, true)
110-
Player.create(ctxPlayer1, -8, 0, true)
109+
Player.create(ctxPlayer1, 280, 160, true)
110+
// Player.create(ctxPlayer1, -8, 0, true)
111111
Player.create(ctxPlayer2, -8, 32)
112112
fetch(`${level}/map.json`)
113113
.then(response => response.json())
@@ -149,7 +149,7 @@ const preload = level => {
149149
)
150150
})
151151
})
152-
// game.ticker()
152+
// game.ticker() // inicia animaciones
153153
}
154154
tileset.src = `${level}/${tileSource.value}`
155155
})

engine/vigilance.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Vigilance {
1010
}
1111

1212
static ctx = document.getElementById('vigilance').getContext('2d')
13+
static ctxSight = document.getElementById('light').getContext('2d')
1314

1415
static getCamera (id) {
1516
return Vigilance.instances.find(itm => itm.id === id)
@@ -82,6 +83,7 @@ class Vigilance {
8283
if (next.x === this.position.x && next.y === this.position.y) {
8384
this.id = next.id
8485
}
86+
this.step = !this.step
8587
this.draw()
8688
}
8789

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,10 @@
117117
<body>
118118
<section class="gameHolder">
119119
<canvas id="background" width="512px" height="512px"></canvas>
120-
<canvas id="vigilance" width="512px" height="512px"></canvas>
120+
<canvas id="light" width="512px" height="512px"></canvas>
121121
<canvas id="walls" width="512px" height="512px"></canvas>
122122
<canvas id="objects" width="512px" height="512px"></canvas>
123+
<canvas id="vigilance" width="512px" height="512px"></canvas>
123124
<canvas id="player1" width="512px" height="512px"></canvas>
124125
<canvas id="player2" width="512px" height="512px"></canvas>
125126
<canvas id="overlayer" width="512px" height="512px"></canvas>

levels/0001/0001.tiled-session

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
13
2626
],
2727
"scale": 1.38974609375,
28-
"selectedLayer": 8,
28+
"selectedLayer": 9,
2929
"viewCenter": {
3030
"x": 233.13611130630312,
3131
"y": 225.22099641627435
@@ -53,8 +53,8 @@
5353
"project": "0001.tiled-project",
5454
"property.type": "bool",
5555
"recentFiles": [
56-
"map.json",
5756
"map.tmx",
57+
"map.json",
5858
"paleta.tsx"
5959
],
6060
"tileset.lastUsedFormat": "tsx",

0 commit comments

Comments
 (0)