calculate direction server side

This commit is contained in:
Mashy 2019-12-06 20:24:14 +10:00
parent a6f654fac1
commit 261ab1d48b
4 changed files with 54 additions and 48 deletions

View File

@ -1,52 +1,30 @@
const direction = (game, account, source, target) => {
const playerTeam = game.players.find(t => t.id === account.id);
const playerTeamIds = playerTeam.constructs.map(c => c.id);
const otherTeam = game.players.find(t => t.id !== account.id);
const otherTeamIds = otherTeam.constructs.map(c => c.id);
const sourceIsPlayer = playerTeamIds.includes(source);
const targetIsPlayer = playerTeamIds.includes(target);
function getAnimSource(resolution, account) {
const { player } = resolution.cast;
const { x, y, target } = resolution.variant[1];
const animY = y && player === account.id ? -1 : y;
const sameTeam = (sourceIsPlayer && targetIsPlayer) || (!sourceIsPlayer && !targetIsPlayer);
let y = 0;
if (!sameTeam) y = targetIsPlayer ? 1 : -1;
const i = sourceIsPlayer
? playerTeamIds.findIndex(c => c === source)
: otherTeamIds.findIndex(c => c === source);
const j = targetIsPlayer
? playerTeamIds.findIndex(c => c === target)
: otherTeamIds.findIndex(c => c === target);
const x = j - i;
return { x, y };
};
function getAnimSource(resolution, game, account) {
const { source, target } = resolution.cast;
const animSource = {
return {
animation: 'sourceCast',
constructId: source,
direction: direction(game, account, source, target),
constructId: target,
direction: { x, y: animY },
};
return animSource;
}
function getAnimTarget(resolution, game, account) {
const { source, target, skill } = resolution.cast;
const player = resolution.cast.player === account;
const animTarget = {
constructId: resolution.cast.target,
player,
direction: direction(game, account, source, target),
function getAnimTarget(resolution, account) {
const { player, skill } = resolution.cast;
const { x, y, target } = resolution.variant[1];
const animY = y && player === account.id ? -1 : y;
const isPlayer = player === account.id;
return {
constructId: target,
player: isPlayer,
skill,
direction: { x, y: animY },
};
return animTarget;
}
function getFocusTargets(resolution, game, account) {
if (resolution.variant[1] === 'AoeHit') {
if (resolution.variant[1] === 'HitAoe') {
const playerTeam = game.players.find(t => t.id === account.id);
const playerTeamIds = playerTeam.constructs.map(c => c.id);
const otherTeam = game.players.find(t => t.id !== account.id);

View File

@ -94,12 +94,12 @@ function registerEvents(store) {
store.dispatch(actions.setAnimFocus(focusTargets));
if (r.variant[0] === 'Cast') {
const animSource = getAnimSource(r, game, account);
const animSource = getAnimSource(r, account);
store.dispatch(actions.setAnimSource(animSource));
store.dispatch(actions.setAnimText(null));
setTimeout(() => store.dispatch(actions.setAnimSource(null)), TIMES.SOURCE_DURATION_MS);
} else if (r.variant[0].includes('Hit')) {
const animTarget = getAnimTarget(r, game, account);
const animTarget = getAnimTarget(r, account);
store.dispatch(actions.setAnimTarget(animTarget));
store.dispatch(actions.setAnimText(null));
setTimeout(() => store.dispatch(actions.setAnimTarget(null)), TIMES.TARGET_DURATION_MS);

View File

@ -517,13 +517,40 @@ impl Game {
self
}
fn direction(&mut self, cast: Cast) -> (i8, i8) {
let i = self.players.iter()
.find(|t| t.constructs.iter().any(|c| c.id == cast.source))
.unwrap().constructs
.iter()
.position(|c| c.id == cast.source)
.unwrap() as i8;
let j = self.players.iter()
.find(|t| t.constructs.iter().any(|c| c.id == cast.target))
.unwrap().constructs
.iter()
.position(|c| c.id == cast.target)
.unwrap() as i8;
let x = j - i;
let target = self.construct_by_id(cast.target).unwrap();
// is the caster player account same as target player account side of screen
let y = match cast.player == target.account {
true => 0,
false => 1
};
(x, y)
}
fn cast(&mut self, cast: Cast) -> &mut Game {
self.event_add(vec![EventVariant::Cast()], cast);
let (x, y) = self.direction(cast);
self.event_add(vec![EventVariant::Cast { target: cast.source, x, y, player: cast.player }], cast);
self
}
fn hit(&mut self, cast: Cast) -> &mut Game {
self.event_add(vec![EventVariant::Hit()], cast);
let (x, y) = self.direction(cast);
self.event_add(vec![EventVariant::Hit { target: cast.target, x, y, player: cast.player }], cast);
self
}

View File

@ -237,9 +237,9 @@ pub struct Event {
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
pub enum EventVariant {
Cast (),
Hit (),
HitAoe (),
Cast { target: Uuid, player: Uuid, x: i8, y: i8 },
Hit { target: Uuid, player: Uuid, x: i8, y: i8 },
HitAoe { target: Uuid },
Damage { target: Uuid, amount: usize, mitigation: usize, colour: Colour, display: EventConstruct },
Effect { target: Uuid, effect: Effect, duration: u8, display: EventConstruct },
@ -265,8 +265,9 @@ impl EventVariant {
let combat_text_overlap = 600; // overlap between animation and combat text
match self {
EventVariant::Hit() => target_duration - combat_text_overlap,
EventVariant::Cast() => target_delay,
EventVariant::Cast { target: _, x: _, y: _, player: _ } => target_delay,
EventVariant::Hit { target: _, x: _, y: _, player: _ } |
EventVariant::HitAoe { target: _ } => target_duration - combat_text_overlap,
_ => combat_text_delay,
}
}