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 2 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,23 @@ 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;

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;
}

Expand Down
18 changes: 1 addition & 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,7 @@ 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);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Break up giga long lines if its not too much trouble.

Also normally this kind of thing would be done through the UnitDispatcher (add a UnitDispatcher property called 'CurrentMovementSpeed' or something and then have that get the value from movement component; the intent is to avoid non-unit code fiddling with specific components, and have the UnitDispatcher provide a clean interface for outside requests. Then this code just goes target.Enemy.CurrentMovementSpeed. If it sounds like too much boilerplate you can leave it like this tho.


return true;
}
Expand Down
19 changes: 17 additions & 2 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: 2
m_Direction: 2
m_Center: {x: 0, y: 0, z: 1}
--- !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_Radius: 0.05
m_Height: 2
m_Direction: 2
m_Center: {x: 0, y: -0.045321055, z: 0}
m_Center: {x: 0, y: 0, z: 1}
--- !u!114 &114877143042695842
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down
89 changes: 61 additions & 28 deletions src/FieldWarning/Assets/Units/ShellBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,28 @@ 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;

private Collider _launchPlatform;
private bool _justLaunched = true;

/// <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 @@ -118,33 +126,61 @@ 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)
if (_justLaunched)
{
transform.position = _targetCoordinates;
Explode();
print("instant collision not avoided");
Copy link
Collaborator

Choose a reason for hiding this comment

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

leftover print

}
_prevDistanceToTarget = distanceToTarget;

//transform.rotation.SetLookRotation(translation);
_justLaunched = false;
}

private void OnTriggerEnter(Collider other)
{
if (other.GetComponent<CaptureZone>() == null)
if (_justLaunched)
{
Explode();
_launchPlatform = other;
_justLaunched = false;
print("instant collision avoided now");
Copy link
Collaborator

Choose a reason for hiding this comment

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

leftover print; if you want to leave it in, use Logger.LogDamage

}
else if(_launchPlatform != other)
{
UnitDispatcher target = other.gameObject.GetComponentInParent<UnitDispatcher>();

if (target != null && !_ammo.IsAoe)
{
Kinetic(target);
}
else if (other.GetComponent<CaptureZone>() == null && _ammo.IsAoe)
{
Explode();
}
else
{
///_verticalSpeed = -_verticalSpeed;
}
}

}

private void Kinetic(UnitDispatcher unit)
{
_dead = true;

unit.HandleHit(
_ammo.DamageType,
_ammo.DamageValue,
transform.TransformDirection(Vector3.forward),
_initialDistanceToTarget);

Destroy(gameObject);
}

private void Explode()
Expand All @@ -165,21 +201,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