diff --git a/Core/Game/Agents/AgentController.cs b/Core/Game/Agents/AgentController.cs index d47a1efa..3f7b4b2a 100755 --- a/Core/Game/Agents/AgentController.cs +++ b/Core/Game/Agents/AgentController.cs @@ -282,7 +282,7 @@ public static int GetStateHash() if (GlobalAgentActive [i]) { LSAgent agent = GlobalAgents [i]; - int n1 = agent.Body._position.GetStateHash() + agent.Body._rotation.GetStateHash(); + int n1 = agent.Body._position.GetHashCode() + agent.Body._rotation.GetStateHash(); switch (operationToggle) { case 0: diff --git a/Core/Game/Projectiles/LSProjectile.cs b/Core/Game/Projectiles/LSProjectile.cs index 7c7ca1b1..f06576dc 100755 --- a/Core/Game/Projectiles/LSProjectile.cs +++ b/Core/Game/Projectiles/LSProjectile.cs @@ -1,811 +1,811 @@ -using System; -using System.Runtime.CompilerServices; -using UnityEngine; -using Lockstep.Data; -using System.Collections.Generic; - -#if UNITY_EDITOR -using UnityEditor; -using Lockstep.Integration; -#endif -namespace Lockstep -{ - public sealed class LSProjectile : CerealBehaviour - { - public const int DefaultTickRate = LockstepManager.FrameRate / 4; - private const long Gravity = FixedMath.One * 98 / 10; - // - // Static Fields - // - private static Vector2d agentPos; - - private static Vector3 newPos; - - private static Vector2d tempDirection; - - private const int defaultMaxDuration = LockstepManager.FrameRate * 16; - - private static Vector2d difference; - - // - // Fields - // - private GameObject cachedGameObject; - - private Transform cachedTransform; - - public Vector3d Position; - - [FixedNumber, SerializeField] - public long _speed; - - private int CountDown; - - public Vector3d Velocity { get; private set; } - - public Vector3d Direction { get; set; } - - - private Vector2d lastDirection; - - private long speedPerFrame; - - private long HeightSpeed; - - private long arcStartVerticalSpeed; - - private long arcStartHeight; - private long linearHeightSpeed; - - [SerializeField] - private bool _visualArc; - - [SerializeField, FrameCount] - private int _delay; - - [SerializeField] - private bool _attachEndEffectToTarget; - - public bool AttachEndEffectToTarget { get { return _attachEndEffectToTarget; } } - - [SerializeField, DataCode("Effects"), UnityEngine.Serialization.FormerlySerializedAs("_endEffect")] - private string _hitFX; - - public string HitFX { get { return _hitFX; } } - - public bool CanRotate = true; - - [SerializeField, DataCode("Effects"), UnityEngine.Serialization.FormerlySerializedAs("_startEffect")] - private string _startFX; - - public string StartFX { get { return _startFX; } } - - public bool IsActive; - - public bool UseEffects; - - [SerializeField] - private bool _canVisualize = true; - - public bool CanVisualize { get { return _canVisualize; } } - - - [SerializeField] - private AgentTag _exclusiveTargetType; - - [SerializeField] - private TargetingType _targetingBehavior; - - public TargetingType TargetingBehavior { get { return _targetingBehavior; } } - - - [SerializeField] - private HitType _hitBehavior; - - public HitType HitBehavior { get { return _hitBehavior; } } - - [FixedNumberAngle, SerializeField] - private long _angle = FixedMath.TenDegrees; - - [FixedNumber, SerializeField] - private long _radius = FixedMath.Create(1); - - - [SerializeField, FrameCount] - private int _lastingDuration; - - [SerializeField, FrameCount] - private int _tickRate = DefaultTickRate; - - // - // Properties - // - - public uint SpawnVersion { get; private set; } - - //PAPPS ADDED THIS: it detaches the children particle effects inside the projectile, and siwtches em off.. REALLY NECESSARY - public bool DoReleaseChildren = false; - - public int AliveTime - { - get; - private set; - } - - public long Angle - { - get { return _angle; } - set { _angle = value; } - } - - public long Damage { get; set; } - - public int Delay { get; set; } - - public int LastingDuration { get; set; } - - public int TickRate { get; set; } - - public Vector3 EndPoint - { - get - { - return this.Target.CachedTransform.position; - } - } - - public long ExclusiveDamageModifier - { - get; - set; - } - - public AgentTag ExclusiveTargetType - { - get; - set; - } - - private bool HeightReached - { - get; - set; - } - - public int ID - { - get; - private set; - } - - public long InterpolationRate - { - get; - set; - } - - private int MaxDuration - { - get - { - int minTime = this.Delay + this.LastingDuration; - return (LockstepManager.FrameRate * 16 <= minTime) ? minTime : LockstepManager.FrameRate * 16; - } - } - - public string MyProjCode - { - get; - private set; - } - - public long Radius - { - get { return _radius; } - set { _radius = value; } - } - - public long Speed - { get; set; } - - public LSAgent Target - { - get; - set; - } - - public long TargetHeight - { - get; - set; - } - - - public Vector2d TargetPosition - { - get; - set; - } - - private uint TargetVersion - { - get; - set; - } - - public Vector2d Forward { get; set; } - - private Action HitEffect { get; set; } - - public int GetStateHash() - { - int hash = 13; - hash ^= Position.StateHash; - return hash; - } - - // - // Static Methods - // - private void ApplyArea(Vector2d center, long radius) - { - long num = radius * radius; - Scan(center, radius); - for (int i = 0; i < ScanOutput.Count; i++) - { - LSAgent agent = ScanOutput[i]; - if (agent.Body._position.FastDistance(center.x, center.y) < num) - { - HitAgent(agent); - } - } - } - - void HitAgent(LSAgent agent) - { - if (this.UseEffects && this.AttachEndEffectToTarget) - { - LSEffect lSEffect = EffectManager.CreateEffect(this.HitFX); - lSEffect.CachedTransform.parent = agent.VisualCenter; - lSEffect.CachedTransform.localPosition = Vector3.up; - lSEffect.CachedTransform.rotation = this.cachedTransform.rotation; - lSEffect.Initialize(); - } - this.HitEffect(agent); - } - - private void ApplyCone(Vector3d center3d, Vector2d forward, long radius, long angle) - { - Vector2d center = center3d.ToVector2d(); - long fastRange = radius * radius; - Scan(center, radius); - for (int i = 0; i < ScanOutput.Count; i++) - { - LSAgent agent = ScanOutput[i]; - Vector2d agentPos = agent.Body._position; - Vector2d difference = agentPos - center; - - if (difference.FastMagnitude() > fastRange) - { - continue; - } - if (forward.Dot(difference) < 0) - { - continue; - - } - difference.Normalize(); - - long cross = forward.Cross(difference).Abs(); - if (cross > angle) - { - continue; - } - HitAgent(agent); - - } - } - - - private bool CheckCollision() - { - return CheckCollision(Target.Body); - } - - private bool CheckCollision(LSBody target) - { - return target._position.FastDistance(Position.x, Position.y) <= target.FastRadius; - } - - - static FastList ScanOutput = new FastList(); - private void Scan(Vector2d center, long radius) - { - InfluenceManager.ScanAll( - center, - radius, - this.AgentConditional, - this.BucketConditional, - ScanOutput - ); - - } - - private void SetupCachedActions() - { - } - - internal void Deactivate() - { - SpawnVersion = 0; - this.TargetVersion = 0u; - this.IsActive = false; - if (cachedGameObject.IsNotNull()) - this.cachedGameObject.SetActive(false); - if (this.cachedTransform.IsNotNull()) - { - this.cachedTransform.parent = null; - } - if (this.onDeactivate.IsNotNull()) - { - this.onDeactivate.Invoke(); - } - } - - public bool IsExclusiveTarget(AgentTag AgentTag) - { - return this.ExclusiveTargetType != AgentTag.None && AgentTag == this.ExclusiveTargetType; - } - - public long CheckExclusiveDamage(AgentTag AgentTag) - { - return IsExclusiveTarget(AgentTag) ? Damage.Mul(this.ExclusiveDamageModifier) : Damage; - } - - private void Hit(bool destroy = true) - { - if (this.TargetingBehavior == TargetingType.Homing && this.HitBehavior == HitType.Single && this.Target.SpawnVersion != this.TargetVersion) - { - ProjectileManager.EndProjectile(this); - return; - } - this.OnHit(); - if (this.onHit.IsNotNull()) - { - this.onHit(); - } - if (this.UseEffects) - { - if (this.AttachEndEffectToTarget) - { - /* - LSEffect lSEffect = EffectManager.CreateEffect(this.HitFX); - lSEffect.CachedTransform.parent = this.Target.VisualCenter; - lSEffect.CachedTransform.localPosition = Vector3.up; - lSEffect.CachedTransform.rotation = this.cachedTransform.rotation; - lSEffect.Initialize(); - */ - } - else - { - - { - EffectManager.LazyCreateEffect(this.HitFX, this.cachedTransform.position, this.cachedTransform.rotation); - } - } - } - - if (destroy) - ProjectileManager.EndProjectile(this); - } - - public Func BucketConditional { get; private set; } - - public Func AgentConditional { get; private set; } - - public bool Deterministic { get; private set; } - - internal void Prepare(int id, Vector3d projectilePosition, Func agentConditional, Func bucketConditional, Action hitEffect, bool deterministic) - { - this.Deterministic = deterministic; - - this.IsActive = true; - this.cachedGameObject.SetActiveIfNot(true); - - this.ResetVariables(); - - this.Position = projectilePosition; - this.HitEffect = hitEffect; - this.ID = id; - - this.AliveTime = 0; - this.IsLasting = false; - - - this.BucketConditional = bucketConditional; - - this.AgentConditional = agentConditional; - - Forward = Vector2d.up; - } - - public void InitializeHoming(LSAgent target) - { - this.HeightReached = false; - this.Target = target; - this.TargetVersion = this.Target.SpawnVersion; - - this.TargetPosition = this.Target.Body._position; - this.TargetHeight = this.Target.Body.HeightPos + this.Target.Body.Height / 2; - - this.cachedTransform.rotation = Quaternion.LookRotation(target.CachedTransform.position - this.Position.ToVector3()); - - - } - - public void InitializeTimed(Vector2d forward) - { - Forward = forward; - Direction = forward.ToVector3d(); - } - - Func BodyConditional; - - public void InitializeFree(Vector3d direction, Func bodyConditional, bool useGravity = false) - { - this.BodyConditional = bodyConditional; - this.Direction = direction; - this.Forward = Direction.ToVector2d(); - - this.cachedTransform.rotation = Quaternion.LookRotation(direction.ToVector3()); - } - - public void InitializePositional(Vector3d position) - { - this.TargetPosition = position.ToVector2d(); - this.TargetHeight = position.z; - - } - - public void UpdateVisuals() - { - cachedTransform.rotation = Quaternion.LookRotation(Forward.ToVector3()); - cachedTransform.position = this.Position.ToVector3(); - } - - private bool IsLasting; - private int tickTimer; - - public void LateInit() - { - - if (this.TargetingBehavior != TargetingType.Timed) - { - this.cachedTransform.position = this.Position.ToVector3(); - this.speedPerFrame = this.Speed / 32L; - } - - - - switch (this.TargetingBehavior) - { - case TargetingType.Timed: - this.CountDown = this.Delay; - - break; - case TargetingType.Positional: - case TargetingType.Homing: - long f = this.Position.ToVector2d().Distance(this.TargetPosition); - long timeToHit = f.Div(this.Speed); - if (this._visualArc) - { - this.arcStartHeight = this.Position.z; - if (timeToHit > 0) - { - - - this.arcStartVerticalSpeed = (this.TargetHeight - this.Position.z).Div(timeToHit) + timeToHit.Mul(Gravity); - } - } - else - { - if (timeToHit > 0) - this.linearHeightSpeed = (this.TargetHeight - Position.z).Div(timeToHit).Abs() / LockstepManager.FrameRate; - - } - Forward = TargetPosition - this.Position.ToVector2d(); - Forward.Normalize(); - break; - case TargetingType.Free: - - Vector3d vel = this.Direction; - vel.Mul(speedPerFrame); - this.Velocity = vel; - - - break; - } - if (this.CanRotate) - { - this.cachedTransform.LookAt(this.Direction.ToVector3()); - } - this.UpdateVisuals(); - - if (this.onInitialize.IsNotNull()) - { - this.onInitialize.Invoke(); - } - - if (UseEffects) - { - LSEffect effect = EffectManager.LazyCreateEffect(this.StartFX, this.Position.ToVector3(), this.cachedTransform.rotation); - if (effect != null) - { - effect.StartPos = this.Position.ToVector3(); - effect.EndPos = this.TargetPosition.ToVector3(this.TargetHeight.ToFloat()); - if (this.Target != null) - effect.Target = Target.transform; - } - } - } - - private void OnHit() - { - if (this.TargetingBehavior == TargetingType.Free) - { - switch (this.HitBehavior) - { - case HitType.Single: - this.HitBodies[0].TestFlash(); - break; - } - } - else - { - switch (this.HitBehavior) - { - case HitType.Single: - if (Target == null) - { - //throw new System.Exception("Cannot use single hit effect without target"); - - break; - } - this.HitAgent(Target); - break; - case HitType.Area: - ApplyArea(this.Position.ToVector2d(), this.Radius); - break; - case HitType.Cone: - Debug.Log(this.Forward); - ApplyCone(this.Position, this.Forward, this.Radius, this.Angle); - break; - } - } - //PAPPS ADDED THIS: - if (DoReleaseChildren) - { - foreach (Transform each in transform) - { - ParticleSystem ps; - if (ps = each.GetComponent()) - { - each.parent = null; - var psEmission = ps.emission; - psEmission.enabled = false; - foreach (Transform eachChild in each) - { - ParticleSystem cps; - if (cps = eachChild.GetComponent()) - { - var cpsEmission = cps.emission; - cpsEmission.enabled = false; - } - } - } - } - } - } - - private void ResetHit() - { - this.ExclusiveTargetType = this._exclusiveTargetType; - this.onHit = null; - } - - private void ResetEffects() - { - } - - private void ResetHelpers() - { - this.lastDirection = Vector2d.zero; - this.Velocity = default(Vector3d); - this.Direction = Vector2d.up.ToVector3d(); - } - - private void ResetTargeting() - { - this.Delay = this._delay; - this.Speed = this._speed; - this.LastingDuration = this._lastingDuration; - this.TickRate = this._tickRate; - - } - - private void ResetTrajectory() - { - } - - private void ResetVariables() - { - this.ResetEffects(); - this.ResetTrajectory(); - this.ResetHit(); - this.ResetTargeting(); - this.ResetHelpers(); - } - - public IProjectileData MyData { get; private set; } - - public void Setup(IProjectileData dataItem) - { - this.SpawnVersion = 1u; - this.MyData = dataItem; - this.MyProjCode = dataItem.Name; - this.cachedGameObject = base.gameObject; - this.cachedTransform = base.transform; - GameObject.DontDestroyOnLoad(this.cachedGameObject); - if (this.onSetup.IsNotNull()) - { - this.onSetup.Invoke(); - } - } - - private FastList HitBodies = new FastList(); - - public void Simulate() - { - this.AliveTime++; - - if (this.AliveTime > this.MaxDuration) - { - ProjectileManager.EndProjectile(this); - return; - } - switch (this.TargetingBehavior) - { - case TargetingType.Timed: - this.CountDown--; - - if (!IsLasting) - { - if (this.CountDown <= 0) - { - IsLasting = true; - tickTimer = 0; - } - } - if (IsLasting) - { - tickTimer--; - if (tickTimer <= 0) - { - tickTimer = TickRate; - this.Hit((this.AliveTime + TickRate - this.Delay) >= this.LastingDuration); - } - } - break; - case TargetingType.Homing: - if (this.CheckCollision()) - { - this.TargetPosition = this.Target.Body._position; - this.Hit(); - } - else - { - TargetPosition = Target.Body.Position; - this.TargetHeight = this.Target.Body.HeightPos + Target.Body.Height / 2; - - MoveToTargetPosition(); - } - break; - case TargetingType.Free: - RaycastMove(this.Velocity); - break; - case TargetingType.Positional: - MoveToTargetPosition(); - - break; - } - } - - void MoveToTargetPosition() - { - if (this._visualArc) - { - long progress = FixedMath.Create(this.AliveTime) / 32; - long height = this.arcStartHeight + this.arcStartVerticalSpeed.Mul(progress) - Gravity.Mul(progress.Mul(progress)); - this.Position.z = height; - } - else - { - this.Position.z = FixedMath.MoveTowards(this.Position.z, TargetHeight, this.linearHeightSpeed); - } - - LSProjectile.tempDirection = TargetPosition - this.Position.ToVector2d(); - if (LSProjectile.tempDirection.Dot(this.lastDirection.x, this.lastDirection.y) < 0L || tempDirection == Vector2d.zero) - { - this.Hit(); - //Debug.Log("asdf"); - } - else - { - //Debug.Log("asdf2"); - LSProjectile.tempDirection.Normalize(); - Forward = tempDirection; - this.lastDirection = LSProjectile.tempDirection; - LSProjectile.tempDirection *= this.speedPerFrame; - this.Position.Add(LSProjectile.tempDirection.ToVector3d()); - } - } - - public void RaycastMove(Vector3d delta) - { -#if true - Vector3d nextPosition = this.Position; - nextPosition.Add(ref delta); - HitBodies.FastClear(); - foreach (LSBody body in Raycaster.RaycastAll(this.Position, nextPosition)) - { - if (this.BodyConditional(body)) - { - HitBodies.Add(body); - } - } - if (HitBodies.Count > 0) - Hit(); - this.Position = nextPosition; -#endif - } - - public void Visualize() - { - if (this.IsActive) - { - if (this.CanVisualize) - { - - LSProjectile.newPos = this.Position.ToVector3(); - Vector3 shiftVelocity = LSProjectile.newPos - this.cachedTransform.position; - this.cachedTransform.position = LSProjectile.newPos; - if (shiftVelocity.sqrMagnitude > 0) - { - this.cachedTransform.rotation = Quaternion.LookRotation(shiftVelocity); - } - } - if (this.onVisualize.IsNotNull()) - { - this.onVisualize.Invoke(); - } - } - else - { - this.cachedGameObject.SetActiveIfNot(false); - } - } - - // - // Events - // - public event Action onDeactivate; - - public event Action onHit; - - public event Action onInitialize; - - public event Action onSetup; - public event Action onVisualize; - - } -} +using System; +using System.Runtime.CompilerServices; +using UnityEngine; +using Lockstep.Data; +using System.Collections.Generic; + +#if UNITY_EDITOR +using UnityEditor; +using Lockstep.Integration; +#endif +namespace Lockstep +{ + public sealed class LSProjectile : CerealBehaviour + { + public const int DefaultTickRate = LockstepManager.FrameRate / 4; + private const long Gravity = FixedMath.One * 98 / 10; + // + // Static Fields + // + private static Vector2d agentPos; + + private static Vector3 newPos; + + private static Vector2d tempDirection; + + private const int defaultMaxDuration = LockstepManager.FrameRate * 16; + + private static Vector2d difference; + + // + // Fields + // + private GameObject cachedGameObject; + + private Transform cachedTransform; + + public Vector3d Position; + + [FixedNumber, SerializeField] + public long _speed; + + private int CountDown; + + public Vector3d Velocity { get; private set; } + + public Vector3d Direction { get; set; } + + + private Vector2d lastDirection; + + private long speedPerFrame; + + private long HeightSpeed; + + private long arcStartVerticalSpeed; + + private long arcStartHeight; + private long linearHeightSpeed; + + [SerializeField] + private bool _visualArc; + + [SerializeField, FrameCount] + private int _delay; + + [SerializeField] + private bool _attachEndEffectToTarget; + + public bool AttachEndEffectToTarget { get { return _attachEndEffectToTarget; } } + + [SerializeField, DataCode("Effects"), UnityEngine.Serialization.FormerlySerializedAs("_endEffect")] + private string _hitFX; + + public string HitFX { get { return _hitFX; } } + + public bool CanRotate = true; + + [SerializeField, DataCode("Effects"), UnityEngine.Serialization.FormerlySerializedAs("_startEffect")] + private string _startFX; + + public string StartFX { get { return _startFX; } } + + public bool IsActive; + + public bool UseEffects; + + [SerializeField] + private bool _canVisualize = true; + + public bool CanVisualize { get { return _canVisualize; } } + + + [SerializeField] + private AgentTag _exclusiveTargetType; + + [SerializeField] + private TargetingType _targetingBehavior; + + public TargetingType TargetingBehavior { get { return _targetingBehavior; } } + + + [SerializeField] + private HitType _hitBehavior; + + public HitType HitBehavior { get { return _hitBehavior; } } + + [FixedNumberAngle, SerializeField] + private long _angle = FixedMath.TenDegrees; + + [FixedNumber, SerializeField] + private long _radius = FixedMath.Create(1); + + + [SerializeField, FrameCount] + private int _lastingDuration; + + [SerializeField, FrameCount] + private int _tickRate = DefaultTickRate; + + // + // Properties + // + + public uint SpawnVersion { get; private set; } + + //PAPPS ADDED THIS: it detaches the children particle effects inside the projectile, and siwtches em off.. REALLY NECESSARY + public bool DoReleaseChildren = false; + + public int AliveTime + { + get; + private set; + } + + public long Angle + { + get { return _angle; } + set { _angle = value; } + } + + public long Damage { get; set; } + + public int Delay { get; set; } + + public int LastingDuration { get; set; } + + public int TickRate { get; set; } + + public Vector3 EndPoint + { + get + { + return this.Target.CachedTransform.position; + } + } + + public long ExclusiveDamageModifier + { + get; + set; + } + + public AgentTag ExclusiveTargetType + { + get; + set; + } + + private bool HeightReached + { + get; + set; + } + + public int ID + { + get; + private set; + } + + public long InterpolationRate + { + get; + set; + } + + private int MaxDuration + { + get + { + int minTime = this.Delay + this.LastingDuration; + return (LockstepManager.FrameRate * 16 <= minTime) ? minTime : LockstepManager.FrameRate * 16; + } + } + + public string MyProjCode + { + get; + private set; + } + + public long Radius + { + get { return _radius; } + set { _radius = value; } + } + + public long Speed + { get; set; } + + public LSAgent Target + { + get; + set; + } + + public long TargetHeight + { + get; + set; + } + + + public Vector2d TargetPosition + { + get; + set; + } + + private uint TargetVersion + { + get; + set; + } + + public Vector2d Forward { get; set; } + + private Action HitEffect { get; set; } + + public int GetStateHash() + { + int hash = 13; + hash ^= Position.StateHash; + return hash; + } + + // + // Static Methods + // + private void ApplyArea(Vector2d center, long radius) + { + long num = radius * radius; + Scan(center, radius); + for (int i = 0; i < ScanOutput.Count; i++) + { + LSAgent agent = ScanOutput[i]; + if (agent.Body._position.FastDistance(center.x, center.y) < num) + { + HitAgent(agent); + } + } + } + + void HitAgent(LSAgent agent) + { + if (this.UseEffects && this.AttachEndEffectToTarget) + { + LSEffect lSEffect = EffectManager.CreateEffect(this.HitFX); + lSEffect.CachedTransform.parent = agent.VisualCenter; + lSEffect.CachedTransform.localPosition = Vector3.up; + lSEffect.CachedTransform.rotation = this.cachedTransform.rotation; + lSEffect.Initialize(); + } + this.HitEffect(agent); + } + + private void ApplyCone(Vector3d center3d, Vector2d forward, long radius, long angle) + { + Vector2d center = center3d.ToVector2d(); + long fastRange = radius * radius; + Scan(center, radius); + for (int i = 0; i < ScanOutput.Count; i++) + { + LSAgent agent = ScanOutput[i]; + Vector2d agentPos = agent.Body._position; + Vector2d difference = agentPos - center; + + if (difference.FastMagnitude() > fastRange) + { + continue; + } + if (forward.Dot(difference) < 0) + { + continue; + + } + difference.Normalize(); + + long cross = forward.Cross(difference).Abs(); + if (cross > angle) + { + continue; + } + HitAgent(agent); + + } + } + + + private bool CheckCollision() + { + return CheckCollision(Target.Body); + } + + private bool CheckCollision(LSBody target) + { + return target._position.FastDistance(Position.x, Position.y) <= target.FastRadius; + } + + + static FastList ScanOutput = new FastList(); + private void Scan(Vector2d center, long radius) + { + InfluenceManager.ScanAll( + center, + radius, + this.AgentConditional, + this.BucketConditional, + ScanOutput + ); + + } + + private void SetupCachedActions() + { + } + + internal void Deactivate() + { + SpawnVersion = 0; + this.TargetVersion = 0u; + this.IsActive = false; + if (cachedGameObject.IsNotNull()) + this.cachedGameObject.SetActive(false); + if (this.cachedTransform.IsNotNull()) + { + this.cachedTransform.parent = null; + } + if (this.onDeactivate.IsNotNull()) + { + this.onDeactivate.Invoke(); + } + } + + public bool IsExclusiveTarget(AgentTag AgentTag) + { + return this.ExclusiveTargetType != AgentTag.None && AgentTag == this.ExclusiveTargetType; + } + + public long CheckExclusiveDamage(AgentTag AgentTag) + { + return IsExclusiveTarget(AgentTag) ? Damage.Mul(this.ExclusiveDamageModifier) : Damage; + } + + private void Hit(bool destroy = true) + { + if (this.TargetingBehavior == TargetingType.Homing && this.HitBehavior == HitType.Single && this.Target.SpawnVersion != this.TargetVersion) + { + ProjectileManager.EndProjectile(this); + return; + } + this.OnHit(); + if (this.onHit.IsNotNull()) + { + this.onHit(); + } + if (this.UseEffects) + { + if (this.AttachEndEffectToTarget) + { + /* + LSEffect lSEffect = EffectManager.CreateEffect(this.HitFX); + lSEffect.CachedTransform.parent = this.Target.VisualCenter; + lSEffect.CachedTransform.localPosition = Vector3.up; + lSEffect.CachedTransform.rotation = this.cachedTransform.rotation; + lSEffect.Initialize(); + */ + } + else + { + + { + EffectManager.LazyCreateEffect(this.HitFX, this.cachedTransform.position, this.cachedTransform.rotation); + } + } + } + + if (destroy) + ProjectileManager.EndProjectile(this); + } + + public Func BucketConditional { get; private set; } + + public Func AgentConditional { get; private set; } + + public bool Deterministic { get; private set; } + + internal void Prepare(int id, Vector3d projectilePosition, Func agentConditional, Func bucketConditional, Action hitEffect, bool deterministic) + { + this.Deterministic = deterministic; + + this.IsActive = true; + this.cachedGameObject.SetActiveIfNot(true); + + this.ResetVariables(); + + this.Position = projectilePosition; + this.HitEffect = hitEffect; + this.ID = id; + + this.AliveTime = 0; + this.IsLasting = false; + + + this.BucketConditional = bucketConditional; + + this.AgentConditional = agentConditional; + + Forward = Vector2d.up; + } + + public void InitializeHoming(LSAgent target) + { + this.HeightReached = false; + this.Target = target; + this.TargetVersion = this.Target.SpawnVersion; + + this.TargetPosition = this.Target.Body._position; + this.TargetHeight = this.Target.Body.HeightPos + this.Target.Body.Height / 2; + + this.cachedTransform.rotation = Quaternion.LookRotation(target.CachedTransform.position - this.Position.ToVector3()); + + + } + + public void InitializeTimed(Vector2d forward) + { + Forward = forward; + Direction = forward.ToVector3d(); + } + + Func BodyConditional; + + public void InitializeFree(Vector3d direction, Func bodyConditional, bool useGravity = false) + { + this.BodyConditional = bodyConditional; + this.Direction = direction; + this.Forward = Direction.ToVector2d(); + + this.cachedTransform.rotation = Quaternion.LookRotation(direction.ToVector3()); + } + + public void InitializePositional(Vector3d position) + { + this.TargetPosition = position.ToVector2d(); + this.TargetHeight = position.z; + + } + + public void UpdateVisuals() + { + cachedTransform.rotation = Quaternion.LookRotation(Forward.ToVector3()); + cachedTransform.position = this.Position.ToVector3(); + } + + private bool IsLasting; + private int tickTimer; + + public void LateInit() + { + + if (this.TargetingBehavior != TargetingType.Timed) + { + this.cachedTransform.position = this.Position.ToVector3(); + this.speedPerFrame = this.Speed / 32L; + } + + + + switch (this.TargetingBehavior) + { + case TargetingType.Timed: + this.CountDown = this.Delay; + + break; + case TargetingType.Positional: + case TargetingType.Homing: + long f = this.Position.ToVector2d().Distance(this.TargetPosition); + long timeToHit = f.Div(this.Speed); + if (this._visualArc) + { + this.arcStartHeight = this.Position.z; + if (timeToHit > 0) + { + + + this.arcStartVerticalSpeed = (this.TargetHeight - this.Position.z).Div(timeToHit) + timeToHit.Mul(Gravity); + } + } + else + { + if (timeToHit > 0) + this.linearHeightSpeed = (this.TargetHeight - Position.z).Div(timeToHit).Abs() / LockstepManager.FrameRate; + + } + Forward = TargetPosition - this.Position.ToVector2d(); + Forward.Normalize(); + break; + case TargetingType.Free: + + Vector3d vel = this.Direction; + vel.Mul(speedPerFrame); + this.Velocity = vel; + + + break; + } + if (this.CanRotate) + { + this.cachedTransform.LookAt(this.Direction.ToVector3()); + } + this.UpdateVisuals(); + + if (this.onInitialize.IsNotNull()) + { + this.onInitialize.Invoke(); + } + + if (UseEffects) + { + LSEffect effect = EffectManager.LazyCreateEffect(this.StartFX, this.Position.ToVector3(), this.cachedTransform.rotation); + if (effect != null) + { + effect.StartPos = this.Position.ToVector3(); + effect.EndPos = this.TargetPosition.ToVector3(this.TargetHeight.ToFloat()); + if (this.Target != null) + effect.Target = Target.transform; + } + } + } + + private void OnHit() + { + if (this.TargetingBehavior == TargetingType.Free) + { + switch (this.HitBehavior) + { + case HitType.Single: + this.HitBodies[0].TestFlash(); + break; + } + } + else + { + switch (this.HitBehavior) + { + case HitType.Single: + if (Target == null) + { + //throw new System.Exception("Cannot use single hit effect without target"); + + break; + } + this.HitAgent(Target); + break; + case HitType.Area: + ApplyArea(this.Position.ToVector2d(), this.Radius); + break; + case HitType.Cone: + Debug.Log(this.Forward); + ApplyCone(this.Position, this.Forward, this.Radius, this.Angle); + break; + } + } + //PAPPS ADDED THIS: + if (DoReleaseChildren) + { + foreach (Transform each in transform) + { + ParticleSystem ps; + if (ps = each.GetComponent()) + { + each.parent = null; + var psEmission = ps.emission; + psEmission.enabled = false; + foreach (Transform eachChild in each) + { + ParticleSystem cps; + if (cps = eachChild.GetComponent()) + { + var cpsEmission = cps.emission; + cpsEmission.enabled = false; + } + } + } + } + } + } + + private void ResetHit() + { + this.ExclusiveTargetType = this._exclusiveTargetType; + this.onHit = null; + } + + private void ResetEffects() + { + } + + private void ResetHelpers() + { + this.lastDirection = Vector2d.zero; + this.Velocity = default(Vector3d); + this.Direction = Vector2d.up.ToVector3d(); + } + + private void ResetTargeting() + { + this.Delay = this._delay; + this.Speed = this._speed; + this.LastingDuration = this._lastingDuration; + this.TickRate = this._tickRate; + + } + + private void ResetTrajectory() + { + } + + private void ResetVariables() + { + this.ResetEffects(); + this.ResetTrajectory(); + this.ResetHit(); + this.ResetTargeting(); + this.ResetHelpers(); + } + + public IProjectileData MyData { get; private set; } + + public void Setup(IProjectileData dataItem) + { + this.SpawnVersion = 1u; + this.MyData = dataItem; + this.MyProjCode = dataItem.Name; + this.cachedGameObject = base.gameObject; + this.cachedTransform = base.transform; + GameObject.DontDestroyOnLoad(this.cachedGameObject); + if (this.onSetup.IsNotNull()) + { + this.onSetup.Invoke(); + } + } + + private FastList HitBodies = new FastList(); + + public void Simulate() + { + this.AliveTime++; + + if (this.AliveTime > this.MaxDuration) + { + ProjectileManager.EndProjectile(this); + return; + } + switch (this.TargetingBehavior) + { + case TargetingType.Timed: + this.CountDown--; + + if (!IsLasting) + { + if (this.CountDown <= 0) + { + IsLasting = true; + tickTimer = 0; + } + } + if (IsLasting) + { + tickTimer--; + if (tickTimer <= 0) + { + tickTimer = TickRate; + this.Hit((this.AliveTime + TickRate - this.Delay) >= this.LastingDuration); + } + } + break; + case TargetingType.Homing: + if (this.CheckCollision()) + { + this.TargetPosition = this.Target.Body._position; + this.Hit(); + } + else + { + TargetPosition = Target.Body.Position; + this.TargetHeight = this.Target.Body.HeightPos + Target.Body.Height / 2; + + MoveToTargetPosition(); + } + break; + case TargetingType.Free: + RaycastMove(this.Velocity); + break; + case TargetingType.Positional: + MoveToTargetPosition(); + + break; + } + } + + void MoveToTargetPosition() + { + if (this._visualArc) + { + long progress = FixedMath.Create(this.AliveTime) / 32; + long height = this.arcStartHeight + this.arcStartVerticalSpeed.Mul(progress) - Gravity.Mul(progress.Mul(progress)); + this.Position.z = height; + } + else + { + this.Position.z = FixedMath.MoveTowards(this.Position.z, TargetHeight, this.linearHeightSpeed); + } + + LSProjectile.tempDirection = TargetPosition - this.Position.ToVector2d(); + if (LSProjectile.tempDirection.Dot(this.lastDirection.x, this.lastDirection.y) < 0L || tempDirection == Vector2d.zero) + { + this.Hit(); + //Debug.Log("asdf"); + } + else + { + //Debug.Log("asdf2"); + LSProjectile.tempDirection.Normalize(); + Forward = tempDirection; + this.lastDirection = LSProjectile.tempDirection; + LSProjectile.tempDirection *= this.speedPerFrame; + this.Position.Add(LSProjectile.tempDirection.ToVector3d()); + } + } + + public void RaycastMove(Vector3d delta) + { +#if true + Vector3d nextPosition = this.Position; + nextPosition.Add(ref delta); + HitBodies.FastClear(); + foreach (LSBody body in Raycaster.RaycastAll(this.Position, nextPosition)) + { + if (this.BodyConditional(body)) + { + HitBodies.Add(body); + } + } + if (HitBodies.Count > 0) + Hit(); + this.Position = nextPosition; +#endif + } + + public void Visualize() + { + if (this.IsActive) + { + if (this.CanVisualize) + { + + LSProjectile.newPos = this.Position.ToVector3(); + Vector3 shiftVelocity = LSProjectile.newPos - this.cachedTransform.position; + this.cachedTransform.position = LSProjectile.newPos; + if (shiftVelocity.sqrMagnitude > 0) + { + this.cachedTransform.rotation = Quaternion.LookRotation(shiftVelocity); + } + } + if (this.onVisualize.IsNotNull()) + { + this.onVisualize.Invoke(); + } + } + else + { + this.cachedGameObject.SetActiveIfNot(false); + } + } + + // + // Events + // + public event Action onDeactivate; + + public event Action onHit; + + public event Action onInitialize; + + public event Action onSetup; + public event Action onVisualize; + + } +} diff --git a/Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master.meta b/Core/Simulation/Math/Pan-Line-Algorithm-master.meta similarity index 100% rename from Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master.meta rename to Core/Simulation/Math/Pan-Line-Algorithm-master.meta diff --git a/Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/FractionalLineAlgorithm.cs b/Core/Simulation/Math/Pan-Line-Algorithm-master/FractionalLineAlgorithm.cs similarity index 100% rename from Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/FractionalLineAlgorithm.cs rename to Core/Simulation/Math/Pan-Line-Algorithm-master/FractionalLineAlgorithm.cs diff --git a/Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/FractionalLineAlgorithm.cs.meta b/Core/Simulation/Math/Pan-Line-Algorithm-master/FractionalLineAlgorithm.cs.meta similarity index 100% rename from Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/FractionalLineAlgorithm.cs.meta rename to Core/Simulation/Math/Pan-Line-Algorithm-master/FractionalLineAlgorithm.cs.meta diff --git a/Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/FractionalLineAlgorithmTest.cs b/Core/Simulation/Math/Pan-Line-Algorithm-master/FractionalLineAlgorithmTest.cs similarity index 100% rename from Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/FractionalLineAlgorithmTest.cs rename to Core/Simulation/Math/Pan-Line-Algorithm-master/FractionalLineAlgorithmTest.cs diff --git a/Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/FractionalLineAlgorithmTest.cs.meta b/Core/Simulation/Math/Pan-Line-Algorithm-master/FractionalLineAlgorithmTest.cs.meta similarity index 100% rename from Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/FractionalLineAlgorithmTest.cs.meta rename to Core/Simulation/Math/Pan-Line-Algorithm-master/FractionalLineAlgorithmTest.cs.meta diff --git a/Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/LICENSE b/Core/Simulation/Math/Pan-Line-Algorithm-master/LICENSE similarity index 100% rename from Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/LICENSE rename to Core/Simulation/Math/Pan-Line-Algorithm-master/LICENSE diff --git a/Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/LICENSE.meta b/Core/Simulation/Math/Pan-Line-Algorithm-master/LICENSE.meta similarity index 100% rename from Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/LICENSE.meta rename to Core/Simulation/Math/Pan-Line-Algorithm-master/LICENSE.meta diff --git a/Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/README.md b/Core/Simulation/Math/Pan-Line-Algorithm-master/README.md similarity index 100% rename from Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/README.md rename to Core/Simulation/Math/Pan-Line-Algorithm-master/README.md diff --git a/Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/README.md.meta b/Core/Simulation/Math/Pan-Line-Algorithm-master/README.md.meta similarity index 100% rename from Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/README.md.meta rename to Core/Simulation/Math/Pan-Line-Algorithm-master/README.md.meta diff --git a/Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/TestScene.unity b/Core/Simulation/Math/Pan-Line-Algorithm-master/TestScene.unity similarity index 100% rename from Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/TestScene.unity rename to Core/Simulation/Math/Pan-Line-Algorithm-master/TestScene.unity diff --git a/Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/TestScene.unity.meta b/Core/Simulation/Math/Pan-Line-Algorithm-master/TestScene.unity.meta similarity index 100% rename from Core/Simulation/Physics/Raycast/Pan-Line-Algorithm-master/TestScene.unity.meta rename to Core/Simulation/Math/Pan-Line-Algorithm-master/TestScene.unity.meta diff --git a/Core/Simulation/Math/Vector2d.cs b/Core/Simulation/Math/Vector2d.cs index 842f6d42..4aed709c 100755 --- a/Core/Simulation/Math/Vector2d.cs +++ b/Core/Simulation/Math/Vector2d.cs @@ -1,479 +1,475 @@ -using UnityEngine; -using System.Collections; -using System; - - -namespace Lockstep -{ - [Serializable] - public struct Vector2d : ICommandData - { - [FixedNumber] - public long x; - [FixedNumber] - public long y; - - - #region Constructors - - public Vector2d(long xFixed, long yFixed) - { - this.x = xFixed; - this.y = yFixed; - } - - public Vector2d(int xInt, int yInt) - { - this.x = xInt << FixedMath.SHIFT_AMOUNT; - this.y = yInt << FixedMath.SHIFT_AMOUNT; - } - - public Vector2d(Vector2 vec2) - { - this.x = FixedMath.Create(vec2.x); - this.y = FixedMath.Create(vec2.y); - } - - public Vector2d(float xFloat, float yFloat) - { - this.x = FixedMath.Create(xFloat); - this.y = FixedMath.Create(yFloat); - } - - public Vector2d(double xDoub, double yDoub) - { - this.x = FixedMath.Create(xDoub); - this.y = FixedMath.Create(yDoub); - } - - public Vector2d(Vector3 vec) - { - this.x = FixedMath.Create(vec.x); - this.y = FixedMath.Create(vec.z); - } - - #endregion - - - #region Local Math - - public void Subtract(ref Vector2d other) - { - this.x -= other.x; - this.y -= other.y; - } - - public void Add(ref Vector2d other) - { - this.x += other.x; - this.y += other.y; - } - - - /// - /// This vector's square magnitude. - /// - /// The magnitude. - public long SqrMagnitude() - { - return (this.x * this.x + this.y * this.y) >> FixedMath.SHIFT_AMOUNT; - } - - /// - /// This vector's magnitude. - /// - public long Magnitude() - { - temp1 = (this.x * this.x + this.y * this.y); - if (temp1 == 0) - return 0; - temp1 >>= FixedMath.SHIFT_AMOUNT; - return FixedMath.Sqrt(temp1); - } - - public long FastMagnitude() - { - return this.x * this.x + this.y * this.y; - } - - /// - /// Normalize this vector. - /// - public void Normalize() - { - tempMag = this.Magnitude(); - if (tempMag == 0) - { - return; - } else if (tempMag == FixedMath.One) - { - return; - } - this.x = (this.x << FixedMath.SHIFT_AMOUNT) / tempMag; - this.y = (this.y << FixedMath.SHIFT_AMOUNT) / tempMag; - } - - public void Normalize(out long mag) - { - mag = this.Magnitude(); - if (mag == 0) - { - return; - } else if (mag == FixedMath.One) - { - return; - } - this.x = (this.x << FixedMath.SHIFT_AMOUNT) / mag; - this.y = (this.y << FixedMath.SHIFT_AMOUNT) / mag; - } - - public void FastNormalize () { - //Blazing fast normalization when accuracy isn't needed - tempMag = x > y ? x + y / 2 : x / 2 + y; - const long errorFactor = 1000 * FixedMath.One / 1118; - - this.x = ((this.x << FixedMath.SHIFT_AMOUNT) / tempMag) * errorFactor >> FixedMath.SHIFT_AMOUNT; - this.y = ((this.y << FixedMath.SHIFT_AMOUNT) / tempMag) * errorFactor >> FixedMath.SHIFT_AMOUNT; - } - - public void Lerp (Vector2d target, long amount) { - Lerp (target.x,target.y,amount); - } - /// - /// Lerp this vector to target by amount. - /// - /// target. - /// amount. - public void Lerp(long targetx, long targety, long amount) - { - if (amount >= FixedMath.One) - { - this.x = targetx; - this.y = targety; - return; - } else if (amount <= 0) - { - return; - } - this.x = (targetx * amount + this.x * (FixedMath.One - amount)) >> FixedMath.SHIFT_AMOUNT; - this.y = (targety * amount + this.y * (FixedMath.One - amount)) >> FixedMath.SHIFT_AMOUNT; - } - public Vector2d Lerped (Vector2d target, long amount) { - Vector2d vec = this; - vec.Lerp(target.x,target.y,amount); - return vec; - } - public void Rotate(long cos, long sin) - { - temp1 = (this.x * cos + this.y * sin) >> FixedMath.SHIFT_AMOUNT; - this.y = (this.x * -sin + this.y * cos) >> FixedMath.SHIFT_AMOUNT; - this.x = temp1; - } - public Vector2d Rotated (long cos, long sin) { - Vector2d vec = this; - vec.Rotate(cos,sin); - return vec; - } - public Vector2d Rotated (Vector2d rotation) { - return Rotated (rotation.x,rotation.y); - } - public void RotateInverse(long cos, long sin) - { - Rotate(cos, -sin); - } - - public void RotateRight() - { - temp1 = this.x; - this.x = this.y; - this.y = -temp1; - } - - static Vector2d retVec = Vector2d.zero; - - public Vector2d rotatedRight - { - get - { - retVec.x = y; - retVec.y = -x; - return retVec; - } - } - - public Vector2d rotatedLeft - { - get - { - retVec.x = -y; - retVec.y = x; - return retVec; - } - } - - public void Reflect(long axisX, long axisY) - { - temp3 = this.Dot(axisX, axisY); - temp1 = (axisX * temp3) >> FixedMath.SHIFT_AMOUNT; - temp2 = (axisY * temp3) >> FixedMath.SHIFT_AMOUNT; - this.x = temp1 + temp1 - this.x; - this.y = temp2 + temp2 - this.y; - } - - public void Reflect(long axisX, long axisY, long projection) - { - temp1 = (axisX * projection) >> FixedMath.SHIFT_AMOUNT; - temp2 = (axisY * projection) >> FixedMath.SHIFT_AMOUNT; - this.x = temp1 + temp1 - this.x; - this.y = temp2 + temp2 - this.y; - } - - public Vector2d Reflected (long axisX, long axisY) { - Vector2d vec = this; - vec.Reflect(axisX,axisY); - return vec; - } - - public long Dot(long otherX, long otherY) - { - return (this.x * otherX + this.y * otherY) >> FixedMath.SHIFT_AMOUNT; - } - public long Dot (Vector2d other) { - return this.Dot(other.x,other.y); - } - - - public long Cross(long otherX, long otherY) - { - return (this.x * otherY - this.y * otherX) >> FixedMath.SHIFT_AMOUNT; - } - - public long Cross (Vector2d vec) { - return Cross (vec.x,vec.y); - } - - static long temp1; - static long temp2; - static long temp3; - static long tempMag; - - public long Distance(long otherX, long otherY) - { - temp1 = this.x - otherX; - temp1 *= temp1; - temp2 = this.y - otherY; - temp2 *= temp2; - return (FixedMath.Sqrt((temp1 + temp2) >> FixedMath.SHIFT_AMOUNT)); - } - - public long Distance(Vector2d other) - { - return Distance(other.x, other.y); - } - - public long SqrDistance(long otherX, long otherY) - { - - temp1 = this.x - otherX; - temp1 *= temp1; - temp2 = this.y - otherY; - temp2 *= temp2; - return ((temp1 + temp2) >> FixedMath.SHIFT_AMOUNT); - } - - /// - /// Returns a value that is greater if the distance is greater. - /// - /// The FastDistance. - public long FastDistance(long otherX, long otherY) - { - temp1 = this.x - otherX; - temp1 *= temp1; - temp2 = this.y - otherY; - temp2 *= temp2; - return (temp1 + temp2); - } - - public bool NotZero() - { - return x.MoreThanEpsilon() || y.MoreThanEpsilon(); - } - - #endregion - - #region Static Math - - public static readonly Vector2d up = new Vector2d(0, 1); - public static readonly Vector2d right = new Vector2d(1, 0); - public static readonly Vector2d down = new Vector2d(0, -1); - public static readonly Vector2d left = new Vector2d(-1, 0); - public static readonly Vector2d one = new Vector2d(1, 1); - public static readonly Vector2d negative = new Vector2d(-1, -1); - public static readonly Vector2d zero = new Vector2d(0, 0); - - public static readonly Vector2d radian0 = new Vector2d(1,0); - public static readonly Vector2d radian1 = new Vector2d(0,1); - public static readonly Vector2d radian2 = new Vector2d(-1,0); - public static readonly Vector2d radian3 = new Vector2d(0,-1); - - public static long Dot(long v1x, long v1y, long v2x, long v2y) - { - return (v1x * v2x + v1y * v2y) >> FixedMath.SHIFT_AMOUNT; - } - - public static long Cross(long v1x, long v1y, long v2x, long v2y) - { - return (v1x * v2y - v1y * v2x) >> FixedMath.SHIFT_AMOUNT; - } - - /// - /// Note: Not deterministic. Use only for serialization and sending in Commands. - /// - /// The from angle. - /// Angle. - public static Vector2d CreateRotation (double angle) - { - return new Vector2d(Math.Cos(angle), Math.Sin(angle)); - } - public static Vector2d CreateRotation (float angle) { - return CreateRotation ((double)angle); - } - - /// - /// Deterministic! - /// - /// The rotation. - /// Angle. - public static Vector2d CreateRotation (long angle) { - return new Vector2d(FixedMath.Trig.Cos(angle),FixedMath.Trig.Sin(angle)); - } - public Vector2d ToDirection () { - return new Vector2d(y,x); - } - public Vector2d ToRotation () { - return new Vector2d(y,x); - } - - #endregion - - #region Convert - - public override string ToString() - { - return ( - "(" + - Math.Round(FixedMath.ToDouble(this.x), 2, MidpointRounding.AwayFromZero).ToString() + - ", " + - Math.Round(FixedMath.ToDouble(this.y), 2, MidpointRounding.AwayFromZero) + - ")" - ); - } - - public Vector2 ToVector2() - { - return new Vector2( - (float)FixedMath.ToDouble(this.x), - (float)FixedMath.ToDouble(this.y) - ); - } - public Vector3d ToVector3d (long z = 0) { - return new Vector3d(x,y,z); - } - - public Vector3 ToVector3(float z = 0f) - { - return new Vector3((float)FixedMath.ToDouble(this.x), z, (float)FixedMath.ToDouble(this.y)); - } - - public Coordinate RoundToCoordinate() - { - return new Lockstep.Coordinate(x.RoundToInt(), y.RoundToInt()); - } - - #endregion - - public override bool Equals(object obj) - { - if (obj is Vector2d) - { - return (Vector2d)obj == this; - } - return false; - } - - public override int GetHashCode() - { - return base.GetHashCode(); - } - - #region Operators - - public static Vector2d operator +(Vector2d v1, Vector2d v2) - { - return new Vector2d(v1.x + v2.x, v1.y + v2.y); - } - - public static Vector2d operator -(Vector2d v1, Vector2d v2) - { - return new Vector2d(v1.x - v2.x, v1.y - v2.y); - } - - public static Vector2d operator *(Vector2d v1, long mag) - { - return new Vector2d((v1.x * mag) >> FixedMath.SHIFT_AMOUNT, (v1.y * mag) >> FixedMath.SHIFT_AMOUNT); - } - - public static Vector2d operator *(Vector2d v1, int mag) - { - return new Vector2d((v1.x * mag), (v1.y * mag)); - } - - public static Vector2d operator /(Vector2d v1, long div) - { - return new Vector2d(((v1.x << FixedMath.SHIFT_AMOUNT) / div), (v1.y << FixedMath.SHIFT_AMOUNT) / div); - } - - public static Vector2d operator /(Vector2d v1, int div) - { - return new Vector2d((v1.x / div), v1.y / div); - } - - public static Vector2d operator >>(Vector2d v1, int shift) - { - return new Vector2d(v1.x >> shift, v1.y >> shift); - } - - public static bool operator ==(Vector2d v1, Vector2d v2) - { - return v1.x == v2.x && v1.y == v2.y; - } - - public static bool operator !=(Vector2d v1, Vector2d v2) - { - return v1.x != v2.x || v1.y != v2.y; - } - - #endregion - public long GetLongHashCode() - { - return x * 31 + y * 7; - } - - public int GetStateHash() - { - return (int)(GetLongHashCode() % int.MaxValue); - } - - public void Write(Writer writer) - { - writer.Write(this.x); - writer.Write(this.y); - } - public void Read (Reader reader) { - this.x = reader.ReadLong(); - this.y = reader.ReadLong(); - } - } - - -} +using UnityEngine; +using System.Collections; +using System; + + +namespace Lockstep +{ + [Serializable] + public struct Vector2d : ICommandData + { + [FixedNumber] + public long x; + [FixedNumber] + public long y; + + + #region Constructors + + public Vector2d(long xFixed, long yFixed) + { + this.x = xFixed; + this.y = yFixed; + } + + public Vector2d(int xInt, int yInt) + { + this.x = xInt << FixedMath.SHIFT_AMOUNT; + this.y = yInt << FixedMath.SHIFT_AMOUNT; + } + + public Vector2d(Vector2 vec2) + { + this.x = FixedMath.Create(vec2.x); + this.y = FixedMath.Create(vec2.y); + } + + public Vector2d(float xFloat, float yFloat) + { + this.x = FixedMath.Create(xFloat); + this.y = FixedMath.Create(yFloat); + } + + public Vector2d(double xDoub, double yDoub) + { + this.x = FixedMath.Create(xDoub); + this.y = FixedMath.Create(yDoub); + } + + public Vector2d(Vector3 vec) + { + this.x = FixedMath.Create(vec.x); + this.y = FixedMath.Create(vec.z); + } + + #endregion + + + #region Local Math + + public void Subtract(ref Vector2d other) + { + this.x -= other.x; + this.y -= other.y; + } + + public void Add(ref Vector2d other) + { + this.x += other.x; + this.y += other.y; + } + + + /// + /// This vector's square magnitude. + /// + /// The magnitude. + public long SqrMagnitude() + { + return (this.x * this.x + this.y * this.y) >> FixedMath.SHIFT_AMOUNT; + } + + /// + /// This vector's magnitude. + /// + public long Magnitude() + { + temp1 = (this.x * this.x + this.y * this.y); + if (temp1 == 0) + return 0; + temp1 >>= FixedMath.SHIFT_AMOUNT; + return FixedMath.Sqrt(temp1); + } + + public long FastMagnitude() + { + return this.x * this.x + this.y * this.y; + } + + /// + /// Normalize this vector. + /// + public void Normalize() + { + tempMag = this.Magnitude(); + if (tempMag == 0) + { + return; + } else if (tempMag == FixedMath.One) + { + return; + } + this.x = (this.x << FixedMath.SHIFT_AMOUNT) / tempMag; + this.y = (this.y << FixedMath.SHIFT_AMOUNT) / tempMag; + } + + public void Normalize(out long mag) + { + mag = this.Magnitude(); + if (mag == 0) + { + return; + } else if (mag == FixedMath.One) + { + return; + } + this.x = (this.x << FixedMath.SHIFT_AMOUNT) / mag; + this.y = (this.y << FixedMath.SHIFT_AMOUNT) / mag; + } + + public void FastNormalize () { + //Blazing fast normalization when accuracy isn't needed + tempMag = x > y ? x + y / 2 : x / 2 + y; + const long errorFactor = 1000 * FixedMath.One / 1118; + + this.x = ((this.x << FixedMath.SHIFT_AMOUNT) / tempMag) * errorFactor >> FixedMath.SHIFT_AMOUNT; + this.y = ((this.y << FixedMath.SHIFT_AMOUNT) / tempMag) * errorFactor >> FixedMath.SHIFT_AMOUNT; + } + + public void Lerp (Vector2d target, long amount) { + Lerp (target.x,target.y,amount); + } + /// + /// Lerp this vector to target by amount. + /// + /// target. + /// amount. + public void Lerp(long targetx, long targety, long amount) + { + if (amount >= FixedMath.One) + { + this.x = targetx; + this.y = targety; + return; + } else if (amount <= 0) + { + return; + } + this.x = (targetx * amount + this.x * (FixedMath.One - amount)) >> FixedMath.SHIFT_AMOUNT; + this.y = (targety * amount + this.y * (FixedMath.One - amount)) >> FixedMath.SHIFT_AMOUNT; + } + public Vector2d Lerped (Vector2d target, long amount) { + Vector2d vec = this; + vec.Lerp(target.x,target.y,amount); + return vec; + } + public void Rotate(long cos, long sin) + { + temp1 = (this.x * cos + this.y * sin) >> FixedMath.SHIFT_AMOUNT; + this.y = (this.x * -sin + this.y * cos) >> FixedMath.SHIFT_AMOUNT; + this.x = temp1; + } + public Vector2d Rotated (long cos, long sin) { + Vector2d vec = this; + vec.Rotate(cos,sin); + return vec; + } + public Vector2d Rotated (Vector2d rotation) { + return Rotated (rotation.x,rotation.y); + } + public void RotateInverse(long cos, long sin) + { + Rotate(cos, -sin); + } + + public void RotateRight() + { + temp1 = this.x; + this.x = this.y; + this.y = -temp1; + } + + static Vector2d retVec = Vector2d.zero; + + public Vector2d rotatedRight + { + get + { + retVec.x = y; + retVec.y = -x; + return retVec; + } + } + + public Vector2d rotatedLeft + { + get + { + retVec.x = -y; + retVec.y = x; + return retVec; + } + } + + public void Reflect(long axisX, long axisY) + { + temp3 = this.Dot(axisX, axisY); + temp1 = (axisX * temp3) >> FixedMath.SHIFT_AMOUNT; + temp2 = (axisY * temp3) >> FixedMath.SHIFT_AMOUNT; + this.x = temp1 + temp1 - this.x; + this.y = temp2 + temp2 - this.y; + } + + public void Reflect(long axisX, long axisY, long projection) + { + temp1 = (axisX * projection) >> FixedMath.SHIFT_AMOUNT; + temp2 = (axisY * projection) >> FixedMath.SHIFT_AMOUNT; + this.x = temp1 + temp1 - this.x; + this.y = temp2 + temp2 - this.y; + } + + public Vector2d Reflected (long axisX, long axisY) { + Vector2d vec = this; + vec.Reflect(axisX,axisY); + return vec; + } + + public long Dot(long otherX, long otherY) + { + return (this.x * otherX + this.y * otherY) >> FixedMath.SHIFT_AMOUNT; + } + public long Dot (Vector2d other) { + return this.Dot(other.x,other.y); + } + + + public long Cross(long otherX, long otherY) + { + return (this.x * otherY - this.y * otherX) >> FixedMath.SHIFT_AMOUNT; + } + + public long Cross (Vector2d vec) { + return Cross (vec.x,vec.y); + } + + static long temp1; + static long temp2; + static long temp3; + static long tempMag; + + public long Distance(long otherX, long otherY) + { + temp1 = this.x - otherX; + temp1 *= temp1; + temp2 = this.y - otherY; + temp2 *= temp2; + return (FixedMath.Sqrt((temp1 + temp2) >> FixedMath.SHIFT_AMOUNT)); + } + + public long Distance(Vector2d other) + { + return Distance(other.x, other.y); + } + + public long SqrDistance(long otherX, long otherY) + { + + temp1 = this.x - otherX; + temp1 *= temp1; + temp2 = this.y - otherY; + temp2 *= temp2; + return ((temp1 + temp2) >> FixedMath.SHIFT_AMOUNT); + } + + /// + /// Returns a value that is greater if the distance is greater. + /// + /// The FastDistance. + public long FastDistance(long otherX, long otherY) + { + temp1 = this.x - otherX; + temp1 *= temp1; + temp2 = this.y - otherY; + temp2 *= temp2; + return (temp1 + temp2); + } + + public bool NotZero() + { + return x.MoreThanEpsilon() || y.MoreThanEpsilon(); + } + + #endregion + + #region Static Math + + public static readonly Vector2d up = new Vector2d(0, 1); + public static readonly Vector2d right = new Vector2d(1, 0); + public static readonly Vector2d down = new Vector2d(0, -1); + public static readonly Vector2d left = new Vector2d(-1, 0); + public static readonly Vector2d one = new Vector2d(1, 1); + public static readonly Vector2d negative = new Vector2d(-1, -1); + public static readonly Vector2d zero = new Vector2d(0, 0); + + public static readonly Vector2d radian0 = new Vector2d(1,0); + public static readonly Vector2d radian1 = new Vector2d(0,1); + public static readonly Vector2d radian2 = new Vector2d(-1,0); + public static readonly Vector2d radian3 = new Vector2d(0,-1); + + public static long Dot(long v1x, long v1y, long v2x, long v2y) + { + return (v1x * v2x + v1y * v2y) >> FixedMath.SHIFT_AMOUNT; + } + + public static long Cross(long v1x, long v1y, long v2x, long v2y) + { + return (v1x * v2y - v1y * v2x) >> FixedMath.SHIFT_AMOUNT; + } + + /// + /// Note: Not deterministic. Use only for serialization and sending in Commands. + /// + /// The from angle. + /// Angle. + public static Vector2d CreateRotation (double angle) + { + return new Vector2d(Math.Cos(angle), Math.Sin(angle)); + } + public static Vector2d CreateRotation (float angle) { + return CreateRotation ((double)angle); + } + + /// + /// Deterministic! + /// + /// The rotation. + /// Angle. + public static Vector2d CreateRotation (long angle) { + return new Vector2d(FixedMath.Trig.Cos(angle),FixedMath.Trig.Sin(angle)); + } + public Vector2d ToDirection () { + return new Vector2d(y,x); + } + public Vector2d ToRotation () { + return new Vector2d(y,x); + } + + #endregion + + #region Convert + + public override string ToString() + { + return ( + "(" + + Math.Round(FixedMath.ToDouble(this.x), 2, MidpointRounding.AwayFromZero).ToString() + + ", " + + Math.Round(FixedMath.ToDouble(this.y), 2, MidpointRounding.AwayFromZero) + + ")" + ); + } + + public Vector2 ToVector2() + { + return new Vector2( + (float)FixedMath.ToDouble(this.x), + (float)FixedMath.ToDouble(this.y) + ); + } + public Vector3d ToVector3d (long z = 0) { + return new Vector3d(x,y,z); + } + + public Vector3 ToVector3(float z = 0f) + { + return new Vector3((float)FixedMath.ToDouble(this.x), z, (float)FixedMath.ToDouble(this.y)); + } + + + + #endregion + + public override bool Equals(object obj) + { + if (obj is Vector2d) + { + return (Vector2d)obj == this; + } + return false; + } + + #region Operators + + public static Vector2d operator +(Vector2d v1, Vector2d v2) + { + return new Vector2d(v1.x + v2.x, v1.y + v2.y); + } + + public static Vector2d operator -(Vector2d v1, Vector2d v2) + { + return new Vector2d(v1.x - v2.x, v1.y - v2.y); + } + + public static Vector2d operator *(Vector2d v1, long mag) + { + return new Vector2d((v1.x * mag) >> FixedMath.SHIFT_AMOUNT, (v1.y * mag) >> FixedMath.SHIFT_AMOUNT); + } + + public static Vector2d operator *(Vector2d v1, int mag) + { + return new Vector2d((v1.x * mag), (v1.y * mag)); + } + + public static Vector2d operator /(Vector2d v1, long div) + { + return new Vector2d(((v1.x << FixedMath.SHIFT_AMOUNT) / div), (v1.y << FixedMath.SHIFT_AMOUNT) / div); + } + + public static Vector2d operator /(Vector2d v1, int div) + { + return new Vector2d((v1.x / div), v1.y / div); + } + + public static Vector2d operator >>(Vector2d v1, int shift) + { + return new Vector2d(v1.x >> shift, v1.y >> shift); + } + + public static bool operator ==(Vector2d v1, Vector2d v2) + { + return v1.x == v2.x && v1.y == v2.y; + } + + public static bool operator !=(Vector2d v1, Vector2d v2) + { + return v1.x != v2.x || v1.y != v2.y; + } + + #endregion + + public long GetLongHashCode() + { + return x * 31 + y * 7; + } + + public int GetStateHash() + { + return (int)(GetLongHashCode() % int.MaxValue); + } + public override int GetHashCode () { + return this.GetStateHash (); + } + + public void Write(Writer writer) + { + writer.Write(this.x); + writer.Write(this.y); + } + public void Read (Reader reader) { + this.x = reader.ReadLong(); + this.y = reader.ReadLong(); + } + } + + +} diff --git a/Example/ExampleDatabase/Example_Database.asset b/Example/ExampleDatabase/Example_Database.asset index 76fd0ddb..876aea4e 100644 Binary files a/Example/ExampleDatabase/Example_Database.asset and b/Example/ExampleDatabase/Example_Database.asset differ diff --git a/Example/ExampleScene.unity b/Example/ExampleScene.unity index 8d0e2fee..69cfcbd8 100644 Binary files a/Example/ExampleScene.unity and b/Example/ExampleScene.unity differ diff --git a/Example/TestAgent.prefab b/Example/TestAgent.prefab old mode 100755 new mode 100644 index ff7520b2..b24a6dd1 --- a/Example/TestAgent.prefab +++ b/Example/TestAgent.prefab @@ -66,6 +66,7 @@ Transform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0.48} m_LocalScale: {x: 0.5, y: 0.5, z: 0.6} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_Children: [] m_Father: {fileID: 450234} m_RootOrder: 0 @@ -78,6 +79,7 @@ Transform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0.6, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_Children: - {fileID: 422538} m_Father: {fileID: 465378} @@ -89,8 +91,9 @@ Transform: m_PrefabInternal: {fileID: 100100000} m_GameObject: {fileID: 100310} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0.778} + m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_Children: - {fileID: 450234} m_Father: {fileID: 0} @@ -104,17 +107,20 @@ MeshRenderer: m_Enabled: 1 m_CastShadows: 1 m_ReceiveShadows: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 m_Materials: - {fileID: 2100000, guid: b30447a543b0c4e6b9494c581702fd8c, type: 2} m_SubsetIndices: m_StaticBatchRoot: {fileID: 0} - m_UseLightProbes: 1 - m_ReflectionProbeUsage: 1 m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} m_ScaleInLightmap: 1 m_PreserveUVs: 1 m_IgnoreNormalsForChartDetection: 0 m_ImportantGI: 0 + m_SelectedWireframeHidden: 0 m_MinimumChartSize: 4 m_AutoUVMaxDistance: 0.5 m_AutoUVMaxAngle: 89 @@ -130,17 +136,20 @@ MeshRenderer: m_Enabled: 1 m_CastShadows: 1 m_ReceiveShadows: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 m_Materials: - {fileID: 2100000, guid: b30447a543b0c4e6b9494c581702fd8c, type: 2} m_SubsetIndices: m_StaticBatchRoot: {fileID: 0} - m_UseLightProbes: 1 - m_ReflectionProbeUsage: 1 m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} m_ScaleInLightmap: 1 m_PreserveUVs: 1 m_IgnoreNormalsForChartDetection: 0 m_ImportantGI: 0 + m_SelectedWireframeHidden: 0 m_MinimumChartSize: 4 m_AutoUVMaxDistance: 0.5 m_AutoUVMaxAngle: 89 @@ -185,6 +194,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: _mapIndex: 0 + _bonusHeight: 0 --- !u!114 &11425566 MonoBehaviour: m_ObjectHideFlags: 1 @@ -196,6 +206,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: a7cc9ef18533c9f4c943ac00aab4efd7, type: 3} m_Name: m_EditorClassIdentifier: + _cooldown: 0 Destination: x: 0 y: 0 @@ -203,7 +214,8 @@ MonoBehaviour: _canTurn: 1 _speed: 131072 _acceleration: 65536 - _canPathfind: 0 + _canPathfind: 1 + StopTimer: 0 DrawPath: 1 --- !u!114 &11439568 MonoBehaviour: @@ -231,6 +243,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: _maxHealth: 65536000 + _currentHealth: 0 --- !u!114 &11479720 MonoBehaviour: m_ObjectHideFlags: 1 @@ -242,17 +255,23 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: f4525e3f8c8889d43a4d23626ea32d54, type: 3} m_Name: m_EditorClassIdentifier: + _cooldown: 0 _projectileCode: TestBullet _range: 786432 _sight: 655360 _damage: 65536 _attackRate: 32 _targetAllegiance: 4 - _targetPlatform: 4 _trackAttackAngle: 1 _attackAngle: 11380 - _energyCost: 0 - _projectileOrigin: {x: 0, y: 0, z: 1.273089} + _projectileOffset: + x: 0 + y: 0 + z: 0 + _secondaryProjectileOffsets: [] + _cycleProjectiles: 0 + _windup: 0 + _increasePriority: 1 --- !u!114 &11485440 MonoBehaviour: m_ObjectHideFlags: 1 @@ -267,15 +286,10 @@ MonoBehaviour: _deathTime: 64 _agentType: 0 _globalID: 0 - _statsBarOffset: {x: 0, y: 1, z: 0} _boxPriority: 0 _selectionPriority: 0 - _attachedAbilities: [] - _body: {fileID: 0} - _triggers: [] - _animator: {fileID: 0} - _cachedTransform: {fileID: 0} - _cachedGameObject: {fileID: 0} + _selectable: 1 + TypeIndex: 0 Tag: 0 _selectionRadius: 1 _visualCenter: {fileID: 465378} @@ -320,6 +334,7 @@ MonoBehaviour: _height: 65536 _positionalTransform: {fileID: 465378} _rotationalTransform: {fileID: 465378} + _rotationOffset: {x: 0, y: 0, z: 0} --- !u!1001 &100100000 Prefab: m_ObjectHideFlags: 1 diff --git a/Resources/DefaultLockstepFrameworkSettings.asset b/Resources/DefaultLockstepFrameworkSettings.asset index 7fc250dc..4acf87f4 100644 Binary files a/Resources/DefaultLockstepFrameworkSettings.asset and b/Resources/DefaultLockstepFrameworkSettings.asset differ