Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wertz public branch #173

Merged
merged 3 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/FieldWarning/.vsconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": "1.0",
"components": [
"Microsoft.VisualStudio.Workload.ManagedGame"
]
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we leave this file in or add it to gitignore instead?

Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can have a private setter

public DataComponent Data { get; private set; }
public Pathfinder Pathfinder { get; private set; }

Expand Down Expand Up @@ -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;
IndustrialDonut marked this conversation as resolved.
Show resolved Hide resolved

transform.position = newPosition;
}

Expand Down
22 changes: 5 additions & 17 deletions src/FieldWarning/Assets/Units/Component/Weapon/Cannon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,23 +184,11 @@ private bool FireWeapon(
}

ShellBehaviour shellBehaviour = shell.GetComponent<ShellBehaviour>();
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<PFW.Units.Component.Movement.MovementComponent>().Velocity
);

return true;
}
Expand Down
21 changes: 18 additions & 3 deletions src/FieldWarning/Assets/Units/Prefab/Resources/shell.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
Expand All @@ -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
Expand Down
81 changes: 51 additions & 30 deletions src/FieldWarning/Assets/Units/ShellBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,25 @@ 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;

/// <summary>
/// Call in the weapon class to initialize the shell/bullet.
/// </summary>
/// <param name="velocity">In meters.</param>
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()
Expand Down Expand Up @@ -91,13 +96,15 @@ 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;
float timeToHighestPoint = distanceToHighestPoint / horizontalSpeed;
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.
Expand All @@ -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<UnitDispatcher>();

if (other.GetComponent<CaptureZone>() == 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()
Expand All @@ -165,21 +189,18 @@ private void Explode()
//emission.enabled = false;
}

if (_ammo.IsAoe)
{
List<UnitDispatcher> units =
MatchSession.Current.FindUnitsAroundPoint(
transform.position, _ammo.ExplosionRadius);
List<UnitDispatcher> 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);
Expand Down