Skip to content
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

Ft maps #24

Closed
wants to merge 12 commits into from
13 changes: 8 additions & 5 deletions src/audio/index.js
Expand Up @@ -2,7 +2,7 @@
Making sounds with: https://xem.github.io/miniMusic/simple.html
*/
export default {
play(data, tempo = 0.1) {
play(data, tempo = 0.1, hz = 440) {
const audioCtx = new AudioContext()
const gainNode = audioCtx.createGain()
for (let i in data) {
Expand All @@ -11,7 +11,7 @@ export default {
oscillator.connect(gainNode)
gainNode.connect(audioCtx.destination)
oscillator.start(i * tempo)
oscillator.frequency.setValueAtTime(440 * 1.06 ** (13 - data[i]), i * tempo)
oscillator.frequency.setValueAtTime(hz * 1.06 ** (13 - data[i]), i * tempo)
oscillator.type='triangle'
gainNode.gain.setValueAtTime(1, i * tempo)
gainNode.gain.setTargetAtTime(0.0001, i * tempo + 0.08, 0.005)
Expand All @@ -23,12 +23,15 @@ export default {
this.play([25])
},
win() {
this.play([25,20,15])
[440, 200].forEach(hz => this.play([25,20,15], 0.1, hz))
},
powerup() {
this.play([13,12,11,10], 0.08)
[440, 200].forEach(hz => this.play([13,12,11,10], 0.08, hz))
},
die() {
this.play([13,14,15,,16,17,18,,19,20,21], 0.2)
[440, 200].forEach(hz => this.play([6,10,12,14], 0.1, hz))
},
fail() {
[440, 200].forEach(hz => this.play([13,14,15,,16,17,18,,19,20,21], 0.2, hz))
}
}
1 change: 1 addition & 0 deletions src/constants.js
Expand Up @@ -11,6 +11,7 @@ export const EVENTS = {

export const BATTERY_LIFE = 30 * 1000 // seconds
export const GAME_LIVES = 3
export const MAX_VISION = 600

export const BATTERY_STATES = {
HIGH: 'healthy',
Expand Down
38 changes: 15 additions & 23 deletions src/engine.js
@@ -1,7 +1,7 @@
import controls from './controls'
import map from './map'
import audio from './audio'
import { EVENTS, BATTERY_STATES, GAME_LIVES } from './constants'
import { EVENTS, BATTERY_STATES, GAME_LIVES, MAX_VISION } from './constants'
import dispatcher from './utils/dispatcher'

export default class Engine {
Expand Down Expand Up @@ -73,6 +73,7 @@ export default class Engine {
if (this.lives === -1) {
dispatcher(this.canvas, EVENTS.GAME_OVER)
} else {
audio.die()
this.startLevel()
}
}
Expand Down Expand Up @@ -124,15 +125,6 @@ export default class Engine {
// Clear drawing
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height)

// Draw Shadow
const vision = 800 * player.getBatteryLife()
const shadow = shadowGenerator.getVisibilityPolygons(
player.x + player.width / 2,
player.y + player.height / 2,
vision
)
shadow.drawShadow()

// Update player from controls
if (controls.jump && (!player.jump && player.grnd)) {
player.jump = true
Expand Down Expand Up @@ -172,6 +164,15 @@ export default class Engine {
}
})

// Draw Shadow
const vision = MAX_VISION * player.getBatteryLife()
const shadow = shadowGenerator.getVisibilityPolygons(
player.x + player.width / 2,
player.y + player.height / 2,
vision
)
shadow.drawShadow()

// Apply player v to position
if (player.grnd) player.v.y = 0
player.x += player.v.x
Expand Down Expand Up @@ -202,19 +203,9 @@ export default class Engine {
this.game.monsters.forEach(enemy => {
const [direction] = enemy.collides(player)
if (direction) dispatcher(this.canvas, EVENTS.LEVEL_FAIL) // TODO: Ability to kill enemy?
// Test if enemy is outside of game view
const [inView] = enemy.collides({
x: 0,
y:0,
width: this.canvas.width,
height: this.canvas.height
})
// If outside of view, reset position
if (!inView) {
enemy.x = enemy.origin.x
enemy.y = enemy.origin.y
}
enemy.draw()
// If enemy has not yet come into view
if (enemy.x < 0) enemy.x = enemy.origin.x - this.game.distanceTravelled
else enemy.draw()
})

// Player reached objective
Expand Down Expand Up @@ -243,6 +234,7 @@ export default class Engine {
this.game.end.draw()
}

this.game.distanceTravelled++
requestAnimationFrame(this.loop.bind(this))
}

Expand Down
4 changes: 2 additions & 2 deletions src/gameObjects/GameObject.js
Expand Up @@ -33,8 +33,8 @@ export default class GameObject {
)
}

draw(render = true) {
this.x -= this.speed || 1
draw(render = true, move = true) {
if (move) this.x -= this.speed || 1
if (render && this.inBounds()) {
if(this.img) {
this.context.drawImage(this.img, this.x, this.y, this.width, this.height)
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -22,7 +22,7 @@ const game = {
},
gameFailSequence() {
this.canvas.removeEventListener(EVENTS.GAME_OVER, this._gameFailSequence);
audio.die()
audio.fail()
this.engine.stop();

closeCurrentScreen();
Expand Down
3 changes: 2 additions & 1 deletion src/map/index.js
Expand Up @@ -2,7 +2,7 @@ import gameObjects from '../gameObjects'
import levels from './levels'
import ShadowGenerator from '../utils/ShadowGenerator'

const map = (canvas, context, levelIndex = 1) => {
const map = (canvas, context, levelIndex = 0) => {
const currentLevel = levels[levelIndex];
// Object factories
const { player, obstacle, deadzone, batteryPack, enemy, door, goal } = gameObjects(context)
Expand Down Expand Up @@ -81,6 +81,7 @@ const map = (canvas, context, levelIndex = 1) => {
totalPowerups,
shadowGenerator,
collectedPowerups: 0,
distanceTravelled: 0,
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/map/levels/level-1.js
@@ -1,22 +1,22 @@
export default [
' # % ',
' % ',
' *** ** *** ** *** ** *** ** *** ** *** ** *** **',
' % % ',
' % # % ',
'** ****** **** ** ****** ****** ****. * ******* *****',
' # % ',
'****** ** ** *** **** *** * *** *** *****',
' # % ',
' % ',
'****** ** ** *** **** *** *** *** *** *****',
' % ',
'** *** ** ******* ******* *** * *** ** *** *****',
' ** ** % # % ',
' ** ** % # % $',
'***** * *** ******* ******* ** *** ******* ******* **',
' # % ',
' *** ** *** ** *** ** *** ** *** ** *** ** *** **',
' % % ',
'** ****** **** ** ****** ****** ****. * ******* *****',
' # % $ ',
' % ',
'****** ** ** *** **** *** * *** *** *****',
' # O ',
'** *** ** ******* ******* *** * *** ** *** *****',
' P ** % # % ',
'********************************************************',
'********* ******* ************* ************** ****',
];
13 changes: 11 additions & 2 deletions src/map/levels/level-2.js
@@ -1,11 +1,20 @@
export default [
' ',
' ',
' ',
' ',
' ',
' ',
' ',
'********************************************************',
' P O $ ',
'********************************************************',
' P % # # % * O % # % # * ',
' ** * * * * * $',
' % # % * # % # % # * * ',
'******* ******************** ****************** *****',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
Expand Down
30 changes: 20 additions & 10 deletions src/map/levels/level-3.js
@@ -1,12 +1,22 @@
export default [
' # % $',
' *** ** *** ** *** ** *** ** *** ** *** ** *** **',
' % % ',
'** ****** **** ** ****** ****** ****. * ******* *****',
' # % ',
'****** ** ** *** **** *** * *** *** *****',
' # % ',
'** *** ** ******* ******* *** * *** ** *** *****',
' P ** ** % # % ',
'***** * *** ******* ******* ** *** ******* ******* **',
' ',
' ',
' **** **** ',
' **** ',
' **** **** # % ',
'**** **** **** ',
' **** # # ',
' **** **** **** ',
' **** ',
' **** **** **** ',
'**** *** % ',
' **** **** ',
' **** # *** % % ',
' **** ',
' # **** **** ',
' **** **** % ',
'**** **** ',
'P **** ',
' **** ###% $',
Copy link
Collaborator

Choose a reason for hiding this comment

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

Needs O object.

'*********** ************',
];