Rework EngineProfile

This commit is contained in:
Felix Linker
2020-11-01 02:19:59 +01:00
parent c44925dd62
commit 8a09d94dfa

View File

@@ -2,98 +2,58 @@ import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import TranslatedComponent from './TranslatedComponent'; import TranslatedComponent from './TranslatedComponent';
import LineChart from '../components/LineChart'; import LineChart from '../components/LineChart';
import * as Calc from '../shipyard/Calculations'; import { getBoostMultiplier, getSpeedMultipliers } from 'ed-forge/lib/stats/SpeedProfile';
import { ShipProps } from 'ed-forge';
const { LADEN_MASS } = ShipProps;
/** /**
* Engine profile for a given ship * Engine profile for a given ship
*/ */
export default class EngineProfile extends TranslatedComponent { export default class EngineProfile extends TranslatedComponent {
static propTypes = { static propTypes = {
code: PropTypes.string.isRequired,
ship: PropTypes.object.isRequired, ship: PropTypes.object.isRequired,
cargo: PropTypes.number.isRequired, cargo: PropTypes.number.isRequired,
fuel: PropTypes.number.isRequired, fuel: PropTypes.number.isRequired,
eng: PropTypes.number.isRequired, pips: PropTypes.number.isRequired,
boost: PropTypes.bool.isRequired, boost: PropTypes.bool.isRequired,
marker: PropTypes.string.isRequired
}; };
/**
* Constructor
* @param {Object} props React Component properties
* @param {Object} context React Component context
*/
constructor(props, context) {
super(props);
const ship = this.props.ship;
this.state = {
calcMaxSpeedFunc: this.calcMaxSpeed.bind(this, ship, this.props.eng, this.props.boost)
};
}
/**
* Update the state if our ship changes
* @param {Object} nextProps Incoming/Next properties
* @param {Object} nextContext Incoming/Next conext
* @return {boolean} Returns true if the component should be rerendered
*/
componentWillReceiveProps(nextProps, nextContext) {
if (nextProps.marker != this.props.marker) {
this.setState({ calcMaxSpeedFunc: this.calcMaxSpeed.bind(this, nextProps.ship, nextProps.eng, nextProps.boost) });
}
return true;
}
/**
* Calculate the top speed for this ship given thrusters, mass and pips to ENG
* @param {Object} ship The ship
* @param {Object} eng The number of pips to ENG
* @param {Object} boost If boost is enabled
* @param {Object} mass The mass at which to calculate the top speed
* @return {number} The maximum speed
*/
calcMaxSpeed(ship, eng, boost, mass) {
// Obtain the top speed
return Calc.calcSpeed(mass, ship.speed, ship.standard[1].m, ship.pipSpeed, eng, ship.boost / ship.speed, boost);
}
/** /**
* Render engine profile * Render engine profile
* @return {React.Component} contents * @return {React.Component} contents
*/ */
render() { render() {
const { language, onWindowResize, sizeRatio, tooltip, termtip } = this.context; const { language } = this.context;
const { formats, translate, units } = language; const { translate } = language;
const { ship, cargo, eng, fuel, boost } = this.props; const { code, ship, pips, boost } = this.props;
// Calculate bounds for our line chart // Calculate bounds for our line chart
const thrusters = ship.standard[1].m; const minMass = ship.getBaseProperty('hullmass');
const minMass = ship.calcLowestPossibleMass({ th: thrusters }); const maxMass = ship.getThrusters().get('enginemaximalmass');
const maxMass = thrusters.getMaxMass(); const baseSpeed = ship.getBaseProperty('speed');
const mass = ship.unladenMass + fuel + cargo; const baseBoost = getBoostMultiplier(ship);
const minSpeed = Calc.calcSpeed(maxMass, ship.speed, thrusters, ship.pipSpeed, 0, ship.boost / ship.speed, false); const cb = (eng, boost, mass) => {
const maxSpeed = Calc.calcSpeed(minMass, ship.speed, thrusters, ship.pipSpeed, 4, ship.boost / ship.speed, true); const mult = getSpeedMultipliers(ship, mass)[(boost ? 4 : eng) / 0.5];
// Add a mark at our current mass return baseSpeed * (boost ? baseBoost : 1) * mult;
const mark = Math.min(mass, maxMass); };
const code = `${ship.toString()}:${cargo}:${fuel}:${eng}:${boost}`;
// This graph can have a precipitous fall-off so we use lots of points to make it look a little smoother // This graph can have a precipitous fall-off so we use lots of points to make it look a little smoother
return ( return (
<LineChart <LineChart
xMin={minMass} xMin={minMass}
xMax={maxMass} xMax={maxMass}
yMin={minSpeed} yMin={cb(0, false, maxMass)}
yMax={maxSpeed} yMax={cb(4, true, minMass)}
xMark={mark} // Add a mark at our current mass
xMark={Math.min(ship.get(LADEN_MASS), maxMass)}
xLabel={translate('mass')} xLabel={translate('mass')}
xUnit={translate('T')} xUnit={translate('T')}
yLabel={translate('maximum speed')} yLabel={translate('maximum speed')}
yUnit={translate('m/s')} yUnit={translate('m/s')}
func={this.state.calcMaxSpeedFunc} func={cb.bind(this, pips.Eng.base + pips.Eng.mc, boost)}
points={1000} points={1000}
code={code} // Encode boost in code to re-render on state change
code={`${Number(boost)}:${code}`}
aspect={0.7} aspect={0.7}
/> />
); );