const preact = require('preact');
const { Component } = require('preact');
const { connect } = require('preact-redux');
// const anime = require('animejs').default;
const reactStringReplace = require('react-string-replace');
const throttle = require('lodash/throttle');
const shapes = require('./shapes');
const { removeTier } = require('../utils');
const addState = connect(
({ game, account, animSkill, animating, itemInfo }) =>
({ game, account, animSkill, animating, itemInfo })
);
class TargetSvg extends Component {
constructor() {
super();
this.state = { width: 3000, height: 1000 };
this.onResize = throttle(() => {
const svg = document.getElementById('targeting');
if (!svg) return setTimeout(this.onResize, 500);
const { width, height } = svg.getBoundingClientRect();
// const path = document.querySelector('#targeting path');
this.setState({ width, height });
}, 500);
}
render(props, state) {
const { game, account, animating, animSkill, itemInfo } = props;
const { width, height } = state;
if (!game) return false; // game will be null when battle ends
// resolutions happening
// just put skill name up
if (animating) {
if (!animSkill) return false;
const itemSource = itemInfo.combos.filter(c => c.item === removeTier(animSkill));
const itemSourceInfo = itemSource.length
? `${itemSource[0].components[0]} ${itemSource[0].components[1]} ${itemSource[0].components[2]}`
: false;
const itemRegEx = /(Red|Blue|Green)/;
const itemSourceDescription = reactStringReplace(itemSourceInfo, itemRegEx, match => shapes[match]());
return (
{animSkill}
{itemSourceDescription}
);
}
const playerTeam = game.players.find(t => t.id === account.id);
const otherTeam = game.players.find(t => t.id !== account.id);
const playerTeamIds = playerTeam.constructs.map(c => c.id);
const outgoing = game.stack.filter(stack => playerTeamIds.includes(stack.source_construct_id));
function getPath(cast) {
const source = playerTeam.constructs.findIndex(c => c.id === cast.source_construct_id);
const defensive = playerTeamIds.includes(cast.target_construct_id);
const target = defensive
? playerTeam.constructs.findIndex(c => c.id === cast.target_construct_id)
: otherTeam.constructs.findIndex(c => c.id === cast.target_construct_id);
const sourceY = height;
const sourceX = (source * width / 3) + width / 24;
const targetX = (target * width / 3) + width / 6
+ (defensive ? width / 64 : 0)
+ (source * width / 18);
const targetY = defensive ? height : 0;
const bendStart = height * (0.7 - 0.1 * source);
const bendEnd = height * 0.20;
if (defensive) {
const path = `
M${sourceX},${sourceY}
L${sourceX},${bendStart}
L${targetX},${bendStart}
L${targetX},${targetY}
L${targetX - (width * 0.005)},${height * 0.875}
M${targetX},${targetY}
L${targetX + (width * 0.005)},${height * 0.875}
`;
return ;
}
const path = `
M${sourceX},${sourceY}
L${sourceX},${bendStart}
L${targetX},${bendEnd}
L${targetX},${targetY}
L${targetX - (width * 0.005)},${height * 0.125}
M${targetX},${targetY}
L${targetX + (width * 0.005)},${height * 0.125}
`;
return ;
}
return (
);
}
componentDidMount() {
window.addEventListener('resize', this.onResize);
this.onResize();
setTimeout(this.onResize, 50);
// anime({
// targets: ['#targeting path'],
// strokeDashoffset: [anime.setDashoffset, 0],
// duration: 1000,
// easing: 'linear',
// loop: true,
// delay: anime.stagger(3000),
// });
}
componentWillUnmount() {
window.removeEventListener('resize', this.onResize);
}
}
module.exports = addState(TargetSvg);