Initial Commit
This commit is contained in:
commit
ee5c2f922d
2255 changed files with 547750 additions and 0 deletions
|
|
@ -0,0 +1,13 @@
|
|||
using Unity.Entities;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
[UpdateInGroup(typeof(PresentationSystemGroup))]
|
||||
public class WeaponAnimationEventResetSystem : ComponentSystem
|
||||
{
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
Entities.ForEach((ref WeaponAnimationEventData e) => { e = new WeaponAnimationEventData(); });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c42f259198cd0bb4caf8b5bd37248a62
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
58
Assets/Scripts/Systems/Weapon/WeaponAnimationSystem.cs
Normal file
58
Assets/Scripts/Systems/Weapon/WeaponAnimationSystem.cs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
using System.Linq;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
using Zenject;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
[UpdateInGroup(typeof(PresentationSystemGroup))]
|
||||
public class WeaponAnimationSystem : InjectableComponentSystem
|
||||
{
|
||||
[Inject] private Hashes hashes;
|
||||
|
||||
protected override void OnSystemUpdate()
|
||||
{
|
||||
Entities.ForEach(
|
||||
(Animator animator, ref WeaponAnimationData animation) =>
|
||||
{
|
||||
if (animator.isActiveAndEnabled)
|
||||
{
|
||||
animator.SetInteger(hashes.Reload, animation.ReloadCount);
|
||||
}
|
||||
});
|
||||
|
||||
Entities.WithAll<WeaponPropertiesData>()
|
||||
.ForEach(
|
||||
(Entity entity, Animator animator, ref WeaponData weaponData) =>
|
||||
{
|
||||
var weaponProp = EntityManager.GetSharedComponentData<WeaponPropertiesData>(entity);
|
||||
var weapon = weaponProp.Weapon;
|
||||
|
||||
if (animator.isActiveAndEnabled)
|
||||
{
|
||||
var animationInfo = animator.GetCurrentAnimatorClipInfo(0);
|
||||
if (animationInfo.Length > 0)
|
||||
{
|
||||
var stateInfo = animator.GetCurrentAnimatorStateInfo(0);
|
||||
var maxAnimTime = animationInfo.Max(c => c.clip.length);
|
||||
if (stateInfo.IsTag("Firing"))
|
||||
{
|
||||
animator.SetFloat(hashes.FiringSpeed, 1f / weapon.Data.RateOfFire / maxAnimTime);
|
||||
}
|
||||
if (stateInfo.IsTag("Reloading"))
|
||||
{
|
||||
animator.SetFloat(hashes.ReloadSpeed, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private class Hashes : IHashes
|
||||
{
|
||||
public readonly int FiringSpeed;
|
||||
public readonly int Reload;
|
||||
public readonly int ReloadSpeed;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Systems/Weapon/WeaponAnimationSystem.cs.meta
Normal file
11
Assets/Scripts/Systems/Weapon/WeaponAnimationSystem.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2a3ce49813299084cb3438a1ccf65154
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
130
Assets/Scripts/Systems/Weapon/WeaponFiringSystem.cs
Normal file
130
Assets/Scripts/Systems/Weapon/WeaponFiringSystem.cs
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
using Cinemachine;
|
||||
using Events;
|
||||
using System;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
using Zenject;
|
||||
using Hash128 = Unity.Entities.Hash128;
|
||||
using Object = UnityEngine.Object;
|
||||
using Random = System.Random;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
[UpdateInGroup(typeof(PresentationSystemGroup)), UpdateAfter(typeof(PlayerWeaponSystem))]
|
||||
public class WeaponFiringSystem : InjectableComponentSystem
|
||||
{
|
||||
[Serializable]
|
||||
public class Settings
|
||||
{
|
||||
public ParticleSystem SmokeParticles;
|
||||
public CinemachineImpulseSource WeaponImpulseSource;
|
||||
}
|
||||
|
||||
[Inject] private readonly SoundManager soundManager;
|
||||
[Inject] private ParticleSystemFactory particleSystemFactory;
|
||||
private readonly Random random = new Random();
|
||||
|
||||
[Inject] private Settings settings;
|
||||
|
||||
protected override void OnSystemUpdate()
|
||||
{
|
||||
Entities.WithAll<WeaponPartsData>()
|
||||
.ForEach(
|
||||
(
|
||||
Entity entity,
|
||||
WeaponPropertiesData weaponProp,
|
||||
ref FireWeaponEvent e,
|
||||
ref AnimatorStateData animatorState,
|
||||
ref WeaponData weaponData,
|
||||
ref WeaponAccuracyData accuracy) =>
|
||||
{
|
||||
var weapon = weaponProp.Weapon;
|
||||
var ammoType = weapon.AmmoType.Data;
|
||||
var weaponParts = EntityManager.GetSharedComponentData<WeaponPartsData>(entity);
|
||||
var rateOfFireDelta = 1f / weapon.Data.RateOfFire;
|
||||
|
||||
if (weaponData.ClipAmmo > 0)
|
||||
{
|
||||
if (EntityManager.HasComponent<ReloadEvent>(entity))
|
||||
{
|
||||
PostUpdateCommands.SetComponent(entity, new ReloadEvent { Cancel = true });
|
||||
}
|
||||
|
||||
if (animatorState.State.IsTag("Interactive") || animatorState.State.IsTag("Firing"))
|
||||
{
|
||||
weaponData.ClipAmmo--;
|
||||
weaponData.FireTimer += rateOfFireDelta;
|
||||
|
||||
for (var j = 0; j < weapon.Data.ProjectileCount; j++)
|
||||
{
|
||||
var projectileObj = Object.Instantiate(weapon.AmmoType.Template);
|
||||
var rigidbody = projectileObj.GetComponent<Rigidbody2D>();
|
||||
rigidbody.MovePosition(weaponParts.Barrel.position);
|
||||
projectileObj.transform.position = weaponParts.Barrel.position;
|
||||
var sharedData = new ProjectileSharedData
|
||||
{
|
||||
Damage = ammoType.Damage * weapon.Data.DamageMultiply,
|
||||
MaxLife = ammoType.MaxLife,
|
||||
RicochetChance = ammoType.RicochetChance
|
||||
};
|
||||
var dir = ((Vector2)weaponParts.Barrel.right).Rotate(
|
||||
((float)random.NextGaussian() - 0.5f) *
|
||||
11.25f *
|
||||
(1 - weapon.Data.Accuracy * Mathf.Clamp01(accuracy.Accuracy)));
|
||||
var projectileData = new ProjectileData
|
||||
{
|
||||
Life = ammoType.MaxLife, Velocity = dir * ammoType.Speed, HitMask = e.LayerMask
|
||||
};
|
||||
|
||||
PostUpdateActions.Enqueue(
|
||||
() =>
|
||||
{
|
||||
var projectileEntity = GameObjectEntity.AddToEntityManager(EntityManager, projectileObj);
|
||||
EntityManager.AddSharedComponentData(projectileEntity, sharedData);
|
||||
EntityManager.AddComponentData(projectileEntity, projectileData);
|
||||
//EntityManager.AddComponentData(projectileEntity, default(TransformMatrix));
|
||||
//todo find Matrix Component data
|
||||
});
|
||||
}
|
||||
|
||||
if (weaponParts.ShellsExit != null)
|
||||
{
|
||||
particleSystemFactory.Create(new Hash128(weapon.AmmoType.ShellsParticles.AssetGUID)).Completed += operation =>
|
||||
{
|
||||
operation.Result.GetComponent<ParticleSystem>()
|
||||
.Emit(
|
||||
new ParticleSystem.EmitParams
|
||||
{
|
||||
position = weaponParts.ShellsExit.position, applyShapeToPosition = true
|
||||
},
|
||||
1);
|
||||
};
|
||||
}
|
||||
|
||||
particleSystemFactory.Create(new Hash128(weapon.MuzzleFlash.AssetGUID)).Completed += operation =>
|
||||
{
|
||||
operation.Result.GetComponent<ParticleSystem>()
|
||||
.Emit(
|
||||
new ParticleSystem.EmitParams
|
||||
{
|
||||
position = weaponParts.Barrel.position,
|
||||
rotation = Vector2.SignedAngle(weaponParts.Barrel.right, Vector2.up)
|
||||
},
|
||||
1);
|
||||
};
|
||||
|
||||
settings.SmokeParticles.Emit(new ParticleSystem.EmitParams { position = weaponParts.Barrel.position }, 1);
|
||||
weapon.FireSound.PlayRandomOneShot(weaponParts.Audio);
|
||||
settings.WeaponImpulseSource.GenerateImpulseAt(weaponParts.Barrel.position, Vector3.one * e.ScreenShake);
|
||||
}
|
||||
}
|
||||
else if (weaponData.Ammo <= 0 && weaponData.ClipAmmo <= 0)
|
||||
{
|
||||
weaponData.FireTimer += rateOfFireDelta;
|
||||
}
|
||||
|
||||
PostUpdateCommands.RemoveComponent<FireWeaponEvent>(entity);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Systems/Weapon/WeaponFiringSystem.cs.meta
Normal file
11
Assets/Scripts/Systems/Weapon/WeaponFiringSystem.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 1e59f4ae38323324d8592352feb2d5cf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
22
Assets/Scripts/Systems/Weapon/WeaponIKSystem.cs
Normal file
22
Assets/Scripts/Systems/Weapon/WeaponIKSystem.cs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
using Unity.Entities;
|
||||
using UnityEngine.Experimental.U2D.IK;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
[UpdateInGroup(typeof(PresentationSystemGroup)), UpdateAfter(typeof(ActorIkSystem)), UpdateBefore(typeof(ActorMeleeIkSystem))]
|
||||
public class WeaponIKSystem : ComponentSystem
|
||||
{
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
Entities.ForEach(
|
||||
(IKManager2D manager, ref WeaponData weaponData) =>
|
||||
{
|
||||
manager.enabled = false;
|
||||
if (manager.gameObject.activeInHierarchy)
|
||||
{
|
||||
manager.UpdateManager();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Systems/Weapon/WeaponIKSystem.cs.meta
Normal file
11
Assets/Scripts/Systems/Weapon/WeaponIKSystem.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4c187b11c6a3c9f4d8d843cbf470c459
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
90
Assets/Scripts/Systems/Weapon/WeaponReloadSystem.cs
Normal file
90
Assets/Scripts/Systems/Weapon/WeaponReloadSystem.cs
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
using Events;
|
||||
using Items;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
using Unity.Jobs;
|
||||
using UnityEngine;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
[UpdateInGroup(typeof(PresentationSystemGroup)), UpdateBefore(typeof(ActorAnimationEventResetSystem))]
|
||||
public class WeaponReloadSystem : JobComponentSystem
|
||||
{
|
||||
private EndSimulationEntityCommandBufferSystem endSimulation;
|
||||
private EntityQuery reloadGroup;
|
||||
|
||||
protected override void OnCreate()
|
||||
{
|
||||
endSimulation = World.GetExistingSystem<EndSimulationEntityCommandBufferSystem>();
|
||||
reloadGroup = GetEntityQuery(
|
||||
ComponentType.ReadOnly<ReloadEvent>(),
|
||||
ComponentType.ReadOnly<WeaponData>(),
|
||||
ComponentType.ReadOnly<WeaponPropertiesData>(),
|
||||
ComponentType.ReadOnly<WeaponAnimationEventData>(),
|
||||
ComponentType.ReadOnly<WeaponAnimationData>());
|
||||
}
|
||||
|
||||
protected override JobHandle OnUpdate(JobHandle inputDeps)
|
||||
{
|
||||
var referenceEntities = reloadGroup.ToEntityArray(Allocator.TempJob);
|
||||
var weapon = new NativeArray<RangedWeaponPrefabData>(referenceEntities.Length, Allocator.TempJob);
|
||||
for (var i = 0; i < referenceEntities.Length; i++)
|
||||
{
|
||||
weapon[i] = EntityManager.GetSharedComponentData<WeaponPropertiesData>(referenceEntities[i]).Weapon.Data;
|
||||
}
|
||||
|
||||
referenceEntities.Dispose();
|
||||
|
||||
var job = new ReloadJob { Weapon = weapon, buffer = endSimulation.CreateCommandBuffer().ToConcurrent() };
|
||||
return job.Schedule(this, inputDeps);
|
||||
}
|
||||
|
||||
private struct ReloadJob : IJobForEachWithEntity<ReloadEvent, WeaponData, WeaponAnimationEventData, WeaponAnimationData>
|
||||
{
|
||||
[DeallocateOnJobCompletion, ReadOnly] public NativeArray<RangedWeaponPrefabData> Weapon;
|
||||
public EntityCommandBuffer.Concurrent buffer;
|
||||
|
||||
public void Execute(
|
||||
Entity entity,
|
||||
int index,
|
||||
ref ReloadEvent e,
|
||||
ref WeaponData weaponData,
|
||||
ref WeaponAnimationEventData animationEvent,
|
||||
ref WeaponAnimationData animation)
|
||||
{
|
||||
var weapon = Weapon[index];
|
||||
|
||||
if (animationEvent.Reload)
|
||||
{
|
||||
var lastClipAmmo = weaponData.ClipAmmo;
|
||||
weaponData.ClipAmmo = Mathf.Min(weapon.ClipCapacity, weaponData.ClipAmmo + Mathf.Min(weaponData.Ammo, weapon.ReloadAmount));
|
||||
var taken = weaponData.ClipAmmo - lastClipAmmo;
|
||||
weaponData.Ammo -= taken;
|
||||
buffer.SetComponent(index, entity, weaponData);
|
||||
if (e.Cancel || weaponData.Ammo <= 0 || weaponData.ClipAmmo >= weapon.ClipCapacity)
|
||||
{
|
||||
animation.ReloadCount = 0;
|
||||
buffer.RemoveComponent<ReloadEvent>(index, entity);
|
||||
}
|
||||
}
|
||||
else if (weaponData.Ammo <= 0)
|
||||
{
|
||||
buffer.RemoveComponent<ReloadEvent>(index, entity);
|
||||
animation.ReloadCount = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
var leftToReload = weapon.ClipCapacity - weaponData.ClipAmmo;
|
||||
var reloadCount = Mathf.CeilToInt(leftToReload / (float)weapon.ReloadAmount);
|
||||
animation.ReloadCount = Mathf.Max(0, reloadCount);
|
||||
if (animation.ReloadCount == 0)
|
||||
{
|
||||
buffer.RemoveComponent<ReloadEvent>(index, entity);
|
||||
}
|
||||
}
|
||||
|
||||
buffer.SetComponent(index, entity, animation);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Systems/Weapon/WeaponReloadSystem.cs.meta
Normal file
11
Assets/Scripts/Systems/Weapon/WeaponReloadSystem.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3e3fa4782ee5fd2419e65ba74cac6b0b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
23
Assets/Scripts/Systems/Weapon/WeaponSystem.cs
Normal file
23
Assets/Scripts/Systems/Weapon/WeaponSystem.cs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
using Unity.Entities;
|
||||
using Unity.Mathematics;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
[UpdateAfter(typeof(WeaponFiringSystem))]
|
||||
public class WeaponSystem : ComponentSystem
|
||||
{
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
var dt = Time.DeltaTime;
|
||||
|
||||
Entities.ForEach(
|
||||
(WeaponPropertiesData weaponProp, ref WeaponData weaponData, ref WeaponAccuracyData accuracy) =>
|
||||
{
|
||||
weaponData.FireTimer = math.max(0, weaponData.FireTimer - dt);
|
||||
weaponData.ReloadTimer = math.max(0, weaponData.ReloadTimer - dt);
|
||||
|
||||
accuracy.Accuracy = 1;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Systems/Weapon/WeaponSystem.cs.meta
Normal file
11
Assets/Scripts/Systems/Weapon/WeaponSystem.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 901b484a7eeaaa343bb9b196f09559e3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Add table
Add a link
Reference in a new issue