Skip to content

Commit

Permalink
Fix lander explosion position and show different end game messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmorris committed Sep 13, 2023
1 parent 67fdbed commit 232ef6a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { TRANSITION_TO_SPACE } from "./helpers/constants.js";
import {
landingScoreDescription,
crashScoreDescription,
destroyedDescription,
} from "./helpers/scoring.js";

// SETUP
Expand Down Expand Up @@ -182,6 +183,8 @@ function onGameEnd(data) {
const finalScore = data.landerScore + bonusPointsManager.getTotalPoints();
const scoreDescription = data.landed
? landingScoreDescription(finalScore)
: data.struckByAsteroid
? destroyedDescription()
: crashScoreDescription(finalScore);
const scoreForDisplay = Intl.NumberFormat().format(finalScore.toFixed(1));

Expand Down
20 changes: 15 additions & 5 deletions lander/explosion.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ export const makeExplosion = (
};
};

export const makeLanderExplosion = (state, position, velocity, angle) => {
export const makeLanderExplosion = (
state,
position,
velocity,
angle,
useTerrain = true
) => {
const gradient = state.get("theme").landerGradient;

const noseCone = makeParticle(
Expand Down Expand Up @@ -66,7 +72,8 @@ export const makeLanderExplosion = (state, position, velocity, angle) => {
CTX.lineTo(LANDER_WIDTH / 2, LANDER_HEIGHT / 4 - 4);
CTX.closePath();
CTX.fill();
}
},
useTerrain
);

const chunk1 = makeParticle(
Expand All @@ -89,7 +96,8 @@ export const makeLanderExplosion = (state, position, velocity, angle) => {
CTX.lineTo(-LANDER_WIDTH / 2, LANDER_HEIGHT / 4);
CTX.closePath();
CTX.fill();
}
},
useTerrain
);

const chunk2 = makeParticle(
Expand All @@ -112,7 +120,8 @@ export const makeLanderExplosion = (state, position, velocity, angle) => {
CTX.lineTo(-LANDER_WIDTH / 2, LANDER_HEIGHT / 4);
CTX.closePath();
CTX.fill();
}
},
useTerrain
);

const randomPieces = makeExplosion(
Expand All @@ -121,7 +130,8 @@ export const makeLanderExplosion = (state, position, velocity, angle) => {
velocity,
gradient,
randomBetween(2, 20),
32
32,
useTerrain
);

const draw = (deltaTime) => {
Expand Down
18 changes: 12 additions & 6 deletions lander/lander.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ export const makeLander = (state, onGameEnd) => {
};
resetProps();

const _setGameEndData = (landed) => {
const _isFixedPositionInSpace = () => _position.y < 0;

const _setGameEndData = (landed, struckByAsteroid = false) => {
gameEndData = {
landed,
struckByAsteroid,
speed: velocityInMPH(_velocity),
angle: Intl.NumberFormat().format(
getAngleDeltaUpright(_angle).toFixed(1)
Expand Down Expand Up @@ -155,9 +158,10 @@ export const makeLander = (state, onGameEnd) => {

_gameEndExplosion = makeLanderExplosion(
state,
_position,
_isFixedPositionInSpace() ? _displayPosition : _position,
_velocity,
_angle
_angle,
!_isFixedPositionInSpace()
);

_velocity = { x: 0, y: 0 };
Expand All @@ -170,14 +174,16 @@ export const makeLander = (state, onGameEnd) => {
if (!gameEndData) {
const averageXVelocity = (_velocity.x + asteroidVelocity.x) / 2;
const averageYVelocity = (_velocity.y + asteroidVelocity.y) / 2;
_velocity = { x: averageXVelocity, y: averageYVelocity };
_velocity = _isFixedPositionInSpace()
? { x: averageXVelocity, y: asteroidVelocity.y }
: { x: averageXVelocity, y: averageYVelocity };
_engineOn = false;
_rotatingLeft = false;
_rotatingRight = false;
audioManager.stopEngineSound();
audioManager.stopBoosterSound1();
audioManager.stopBoosterSound2();
_setGameEndData(false);
_setGameEndData(false, true);
}
};

Expand Down Expand Up @@ -459,7 +465,7 @@ export const makeLander = (state, onGameEnd) => {
_position.y < TRANSITION_TO_SPACE ? TRANSITION_TO_SPACE : _position.y;

// Zone 2 positioning
if (_position.y < 0) {
if (_isFixedPositionInSpace()) {
CTX.translate(
0,
transition(
Expand Down

0 comments on commit 232ef6a

Please sign in to comment.