Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from seiyria/refactor-game-states
refactor(game): begin refactoring into a game state and game level pa…
- Loading branch information
Showing
4 changed files
with
82 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
|
||
export default class GameMode { | ||
static loop() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
import GameMode from './_gamemode'; | ||
import config from '../config'; | ||
import gamestate from '../gamestate'; | ||
import {shakeUpdate} from '../ScreenShake.js' | ||
import * as Cars from '../Cars.js'; | ||
|
||
import _ from 'lodash'; | ||
|
||
let lastTime = 0; | ||
|
||
export default class Valet { | ||
static loop(now) { | ||
const delta = now - lastTime; | ||
lastTime = now | ||
|
||
// Only update the topmost (last) (currently "active") menu layer | ||
if (gamestate.menus.length > 0) { | ||
gamestate.menus[gamestate.menus.length - 1].update(now, delta) | ||
} | ||
|
||
shakeUpdate() | ||
|
||
//TODO: Have a gameloop function. Maybe have a seperate one for each gametype | ||
if (gamestate.playing) { | ||
// TODO: do initialization better somehow? | ||
if (!gamestate.player) { | ||
var car = _.sample(Cars); | ||
let spawn = _.sample(gamestate.level.spawnPoints); | ||
|
||
gamestate.player = new car(spawn); | ||
} | ||
gamestate.player.update(); | ||
|
||
if (p2.vec2.length(gamestate.player.chassisBody.velocity) <= 0.05) { | ||
gamestate.player.chassisBody.backWheel.setBrakeForce(2); | ||
gamestate.player.boxShape.collisionGroup = config.CAR; | ||
gamestate.cars.push(gamestate.player); | ||
var car = _.sample(Cars); | ||
let spawn = _.sample(gamestate.level.spawnPoints); | ||
gamestate.player = new car(spawn); | ||
} | ||
|
||
for (let i = gamestate.cars.length - 1; i >= 0; i--) { | ||
gamestate.cars[i].update() | ||
} | ||
} | ||
|
||
requestAnimationFrame(Valet.loop); | ||
if (gamestate.playing) config.world.step(1/60); | ||
config.renderer.render(config.stage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
export default { | ||
playing: false, | ||
player: null, | ||
menus: [], | ||
cars: [] | ||
}; |