From 261ab1d48b982dcaeb167f8b351e8f57be437a86 Mon Sep 17 00:00:00 2001 From: Mashy Date: Fri, 6 Dec 2019 20:24:14 +1000 Subject: [PATCH] calculate direction server side --- client/src/animations.utils.jsx | 56 ++++++++++----------------------- client/src/events.jsx | 4 +-- core/src/game.rs | 31 ++++++++++++++++-- core/src/skill.rs | 11 ++++--- 4 files changed, 54 insertions(+), 48 deletions(-) diff --git a/client/src/animations.utils.jsx b/client/src/animations.utils.jsx index 248d3517..e0cc1bdb 100644 --- a/client/src/animations.utils.jsx +++ b/client/src/animations.utils.jsx @@ -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); diff --git a/client/src/events.jsx b/client/src/events.jsx index ba78e4d2..84753cec 100644 --- a/client/src/events.jsx +++ b/client/src/events.jsx @@ -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); diff --git a/core/src/game.rs b/core/src/game.rs index f351a70d..f07bce57 100644 --- a/core/src/game.rs +++ b/core/src/game.rs @@ -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 } diff --git a/core/src/skill.rs b/core/src/skill.rs index a8b1db8f..58313f43 100644 --- a/core/src/skill.rs +++ b/core/src/skill.rs @@ -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, } }