diff --git a/src/FieldWarning/.vsconfig b/src/FieldWarning/.vsconfig new file mode 100644 index 00000000..1586a483 --- /dev/null +++ b/src/FieldWarning/.vsconfig @@ -0,0 +1,6 @@ +{ + "version": "1.0", + "components": [ + "Microsoft.VisualStudio.Workload.ManagedGame" + ] +} diff --git a/src/FieldWarning/Assets/Units/Component/Movement/MovementComponent.cs b/src/FieldWarning/Assets/Units/Component/Movement/MovementComponent.cs index fc86024c..309b0266 100644 --- a/src/FieldWarning/Assets/Units/Component/Movement/MovementComponent.cs +++ b/src/FieldWarning/Assets/Units/Component/Movement/MovementComponent.cs @@ -24,6 +24,7 @@ public sealed class MovementComponent : MonoBehaviour private const float ORIENTATION_RATE = 8.0f; private const float TRANSLATION_RATE = 6.0f; + public Vector3 Velocity; //{ get; private set; } public DataComponent Data { get; private set; } public Pathfinder Pathfinder { get; private set; } @@ -74,14 +75,22 @@ private void Update() UpdateCurrentPosition(); } - private void UpdateCurrentPosition() + private void UpdateCurrentPosition() { + //update position and velocity param of this unit + Vector3 diff = (_moveStrategy.NextPosition - transform.position) * Time.deltaTime; Vector3 newPosition = transform.position; newPosition.x += TRANSLATION_RATE * diff.x; newPosition.y = _moveStrategy.NextPosition.y; newPosition.z += TRANSLATION_RATE * diff.z; + //calculate instantaneous velocity property to be read from outside for shell-lead + Velocity.x = TRANSLATION_RATE * diff.x; + Velocity.y = newPosition.y - transform.position.y; + Velocity.z = TRANSLATION_RATE * diff.z; + Velocity = Velocity / Time.deltaTime; + transform.position = newPosition; } diff --git a/src/FieldWarning/Assets/Units/Component/Weapon/Cannon.cs b/src/FieldWarning/Assets/Units/Component/Weapon/Cannon.cs index 06c96da5..f7fa666c 100644 --- a/src/FieldWarning/Assets/Units/Component/Weapon/Cannon.cs +++ b/src/FieldWarning/Assets/Units/Component/Weapon/Cannon.cs @@ -184,23 +184,11 @@ private bool FireWeapon( } ShellBehaviour shellBehaviour = shell.GetComponent(); - shellBehaviour.Initialize(shellDestination, ammo); - - if (isServer) - { - if (target.IsUnit) - { - if (isHit && !ammo.IsAoe) - { - target.Enemy.HandleHit( - ammo.DamageType, ammo.DamageValue, displacement, distance); - } - } - else - { - // HE damage is applied by the shellBehavior when it explodes - } - } + shellBehaviour.Initialize( + shellDestination, + ammo, + target.Enemy.gameObject.GetComponent().Velocity + ); return true; } diff --git a/src/FieldWarning/Assets/Units/Prefab/Resources/shell.prefab b/src/FieldWarning/Assets/Units/Prefab/Resources/shell.prefab index 496df3c7..9fbda24a 100644 --- a/src/FieldWarning/Assets/Units/Prefab/Resources/shell.prefab +++ b/src/FieldWarning/Assets/Units/Prefab/Resources/shell.prefab @@ -10,6 +10,7 @@ GameObject: m_Component: - component: {fileID: 4947632754763960} - component: {fileID: 136052954316136422} + - component: {fileID: 9121148859323275930} - component: {fileID: 114877143042695842} - component: {fileID: 54854243726031328} m_Layer: 0 @@ -35,6 +36,20 @@ Transform: m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!136 &136052954316136422 +CapsuleCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1549430693258728} + m_Material: {fileID: 0} + m_IsTrigger: 1 + m_Enabled: 1 + m_Radius: 0.05 + m_Height: 1 + m_Direction: 2 + m_Center: {x: 0, y: 0, z: 0.8} +--- !u!136 &9121148859323275930 CapsuleCollider: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} @@ -44,10 +59,10 @@ CapsuleCollider: m_Material: {fileID: 0} m_IsTrigger: 0 m_Enabled: 1 - m_Radius: 0.9437419 - m_Height: 2 + m_Radius: 0.05 + m_Height: 1 m_Direction: 2 - m_Center: {x: 0, y: -0.045321055, z: 0} + m_Center: {x: 0, y: 0, z: 0.8} --- !u!114 &114877143042695842 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/src/FieldWarning/Assets/Units/ShellBehaviour.cs b/src/FieldWarning/Assets/Units/ShellBehaviour.cs index 117b3786..3de2f1a9 100644 --- a/src/FieldWarning/Assets/Units/ShellBehaviour.cs +++ b/src/FieldWarning/Assets/Units/ShellBehaviour.cs @@ -37,9 +37,12 @@ public class ShellBehaviour : MonoBehaviour private float _forwardSpeed => _ammo.Velocity; private float _verticalSpeed = 0F; private Vector3 _targetCoordinates; + private Vector3 _targetVelocity; + //targetVelocity is the instantaneous target velocity when this shell is fired. private bool _dead = false; private float _prevDistanceToTarget = 100000F; + private float _initialDistanceToTarget; private Ammo _ammo; @@ -47,10 +50,12 @@ public class ShellBehaviour : MonoBehaviour /// Call in the weapon class to initialize the shell/bullet. /// /// In meters. - public void Initialize(Vector3 target, Ammo ammo) + public void Initialize(Vector3 target, Ammo ammo, Vector3 targetVelocity) { _targetCoordinates = target; _ammo = ammo; + _targetVelocity = targetVelocity; + _initialDistanceToTarget = (_targetCoordinates - transform.position).magnitude; } private void Start() @@ -91,6 +96,7 @@ public static Quaternion CalculateBarrelAngle( Vector3 targetXZPos = new Vector3(target.x, 0.0f, target.z); float horizontalDistanceToTarget = Vector3.Distance(projectileXZPos, targetXZPos); + float verticalDistanceToTarget = target.y - start.y; // TODO adjust based on height difference between start and target points float distanceToHighestPoint = horizontalDistanceToTarget / 2f; @@ -98,6 +104,7 @@ public static Quaternion CalculateBarrelAngle( float gravityEffectToHighestPoint = GRAVITY * timeToHighestPoint; verticalSpeed = gravityEffectToHighestPoint; + verticalSpeed = verticalSpeed + GRAVITY * verticalDistanceToTarget / (2f * verticalSpeed); // The vectors for the horizontal speed, vertical speed and // the direction of the barrel form a triangle. @@ -118,33 +125,50 @@ private void Update() Vector3 worldForward = transform.TransformDirection(Vector3.forward); worldForward = new Vector3(worldForward.x, 0, worldForward.z); Vector3 translation = _forwardSpeed * worldForward * Time.deltaTime - + _verticalSpeed * Vector3.up * Time.deltaTime; - transform.LookAt(transform.position + translation); + + _verticalSpeed * Vector3.up * Time.deltaTime + + _targetVelocity * Time.deltaTime; /// * Constants.MAP_SCALE; + ///transform.LookAt(transform.position + translation); transform.Translate( translation, Space.World); _verticalSpeed -= GRAVITY * Time.deltaTime; - - - // small trick to detect if shell has reached the target - float distanceToTarget = Vector3.Distance(transform.position, _targetCoordinates); - if (distanceToTarget > _prevDistanceToTarget) - { - transform.position = _targetCoordinates; - Explode(); - } - _prevDistanceToTarget = distanceToTarget; - - //transform.rotation.SetLookRotation(translation); } private void OnTriggerEnter(Collider other) { + + UnitDispatcher target = other.gameObject.GetComponentInParent(); + if (other.GetComponent() == null) { - Explode(); + if (target != null && !_ammo.IsAoe) + { + KineticHit(target); + } + else if (_ammo.IsAoe) + { + Explode(); + } + else + { + ///_verticalSpeed = -_verticalSpeed; riccochet logic here? + } } + + } + + private void KineticHit(UnitDispatcher unit) + { + _dead = true; + + unit.HandleHit( + _ammo.DamageType, + _ammo.DamageValue, + transform.TransformDirection(Vector3.forward), + _initialDistanceToTarget); + + Destroy(gameObject); } private void Explode() @@ -165,21 +189,18 @@ private void Explode() //emission.enabled = false; } - if (_ammo.IsAoe) - { - List units = - MatchSession.Current.FindUnitsAroundPoint( - transform.position, _ammo.ExplosionRadius); + List units = + MatchSession.Current.FindUnitsAroundPoint( + transform.position, _ammo.ExplosionRadius); - foreach (UnitDispatcher unit in units) - { - Vector3 vectorToTarget = unit.transform.position - transform.position; - unit.HandleHit( - _ammo.DamageType, - _ammo.DamageValue, - vectorToTarget, - vectorToTarget.magnitude); - } + foreach (UnitDispatcher unit in units) + { + Vector3 vectorToTarget = unit.transform.position - transform.position; + unit.HandleHit( + _ammo.DamageType, + _ammo.DamageValue, + vectorToTarget, + vectorToTarget.magnitude); } Destroy(gameObject);